Skip to content

Commit

Permalink
feat(many): introduce new spacing tokens; add margin prop for more co…
Browse files Browse the repository at this point in the history
…mponents
  • Loading branch information
balzss committed Feb 11, 2025
1 parent b092b45 commit 581f246
Show file tree
Hide file tree
Showing 23 changed files with 463 additions and 35 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
96 changes: 96 additions & 0 deletions docs/guides/layout-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
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 `tags` and `buttons` are 0.75rem, `buttons` should be used for spacing between buttons.

## Tokens

| Key | Value | Value in pixels |
| ----------------- | -------- | --------------- |
| space0 | 0rem | 0px |
| space2 | 0.125rem | 2px |
| space4 | 0.25rem | 4px |
| space8 | 0.5rem | 8px |
| space12 | 0.75rem | 12px |
| space16 | 1rem | 16px |
| space24 | 1.5rem | 24px |
| space36 | 2.25rem | 36px |
| space48 | 3rem | 48px |
| space60 | 3.75rem | 60px |
| sections | 2.25rem | 36px |
| sectionElements | 1.5em | 24px |
| trayElements | 1.5em | 24px |
| modalElements | 1.5em | 24px |
| moduleElements | 1em | 16px |
| paddingCardLarge | 1.5rem | 24px |
| paddingCardMedium | 1rem | 16px |
| paddingCardSmall | 0.75rem | 12px |
| selects | 1rem | 16px |
| textAreas | 1rem | 16px |
| inputFields | 1rem | 16px |
| checkboxes | 1rem | 16px |
| radios | 1rem | 16px |
| toggles | 1rem | 16px |
| buttons | 0.75rem | 12px |
| tags | 0.75rem | 12px |
| statusIndicators | 0.75rem | 12px |
| dataPoints | 0.75rem | 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>
```

## Legacy tokens

For compatibility reasons we still provide the legacy spacing tokens (`xxLarge`, `medium`, etc.) so old layouts don't break when updating InstUI but these tokens shouldn't be used when creating new layouts.
60 changes: 60 additions & 0 deletions packages/__examples__/.storybook/stories/MarginProp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 React from 'react'
import {
Button,
ColorPicker,
TextInput,
TextArea,
NumberInput,
DateInput2
} from '@instructure/ui'

function MarginProp() {
return (
<div>
<Button margin="space12">hello</Button>
<TextInput margin="space12" />
<TextArea margin="space12" label="label" />
<NumberInput margin="space12" renderLabel="label" />
<ColorPicker
placeholderText="placeholder"
label="label"
margin="space12"
/>
<DateInput2
margin="space12"
renderLabel="label"
screenReaderLabels={{
calendarIcon: 'asdf',
prevMonthButton: 'asdf',
nextMonthButton: 'asdf'
}}
/>
</div>
)
}

export default MarginProp
71 changes: 71 additions & 0 deletions packages/__examples__/.storybook/stories/SpacingTokens.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 React from 'react'
import { Button } from '@instructure/ui'

function SpacingTokens() {
const spaceTokens = [
'space0',
'space2',
'space4',
'space8',
'space12',
'space16',
'space24',
'space36',
'space48',
'space60',
'sections',
'sectionElrements',
'trayElrements',
'modalElrements',
'moduleElrements',
'paddingCardLarge',
'paddingCardMedium',
'paddingCardSmall',
'selects',
'textareas',
'inputFields',
'checkboxes',
'radios',
'toggles',
'buttons',
'tags',
'statusIndicators',
'dataPoints'
]

return (
<div>
{spaceTokens.map((token) => (
<Button margin={token} key={token}>
{token}
</Button>
))}
</div>
)
}

export default SpacingTokens
20 changes: 20 additions & 0 deletions packages/__examples__/.storybook/stories/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { renderPage } from './renderPage'
import propJSONData from '../../prop-data.json'
import TooltipPositioning from './TooltipPositioning'
import FormErrors from './FormErrors'
import SpacingTokens from './SpacingTokens'
import MarginProp from './MarginProp'
import SourceCodeEditorExamples from './SourceCodeEditorExamples'

type AdditionalExample = {
Expand Down Expand Up @@ -78,6 +80,24 @@ const additionalExamples: AdditionalExample[] = [
}
]
},
{
title: 'Spacing tokens',
stories: [
{
storyName: 'Spacing tokens',
storyFn: () => SpacingTokens()
}
]
},
{
title: 'Margin prop',
stories: [
{
storyName: 'Margin prop',
storyFn: () => MarginProp()
}
]
},
// TODO: try to fix the editor not rendering fully on chromatic screenshot,
// even with delay
{
Expand Down
37 changes: 34 additions & 3 deletions packages/emotion/src/styleUtils/ThemeablePropValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,40 @@ const ThemeablePropValues = {
} as const

// SPACING
type SpacingKeys = keyof typeof ThemeablePropValues.SPACING
type SpacingValues = (typeof ThemeablePropValues.SPACING)[SpacingKeys]
type Spacing = CSSShorthandValue<SpacingValues>
type OldSpacingKeys = keyof typeof ThemeablePropValues.SPACING
type OldSpacingValues = (typeof ThemeablePropValues.SPACING)[OldSpacingKeys]
type NewSpacingValues =
| 'space0'
| 'space2'
| 'space4'
| 'space8'
| 'space12'
| 'space16'
| 'space24'
| 'space36'
| 'space48'
| 'space60'
| 'sections'
| 'sectionElrements'
| 'trayElrements'
| 'modalElrements'
| 'moduleElrements'
| 'paddingCardLarge'
| 'paddingCardMedium'
| 'paddingCardSmall'
| 'selects'
| 'textareas'
| 'inputFields'
| 'checkboxes'
| 'radios'
| 'toggles'
| 'buttons'
| 'tags'
| 'statusIndicators'
| 'dataPoints'
type SpacingValues = OldSpacingValues | NewSpacingValues
// adding `(string & {})` allows to use any string while still allowing specific string values to be suggested by IDEs
type Spacing = SpacingValues | (string & {})

// SHADOW_TYPES
type ShadowKeys = keyof typeof ThemeablePropValues.SHADOW_TYPES
Expand Down
14 changes: 12 additions & 2 deletions packages/ui-color-picker/src/ColorPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,15 @@ class ColorPicker extends Component<ColorPickerProps, ColorPickerState> {
}}
>
<Tooltip renderTip={<span aria-hidden={true}>{tooltip}</span>}>
<IconButton withBackground={false} withBorder={false} screenReaderLabel={tooltip} size="small" shape="circle" width="auto" renderIcon={IconInfoLine}/>
<IconButton
withBackground={false}
withBorder={false}
screenReaderLabel={tooltip}
size="small"
shape="circle"
width="auto"
renderIcon={IconInfoLine}
/>
</Tooltip>
</InstUISettingsProvider>
</span>
Expand Down Expand Up @@ -602,7 +610,8 @@ 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 +636,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
17 changes: 14 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,11 @@ 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 +229,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](https://instructure.design/#layout-spacing).
*/
margin?: Spacing
}

type ColorPickerState = {
Expand Down Expand Up @@ -316,7 +325,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 +349,8 @@ const allowedProps: AllowedPropKeys = [
'tooltip',
'value',
'width',
'withAlpha'
'withAlpha',
'margin'
]

export type {
Expand Down
Loading

0 comments on commit 581f246

Please sign in to comment.