Stitches CSS-in-JS

repository·canary·Indexed 11 days ago

https://github.com/stitchesjs/stitches

A high-performance CSS-in-JS library featuring near-zero runtime, SSR support, and multi-variant component styling. It provides a framework-agnostic core (@stitches/core), a React integration (@stitches/react) with a styled API, and a stringify utility (@stitches/stringify) for converting JavaScript objects to CSS strings.

Tokens
29.5K
Snippets
85
Records
144
Agent score
90%

What's inside Stitches

  1. Use @stitches/core via CDN

    canary

    You can use @stitches/core directly in the browser using ES modules via Skypack or by including the global script from unpkg.

    <script type="module">
    import { css } from 'https://cdn.skypack.dev/@stitches/core'
    </script>
    <script src="https://unpkg.com/@stitches/core/dist/index.global.js"></script>
    <script>
    const { css } = stitches
    </script>
  2. Use @stitches/react via CDN

    canary

    If you are working in a browser environment without a bundler, you can import styled directly from a CDN like Skypack or use the global stitches object from unpkg.

    <!-- Using Skypack ESM -->
    <script type="module">
    import { styled } from 'https://cdn.skypack.dev/@stitches/react'
    </script>
    
    <!-- Using unpkg Global Script -->
    <script src="https://unpkg.com/@stitches/react/dist/index.global.js"></script>
    <script>
      const { styled } = stitches
    </script>
  3. Use standard CSS shorthand properties

    canary

    Stitches supports standard CSS shorthand properties to set multiple related properties at once. Common shorthand properties include:

    • animation: Shorthand for animation-name, animation-duration, animation-timing-function, animation-delay, animation-iteration-count, animation-direction, animation-fill-mode, and animation-play-state.
    • background: Sets all background style properties (color, image, origin, size, repeat, etc.).
    • border: Sets border-width, border-style, and border-color.
    • flex: Sets how a flex item grows or shrinks.
    • font: Sets font-related properties like size, weight, and family.
    • gap: Shorthand for row-gap and column-gap.
    • grid: Shorthand for defining grid templates and tracks.
    • margin: Shorthand for margin-top, margin-right, margin-bottom, and margin-left.
    • padding: Shorthand for all four sides of an element's padding.
    • text-decoration: Shorthand for text-decoration-line, text-decoration-color, text-decoration-style, and text-decoration-thickness.
    • transition: Shorthand for transition-property, transition-duration, transition-timing-function, and transition-delay.
  4. Understand the Token interface structure

    canary

    In Stitches, a Token represents a design system value (like a color or spacing value) that can be serialized into a CSS custom property (variable).

    Each token contains:

    • token: The name of the token.
    • value: The raw value (number or string).
    • scale: The category/scale the token belongs to (e.g., 'colors', 'space').
    • prefix: An optional prefix for the CSS variable.
    • variable: The fully serialized CSS custom property name (e.g., --prefix-scale-token).
    • computedValue: The CSS var() representation of the token.

    Tokens can be converted to their CSS variable string representation using .toString().

    // Conceptual representation of a Token object
    const myToken: Token = {
      token: 'primary',
      value: '#000',
      scale: 'colors',
      prefix: 'app',
      variable: '--app-colors-primary',
      computedValue: 'var(--app-colors-primary)',
      toString: () => 'var(--app-colors-primary)'
    };
  5. Understand the CSS type definition

    canary

    The CSS type is the core interface used to define styles in Stitches. It is a highly flexible type that allows for several different ways to specify values for CSS properties, utilities, and theme scales:

    1. Standard CSS Properties: You can use standard CSS property names (e.g., color, margin) with their corresponding values.
    2. Theme Tokens: You can use theme tokens by prefixing the scale name with $ (e.g., $colors$blue500).
    3. Custom Utilities: If you have defined custom utilities via the utils configuration, you can use those utility names as keys. The type system will validate the arguments passed to these utilities.
    4. Media Queries: You can nest styles under media query keys, which are prefixed with @ (e.g., @media (min-width: 400px)).
    5. Theme Scales: You can reference theme scales directly.
    6. Unknown Properties: The type allows for arbitrary string keys to support unknown or future CSS properties, accepting number, string, or nested objects.
  6. How Token serialization works

    canary

    Stitches tokens are automatically serialized into CSS custom properties using a specific naming convention. The variable property follows this pattern:

    --${prefix}-${scale}-${token}

    If prefix or scale are empty strings, they are omitted from the resulting variable name. The computedValue property provides the ready-to-use CSS var() string, and calling .toString() on a token instance will return this same var() string.

  7. Understand the CSS type structure in Stitches

    canary

    The CSS type is the core interface used to define styles in Stitches. It is a highly sophisticated type that intelligently merges several different styling capabilities into a single object. When you define a style object, the CSS type allows you to use:

    1. Standard CSS Properties: Direct longhand and shorthand properties (e.g., color, margin).
    2. Theme Tokens: Values mapped to your theme scales (e.g., $colors$blue500 or $space$2).
    3. Custom Utils: Any custom utility functions defined in your Stitches configuration.
    4. Media Queries: Nested at-rules prefixed with @ (e.g., @media screen or @mobile).
    5. Theme Map Keys: Direct access to theme scales.
    6. Arbitrary Properties: Support for unknown CSS properties as strings.

    The type is generic, allowing it to adapt based on your specific Media, Theme, ThemeMap, and Utils configurations.

  8. Define a custom Theme in Stitches

    canary

    The theme configuration object defines the design tokens available in your application. Stitches provides several default scales that you can override or extend:

    • colors
    • fonts
    • fontSizes
    • fontWeights
    • lineHeights
    • letterSpacings
    • radii (mapped to borderRadius)
    • shadows (mapped to boxShadow)
    • sizes (mapped to width, height, etc.)
    • space (mapped to margin, padding, etc.)
    • transitions
    • zIndices
    • borderWidths
    • borderStyles

    Each scale is a collection of tokens where the key is the token name and the value is a boolean | number | string.

    theme: {
      colors: {
        brand: '#007bff',
      },
      space: {
        small: '4px',
        medium: '8px',
      },
      radii: {
        round: '9999px',
      }
    }