|
| 1 | +--- |
| 2 | +title: Layout Spacing |
| 3 | +category: Guides |
| 4 | +order: 8 |
| 5 | +--- |
| 6 | + |
| 7 | +# Layout Spacing |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +## Tokens |
| 12 | + |
| 13 | +| Key | Value | Value in pixels | |
| 14 | +| ----------------- | -------- | --------------- | |
| 15 | +| space0 | 0rem | 0px | |
| 16 | +| space2 | 0.125rem | 2px | |
| 17 | +| space4 | 0.25rem | 4px | |
| 18 | +| space8 | 0.5rem | 8px | |
| 19 | +| space12 | 0.75rem | 12px | |
| 20 | +| space16 | 1rem | 16px | |
| 21 | +| space24 | 1.5rem | 24px | |
| 22 | +| space36 | 2.25rem | 36px | |
| 23 | +| space48 | 3rem | 48px | |
| 24 | +| space60 | 3.75rem | 60px | |
| 25 | +| sections | 2.25rem | 36px | |
| 26 | +| sectionElements | 1.5em | 24px | |
| 27 | +| trayElements | 1.5em | 24px | |
| 28 | +| modalElements | 1.5em | 24px | |
| 29 | +| moduleElements | 1em | 16px | |
| 30 | +| paddingCardLarge | 1.5rem | 24px | |
| 31 | +| paddingCardMedium | 1rem | 16px | |
| 32 | +| paddingCardSmall | 0.75rem | 12px | |
| 33 | +| selects | 1rem | 16px | |
| 34 | +| textAreas | 1rem | 16px | |
| 35 | +| inputFields | 1rem | 16px | |
| 36 | +| checkboxes | 1rem | 16px | |
| 37 | +| radios | 1rem | 16px | |
| 38 | +| toggles | 1rem | 16px | |
| 39 | +| buttons | 0.75rem | 12px | |
| 40 | +| tags | 0.75rem | 12px | |
| 41 | +| statusIndicators | 0.75rem | 12px | |
| 42 | +| dataPoints | 0.75rem | 12px | |
| 43 | + |
| 44 | +## Applying Spacing |
| 45 | + |
| 46 | +There are three main ways to apply spacing in our component library: |
| 47 | + |
| 48 | +### 1. Using the `margin` Prop |
| 49 | + |
| 50 | +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). |
| 51 | + |
| 52 | +```ts |
| 53 | +--- |
| 54 | +type: example |
| 55 | +--- |
| 56 | +<div> |
| 57 | + <Button margin="0 buttons 0 0">Button 1</Button> |
| 58 | + <Button>Button 2</Button> |
| 59 | +</div> |
| 60 | +``` |
| 61 | + |
| 62 | +### 2. Using a Container Component with the `gap` Prop |
| 63 | + |
| 64 | +For layouts, container components like `Flex` and `Grid` can be used with the gap prop to manage spacing between child elements. |
| 65 | + |
| 66 | +```ts |
| 67 | +--- |
| 68 | +type: example |
| 69 | +--- |
| 70 | +<Flex gap="buttons"> |
| 71 | + <Button>Button 1</Button> |
| 72 | + <Button>Button 2</Button> |
| 73 | +</Flex> |
| 74 | +``` |
| 75 | + |
| 76 | +### 3. Importing Values from the Theme |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +```ts |
| 81 | +--- |
| 82 | +type: code |
| 83 | +--- |
| 84 | +// import the canvas theme |
| 85 | +import canvas from '@instructure/ui-themes' |
| 86 | + |
| 87 | +// use spacing values |
| 88 | +<div style={{display: 'flex', gap: canvas.spacing.buttons}}> |
| 89 | + <button>Button 1</button> |
| 90 | + <button>Button 2</button> |
| 91 | +</div> |
| 92 | +``` |
| 93 | + |
| 94 | +## Legacy tokens |
| 95 | + |
| 96 | +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. |
0 commit comments