API Essentials

Variables

Styleframe variables are the foundation of your design system. They let you define design tokens such as colors, spacing, typography, and more.

Overview

Variables are the foundation of your design system. They let you define design tokens such as colors, spacing, typography, and more, with full type safety and auto-complete. Variables are reusable, composable, and become CSS custom properties in your generated styles.

Why use variables?

Variables help you:

  • Centralize your design decisions: Define a color, spacing value, or font once, and reference it everywhere.
  • Enable easy theming: Quickly override variables to create dark mode, brand themes, or user personalization.
  • Prevent mistakes: Change a value in one place, update your whole app — with zero risk of typos or missed selectors.

Defining Variables

You define a variable using the variable() function from your styleframe instance:

import { styleframe } from 'styleframe';

const s = styleframe();
const { variable } = s;

const colorPrimary = variable('color--primary', '#006cff');
const spacingMd = variable('spacing--md', '1rem');

export default s;

Each variable gets a name (used for the CSS custom property) and a value.

Pro tip: Name variables using a clear convention (e.g. color--primary, spacing--md). This makes your design system more readable.

Default Values

You can also define a variable with a default value. The default value will be used only if the variable is not already defined in the styleframe instance:

import { styleframe } from 'styleframe';

const s = styleframe();
const { variable } = s;

function useDefaultVariables() {
    variable('spacing', '1rem', { default: true }); 
}

variable('spacing', '2rem');

useDefaultVariables(); // This will not override the previous value

export default s;
Using default values is useful when working with Composables which are likely to get called multiple times, or when you want to ensure a variable has a fallback value if not set elsewhere.

Referencing Variables

To use a variable in other style definitions, pass its reference using the ref() helper:

a. Within Variables

import { styleframe } from 'styleframe';

const s = styleframe();
const { variable, ref } = s; 

const colorBlue = variable('color--blue', '#006cff')

const colorPrimary = variable('color--primary', ref(colorBlue));

export default s;

b. Within Selectors

import { styleframe } from 'styleframe';

const s = styleframe();
const { variable, ref, selector } = s;

const colorPrimary = variable('color--primary', '#006cff');

selector('.button', {
    backgroundColor: ref(colorPrimary),
});

export default s;

This tells styleframe to inject the actual variable value (as a CSS custom property) in the generated CSS.

Referencing Variables with Fallback Values

You can also provide a fallback value when referencing a variable. This is useful if you want to ensure a default value is used if the variable is not defined:

import { styleframe } from 'styleframe';

const s = styleframe();
const { variable, ref } = s;

const colorSecondary = variable(
    'color--secondary', 
    ref('color--purple', '#ff5733') // Fallback to #ff5733 if not defined
);

export default s;

Best Practices

  • Use variables for all design tokens (colors, font sizes, spacing, radii, etc.).
  • Avoid hardcoding values in selectors, always referencing a variable where possible.
  • Group related variables in separate files with composables for clarity (e.g., useColors, useSpacing).
  • Document your variable choices for your team — explain why you chose certain values.

FAQ