Native Select
SaveA native select styled like Input, with label, helper, and error text, for simple lists and phone pickers.
- Category
- Forms
- Version
- 0.1.0
- Frameworks
- Next.js, Vite + React
- Styling
- Tailwind CSS v4
Preview
Native Select preview
Installation
npx @shakenui/cli add native-selectReview the plan first. A dry run lists every file and package without changing anything:
npx @shakenui/cli add native-select --dry-runUsage
native-select-example.tsx
import { NativeSelect } from "@/components/shaken/ui/native-select";
export default function NativeSelectExample() {
return (
<form className="grid w-full max-w-xs gap-5">
<NativeSelect label="Timezone" defaultValue="Europe/Berlin" description="Schedules and digests use this timezone.">
<optgroup label="Europe">
<option value="Europe/London">London (GMT+0)</option>
<option value="Europe/Berlin">Berlin (GMT+1)</option>
<option value="Europe/Istanbul">Istanbul (GMT+3)</option>
</optgroup>
<optgroup label="Americas">
<option value="America/New_York">New York (GMT-5)</option>
<option value="America/Los_Angeles">Los Angeles (GMT-8)</option>
</optgroup>
</NativeSelect>
<NativeSelect label="Retention" defaultValue="" error="Choose how long to keep run logs.">
<option value="" disabled>
Select a period
</option>
<option value="7">7 days</option>
<option value="30">30 days</option>
<option value="90">90 days</option>
</NativeSelect>
<NativeSelect aria-label="Rows per page" size="sm" defaultValue="25" containerClassName="w-28">
<option value="10">10 rows</option>
<option value="25">25 rows</option>
<option value="50">50 rows</option>
</NativeSelect>
</form>
);
}API
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | โ | Visible label, linked to the select. |
| description | ReactNode | โ | Helper text, linked with aria-describedby. |
| error | ReactNode | โ | Error message. Sets aria-invalid and replaces the description. |
| size | "sm" | "md" | "md" | 32px or 36px tall. |
| containerClassName | string | โ | Classes for the wrapper. className styles the select. |
| ...props | ComponentProps<"select"> | โ | Any native select attribute; pass <option> and <optgroup> children. |
Accessibility
- Renders a native <select>, so keyboard, typeahead, and screen reader behavior come from the browser.
- The label is bound with htmlFor; helper and error text are linked with aria-describedby, and errors set aria-invalid.
- In dark mode the select sets color-scheme: dark, so the system option list opens dark.
- The chevron is decorative and ignores pointer events, so clicks reach the select.
Files
Installed into src/components/shaken, or components/shaken when the project has no src directory.
src/components/shaken/ui/native-select.tsxnative-select- npm packages: noneReact and Tailwind CSS v4 required
Source
The exact source the CLI installs, from /r/items/react/native-select/0.1.0.json.
ui/native-select.tsxShowHide
ui/native-select.tsx
import { type ComponentProps, type ReactNode, useId } from "react";
function cx(...classes: (string | false | null | undefined)[]) {
return classes.filter(Boolean).join(" ");
}
const sizes = {
sm: "h-8 pr-8 pl-2.5",
md: "h-9 pr-9 pl-3",
} as const;
export interface NativeSelectProps extends Omit<ComponentProps<"select">, "size"> {
/** Visible label. Pass `aria-label` instead when the label is shown elsewhere. */
label?: ReactNode;
/** Helper text below the field. */
description?: ReactNode;
/** Error message. Marks the field invalid and replaces the description. */
error?: ReactNode;
/** Matches the Input height at `md`; `sm` for toolbars and dense forms. */
size?: keyof typeof sizes;
/** Classes for the wrapping element. `className` styles the select itself. */
containerClassName?: string;
}
/**
* A native `<select>` styled like Input. The browser draws the option list, so it works with
* no JavaScript, opens the system picker on phones, and suits short, simple lists. Pass
* `<option>` and `<optgroup>` children as usual. For icons, groups with custom rows, or a
* styled list, use Select.
*/
export function NativeSelect({
label,
description,
error,
size = "md",
id,
className,
containerClassName,
disabled,
"aria-describedby": describedBy,
children,
...props
}: NativeSelectProps) {
const generatedId = useId();
const selectId = id ?? generatedId;
const messageId = `${selectId}-message`;
const invalid = error != null && error !== false;
const message = invalid ? error : description;
return (
<div className={cx("grid gap-1.5", containerClassName)}>
{label ? (
<label htmlFor={selectId} className="text-sm leading-5 font-medium text-zinc-950 dark:text-white">
{label}
</label>
) : null}
<div className="group/select relative">
<select
id={selectId}
disabled={disabled}
aria-invalid={invalid || undefined}
aria-describedby={cx(message ? messageId : null, describedBy) || undefined}
className={cx(
"w-full min-w-0 appearance-none truncate rounded-lg border bg-white text-sm text-zinc-900 shadow-sm",
sizes[size],
"transition-[border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none",
"hover:border-zinc-400 dark:hover:border-zinc-600",
"focus-visible:border-sky-500 focus-visible:ring-3 focus-visible:ring-sky-500/20 focus-visible:outline-none",
"disabled:cursor-not-allowed disabled:bg-zinc-50 disabled:opacity-60",
// The system option list follows the theme instead of opening white on a black page.
"dark:bg-zinc-950 dark:text-zinc-100 dark:scheme-dark dark:disabled:bg-zinc-900",
invalid
? "border-red-400 hover:border-red-400 focus-visible:border-red-500 focus-visible:ring-red-500/20 dark:border-red-500/60 dark:hover:border-red-500/60"
: "border-zinc-300 dark:border-zinc-700",
className,
)}
{...props}
>
{children}
</select>
<svg
aria-hidden="true"
viewBox="0 0 16 16"
fill="none"
className={cx(
"pointer-events-none absolute top-1/2 size-4 -translate-y-1/2 text-zinc-400 dark:text-zinc-500",
"transition-colors duration-150 group-hover/select:text-zinc-600 motion-reduce:transition-none dark:group-hover/select:text-zinc-300",
size === "sm" ? "right-2" : "right-2.5",
)}
>
<path d="M4.5 6.25L8 9.75l3.5-3.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</div>
{message ? (
<p id={messageId} className={invalid ? "text-sm text-red-600 dark:text-red-400" : "text-sm text-zinc-500 dark:text-zinc-400"}>
{message}
</p>
) : null}
</div>
);
}