Native Select
The browser's own select in a drawn box: the platform picker, a drawn outline and chevron.
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
export default function NativeSelectDemo() {
return (
<NativeSelect aria-label="Status" defaultValue="" seed="ns-demo" className="w-52">
<NativeSelectOption value="" disabled>
Select status
</NativeSelectOption>
<NativeSelectOption value="todo">Todo</NativeSelectOption>
<NativeSelectOption value="in-progress">In progress</NativeSelectOption>
<NativeSelectOption value="done">Done</NativeSelectOption>
<NativeSelectOption value="cancelled">Cancelled</NativeSelectOption>
</NativeSelect>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ native-select"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { InkGlyph } from "@/lib/ink-glyphs";
import { InkOutline } from "@/lib/ink-outline";
/**
* The browser's own <select> in a drawn box. It gives up the drawn popup
* for the platform's picker, which is what you want on a phone and what
* forms posting plain values expect. `className` styles the box; every
* other prop goes to the <select>.
*/
function NativeSelect({
className,
size = "default",
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: Omit<ComponentProps<"select">, "size"> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { size?: "sm" | "default"; seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<div
data-slot="native-select-wrapper"
data-size={size}
className={cn("group/native-select ink-within relative inline-flex h-10 w-fit items-center has-[select:disabled]:opacity-50 data-[size=sm]:h-8", className)}
>
<InkOutline
pen={pen}
seed={seed}
estimate={[200, 40]}
focusPass
className={cn(
"text-ink-3 transition-colors duration-(--dur-hover) group-hover/native-select:text-ink group-focus-within/native-select:text-ink",
"group-has-[select[aria-invalid=true]]/native-select:text-destructive",
)}
/>
<select
data-slot="native-select"
data-size={size}
className="relative h-full w-full min-w-0 cursor-pointer appearance-none bg-transparent pr-9 pl-3 text-base text-foreground outline-none disabled:cursor-not-allowed data-[size=sm]:text-sm"
{...props}
>
{children}
</select>
<InkGlyph name="chevron-down" className="pointer-events-none absolute right-3 size-4 text-ink-3" />
</div>
);
}
function NativeSelectOption(props: ComponentProps<"option">) {
return <option data-slot="native-select-option" {...props} />;
}
function NativeSelectOptGroup(props: ComponentProps<"optgroup">) {
return <optgroup data-slot="native-select-optgroup" {...props} />;
}
export { NativeSelect, NativeSelectOptGroup, NativeSelectOption };Usage
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select"
<NativeSelect defaultValue="todo">
<NativeSelectOption value="todo">Todo</NativeSelectOption>
<NativeSelectOption value="done">Done</NativeSelectOption>
</NativeSelect>Examples
Groups
Options under headings with NativeSelectOptGroup.
import { NativeSelect, NativeSelectOptGroup, NativeSelectOption } from "@/components/ui/native-select";
export default function NativeSelectGroups() {
return (
<NativeSelect aria-label="Department" defaultValue="eng" seed="ns-groups" className="w-56">
<NativeSelectOptGroup label="Engineering">
<NativeSelectOption value="eng">Frontend</NativeSelectOption>
<NativeSelectOption value="backend">Backend</NativeSelectOption>
<NativeSelectOption value="devops">DevOps</NativeSelectOption>
</NativeSelectOptGroup>
<NativeSelectOptGroup label="Sales">
<NativeSelectOption value="sales">Sales</NativeSelectOption>
<NativeSelectOption value="support">Support</NativeSelectOption>
</NativeSelectOptGroup>
</NativeSelect>
);
}Sizes and states
A shorter size, an invalid select and a disabled one.
The small size is a little shorter.
Pick a country to carry on.
import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field";
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
export default function NativeSelectStates() {
return (
<div className="flex w-full max-w-xs flex-col gap-6">
<Field>
<FieldLabel htmlFor="ns-size">Size</FieldLabel>
<NativeSelect id="ns-size" size="sm" defaultValue="m" seed="ns-sm" className="w-full">
<NativeSelectOption value="s">Small</NativeSelectOption>
<NativeSelectOption value="m">Medium</NativeSelectOption>
</NativeSelect>
<FieldDescription>The small size is a little shorter.</FieldDescription>
</Field>
<Field data-invalid>
<FieldLabel htmlFor="ns-country">Country</FieldLabel>
<NativeSelect id="ns-country" aria-invalid defaultValue="" seed="ns-invalid" className="w-full">
<NativeSelectOption value="" disabled>
Choose one
</NativeSelectOption>
<NativeSelectOption value="fr">France</NativeSelectOption>
<NativeSelectOption value="ma">Morocco</NativeSelectOption>
</NativeSelect>
<FieldError>Pick a country to carry on.</FieldError>
</Field>
<Field>
<FieldLabel htmlFor="ns-disabled">Plan</FieldLabel>
<NativeSelect id="ns-disabled" disabled defaultValue="free" seed="ns-disabled" className="w-full">
<NativeSelectOption value="free">Free</NativeSelectOption>
</NativeSelect>
</Field>
</div>
);
}API reference
- sizedefault
"default" "default" | "sm"Height of the box.
- className
stringStyles the drawn box (width, margins). Every other prop goes to the <select>.
- aria-invalid
booleanRedraws the box in red pen.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.