Overview
These are opinionated wrappers built on top of Base UI and shadcn/ui primitives. Composing raw primitives throughout an application leads to verbose, repetitive code — the wrappers give you a clean, consistent API while keeping full access to the underlying primitives when you need advanced control.
<SelectWrapper
placeholder="Pick a fruit"
items={["Apple", "Banana", { label: "Cherry", value: "cherry" }]}
/>Key Benefits
- Reduced boilerplate — pass an
itemsarray instead of composing triggers, portals, lists and items by hand. - Type safety — built-in types for option values, groups, separators and menus.
- Consistent API — the same item format works across Select, Combobox, Autocomplete, Radio, Checkbox, Toggle and every menu.
- Flexibility — every file also exports the plain shadcn parts for advanced use cases.
- Framework-agnostic — nothing depends on Next.js; router links are passed in.
- i18n ready — every built-in string can be translated through
UIProvider.
Get started
Use the components in one of two ways — pick the one that fits your project:
shadcn registry
Copy the source into your project with the shadcn CLI. Full control: edit the files directly.
npm package
Install @glraj/ui as a dependency. Update by bumping the version; theme app-wide with configureUI.
| shadcn registry | npm package | |
|---|---|---|
| Code lives in | your project | node_modules |
| Customise | edit the files directly | configureUI, className / *Cls props |
| Updates | re-run shadcn add (overwrites your edits) | bump @glraj/ui |
| Imports | @/components/ui/<name> | @glraj/ui/<name> |
Examples across these docs use the registry paths (@/components/ui/…). With the npm package, replace @/components/ui/ with @glraj/ui/ and @/lib/utils with @glraj/ui/utils.
Type Definitions
Core Types
The wrapper system supports three primitive value types that cover the majority of form and selection use cases:
type allowedPrimitiveT = string | number | boolean
type itemT = {
label: React.ReactNode
value: allowedPrimitiveT
className?: string
disabled?: boolean
}
type groupT = {
group: string
items: (allowedPrimitiveT | itemT)[]
className?: string
}
type itemsT = (allowedPrimitiveT | itemT | groupT)[]
type indicatorAtT = 'right' | 'left' | ''
With the registry these are global declaration files; the npm package exports them instead (import type { itemsT } from "@glraj/ui/types").
Design Rationale
Primitive Type Restriction: The system intentionally limits allowed values to string, number, and boolean. These types handle the vast majority of form scenarios.
Empty Values: Use empty strings ("") for empty values, or omit the property entirely. Extend the type system as needed for your specific requirements.
Items Configuration
Flexible Item Formats
The wrapper system supports multiple item formats to accommodate different use cases:
const items: itemsT = [
// Simple primitive values (label equals value)
"Data 1",
false,
12,
// Visual separator
"---",
// Object format with custom label
{
label: "Obj 1",
value: "obj-1",
},
// Object with custom styling
{
label: "Obj 2",
value: "obj-2",
className: "bg-red-50",
},
// Rich content with React nodes
{
value: "apple",
label: <><Apple className="mr-2" /> Apple</>
},
"---",
// Grouped items
{
group: "Group 1",
items: [
"grp 1",
21,
true,
{ value: "banana", label: <><Banana className="mr-2" /> Banana</> }
],
},
// Grouped items with styling
{
group: "Group 2",
items: ["grp 2", 22],
className: "bg-amber-50",
},
]Item Format Guidelines
- Primitive values: Use directly when the display label matches the value
- Separators: Use
"---"string for visual separation between items - Object format: Use when you need custom labels, styling, or rich content
- Grouped items: Organize related items under labeled groups
- Custom styling: Apply Tailwind classes via
classNamefor visual distinction
Value Conversion Pattern
Understanding String Conversion
Controlled components use string values internally. The wrapper system handles conversion between strings and primitive types transparently. Some components directly support type conversion. If a component doesn't support this feature, use the following guide.
Implementation Patterns
// ❌ Incorrect: Type mismatch
const [value, setValue] = useState(true)
<SelectWrapper
value={value}
onValueChange={setValue}
/>
// ✅ Correct: Convert to string
const [value, setValue] = useState("true")
<SelectWrapper
value={value}
onValueChange={setValue}
/>
// ✅ Recommended: Preserve primitive types
const [value, setValue] = useState<allowedPrimitiveT>(true)
<SelectWrapper
value={`${value}`}
onValueChange={v => setValue(parseAllowedPrimitive(v))}
/>Best Practices
- String State: Simplest approach for string-only values
- Primitive State with Conversion: Use
parseAllowedPrimitiveto maintain type fidelity - Template Literals: Use
`${value}`for type-safe string conversion - Type Annotations: Explicitly type state as
allowedPrimitiveTwhen using conversion
Text and i18n
Every built-in string — field placeholders, loading / empty states, pagination labels, close / cancel buttons, screen-reader labels — comes from UIProvider. Wrap your app once and override any subset; props on a component still win. Works the same with the registry (@/components/ui/ui-provider) and the npm package (@glraj/ui/ui-provider).
"use client"
import { UIProvider } from "@/components/ui/ui-provider"
export function Providers({ children }: { children: React.ReactNode }) {
const t = useTranslations("common") // any i18n library
return (
<UIProvider
text={{
enterPlaceholder: label => t("enterPlaceholder", { label }),
selectPlaceholder: label => t("selectPlaceholder", { label }),
loading: t("loading"),
noItems: t("noItems"),
rowsPerPage: t("rowsPerPage"),
pageOf: (page, pageCount) => t("pageOf", { page, pageCount }),
}}
>
{children}
</UIProvider>
)
}'use client'
import * as React from 'react'
type uiTextT = {
enterPlaceholder: (label: string) => string
selectPlaceholder: (label: string) => string
loading: string
noItems: string
noResults: string
cancel: string
confirm: string
close: string
remove: string
confirmTitle: string
confirmDescription: string
pickDate: string
pickDateTime: string
time: string
selectTime: string
previous: string
next: string
previousSlide: string
nextSlide: string
totalRows: (count: number) => string
rowsPerPage: string
pageOf: (page: number, pageCount: number) => string
firstPage: string
previousPage: string
nextPage: string
lastPage: string
toggleSidebar: string
}
const defaultUIText: uiTextT = {
enterPlaceholder: label => `Enter ${label}`,
selectPlaceholder: label => `Select ${label}`,
loading: 'Loading...',
noItems: 'No items found',
noResults: 'No matching results.',
cancel: 'Cancel',
confirm: 'Confirm',
close: 'Close',
remove: 'Remove',
confirmTitle: 'Are you absolutely sure?',
confirmDescription:
'This action cannot be undone. This will permanently remove your data from our servers.',
pickDate: 'Pick a date',
pickDateTime: 'Pick date & time',
time: 'Time',
selectTime: 'Select time',
previous: 'Previous',
next: 'Next',
previousSlide: 'Previous slide',
nextSlide: 'Next slide',
totalRows: count => `Total: ${count} row(s)`,
rowsPerPage: 'Rows per page',
pageOf: (page, pageCount) => `Page ${page} of ${pageCount}`,
firstPage: 'Go to first page',
previousPage: 'Go to previous page',
nextPage: 'Go to next page',
lastPage: 'Go to last page',
toggleSidebar: 'Toggle Sidebar',
}
const UITextContext = React.createContext<uiTextT>(defaultUIText)
type UIProviderProps = {
text?: Partial<uiTextT>
children: React.ReactNode
}
function UIProvider({ text, children }: UIProviderProps) {
const parent = React.useContext(UITextContext)
const value = React.useMemo(() => ({ ...parent, ...text }), [parent, text])
return <UITextContext.Provider value={value}>{children}</UITextContext.Provider>
}
function useUIText() {
return React.useContext(UITextContext)
}
export { UIProvider, useUIText, defaultUIText, type uiTextT, type UIProviderProps }
Utility Functions
import { isValidElement, type ReactNode } from 'react'
export { cn } from "cn"
export function isAllowedPrimitive(value: unknown): value is allowedPrimitiveT {
return ['string', 'number', 'boolean'].includes(typeof value)
}
export function parseAllowedPrimitive(value: allowedPrimitiveT): allowedPrimitiveT {
if (typeof value !== 'string') return value
const trimmed = value.trim()
if (trimmed === 'true') return true
if (trimmed === 'false') return false
if (/^-?\d+(\.\d+)?$/.test(trimmed)) return Number(trimmed)
return trimmed
}
export function itemTypeChecker<T>(key: keyof T) {
return (option: any): option is T => !!option && typeof option === 'object' && key in option
}
export const isSeparator = (item: any) => item === '---'
export const isOption = itemTypeChecker<itemT>('value')
export const isGroup = itemTypeChecker<groupT>('group')
export const getValue = (item: allowedPrimitiveT | itemT) =>
typeof item === 'object' ? item.value : item
export const getLabel = (item: allowedPrimitiveT | itemT) =>
typeof item === 'object' ? item.label : `${item}`
export function getKey(item: allowedPrimitiveT | itemT, i: number): string {
const val = getValue(item)
if (typeof val === 'boolean') return `key-${val}`
if (val === '---') return `---${i}`
return `${val}`
}
export function extractText(node: ReactNode): string {
if (node == null || typeof node === 'boolean') return ''
if (typeof node === 'string' || typeof node === 'number') return String(node)
if (isValidElement(node)) return extractText((node.props as { children?: ReactNode }).children)
if (Array.isArray(node)) return node.map(extractText).join('')
return ''
}