style
Reactive derivation of CSS custom properties directly on the host element.
TypeScript Signature
export const style: Record<string, ((context: { state: Record<string, any>; isServer: boolean }) => string | number | null | undefined | false) | string | number>
Declarative Styling
Instead of manually mutating DOM classes or inline styles inside your client() block, you can use the style export. It takes an object where the keys are CSS properties (or custom properties) and the values are either static values or synchronous functions that receive a context object with state.
export const style = {
'--card-bg': ({ state }) => state.isDark ? '#0f172a' : '#f8fafc',
'--card-text': ({ state }) => state.isDark ? '#f8fafc' : '#0f172a',
opacity: ({ state }) => state.isDisabled ? 0.5 : 1
}
Synchronous Constraint
Style functions must be strictly synchronous. If you return a Promise, Coralite will throw an error and fail the render.
You cannot use async/await inside the style block. If you need asynchronous data to compute a style, fetch it in server() and place it in state.
Nullish Omission
Returning null, undefined, or false from a style function removes the CSS property from the host element entirely. Numeric 0 is preserved as a valid CSS value.
export const style = {
// Removes margin if state.margin is null/undefined
margin: ({ state }) => state.margin
}