變數
A guide to using variables to implement design tokens.
Overview
Variable Name | Value | Namespace |
---|---|---|
full | "100%" | - |
fit | "fit-content" | - |
max | "max-content" | - |
min | "min-content" | - |
font-family-sans | "\"Inter\", $font-family-sans-fallback" | font-family |
font-family-sans-fallback | "ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'" | font-family |
font-family-serif-fallback | "ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif" | font-family |
font-family-mono-fallback | "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace" | font-family |
letter-spacing-tightest | "-0.072em" | letter-spacing |
letter-spacing-tighter | "-0.04em" | letter-spacing |
letter-spacing-tight | "-0.02em" | letter-spacing |
letter-spacing-normal | "0" | letter-spacing |
letter-spacing-wide | "0.02em" | letter-spacing |
letter-spacing-wider | "0.04em" | letter-spacing |
letter-spacing-widest | "0.12em" | letter-spacing |
line-height-xs | 1.2 | line-height |
line-height-sm | 1.4 | line-height |
line-height-md | 1.6 | line-height |
line-height-lg | 1.8 | line-height |
line-height-xl | 2 | line-height |
font-weight-thin | 100 | font-weight |
font-weight-extralight | 200 | font-weight |
font-weight-light | 300 | font-weight |
font-weight-regular | 400 | font-weight |
font-weight-medium | 500 | font-weight |
font-weight-semibold | 600 | font-weight |
font-weight-bold | 700 | font-weight |
font-weight-extrabold | 800 | font-weight |
font-weight-heavy | 900 | font-weight |
font-size-3xs | 8 | font-size |
font-size-2xs | 10 | font-size |
font-size-xs | 12 | font-size |
font-size-sm | 14 | font-size |
font-size-md | 16 | font-size |
font-size-lg | 18 | font-size |
font-size-xl | 20 | font-size |
font-size-2xl | 24 | font-size |
font-size-3xl | 32 | font-size |
font-size-4xl | 36 | font-size |
font-size-5xl | 40 | font-size |
font-size-6xl | 48 | font-size |
font-size-7xl | 60 | font-size |
font-size-8xl | 72 | font-size |
font-size-9xl | 96 | font-size |
font-size-10xl | 128 | font-size |
border-radius-xs | 2 | border-radius |
border-radius-sm | 4 | border-radius |
border-radius-md | 6 | border-radius |
border-radius-lg | 8 | border-radius |
border-radius-xl | 12 | border-radius |
border-radius-2xl | 16 | border-radius |
border-radius-3xl | 24 | border-radius |
border-radius-4xl | 32 | border-radius |
order-first | -999999 | order |
order-last | 999999 | order |
spacing-4xs | 2 | spacing |
spacing-3xs | 4 | spacing |
spacing-2xs | 6 | spacing |
spacing-xs | 8 | spacing |
spacing-sm | 12 | spacing |
spacing-md | 16 | spacing |
spacing-lg | 24 | spacing |
spacing-xl | 32 | spacing |
spacing-2xl | 48 | spacing |
spacing-3xl | 64 | spacing |
spacing-4xl | 96 | spacing |
spacing-5xl | 128 | spacing |
color-current | "currentColor" | color |
font-family-serif | "ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif" | font-family |
font-family-mono | "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace" | font-family |
screen-4xs | 360 | screen |
screen-3xs | 480 | screen |
screen-2xs | 600 | screen |
screen-xs | 768 | screen |
screen-sm | 834 | screen |
screen-md | 1024 | screen |
screen-lg | 1280 | screen |
screen-xl | 1440 | screen |
screen-2xl | 1600 | screen |
screen-3xl | 1920 | screen |
screen-4xl | 2560 | screen |
In Master CSS, variables are used to manage tokens in the design system, such as colors, font families, etc., even based on different theme modes.
import { variables } from '@master/css'export default { variables: { spacing: { sm: 10 }, primary: '#000000' }}
Apply custom variables using ambiguous shorthand:
<div class="font:sans max-w:screen-desktop m:sm bg:primary">…</div>
Add a string variable
Create a string variable to reuse it in your markup.
export default { variables: { content: { hash: '" #"', external: '" ↗"' }, }}
Use the variable in your markup:
<div class="content:hash content:external">…</div>
Use custom colors
Add a color
Add the primary color using #000
, oklch(0% 0 none)
, or hsl(0 0% 0%)
.
export default { variables: { color: { primary: '#000' } }}
Apply it using color-related syntax and even change the opacity.
<div class="fg:primary bg:primary/.5">…</div>
Generated CSS
@layer base, theme, preset, components, general;@layer general { .bg\:primary\/\.5 { background-color: rgb(0 0 0/0.5) } .fg\:primary { color: rgb(0 0 0) }}
If a color is used only once in all markups, you can optionally not abstract it into a token.
<button class="bg:#ceb195">Milk Tea</button>
Variables only support rgb
and hsl
, in the future, they will be compatible with all native CSS functions, such as rgba
, hsla
, lab
, and others. #346
Add a color with opacity
Add color variables with opacity using #00000080
, rgb(0 0 0/.5)
, or hsl(0 0% 0%/.5)
.
export default { variables: { primary: 'rgb(0 0 0/0.5)' }}
Apply it using color-related syntax like fg:
.
<div class="fg:primary">…</div>
Generated CSS
@layer base, theme, preset, components, general;@layer general { .fg\:primary { color: rgb(0 0 0/0.5) }}
Color variables with opacity cannot use /alpha
to change the opacity.
<div class="fg:primary/.5">…</div>
Add a color alias
Create an alias for a color to link its value to an existing color.
export default { variables: { secondary: '$color-white' /* secondary */ }}
Apply it using color-related syntax.
<div class="fg:secondary">…</div>
Generated CSS
@layer base, theme, preset, components, general;@layer general { .fg\:secondary { color: oklch(100% 0 none) }}
For example, say you have multiple color variables referencing the same color token. If that color needs updating, you would only need to update the source instead of manually updating every instance of the color.
Add a color alias with opacity
Create an alias for a color with opacity to link its value to an existing color.
export default { variables: { color: { primary: '$color-black/.5', /* <─┐ */ secondary: '$color-primary/.5' /* ──┘ linked to primary */ } }}
Apply it using color-related syntax like fg:
.
<div class="bg:primary fg:secondary">…</div>
Generated CSS
@layer base, theme, preset, components, general;@layer general { .bg\:primary { background-color: oklch(0% 0 none/0.5) } .fg\:secondary { color: oklch(0% 0 none/0.25) }}
As shown above, creating an alias secondary
linked to an existing alias primary
with opacity /.5
is possible, and the opacity will be multiplied 0.5 * 0.5 = 0.25
.
Add color shades
Sets multiple shades for a single color.
export default { variables: { color: { primary: { '': '#1192e8', /* primary */ 10: '#e5f6ff', /* primary-10 */ 20: '#bae6ff', /* primary-20 */ } } }}
Apply it using color-related syntax like fill:
.
<svg class="fill:primary-20 …">20</svg>
Add colors based on modes
Add color variables in different theme modes.
export default { variables: { color: { primary: '#ff0000' } }, modes: { light: { color: { primary: '#000000' } }, dark: { color: { primary: '#ffffff' } } }}
Using modes for variables, you can access a single source of truth and simplify the markup.
<div class="bg:primary">…</div>
Generated CSS
@layer base, theme, preset, components, general;@layer theme { :root { --color-primary: rgb(255 0 0) } @media (prefers-color-scheme:light) { :root { --color-primary: rgb(0 0 0) } } @media (prefers-color-scheme:dark) { :root { --color-primary: rgb(255 255 255) } }}@layer general { .bg\:primary { background-color: var(--color-primary) }}
Override default colors
Access the same key as the preset to override default colors, like blue-5
~ blue-95
.
export default { variables: { color: { blue: '#4589ff' } }}