API
css.create
★★★★★ FrozenThe core styling API that most of your components are written with.
export type create = <const T extends Record<string, CreateStyleValue>>(rule: T)=> CreateReturnType<T>;
export const create: create;create defines styles as plain objects that are type-checked, autocompleted and linted.
Only the styles an element uses are compiled.
Example
import * as css from '@plumeria/core'
const styles = css.create({
container: {
display: 'flex',
justifyContent: 'space-between',
},
text: {
fontSize: '14px',
textDecoration: 'none',
},
})
export default function App() {
return (
<div
sx={[
styles.container,
styles.text
]}
/>
);
}The compiler replaces the call with its class names, and the import goes with it.
Compiled:
<div className="xxp6epoh xwbwwha1 xbief8v4 xzllkng3" />Generated CSS:
.xxp6epoh {
display: flex;
}
.xwbwwha1 {
justify-content: space-between;
}
.xbief8v4:not(#\#) {
font-size: 14px;
}
.xzllkng3 {
text-decoration: none;
}Each declaration compiles to one atomic class whose name is a hash of the property and the value. The same declaration written anywhere else reuses that class, and two different declarations never collide.
Dynamic values
A key whose value is a function is a function key. Calling it gives a dynamic style: the class names are fixed at build time, and the values reach the element as CSS variables.
const styles = css.create({
palette: (color: string) => ({
backgroundColor: color,
}),
});
export const Color = ({ color }) => {
return (
<span sx={styles.palette(color)}>Dynamic Color</span>
)
}Pass a plain value. The argument is written into the element, so an expression that is not the same on every read, such as a call with a side effect or a getter, may be read more than once. A definition such as a css.createStatic value is not compiled there and has no value at runtime, so read it inside css.create instead.
A number takes px the way it does in a static style, so width gets one and opacity does not. A number inside a color, filter or scale function, such as hsl(), brightness() or scaleX(), stays bare.
Variants bracket notation
A variant is read with bracket notation, as in variantStyles[size]. The compiler turns each such read into a static lookup table at build time.
import * as css from '@plumeria/core';
const styles = css.create({
base: {
display: 'inline-flex',
borderRadius: '4px',
backgroundColor: 'blue',
color: 'white',
}
});
const sizeStyles = css.create({
small: {
fontSize: '12px',
padding: '4px 8px',
},
large: {
fontSize: '18px',
padding: '12px 24px',
}
});
type Size = 'small' | 'large';
// can also use the type exhaustively via typeof sizeStyles
export const Button = ({ size }: { size: Size }) => {
return (
<button sx={[styles.base, sizeStyles[size]]}>Button</button>
);
};Function keys are ignored
Function keys within the style object are ignored during bracket notation lookup table compilation.
The last source wins
An element takes a list, and a property set by more than one entry keeps the value from the one written last. That is what lets a variant override the base above.
The rule holds whatever the entries are — a plain style, a condition, a bracket group. Swapping two of them swaps which one applies:
const styles = css.create({
base: { color: 'white' },
muted: { color: 'slategray' },
});
const toneStyles = css.create({
brand: { color: 'royalblue' },
danger: { color: 'crimson' },
});
// muted wins — it is written last
<button sx={[styles.base, toneStyles[tone], isMuted && styles.muted]} />
// toneStyles wins — now it is
<button sx={[styles.base, isMuted && styles.muted, toneStyles[tone]]} />All three set color here. Entries that touch different properties never compete, so their order does not matter — [styles.base, sizeStyles[size]] above reads the same either way.
Merging goes property by property, and that includes nested blocks. Overriding a :hover color leaves the rest of the :hover block in place.
This merging rule applies to the style list. Object spreads inside a style definition follow JavaScript semantics: a later property replaces the whole earlier value, including a nested object.
One pair the list does not settle is a shorthand and a longhand of the same property — margin against marginTop. The longhand wins wherever the two meet, whichever is written last. See Specificity.
Conditions with bracket notation
The condition can sit inside the brackets or around them. Both compile to a single lookup:
<button sx={sizeStyles[isLarge ? keyA : keyB]} />
<button sx={isLarge ? sizeStyles[keyA] : sizeStyles[keyB]} />
<button sx={isActive && sizeStyles[key]} />Compiled:
({"small":"xhrr6ses x1v10r0u","large":"xpdfgu2b xj7b4jff"}[isLarge ? keyA : keyB] || "")The branches of a condition are mutually exclusive — only one of them ever applies — so the compiler folds the condition into the key expression instead of expanding a combination per branch.
What the lookup holds depends on how the condition is written:
| Written as | Lookup holds |
|---|---|
sizeStyles[isLarge ? keyA : keyB] | the keys of sizeStyles |
isLarge ? sizeStyles[keyA] : sizeStyles[keyB] | the same |
isLarge ? sizeStyles[keyA] : themeStyles[keyB] | the keys of both objects |
isLarge ? sizeStyles.large : sizeStyles.small | nothing — see below |
Reaching into two different objects is fine; the table simply carries both key sets.
When both keys are literals, read the property directly. A condition between two styles the compiler already knows needs no table at all:
- <button sx={sizeStyles[isLarge ? 'large' : 'small']} />
+ <button sx={isLarge ? sizeStyles.large : sizeStyles.small} />Compiled:
(isLarge ? "xpdfgu2b xj7b4jff" : "xhrr6ses x1v10r0u")Keep common properties in a base style
Properties that do not vary belong in a base style — styles.base above — rather than repeated across every key of a variant object.
Anything the variants do not compete for stays outside the decision table, so it is emitted once instead of being multiplied into every combination.
Zero-runtime logic
Variants, dynamic props and conditional expressions are all resolved at compile time. Nothing merges objects or concatenates class strings in the browser, as many CSS-in-JS libraries do at every render.
1. Only conflicting axes are multiplied
The compiler builds a property conflict graph across the styles an element can receive, then expands only the axes that actually collide.
- Independent axes — a
sizeand athemeobject touching entirely different properties — stay separate lookups. Two axes of three options cost3 + 3entries. - Conflicting axes — two objects that both set
color, say — are grouped, and only that group becomes a combined decision table. The same two axes now cost3 × 3. - Mutually exclusive branches — the arms of a
?:or&&— are never a product at all. Only one arm applies, so they share one table keyed by which arm won.
Grouping is per conflict, not per element. Two objects competing for color and two more competing for fontSize are two groups on the same element, costing 3 × 3 plus 3 × 3 rather than one table of 3 × 3 × 3 × 3.
It is also transitive, because overriding is: an object that sets both color and fontSize sits between those two groups and folds them into one, which is the answer that table has to give.
The output is therefore bound by the element's local axes of conflict, not by how many styles are in play. Stacking variants that never touch the same property stays additive however many you add.
2. Constant runtime cost
Whatever the table grows to is paid at build time. At runtime the browser reads a single entry from a flat, fully pre-computed object in — no object allocation, no string building, and nothing for the garbage collector to reclaim.
Atomic static API
An atomic class is emitted once and reused wherever the same declaration appears, so the stylesheet grows with the number of distinct declarations, not with the number of components. The compiler also removes the imports.