Skip to content

UI components ​

Weft's primitives (Pressable, TextField, Checkbox, Switch, Select, Popover, Dialog…) are headless: they have behaviour, keyboard, focus, accessibility and their states, but no look. The look is code your project owns. The one thing they draw is what overlays need to be readable: a popover, menu, tooltip or toast is opaque on the theme's popover colours (with a 1px border until you give it a class), a dialog, sheet or drawer on its background colours, and a scrim dims the page. Any class you give wins over that. Weft ships a registry of styled components, shadcn/ui's, written in Weft on the primitives, and weft component copies them into your project, the way shadcn's CLI does.

Adding components ​

sh
weft component list                 # what the registry has
weft component button dialog card   # copies them into components/ui/

Each component lands in components/ui/ as a .weft file you can read and change: Button.weft, Dialog.weft, Card.weft with CardHeader.weft and the rest. The registry components one uses come along (alert-dialog brings button). The first time, it also writes theme.weft: the theme as Weft (colours for light and dark, the radius, fonts, shadows), evaluated into the theme's tokens whenever the project is checked or built. Change a value there and every component follows; a component added later brings the tokens it needs into it, leaving yours.

theme.weft
weft
import { Theme, oklch, rem } from "std/theme"

let radius = rem(0.625)

export let theme: Theme = {
  light: { primary: oklch(0.205, 0, 0), primaryForeground: oklch(0.985, 0, 0) },
  dark: { primary: oklch(0.922, 0, 0), primaryForeground: oklch(0.205, 0, 0) },
  radius,
  radiusSm: radius * 0.6, // follows `radius`
}

Each field is a theme token, which classes name in kebab case: primaryForeground is what bg-primary-foreground paints with. A token left out keeps the standard theme's value. radiusSm: radius * 0.6 is a length expression in the style model, so it is recomputed when radius changes, on every renderer. The whole of it: std/theme.

On the web

The web renderer emits tokens as CSS variables: primaryForeground is --primary-foreground, and radiusSm is calc(var(--radius) * 0.6).

A file you changed is yours: running the command again keeps it and says so; --force replaces it with the registry's. --dir names the project when you are not inside it.

Using them ​

Components under components/ are in scope by their file name, so a page uses them without imports. variant and size are their props, and class adds to or overrides their look:

components/ui/Tag.weft
weft
// A small styled component on a primitive, as the registry's are.
prop variant: "default" | "outline" = "default"
prop className: string = ""
---
<Box class={`inline-flex h-5 items-center rounded-4xl px-2 text-xs font-medium ${variant == "outline" ? "border text-foreground" : "bg-primary text-primary-foreground"} ${className}`}>
  @render children?.()
</Box>
routes/page.weft
weft
---
<Tag>New</Tag>
<Tag variant="outline" class="ml-2">Draft</Tag>

class is a reserved word, so a component receives the class its tag is given as its className prop.

Styling a primitive ​

A component may take a primitive's name: then it shadows the primitive, and reaches it under another name from the built-in primitives module. A primitive's own element takes class; the parts it draws inside itself take <part>Class props, styled by the primitive's states (checked:, focus-visible:, disabled:):

components/ui/Switch.weft
weft
import { Switch as SwitchPrimitive } from "primitives"

prop bind checked: boolean = false
prop label?: string
prop className: string = ""
---
<SwitchPrimitive bind:checked {label} class={`inline-flex items-center gap-2 text-sm ${className}`}
  indicatorClass="inline-flex h-[18.4px] w-[32px] items-center rounded-full bg-input checked:bg-primary"
  thumbClass="block size-4 rounded-full bg-background checked:translate-x-[calc(100%-2px)]" />

Overlays (Popover, Menu, Tooltip, Dialog) move only with the motion they are given: enter, exit, and slide px in from the side facing their anchor. The reference has every primitive's parts and props.

Weft is in development. The reference is the spec as it stands today.