Skip to content

Commit

Permalink
WIP: research
Browse files Browse the repository at this point in the history
  • Loading branch information
balzss committed Dec 9, 2024
1 parent ef3e930 commit ac2ee2d
Show file tree
Hide file tree
Showing 21 changed files with 321 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/contributor-docs/v10-upgrade-guide.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Upgrade Guide for Version 10.0
category: Guides
order: 7
order: 98
---

# Upgrade Guide for Version 10
Expand Down
92 changes: 92 additions & 0 deletions docs/guides/layout-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: Layout Spacing
category: Guides
order: 8
---

# Layout Spacing

Our design system provides a set of spacing tokens for consistent layouts and components. Some tokens share values but should be used semantically. For instance, while both `space12` and `buttons` are 12px, `buttons` should be used for spacing between buttons.

## Tokens

| Key | Value |
|--------------------|--------|
| space0 | 0px |
| space2 | 2px |
| space4 | 4px |
| space8 | 8px |
| space12 | 12px |
| space16 | 16px |
| space24 | 24px |
| space36 | 36px |
| space48 | 48px |
| space60 | 60px |
| sections | 36px |
| sectionElements | 24px |
| trayElements | 24px |
| modalElements | 24px |
| moduleElements | 16px |
| paddingCardLarge | 24px |
| paddingCardMedium | 16px |
| paddingCardSmall | 12px |
| selects | 16px |
| textAreas | 16px |
| inputFields | 16px |
| checkboxes | 16px |
| radios | 16px |
| toggles | 16px |
| buttons | 12px |
| tags | 12px |
| statusIndicators | 12px |
| dataPoints | 12px |

## Applying Spacing

There are three main ways to apply spacing in our component library:

### 1. Using the `margin` Prop

Most components in the library support a `margin` prop that works similarly to the CSS margin property. You can specify a single value or fine-tune individual margins (e.g., top, right, bottom, left).

```ts
---
type: example
---
<div>
<Button margin="0 buttons 0 0">Button 1</Button>
<Button>Button 2</Button>
</div>
```

### 2. Using a Container Component with the `gap` Prop

For layouts, container components like `Flex` and `Grid` can be used with the gap prop to manage spacing between child elements.

```ts
---
type: example
---
<Flex gap="buttons">
<Button>Button 1</Button>
<Button>Button 2</Button>
</Flex>
```

### 3. Importing Values from the Theme

If you need to directly reference spacing values, you can import them from the theme. This approach is useful for applying spacing in inline styles or custom components.

```ts
---
type: code
---
// import the canvas theme
import canvas from '@instructure/ui-themes'

// use spacing values
<div style={{display: 'flex', gap: canvas.spacing.buttons}}>
<button>Button 1</button>
<button>Button 2</button>
</div>
```
2 changes: 1 addition & 1 deletion packages/emotion/src/styleUtils/ThemeablePropValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ThemeablePropValues = {
medium: 'medium',
large: 'large',
xLarge: 'x-large',
xxLarge: 'xx-large'
xxLarge: 'xx-large',
}
} as const

Expand Down
3 changes: 2 additions & 1 deletion packages/ui-color-picker/src/ColorPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ class ColorPicker extends Component<ColorPickerProps, ColorPickerState> {
)

render() {
const { disabled, isRequired, placeholderText, width, id } = this.props
const { disabled, isRequired, placeholderText, width, id, margin } = this.props

return (
<div
Expand All @@ -627,6 +627,7 @@ class ColorPicker extends Component<ColorPickerProps, ColorPickerState> {
onPaste={(event) => this.handleOnPaste(event)}
onBlur={() => this.handleOnBlur()}
messages={this.renderMessages()}
margin={margin}
/>
{!this.isSimple && (
<div
Expand Down
13 changes: 10 additions & 3 deletions packages/ui-color-picker/src/ColorPicker/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import React from 'react'
import PropTypes from 'prop-types'

import type { FormMessage } from '@instructure/ui-form-field'
import type { WithStyleProps, ComponentStyle } from '@instructure/emotion'
import type { WithStyleProps, ComponentStyle, Spacing } from '@instructure/emotion'
import type {
ColorPickerTheme,
OtherHTMLAttributes,
Expand Down Expand Up @@ -225,6 +225,11 @@ type ColorPickerOwnProps = {
* If true, alpha slider will be rendered. Defaults to false
*/
withAlpha?: boolean

/**
* Margin around the component. Accepts a `Spacing` token. See token values and example usage in [this guide](/#layout-spacing).
*/
margin?: Spacing
}

type ColorPickerState = {
Expand Down Expand Up @@ -316,7 +321,8 @@ const propTypes: PropValidators<PropKeys> = {
id: PropTypes.string,
value: PropTypes.string,
width: PropTypes.string,
withAlpha: PropTypes.bool
withAlpha: PropTypes.bool,
margin: PropTypes.string,
}

const allowedProps: AllowedPropKeys = [
Expand All @@ -339,7 +345,8 @@ const allowedProps: AllowedPropKeys = [
'tooltip',
'value',
'width',
'withAlpha'
'withAlpha',
'margin',
]

export type {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui-date-input/src/DateInput2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const DateInput2 = ({
placeholder,
dateFormat,
onRequestValidateDate,
// margin, TODO enable this prop
margin,
...rest
}: DateInput2Props) => {
const localeContext = useContext(ApplyLocaleContext)
Expand Down Expand Up @@ -277,7 +277,6 @@ const DateInput2 = ({
return (
<TextInput
{...passthroughProps(rest)}
// margin={'large'} TODO add this prop to TextInput
renderLabel={renderLabel}
onChange={handleInputChange}
onBlur={handleBlur}
Expand All @@ -288,6 +287,7 @@ const DateInput2 = ({
display={isInline ? 'inline-block' : 'block'}
messages={inputMessages}
interaction={interaction}
margin={margin}
renderAfterInput={
<Popover
renderTrigger={
Expand Down
10 changes: 8 additions & 2 deletions packages/ui-date-input/src/DateInput2/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
Renderable,
PropValidators
} from '@instructure/shared-types'
import type { Spacing } from '@instructure/emotion'

type DateInput2OwnProps = {
/**
Expand Down Expand Up @@ -164,7 +165,11 @@ type DateInput2OwnProps = {
value: string,
utcDateString: string
) => void
// margin?: Spacing // TODO enable this prop

/**
* Margin around the component. Accepts a `Spacing` token. See token values and example usage in [this guide](/#layout-spacing).
*/
margin?: Spacing
}

type PropKeys = keyof DateInput2OwnProps
Expand Down Expand Up @@ -195,7 +200,8 @@ const propTypes: PropValidators<PropKeys> = {
timezone: PropTypes.string,
withYearPicker: PropTypes.object,
dateFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
onRequestValidateDate: PropTypes.func
onRequestValidateDate: PropTypes.func,
margin: PropTypes.string,
}

export type { DateInput2Props }
Expand Down
1 change: 1 addition & 0 deletions packages/ui-form-field/src/FormField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class FormField extends Component<FormFieldProps> {
as="label"
htmlFor={this.props.id}
elementRef={this.handleRef}
margin={this.props.margin}
/>
)
}
Expand Down
12 changes: 10 additions & 2 deletions packages/ui-form-field/src/FormField/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
PropValidators
} from '@instructure/shared-types'
import type { FormMessage } from '../FormPropTypes'
import type { Spacing } from '@instructure/emotion'

type FormFieldOwnProps = {
label: React.ReactNode
Expand Down Expand Up @@ -62,6 +63,11 @@ type FormFieldOwnProps = {
* provides a reference to the underlying html root element
*/
elementRef?: (element: Element | null) => void

/**
* Margin around the component. Accepts a `Spacing` token. See token values and example usage in [this guide](/#layout-spacing).
*/
margin?: Spacing
}

type PropKeys = keyof FormFieldOwnProps
Expand All @@ -82,7 +88,8 @@ const propTypes: PropValidators<PropKeys> = {
vAlign: PropTypes.oneOf(['top', 'middle', 'bottom']),
width: PropTypes.string,
inputContainerRef: PropTypes.func,
elementRef: PropTypes.func
elementRef: PropTypes.func,
margin: PropTypes.string,
}

const allowedProps: AllowedPropKeys = [
Expand All @@ -97,7 +104,8 @@ const allowedProps: AllowedPropKeys = [
'vAlign',
'width',
'inputContainerRef',
'elementRef'
'elementRef',
'margin'
]

export type { FormFieldOwnProps, FormFieldProps }
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-form-field/src/FormFieldLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ import generateStyle from './styles'

import { propTypes, allowedProps } from './props'
import type { FormFieldLayoutProps } from './props'
import generateComponentTheme from './theme'

/**
---
parent: FormField
---
**/
@withDeterministicId()
@withStyle(generateStyle, null)
@withStyle(generateStyle, generateComponentTheme)
class FormFieldLayout extends Component<FormFieldLayoutProps> {
static readonly componentId = 'FormFieldLayout'

Expand Down
13 changes: 10 additions & 3 deletions packages/ui-form-field/src/FormFieldLayout/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {
OtherHTMLAttributes,
PropValidators
} from '@instructure/shared-types'
import type { WithStyleProps, ComponentStyle } from '@instructure/emotion'
import type { WithStyleProps, ComponentStyle, Spacing } from '@instructure/emotion'
import type { FormMessage } from '../FormPropTypes'
import type { WithDeterministicIdProps } from '@instructure/ui-react-utils'

Expand Down Expand Up @@ -68,6 +68,11 @@ type FormFieldLayoutOwnProps = {
*/
elementRef?: (element: Element | null) => void
isGroup?: boolean

/**
* Margin around the component. Accepts a `Spacing` token. See token values and example usage in [this guide](/#layout-spacing).
*/
margin?: Spacing
}

type PropKeys = keyof FormFieldLayoutOwnProps
Expand Down Expand Up @@ -97,7 +102,8 @@ const propTypes: PropValidators<PropKeys> = {
width: PropTypes.string,
inputContainerRef: PropTypes.func,
elementRef: PropTypes.func,
isGroup: PropTypes.bool
isGroup: PropTypes.bool,
margin: PropTypes.string,
}

const allowedProps: AllowedPropKeys = [
Expand All @@ -112,7 +118,8 @@ const allowedProps: AllowedPropKeys = [
'labelAlign',
'width',
'inputContainerRef',
'elementRef'
'elementRef',
'margin'

// added vAlign because FormField and FormFieldGroup passes it, but not adding
// it to allowedProps to prevent it from getting passed through accidentally
Expand Down
7 changes: 4 additions & 3 deletions packages/ui-form-field/src/FormFieldLayout/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import type { FormFieldLayoutProps, FormFieldLayoutStyle } from './props'
* @return {Object} The final style object, which will be used in the component
*/
const generateStyle = (
_componentTheme: null,
componentTheme: any,
props: FormFieldLayoutProps
): FormFieldLayoutStyle => {
const { inline } = props
const { inline, margin } = props
const { spacing } = componentTheme

return {
groupErrorMessage: {
Expand All @@ -49,7 +50,7 @@ const generateStyle = (
all: 'initial',
border: '0',
padding: '0',
margin: '0',
margin: margin ? spacing[margin] : '0',
minWidth: '0',
direction: 'inherit',
textAlign: 'start',
Expand Down
33 changes: 33 additions & 0 deletions packages/ui-form-field/src/FormFieldLayout/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import type { Theme } from '@instructure/ui-themes'

const generateComponentTheme = (theme: Theme): any => {
const { spacing } = theme

return { spacing }
}

export default generateComponentTheme
Loading

0 comments on commit ac2ee2d

Please sign in to comment.