API reference
Every export of solid-styled, solid-styled/compiler and unplugin-solid-styled, with signatures and notes.
solid-styled
Exports you call
css
const css: (template: TemplateStringsArray, ...spans: (string | boolean)[]) => void;- A tagged template the compiler replaces. See Styling.
- Calling it at runtime throws
Unexpected use of `css`. Make sure that solid-styled's plugin is setup correctly. - The
spanstype only accepts strings and booleans. See Known limits.
StyleRegistry
function StyleRegistry(props: StyleRegistryProps): JSX.Element;
interface StyleRegistryProps {
auto?: boolean;
nonce?: string;
root?: ShadowRoot | Element;
styles?: StyleData[];
children?: JSX.Element;
}
interface StyleData {
id: string;
sheet: string;
}stylescollects every sheet used during a server render. Each sheet is pushed once per registry.autoinserts the collected sheets into the head throughuseHead.nonceis put on every<style>element the registry inserts. See Content Security Policy.rootis the node the sheets are inserted into on the client. It defaults todocument.head. Pass theShadowRootwhen the tree renders inside one. It is read once, when the registry mounts, and has no effect on the server.- On the client,
autoandstyleshave no effect. Sheets go intoroot, ordocument.headwithout it. - The registry is optional on the client. See SSR.
renderSheets
function renderSheets(sheets: StyleData[], nonce?: string): string;- Returns one
<style s:id="...">element per entry, as a single string. nonceis put on every element asnonce="...". See Content Security Policy.
Compiler output helpers
The compiled code imports these. You normally do not call them.
useSolidStyled
function useSolidStyled(id: string, offset: string, sheet: string): void;- Inserts the sheet under the id
${id}-${offset}and removes it on cleanup. On the server it only records the sheet in the registry.
useSolidStyledGlobal
function useSolidStyledGlobal(
id: string,
offset: string,
sheet: string,
vars?: () => Record<string, string>,
): void;- Used by
<style jsx global>. Inserts the sheet and sets the values fromvarson the document root.
createCSSVars
function createCSSVars(): (vars?: () => Record<string, string>) => JSX.CSSProperties | undefined;- Returns a holder. Calling it with a function registers values. Calling it with no argument returns the merged values as a style object.
mergeStyles
function mergeStyles(
source: JSX.CSSProperties | string | null | undefined,
other: JSX.CSSProperties,
): string;- Serializes both styles and joins them into one style string.
othercomes last.
spreadStyle
function spreadStyle(
input: unknown,
// Holders returned by `createCSSVars`.
...varsList: ((vars?: () => Record<string, string>) => JSX.CSSProperties | undefined)[]
): unknown;- Wraps a spread on an element with CSS variables. A source with a
stylekey gets that style merged with the variables, through Solid'smerge. Any other source is returned as is. - See Spread attributes.
Types
StyleRegistryPropsandStyleData, shown above.CSSConstructor, the type ofcss.SolidStyledVariables, an alias ofRecord<string, string>.- The package types add
jsxandglobalto<style>, anduse:solid-styledtoJSX.IntrinsicAttributes, in the@solidjs/webJSXnamespace. See TypeScript.
solid-styled/compiler
compile
function compile(id: string, code: string, options?: SolidStyledOptions): CompileOutput;
interface SolidStyledOptions {
verbose?: boolean;
prefix?: string;
browserslist?: string;
}
interface CompileOutput {
code: string;
map: SourceMap;
}import { compile } from 'solid-styled/compiler';
const { code, map } = compile('src/App.tsx', source, {
prefix: 'my-app',
});compileis synchronous.idis the module path. Its query string is ignored for the scope hash.- Files ending in
.ts,.tsx,.mtsor.ctsare parsed as TSX. Every other file is parsed as JSX. mapis a magic-string source map withhires: true.- The options are the plugin options without
filter. See Configuration.
CompileError
class CompileError extends Error {
// `name` is 'CompileError'.
line: number;
column: number;
}lineandcolumnare 1-based, and the message ends with(line:column).compilethrows it for a parse error, a CSS error in a template, or an invalidstylevalue.- It also throws it for an arrow function with an expression body that needs CSS variables.
- An invalid
browserslistoption throws the browserslist error instead.
unplugin-solid-styled
import solidStyled from 'unplugin-solid-styled';
solidStyled.vite(options);- The default export is an unplugin instance.
- Entries:
vite,rollup,rolldown,webpack,rspack,rsbuild,esbuild,farm,bunandunloader.rawreturns the plain plugin object. optionsisSolidStyledOptionsplusfilter: { include?, exclude? }. Both filter fields take aFilterPatternfrom@rollup/pluginutils. See Configuration.- The types
SolidStyledPluginOptionsandSolidStyledPluginFilterare exported. - Only the Vite entry reorders itself before the Solid plugin. See Plugin order.