Components

A .solid file compiles to one component, the default export of the module.

#Parts of a file

<script module>
  export const MAX = 10;
</script>

<script>
  import Button from './Button.solid';

  interface Props {
    initial: number;
  }

  const props = $props<Props>();
  let count = $signal(props.initial);
</script>

<style>
  p {
    color: ${count > MAX ? 'red' : 'black'};
  }
</style>

<p>{count}</p>
<Button onClick=(() => count++) />
Part What it is
<script> The body of the component function. It runs once for each instance of the component.
<script module> Code at the top level of the module. It runs once, when the module loads.
<style> CSS scoped to the component. See Styles.
Everything else The markup that the component returns. See Markup.

A file can have one <script> and one <script module> at the top level, and any number of <style> blocks. The parts can come in any order. A block of markup, such as #for, can also start with its own <script>. See scripts in blocks.

The example compiles to this TSX:

import { createSignal as _createSignal } from 'solid-js';
import Button from './Button.solid';
import { useStyle as __useStyle } from 'solid-sfc/runtime';

export const MAX = 10;

interface Props {
  initial: number;
}
const __stylesheet = "p[s-x1y2z3]{color:var(--s-x1y2z3-0)}";

export default function Counter($$props: Props) {
  // Adds the stylesheet to the page while the component is rendered.
  __useStyle("s-x1y2z3", __stylesheet);
  const __styleVariables = () => ({
    '--s-x1y2z3-0': (count() > MAX ? 'red' : 'black'),
  });

  const props = $$props;
  let [count, setCount] = _createSignal(() => props.initial);

  return (
    <>
      <p s-x1y2z3 style={__styleVariables()}>{count()}</p>
      <Button onClick={() => setCount(current => ++current)} />
    </>
  );
}

#Component name

The component function is named after the file. my-button.solid becomes MyButton. A name that would start with a digit gets a Component prefix, so 404.solid becomes Component404. The name shows up in stack traces and developer tools.

A component can render itself by its name, which is useful for trees:

<!-- Tree.solid -->
<script>
  interface Node {
    name: string;
    children: Node[];
  }

  const { node } = $props<{ node: Node }>();
</script>

<li>
  {node.name}
  <ul>
    #for (child of node.children) {
      <Tree node=child />
    }
  </ul>
</li>

Because the name is taken, the file cannot import or declare anything else with the same name. To wrap a Button from a library in Button.solid, rename the import:

<script>
  import { Button as BaseButton } from 'ui-library';
</script>

#The script

The <script> block is always TypeScript, and plain JavaScript works in it unchanged. It needs no lang attribute. lang="ts" is allowed and changes nothing in the compiler or the editor. Add it if another tool you use needs it to read the script as TypeScript.

The compiler moves these statements out of the component function, to the top of the module:

  • Imports.
  • Interfaces and type aliases, including export interface and export type.

Everything else stays in the component function, in order.

The script cannot export values, because it runs once for each instance. Put shared values in <script module>:

<script module>
  export const sizes = ['small', 'medium', 'large'] as const;
  export type Size = (typeof sizes)[number];
</script>

<script>
  const props = $props<{ size: Size }>();
</script>

The script also cannot have export default or export *.

#Context

Create a context in <script module>, so it is created once and other files can import it. In Solid 2.0 the context is also its provider component:

<!-- ThemeProvider.solid -->
<script module>
  import { createContext } from 'solid-js';

  export const ThemeContext = createContext<'light' | 'dark'>('light');
</script>

<script>
  import type { Element } from 'solid-js';

  const props = $props<{ theme: 'light' | 'dark'; children: Element }>();
</script>

<ThemeContext value=props.theme>{props.children}</ThemeContext>

Another file imports the context by name and reads it with useContext:

<!-- ThemeLabel.solid -->
<script>
  import { useContext } from 'solid-js';
  import { ThemeContext } from './ThemeProvider.solid';

  const theme = useContext(ThemeContext);
</script>

<p>{theme}</p>

#Markup in the script

Elements in a script use the same syntax as the markup, not plain JSX. They are scoped by the <style> blocks of the file, like any other element:

<script>
  const icon = <Icon size=16 {label} />;

  function cell(item: Item) {
    return <td class={ done: item.done }>{item.title}</td>;
  }
</script>

< in a place where a value is expected starts an element, except before type parameters. <T,>, <T extends U> and <T>(…) followed by => or a return type are type parameters, so generic functions and function types work as in a .ts file. Write type assertions as value as T:

const id = <T>(value: T): T => value;
let pick: <T>(items: T[]) => T;
const count = input as number;

For markup that is only rendered, a #template is often clearer.

#Props

$props<T>() returns the props of the component. T becomes the type of the props parameter.

<script>
  interface Props {
    label: string;
    disabled?: boolean;
  }

  const props = $props<Props>();
</script>

<button disabled=props.disabled>{props.label}</button>

Read props through the object (props.label) to keep them reactive. To destructure them, destructure the call itself:

<script>
  const { label, disabled = false, ...rest } = $props<Props>();
</script>

<button {...rest} {disabled}>{label}</button>

Each destructured name becomes a reactive variable that reads the prop when it is used. disabled uses its default value when the prop is undefined. rest holds the remaining props, without label and disabled.

$props() without a type argument types the props as Record<string, any>. A component that never calls $props() takes no props.

#Generics

The generics attribute adds type parameters to the component function:

<script generics="T extends { id: string }">
  import type { Element } from 'solid-js';

  const { items, render } = $props<{ items: T[]; render: (item: T) => Element }>();
</script>

<ul>
  #for (item of items) {
    <li>{render(item)}</li>
  }
</ul>

This compiles to export default function List<T extends { id: string }>($$props: { ... }).

#Using components

Import a .solid file like any other module. The default export is the component.

import List from './List.solid';

<List items={users} render={(user) => user.name} />;

.solid components can also import components from .tsx files, and the other way around.