Skip to content

Symfony UX CSS

Edit this page

Caution

EXPERIMENTAL This bundle is currently experimental and is likely to change, possibly significantly, before its first stable release.

Symfony UX CSS gives Twig a css() function inspired by Panda CSS. You pass it a hash of style properties, and it returns atomic class names built from your design tokens:

1
2
3
4
5
6
7
8
9
{# templates/components/Card.html.twig #}
<div class="{{ css({
    p: 'md',
    color: 'fg',
    _hover: { color: 'primary' },
    md: { p: 'lg' }
}) }}">
    ...
</div>

The attribute becomes class="p_md c_fg hover:c_primary md:p_lg", and the bundle writes the matching rules to one CSS file:

1
2
3
4
5
6
7
.p_md { padding: var(--spacing-md); }
.c_fg { color: var(--colors-fg); }
.hover\:c_primary:is(:hover, [data-hover]) { color: var(--colors-primary); }

@media screen and (min-width: 48rem) {
    .md\:p_lg { padding: var(--spacing-lg); }
}

It is an alternative to utility classes written as strings. The hash is checked when Twig compiles the template: a misspelled property, an unknown token or a wrong keyword is a Twig syntax error that points to the template line. You find the mistake when you open the page, and lint:twig finds it in CI, before it reaches production.

The bundle needs no Node.js. PHP reads the templates and writes the CSS file. AssetMapper serves it as is, and Symfony Reprise or Webpack Encore can import it like any other stylesheet.

Mental model

Question Answer
When is css() resolved? When Twig compiles the template, for every value written in the template. Values only known at runtime are resolved when the template renders.
Where does the CSS go? In var/ux_css/styles.css, one file for the whole application.
Who writes the file? In dev, the bundle, before the response of any request that follows a template change. For production, cache:warmup.
What does the file contain? Every style written in a template, the styles listed in static_css, and every token as a CSS variable.
When are mistakes reported? When Twig compiles the template: on the page in dev, in lint:twig and in cache:warmup.

Start here

Write styles

Ship it

The bundle supports PHP 8.4 or later, Symfony 7.4 or 8.x, and Twig 3.24 or later.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version