Restructure the repo into npm workspaces so the component kit publishes as its own package, like MUI and shadcn/ui: - packages/ui: @openkhalix/tansy-ui (published), with components, stories, tests, tokens and the library build - packages/brand: private logo and catalogue data shared by both apps - apps/site: the landing page - apps/docs: Storybook The package: - MIT licence (copyright OpenKhalix) - per-component ES modules and type declarations - "use client" on component modules so they import into Next.js Server Components; helpers stay server-safe - tokens.css for Tailwind v4 apps, and a prebuilt styles.css for apps without Tailwind - lucide-react 1.x, whose types compile against current React 19 types The apps resolve the package through a "source" export condition, so they hot-reload component edits, while outside users get dist/. scripts/test-package.mjs packs the package and installs the tarball into Vite + Tailwind (strict types), plain React and Next.js apps; each must build and render correctly. Gitea workflows run CI on push and publish to npm when a vX.Y.Z tag matching the package version is pushed.
148 lines
5.2 KiB
TypeScript
148 lines
5.2 KiB
TypeScript
import { Field as BaseField } from '@base-ui/react/field'
|
|
import type { ComponentProps, ReactNode } from 'react'
|
|
import { cn } from '../lib/cn'
|
|
import { controlHeight, controlPaddingX, focusRing, iconSize } from '../lib/styles'
|
|
import { Field } from './Field'
|
|
import { Chevron, Spinner } from './SelectParts'
|
|
import { FIELD_BOX, groupOptions, type SelectOption, type SelectSize, type SelectVariant } from './select.tokens'
|
|
|
|
export type NativeSelectProps = Omit<
|
|
ComponentProps<'select'>,
|
|
'size' | 'value' | 'defaultValue' | 'multiple' | 'className' | 'children'
|
|
> & {
|
|
/** Visible label, linked to the select. */
|
|
label?: ReactNode
|
|
/** Help text under the select, announced with it. */
|
|
description?: ReactNode
|
|
/** Error message. Marks the select invalid and replaces `description`. */
|
|
error?: ReactNode
|
|
size?: SelectSize
|
|
/** `outline` draws a bordered box; `filled` a tinted one. */
|
|
variant?: SelectVariant
|
|
/** The choices. Options sharing a `group` become an `<optgroup>`. */
|
|
options: SelectOption[]
|
|
/** A first, empty option shown while nothing is chosen. */
|
|
placeholder?: string
|
|
/** The selected value (controlled). `''` selects the placeholder. */
|
|
value?: string
|
|
/** The initial value (uncontrolled). */
|
|
defaultValue?: string
|
|
/** Fires with the new value. Native `onChange` still works too. */
|
|
onValueChange?: (value: string) => void
|
|
/** Disables the select and shows a spinner while options load. */
|
|
loading?: boolean
|
|
/** Icon before the value. */
|
|
startIcon?: ReactNode
|
|
/** Classes for the outer field wrapper. Use `selectClassName` for the select itself. */
|
|
className?: string
|
|
selectClassName?: string
|
|
}
|
|
|
|
const START_ICON_PAD: Record<SelectSize, string> = { sm: 'pl-8', md: 'pl-9', lg: 'pl-11' }
|
|
const END_PAD: Record<SelectSize, string> = { sm: 'pr-8', md: 'pr-9', lg: 'pr-11' }
|
|
const ICON_LEFT: Record<SelectSize, string> = { sm: 'left-2.5', md: 'left-3', lg: 'left-4' }
|
|
const ICON_RIGHT: Record<SelectSize, string> = { sm: 'right-2.5', md: 'right-3', lg: 'right-4' }
|
|
|
|
/**
|
|
* A styled native `<select>`, with its label above. It uses the platform's
|
|
* own picker, which suits mobile and simple forms. For search, multiple
|
|
* values or rich options, use Select, FeaturedSelect or Combobox.
|
|
*/
|
|
export function NativeSelect({
|
|
label,
|
|
description,
|
|
error,
|
|
required,
|
|
disabled,
|
|
size = 'md',
|
|
variant = 'outline',
|
|
options,
|
|
placeholder,
|
|
value,
|
|
defaultValue,
|
|
onValueChange,
|
|
loading,
|
|
startIcon,
|
|
name,
|
|
className,
|
|
selectClassName,
|
|
...props
|
|
}: NativeSelectProps) {
|
|
const invalid = error != null && error !== false && error !== ''
|
|
// Without a placeholder, an uncontrolled select starts on its first option.
|
|
const initial = defaultValue ?? (placeholder != null && value === undefined ? '' : undefined)
|
|
|
|
return (
|
|
<Field
|
|
label={label}
|
|
description={description}
|
|
error={error}
|
|
required={required}
|
|
disabled={disabled || loading}
|
|
name={name}
|
|
className={className}
|
|
>
|
|
<div className={cn('relative w-full', iconSize[size])}>
|
|
{startIcon != null && (
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn('pointer-events-none absolute top-1/2 flex -translate-y-1/2 text-fg-muted', ICON_LEFT[size])}
|
|
>
|
|
{startIcon}
|
|
</span>
|
|
)}
|
|
<BaseField.Control
|
|
value={value}
|
|
defaultValue={initial}
|
|
onValueChange={onValueChange ? (next) => onValueChange(next) : undefined}
|
|
required={required}
|
|
aria-invalid={invalid || undefined}
|
|
aria-busy={loading || undefined}
|
|
render={(controlProps) => (
|
|
<select {...controlProps}>
|
|
{placeholder != null && (
|
|
<option value="" disabled={required}>
|
|
{placeholder}
|
|
</option>
|
|
)}
|
|
{groupOptions(options).map((group) => {
|
|
const items = group.items.map((option) => (
|
|
<option key={option.value} value={option.value} disabled={option.disabled}>
|
|
{option.label}
|
|
</option>
|
|
))
|
|
return group.label === null ? (
|
|
items
|
|
) : (
|
|
<optgroup key={group.label} label={group.label}>
|
|
{items}
|
|
</optgroup>
|
|
)
|
|
})}
|
|
</select>
|
|
)}
|
|
className={cn(
|
|
'w-full cursor-pointer appearance-none truncate',
|
|
controlHeight[size],
|
|
controlPaddingX[size],
|
|
FIELD_BOX[variant],
|
|
focusRing,
|
|
END_PAD[size],
|
|
startIcon != null && START_ICON_PAD[size],
|
|
'has-[option[value=""]:checked]:text-fg-muted [&_optgroup]:bg-surface [&_optgroup]:text-fg-muted [&_option]:bg-surface [&_option]:text-fg',
|
|
'disabled:cursor-not-allowed disabled:opacity-55',
|
|
selectClassName,
|
|
)}
|
|
{...(props as ComponentProps<typeof BaseField.Control>)}
|
|
/>
|
|
<span
|
|
aria-hidden="true"
|
|
className={cn('pointer-events-none absolute top-1/2 flex -translate-y-1/2 text-fg-muted', ICON_RIGHT[size])}
|
|
>
|
|
{loading ? <Spinner /> : <Chevron />}
|
|
</span>
|
|
</div>
|
|
</Field>
|
|
)
|
|
}
|