React

Snippet

Learn how to boost development efficiency by providing frequently used UI patterns as snippets.

Overview

Snippet is a solution to optimize developer experience (DX) while adopting Compound Components as the default. It's a pattern that helps you use complex compound components easily.

Compound Components

In the traditional closed component approach, component customization was limited. For example, with a closed component like:

<Checkbox>Click me</Checkbox>

The internal implementation was hidden, making it difficult to meet customization requirements like:

  • How to add styles to the label
  • How to change the checkbox icon
  • How to pass refs to div and input separately

To solve these issues, we adopted the compound components pattern:

<Checkbox.Root>
  <Checkbox.Control>
    <Checkbox.Indicator />
  </Checkbox.Control>
  <Checkbox.Label>Click me</Checkbox.Label>
  <Checkbox.HiddenInput />
</Checkbox.Root>

Why Snippet matters

Compound components provide excellent flexibility, but writing such detailed implementations each time can be tedious. To solve this trade-off between flexibility and DX, the Snippet concept was introduced.

Using Snippet

Run CLI

You can add snippets of required components to your project using the following command:

npx @seed-design/cli add

CLI Commands

Learn more about CLI commands.

Using generated snippets

After running CLI, snippets are created in the seed-design/ui/* directory. For example:

// seed-design/ui/checkbox.tsx

export const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
  ({ inputProps, rootRef, label, variant = "square", ...otherProps }, ref) => {
    return (
      <SeedCheckbox.Root ref={rootRef} variant={variant} {...otherProps}>
        <SeedCheckbox.Control>
          <SeedCheckbox.Indicator
            unchecked={variant === "ghost" ? <IconCheckmarkFatFill /> : null}
            checked={<IconCheckmarkFatFill />}
            indeterminate={<IconMinusFatFill />}
          />
        </SeedCheckbox.Control>
        <SeedCheckbox.Label>{label}</SeedCheckbox.Label>
        <SeedCheckbox.HiddenInput ref={ref} {...inputProps} />
      </SeedCheckbox.Root>
    );
  }
);

Using it

Through Snippet, you can use complex compound components simply:

import { Checkbox } from "@/seed-design/ui/checkbox";

const Demo = () => <Checkbox>Click me</Checkbox>;

Table of contents