API Essentials

Styleframe Instance

Learn how to create and configure Styleframe instances to build type-safe, maintainable design systems with full control over CSS generation.

The Styleframe instance is the core of your design system. It provides all the tools you need to define variables, create selectors, build utilities, manage themes, and generate CSS—all with full TypeScript support and type safety.

Overview

When you create a Styleframe instance, you get access to a complete API for building your design system:

Create a Styleframe instance by calling the styleframe() function, optionally with configuration options:

styleframe.config.ts
import { styleframe } from 'styleframe';

const s = styleframe();

// Access all Styleframe features
const { 
    variable,   // Define design tokens
    selector,   // Create CSS rules
    utility,    // Build utility classes
    modifier,   // Define reusable modifiers
    recipe,     // Create component variants
    theme,      // Manage theme variations
    atRule,     // Use modern CSS at-rules
    keyframes,  // Define animations
    media,      // Create media queries
    ref,        // Reference variables
    css,        // Interpolate dynamic values
    root,       // Access the style tree
    options     // Configuration settings
} = s;

export default s;

The instance serves as your central hub for design system development, providing consistent patterns and type safety across all your styling needs.

Instance Properties

Every Styleframe instance provides the following properties and methods:

root

Root
The root style tree containing all defined styles, variables, and rules. This is used internally by Styleframe and when generating CSS output.

atRule

function
Enables modern CSS features like @supports, @font-face, @layer, and @container. Use at-rules for progressive enhancement and advanced CSS capabilities. Learn more about At-Rules →

css

function
Template literal utility for interpolating dynamic values into CSS strings. Perfect for complex calculations, gradients, and compound values. Learn more about Interpolation →

keyframes

function
Defines CSS animation keyframes with type-safe property definitions. Create smooth animations that enhance user experience. Learn more about Keyframes →

media

function
Creates responsive media queries for adaptive layouts. Build designs that work seamlessly across all device sizes. Learn more about Media Queries →

modifier

function
Defines reusable modifiers that can be applied to utilities. Modifiers enable consistent patterns like hover states, focus states, and responsive variations. Learn more about Utilities →

ref

function
References CSS variables created with variable(). The ref function generates the correct CSS custom property reference automatically. Learn more about Variables →

recipe

function
Creates component variant generators with type-safe configuration options. Recipes enable runtime variant selection for flexible UI components. Learn more about Recipes →

selector

function
Creates CSS rules with type-safe property definitions. Use selectors to style components, define layouts, and create complex CSS structures. Learn more about Selectors →

theme

function
Creates theme variations by overriding variables in specific contexts. Perfect for dark mode, brand themes, or user personalization. Learn more about Themes →

utility

function
Creates reusable utility class generators that produce atomic CSS classes. Utilities support modifiers and variants for flexible, composable styling. Learn more about Utilities →

variable

function
Creates CSS custom properties (variables) that form the foundation of your design system. Variables can be referenced throughout your styles and overridden in themes. Learn more about Variables →

options

StyleframeOptions
The configuration options passed when creating the instance. Access this property to inspect or reference the current configuration.

Configuration Options

The Styleframe instance accepts configuration options that allow you to customize how your styles are generated. These options give you fine-grained control over indentation, variable naming conventions, utility class selectors, and theme selectors — ensuring the generated CSS matches your project's conventions and requirements.

Configuration helps you:

  • Match your code style: Set custom indentation to align with your project's formatting rules.
  • Follow naming conventions: Customize how CSS variable names are generated to match your design system.
  • Control selector output: Define how utility classes and theme selectors are generated.
  • Ensure consistency: Maintain consistent patterns across your entire codebase.

You configure Styleframe by passing an options object when creating an instance:

export type StyleframeOptions = {
    variables?: {
        name?: VariableNameFn;
    };
    utilities?: {
        selector?: UtilitySelectorFn;
    };
    theme?: {
        selector?: ThemeSelectorFn;
    };
};

Let's explore each configuration option in detail.


variables.name

function
type VariableNameFn = (options: { name: string }) => string;
By default, variable names are converted to CSS custom properties by adding the -- prefix if not present. You can provide a custom function to transform variable names.

utilities.selector

function
type UtilitySelectorFn = (options: {
    name: string;
    value: string;
    modifiers: string[];
}) => string;
By default, utility class selectors use the pattern ._${name}:${value}:${modifiers}. You can provide a custom function to change how utility selectors are generated.

theme.selector

function
type ThemeSelectorFn = (options: { name: string }) => string;
By default, theme selectors use the pattern [data-theme="${name}"]. You can provide a custom function to change how theme selectors are generated.

Best Practices

  • Define configuration once: Create a single configured instance and export it for use across your project.
  • Match your project conventions: Align the configuration with your existing CSS and naming conventions.
  • Document your choices: Add comments explaining why specific configuration options were chosen.
  • Use consistent patterns: Ensure your naming functions produce predictable, consistent results.
  • Test generated output: Verify that the generated CSS matches your expectations after configuration changes.

FAQ