Building a Design System
This tutorial builds a small design system from scratch: color and size tokens with a light and a dark theme, a Button with two variants, and a Card whose content and actions are slots. Every step uses features described in more detail in Variables, Auto Layout, and Components.
Download the finished file to compare with your own, or to start from it. It has two pages: Components holds the main components, and Examples shows the same cards in the Light and Dark modes.
1. Define the tokens
Tokens are variables that every component reads, so a theme change happens in one place.
Open View → Variables….
Click + next to Collections and name the collection
Theme. Rename its first mode toLight, then click + next to Modes and name the new modeDark.Click Create variable, choose Color, and add these variables with a value for each mode. A slash in a name groups variables, so
color/primaryappears as primary under color.Variable Light Dark color/background#F4F5F7#0F1115color/surface#FFFFFF#1A1D23color/border#E1E4EA#2D323Bcolor/text#191C21#F1F3F6color/text-muted#5A616E#A2A9B5color/primary#3451D1#8098F7color/on-primary#FFFFFF#0D1222color/secondary#E9ECF2#272B33Create a second collection named
Sizewith Number variables:radius/control= 8,radius/card= 12,space/sm= 8,space/md= 16, andspace/lg= 24. Sizes do not change with the theme, so this collection keeps a single mode.
The stylesheet under the list already shows the result: the Light values in :root and the Dark values under [data-theme="dark"].
2. Build the Button
- Rename the page
Components. Draw a frame, press ⇧A to give it auto layout, and set both axes to Hug. - Add a text layer that reads
Button, in Inter Medium at 14 px, and name itLabel. - Bind the tokens. In Fill, use Apply variable to bind the frame's fill to
color/primaryand the text's fill tocolor/on-primary. Number fields take variables the same way: bind the corner radius toradius/controland the left and right padding tospace/md, and set the top and bottom padding to 10. - Press ⌥⌘K (Ctrl + Alt + K) to make it a component.
- Double-click into the component and select the Label layer. Next to its text in Typography, use Apply variable/property → Create property… to create a text property named
Label. Instances now set their label without detaching. - Select the component and click Add variant in the panel header. The component becomes a set with two variants. In Properties, rename the variant property to
Variantand its values toPrimaryandSecondary, then name the setButton. - In the Secondary variant, bind the fill to
color/secondaryand the label's fill tocolor/text. - Select the set and, in Behaviour, click + and choose Button. Preview (⌥⌘↩ / Ctrl + Alt + Enter) now runs it as a real button.
3. Build the Card
- Draw a frame 320 px wide, give it vertical auto layout, and set its height to Hug. Bind its padding to
space/lg, its gap tospace/md, its corner radius toradius/card, its fill tocolor/surface, and a 1 px stroke tocolor/border. - Add a text layer that reads
Card title, in Inter Semi Bold at 18 px, and name itTitle. Bind its fill tocolor/textand set its width to Fill. - Below it, add a frame named
Contentwith vertical auto layout, no fill, Fill width, and Hug height. Put a text layer in it, such asSlots hold content that each card chooses for itself., in Inter Regular at 14 px, bound tocolor/text-muted, and name itBody. - Add a frame named
Actionswith horizontal auto layout and a gap bound tospace/sm. From Assets, drag a Button into it. - Select the outer frame and make it a component named
Card. Link the title to a text property namedTitle, as you did for the button's label. - Select the
Contentframe and choose Create slot from the context menu or the Slots section. Do the same forActions. Each instance can now replace what these frames hold, and the main component's content becomes the default.
Slot settings can describe a slot, limit how many layers it holds, and name preferred components; for example, Actions could prefer Button and hold at most 2 layers.
4. Use the components
- Add a page named
Examplesand draw a frame namedLightwith vertical auto layout, padding and gap bound tospace/lg, and a fill bound tocolor/background. - Place two Card instances in it: right-click the Card and choose Create instance, or drag it from Assets. Set their Title properties to
Welcome backandPro plan. - In the second card, select the button in the
Actionsslot and delete it, then paste or drag in two Button instances: a Secondary one with the labelNot nowand a Primary one withUpgrade. Edit the text in itsContentslot. Slot content belongs to the instance, so it stays when the Card changes; Reset slot in the slot's menu brings back the component's content. - Duplicate the
Lightframe and name the copyDark. In Appearance, use Apply variable mode to set theThemecollection toDark. Every bound color in the frame switches, while the cards stay linked to their components.
Now edit a token or a main component, for example radius/card or the Button's padding, and watch both frames update.
5. Take it further
- Publish it as a library. Open Assets → Manage libraries → Publish library so other files can use the Button and Card, and accept updates when you change them (Component Libraries).
- Hand the tokens to code.
pairdraw tokens design-system.figprints the variables as CSS custom properties,--format tailwindas a Tailwind v4 theme, and--format dtcg --out tokens/as W3C design token files. HTML export writes the bound values asvar()references to the same properties (Exporting). - Check it. Checking Designs and
pairdraw lint design-system.fig --fix -o fixed.figfind colors that match a token but are not bound to it, and bind them. - Track changes. Keep the file in Git and review changes with
pairdraw diff files.
Build the example with a script
The example file is generated by this script, which uses the same Figma-compatible plugin API as pairdraw eval. Run it to build the file yourself, or change it to try your own tokens:
echo '{"children":[]}' > empty.pen
pairdraw eval empty.pen --stdin -o design-system.fig < design-system.js// Builds design-system.fig, the example file of the design system tutorial.
// Start from an empty document and write the result to a new file:
// echo '{"children":[]}' > empty.pen
// pairdraw eval empty.pen --stdin -o design-system.fig < design-system.js
await figma.loadFontAsync({ family: 'Inter', style: 'Regular' })
await figma.loadFontAsync({ family: 'Inter', style: 'Medium' })
await figma.loadFontAsync({ family: 'Inter', style: 'Semi Bold' })
// 1. Tokens: a Theme collection with Light and Dark modes, and a Size collection.
// A new .pen document comes with an empty Theme collection; start from none.
for (const collection of figma.variables.getLocalVariableCollections()) {
if (collection.variableIds.length === 0) collection.remove()
}
const theme = figma.variables.createVariableCollection('Theme')
const light = theme.modes[0].modeId
theme.renameMode(light, 'Light')
const dark = theme.addMode('Dark')
function hex(value) {
const n = Number.parseInt(value.slice(1), 16)
return { r: ((n >> 16) & 255) / 255, g: ((n >> 8) & 255) / 255, b: (n & 255) / 255, a: 1 }
}
const colors = {}
for (const [name, lightValue, darkValue] of [
['color/background', '#F4F5F7', '#0F1115'],
['color/surface', '#FFFFFF', '#1A1D23'],
['color/border', '#E1E4EA', '#2D323B'],
['color/text', '#191C21', '#F1F3F6'],
['color/text-muted', '#5A616E', '#A2A9B5'],
['color/primary', '#3451D1', '#8098F7'],
['color/on-primary', '#FFFFFF', '#0D1222'],
['color/secondary', '#E9ECF2', '#272B33']
]) {
const variable = figma.variables.createVariable(name, theme, 'COLOR')
variable.setValueForMode(light, hex(lightValue))
variable.setValueForMode(dark, hex(darkValue))
colors[name] = variable
}
const size = figma.variables.createVariableCollection('Size')
const sizes = {}
for (const [name, value] of [
['radius/control', 8],
['radius/card', 12],
['space/sm', 8],
['space/md', 16],
['space/lg', 24]
]) {
const variable = figma.variables.createVariable(name, size, 'FLOAT')
variable.setValueForMode(size.modes[0].modeId, value)
sizes[name] = variable
}
function paint(variable) {
return figma.variables.setBoundVariableForPaint(
{ type: 'SOLID', color: { r: 0, g: 0, b: 0 } },
'color',
variable
)
}
function bindAll(node, fields, variable) {
for (const field of fields) node.setBoundVariable(field, variable)
}
const RADII = ['topLeftRadius', 'topRightRadius', 'bottomLeftRadius', 'bottomRightRadius']
function text(name, characters, style, fontSize, color) {
const node = figma.createText()
node.name = name
node.fontName = { family: 'Inter', style }
node.fontSize = fontSize
node.characters = characters
node.fills = [paint(color)]
return node
}
const componentsPage = figma.currentPage
componentsPage.name = 'Components'
// 2. Button: two variants that share a Label text property.
function buttonVariant(variant, fill, ink) {
const button = figma.createComponent()
button.name = `Variant=${variant}`
button.layoutMode = 'HORIZONTAL'
button.primaryAxisSizingMode = 'AUTO'
button.counterAxisSizingMode = 'AUTO'
button.primaryAxisAlignItems = 'CENTER'
button.counterAxisAlignItems = 'CENTER'
button.paddingTop = 10
button.paddingBottom = 10
bindAll(button, ['paddingLeft', 'paddingRight'], sizes['space/md'])
bindAll(button, RADII, sizes['radius/control'])
button.fills = [paint(fill)]
button.appendChild(text('Label', 'Button', 'Medium', 14, ink))
return button
}
const primary = buttonVariant('Primary', colors['color/primary'], colors['color/on-primary'])
const secondary = buttonVariant('Secondary', colors['color/secondary'], colors['color/text'])
const buttonSet = figma.combineAsVariants([primary, secondary], componentsPage)
buttonSet.name = 'Button'
buttonSet.x = 0
buttonSet.y = 0
buttonSet.layoutMode = 'HORIZONTAL'
buttonSet.primaryAxisSizingMode = 'AUTO'
buttonSet.counterAxisSizingMode = 'AUTO'
buttonSet.itemSpacing = 16
buttonSet.paddingLeft = buttonSet.paddingRight = buttonSet.paddingTop = buttonSet.paddingBottom = 20
const labelKey = buttonSet.addComponentProperty('Label', 'TEXT', 'Button')
for (const variant of [primary, secondary]) {
variant.findOne((node) => node.name === 'Label').componentPropertyReferences = {
characters: labelKey
}
}
pairdraw.setBehaviour(buttonSet, 'button')
// 3. Card: a title property and two slots, Content and Actions.
const card = figma.createComponent()
card.name = 'Card'
card.x = 0
card.y = 160
card.resize(320, 200)
card.layoutMode = 'VERTICAL'
card.primaryAxisSizingMode = 'AUTO'
card.counterAxisSizingMode = 'FIXED'
bindAll(card, ['paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom'], sizes['space/lg'])
card.setBoundVariable('itemSpacing', sizes['space/md'])
bindAll(card, RADII, sizes['radius/card'])
card.fills = [paint(colors['color/surface'])]
card.strokes = [paint(colors['color/border'])]
card.strokeWeight = 1
const title = text('Title', 'Card title', 'Semi Bold', 18, colors['color/text'])
card.appendChild(title)
title.layoutSizingHorizontal = 'FILL'
const titleKey = card.addComponentProperty('Title', 'TEXT', 'Card title')
title.componentPropertyReferences = { characters: titleKey }
function slotFrame(name, layoutMode) {
const frame = figma.createFrame()
frame.name = name
frame.fills = []
frame.layoutMode = layoutMode
frame.primaryAxisSizingMode = 'AUTO'
frame.counterAxisSizingMode = 'AUTO'
frame.setBoundVariable('itemSpacing', sizes['space/sm'])
card.appendChild(frame)
frame.layoutSizingHorizontal = 'FILL'
return frame
}
const content = slotFrame('Content', 'VERTICAL')
const body = text(
'Body',
'Slots hold content that each card chooses for itself.',
'Regular',
14,
colors['color/text-muted']
)
content.appendChild(body)
body.layoutSizingHorizontal = 'FILL'
pairdraw.createSlot(content)
const actions = slotFrame('Actions', 'HORIZONTAL')
const action = primary.createInstance()
actions.appendChild(action)
pairdraw.createSlot(actions)
// 4. Examples: the same cards in each mode, with their own slot content.
const examplesPage = figma.createPage()
examplesPage.name = 'Examples'
function board(name, mode, x) {
const frame = figma.createFrame()
frame.name = name
frame.x = x
frame.y = 0
frame.layoutMode = 'VERTICAL'
frame.primaryAxisSizingMode = 'AUTO'
frame.counterAxisSizingMode = 'AUTO'
bindAll(frame, ['paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom'], sizes['space/lg'])
frame.setBoundVariable('itemSpacing', sizes['space/lg'])
frame.fills = [paint(colors['color/background'])]
frame.setExplicitVariableModeForCollection(theme, mode)
examplesPage.appendChild(frame)
const welcome = card.createInstance()
welcome.setProperties({ [titleKey]: 'Welcome back' })
frame.appendChild(welcome)
const plan = card.createInstance()
plan.setProperties({ [titleKey]: 'Pro plan' })
plan.findOne((node) => node.name === 'Body').characters =
'Unlimited files, version history, and shared libraries.'
const planActions = plan.findOne((node) => node.name === 'Actions')
for (const child of [...planActions.children]) child.remove()
const upgrade = primary.createInstance()
upgrade.setProperties({ [labelKey]: 'Upgrade' })
const later = secondary.createInstance()
later.setProperties({ [labelKey]: 'Not now' })
planActions.appendChild(later)
planActions.appendChild(upgrade)
frame.appendChild(plan)
return frame
}
board('Light', light, 0)
board('Dark', dark, 420)
return {
pages: figma.root.children.map((page) => page.name),
variables: Object.keys(colors).length + Object.keys(sizes).length
}