Style helpers
The style helpers return the same token classes the components use, for native elements the package doesn’t wrap. They don’t call React hooks, despite the use prefix, so they work in Server Components.
import { clsx, cva, useBorderRadius, useBorderStyle, useFontFamily, useFontWeight, useMargin, usePadding } from '@blocks/design-system'Source · foundations/style-helpers.tsx
// The style helpers return class names from the tokens, for elements the
// package doesn't wrap. None calls a React hook, so they work on the server.
import {
clsx,
cva,
useBorderRadius,
useBorderStyle,
useFontFamily,
useFontWeight,
useMargin,
usePadding,
} from '@blocks/design-system'
const reading = cva({ base: 'reading', variants: { state: { fresh: 'reading-fresh', stale: 'reading-stale' } } })
export default function StyleHelpers() {
const padding = usePadding({ paddingX: 'md', paddingY: 'sm' }).className
const margin = useMargin({ marginBottom: 'sm' }).className
const border = useBorderStyle({ borderColor: 'border', borderWidth: 'sm', borderStyle: 'dashed' }).className
const radius = useBorderRadius({ borderRadius: 'md' }).className
const font = useFontFamily({ fontFamily: 'monospace' }).className
const weight = useFontWeight({ fontWeight: 'semibold' }).className
return (
<figure className={clsx(padding, border, radius, reading({ state: 'fresh' }))} style={{ margin: 0 }} aria-label="Raw reading">
<figcaption className={clsx(margin, weight)}>Latest raw reading</figcaption>
<output className={font}>SD=84.0in SWE=31.2in T=-4.1C</output>
</figure>
)
}| Helper | Returns |
|---|---|
usePadding(props) | { className, restProps } for padding, paddingX… (responsive) |
useMargin(props) | { className, restProps } for margin, marginX… (responsive) |
useBorderStyle(props) | { className } for borderColor, borderWidth, borderStyle |
useBorderRadius(props) | { className } for borderRadius |
useFontFamily(props) | { className } for fontFamily |
useFontWeight(props) | { className } for fontWeight |
useControlClassName(props) | Input’s control classes, see Label, Input and Textarea |
clsx(...values) | Joined class names, skipping falsy values |
cva(config) | A function from variant props to class names |