LogoLogo
GithubTwitterDiscordPlayground
English
English
  • 👋Atomico
  • 🚀Getting started with Atomico
  • ⚛️Getting started with Atomico for React users
  • 💡What can you do with Atomico?
    • You can create amazing webcomponents
    • You can create design systems
    • You can create websites
    • You can create mobile applications
    • You can create web applications
  • API
    • 🧬Props(Properties)
      • Value cycle as prop
      • Is it advisable to declare events using the props API?
    • 🧩VirtualDOM
      • Advanced
    • 🎣Hooks
      • useProp
      • useEvent
      • useRef
      • useHost
      • useState
      • useReducer
      • useEffect, useLayoutEffect and useInsertionEffect
      • useMemo and useCallback
      • useUpdate
      • useId
      • useContext
        • useProvider
      • usePromise
      • useAsync and useSuspense
      • useAbortController
    • 🔬Testing
      • Render cycle
      • atomico/test-hooks
      • atomico/test-dom
  • Guides
    • 🤔Frequent questions
      • Is it advisable to declare events using the props API?
      • How to declare events for your component at the type level for TSX?
      • When and why to use shadowDom?
      • Can I use Atomico in browsers without ESM support?
    • 🤩Component
      • Naming
      • CSS Styles with Shadow DOM
    • 🛡️Atomico with Typescript
      • Props
      • Component
      • Meta-types
        • Type
        • Event declaration
        • Method declaration
      • Check the correct use of hooks
    • 🤝Atomico and React
      • Integrating Atomico in React
      • From React to Atomico
        • Rendering Differences
        • VirtualDOM api differences
    • ⏳Atomico and Asynchrony
    • 🧠Atomico design patterns
      • ♻️Webcomponents with hybrid rendering
      • 🔗Slot as templates
      • 🔀Slot
    • 📖Atomico and Storybook
      • Frequent questions
    • 💧SSR / SSG
    • 🗃️Archives
      • Class inheritance
      • Forms and shadowDOM
      • Tips
      • Design systems
      • Atomico style guide
        • File structure
          • Monorepo
          • Design systems
  • packages
    • @atomico/hooks
      • useCurrentValue
      • use-intersection-observer
      • use-ref-values
      • use-script
      • use-attributes
      • use-prop-proxy
      • use-click-press
      • use-dollars
      • use-reflect-event
      • use-keyboar
      • use-click-coordinates
      • use-copy
      • use-debounce-state
      • use-form
      • use-listener
      • use-disabled
      • use-css
      • use-channel
      • use-promise
      • use-responsive-state
      • use-parent
      • use-resize-observer
      • use-slot
        • useSlot
        • useProxySlot
      • use-render
      • use-mutation-observer
      • use-css-light-dom
      • use-controller
      • use-router
      • use-async-effect
      • use-child-nodes
      • use-force-render
    • @atomico/store
      • Store
    • @atomico/storybook
    • @atomico/router
    • @atomico/vite
      • ⚙️Process CSS tag template blocks with PostCSS.
      • 🏗️Compile your code to be distributed optimally as a package.
      • 📕Use the JSX/TSX syntax of Atomico when using Storybook.
      • 🤖Automatically create customElements.define
      • ☑️Support for Vitest for Atomico.
      • 🧙‍♂️Server actions
    • @atomico/exports
      • 🚩CLI and Flags
      • 🤖Wrapper for React
    • @atomico/components
      • @atomico/keen-slider
      • @atomico/modal
      • @atomico/lottie
      • @atomico/table
    • @atomico/react
    • @atomico/postcss-tokens
      • Example with @atomico/vite
    • @atomico/wrapper
    • 🚫Deprecated
      • @atomico/magic-form
        • MagicFormProvider | <magic-form-provider>
        • MagicForm | <magic-form>
        • MagicForm Hooks
        • MagicForm in React and Preact
        • MagicForm in Microfrontend
        • MagicForm Patterns
      • @atomico/design-tokens
        • @atomico/design-tokens api
  • Support
    • Discord
    • Github
    • Twitter
Powered by GitBook
On this page
  • Token syntax
  • Example of simple tokens
  • Example of variations by attributes
  • AtRule @tokens
  • root: string
  • filter: string
  • use: string
  • Configuration in postcss
  • prefix: string
  • defaultValue : boolean

Was this helpful?

Edit on GitHub
Export as PDF
  1. packages

@atomico/postcss-tokens

Makes it easy to manage tokens as CSS custom properties for design system development

Previous@atomico/reactNextExample with @atomico/vite

Last updated 1 year ago

Was this helpful?

@atomico/postcss-tokens works through yaml or json files that declare tokens to be later imported from CSS files using the atRule. Through @tokens you can define if the resource to be generated should be global in scope (:root) or at the webcomponent level (:host)

Token syntax

Tokens are declared using YAML to JSON files, token declarations can be:

  1. simple: object declaring only token property and value

  2. variations by attributes: object that declares the tokens according to the assignment of an attribute at the webcomponent level

Example of simple tokens

color:
  primary:
    text: white
    background: tomato
{
  "color": {
    "primary": {
      "text": "white",
      "background": "tomato"
    }
  }
}
{
  "color": {
    "primary": {
      "text": { "value": "white" },
      "background": { "value": "tomato" }
    }
  }
}

Example of variations by attributes

This type of declaration allows assigning tokens according to the attribute of a webcomponent

space:
  around: 1rem
  between: 1rem
  safe: .5rem
  (size=small):
    around: .8rem
    between: .8rem
    safe: .4rem

Thanks to this type of declaration you will be able to manage the variations directly from the token file.

AtRule @tokens

This atRule allows importing yaml or json files to be used as custom properties, example:

color:
  primary:
    text: white
    background: tomato

Importing the tokens.yaml file

@tokens "./tokens.yaml" scope(:root);

Output

:root{
    --myprefix--color-primary-text: white;
    --myprefix--color-primary-background: tomato;
}

Importing the tokens.yaml file

@tokens "./tokens.yaml";

Output

:host{
    --color-primary-text: var(--myprefix--color-primary-text);
    --color-primary-background: var(--myprefix--color-primary-background);
}

root: string

Defines the type of root that will use the tokens, by default ":host", example:

@tokens "./tokens.yaml" scope(:root);

Consider the following behaviors:

  1. root puede ser cualquier tipo de selector

  2. if root is equal to ":root", the build rules will work at the :root/lightDOM level.

  3. if root is equal to ":host", the generation rules will work at the :host/shadowDOM level.

filter: string

It will only take the tokens assigned in the filter to create the custom properties, example:

export const GenericStateTokens = css`
    @tokens "./tokens.yaml" filter(color);
`;
:host{
    --color-primary: var(--myprefix--color-primary);
    --color-secondary: var(--myprefix--color-secondary);
}
size: 
    small: 1rem
    normal: 2rem
color: 
    primary: red
    secondary: gold

From the previous example @atomic/postcss-tokens will only take the tokens from the color property.

use: string

It behaves similarly to filter, but with the big difference that filter preserves the scope structure vs use that shortens it, example:

export const GenericStateTokens = css`
    @tokens "./tokens.yaml" use(color);
`;
:host{
    --primary: var(--myprefix--color-primary);
    --secondary: var(--myprefix--color-secondary);
}
size: 
    small: 1rem
    normal: 2rem
color: 
    primary: red
    secondary: gold

Configuration in postcss

prefix: string

Define el prefijo para las custom properties

defaultValue : boolean

Define que las customProperties que trabaje a nivel de shadowDom poseeran un valor por defecto si no se declara el tokens en :root, ejemplo:

:host{
    --color-primary: var(--my-ds--color-primary, red);
}

@tokens