.. _stylex-authoring:
===========================
StyleX authoring and themes
===========================
StyleX Connector resolves semantic keys from one or more generated manifests.
It does not compile ``.stylex.js`` files and it does not deliver their CSS.
The consuming site or extension owns its StyleX compiler, Vite entrypoints,
token sources, themes, recipes and generated assets.
This chapter recommends a project structure and source conventions for TYPO3
packages. The Connector does not enforce these names or paths; configure the
Vite build and manifest producer to match the project's chosen structure.
Recommended source layout
=========================
Keep shared, reusable design-system sources in the sitepackage or reusable
extension that owns them. Keep block-only recipes beside the block. The
following tree is a convention, not a TYPO3 or StyleX requirement:
.. code-block:: text
my_sitepackage/
├── Resources/Private/JavaScript/
│ ├── Main.entry.js
│ └── StyleX/
│ ├── tokens/
│ │ ├── color-tokens.stylex.js
│ │ ├── spacing-tokens.stylex.js
│ │ └── components/
│ │ └── button-tokens.stylex.js
│ ├── themes/
│ │ ├── neutral-theme.stylex.js
│ │ └── ocean-theme.stylex.js
│ └── components/
│ └── button.stylex.js
├── ContentBlocks/ContentElements/card/
│ ├── src/card.stylex.js
│ ├── templates/frontend.html
│ └── templates/backend-preview.html
├── Resources/Private/CSS/
│ └── rte.css # only when editor integration needs CSS
└── Resources/Public/
├── Assets/ # generated CSS, JS, fonts and chunks
└── StylexManifest/
└── stylex-manifest.json # generated manifest
Use the project's existing TYPO3 casing for paths such as
``Resources/Private``. Use lowercase kebab-case for new StyleX folders and
source filenames, for example ``card.stylex.js`` and
``ocean-theme.stylex.js``. Keep source files out of published ``assets/``
directories; put Content Block build inputs in a private ``src/`` folder.
Token families and names
========================
Tokens name reusable values; recipes describe how components use those
values. Use ``stylex.defineVars()`` for themeable or runtime-overridable
values, and use ``stylex.defineConsts()`` for shared compile-time constants
that do not need theming. A useful family split is color, typography, spacing,
layout, shape, elevation and motion, with a component-specific family only
when a stable component contract warrants it.
.. code-block:: javascript
// tokens/color-tokens.stylex.js
import * as stylex from '@stylexjs/stylex';
export const colors = stylex.defineVars({
textPrimary: '#171717',
textSecondary: '#555555',
surface: '#ffffff',
accent: '#005fcc',
onAccent: '#ffffff',
});
// tokens/spacing-tokens.stylex.js
import * as stylex from '@stylexjs/stylex';
export const spacing = stylex.defineVars({
compact: '0.5rem',
normal: '1rem',
spacious: '1.5rem',
});
Prefer semantic roles such as ``textPrimary``, ``surface`` and
``statusError``. Avoid names tied to a component's temporary appearance, a
raw hex value, or a theme/mode such as ``oceanBlue`` or ``darkText``. When
raw CSS needs a stable public custom property, define an explicit,
namespaced bridge and generate it from the same token source where possible;
do not couple CSS to StyleX's compiler-generated variable identifiers.
Put token references into recipes rather than copying their values:
.. code-block:: javascript
// components/button.stylex.js
import * as stylex from '@stylexjs/stylex';
import { colors } from '../tokens/color-tokens.stylex.js';
import { spacing } from '../tokens/spacing-tokens.stylex.js';
export const styles = stylex.create({
root: {
color: colors.onAccent,
backgroundColor: colors.accent,
paddingBlock: spacing.compact,
paddingInline: spacing.normal,
},
});
Use token values across independent components when they represent a shared
design decision. Keep local structural values such as a one-off display or
layout rule inside the recipe instead of turning every literal into a public
token.
Recipes and manifest keys
=========================
Use one recipe module per reusable component or cohesive block. Keep styles
local to the elements that use them, and use named slots and finite variants
such as ``root``, ``title``, ``variant_primary`` and ``size_small``. Avoid
global selector trees and avoid encoding every combination as a new recipe.
Apply conditions with the project's supported StyleX composition pattern or
with the Connector's static ``when``/``else`` manifest keys.
.. code-block:: html
{data.header}