Dropdown menu example

Checkbox and radio preferences.

Controlled checkbox and radio values remain editable while the menu stays open.

Complete React client module · MIT · Next.js and Vite

Interactive example

Checkbox and radio preferences

Controlled checkbox and radio values remain editable while the menu stays open.

Keyboard

  • Space toggles Show labels without closing the menu.
  • Move to a spacing option and press Space or Enter to choose it.
  • Escape or Close view preferences returns focus to the trigger.

States covered

  • Controlled popup
  • Controlled checkbox
  • Controlled radio group
  • Disabled radio option
  • State retained across reopening

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-dropdown-preferences.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useState } from 'react';
import { Button, DropdownMenu, type DropdownMenuEntry } from '@bld8/ui';

export function ViewPreferencesExample() {
  const [open, setOpen] = useState(false);
  const [labels, setLabels] = useState(true);
  const [spacing, setSpacing] = useState('comfortable');
  const items: DropdownMenuEntry[] = [
    { type: 'group', label: 'Display', items: [
      { type: 'checkbox', label: 'Show labels', checked: labels, onCheckedChange: setLabels },
    ] },
    { type: 'separator' },
    { type: 'radio-group', label: 'Row spacing', value: spacing, onValueChange: setSpacing, items: [
      { value: 'compact', label: 'Compact spacing' },
      { value: 'comfortable', label: 'Comfortable spacing' },
      { value: 'automatic', label: 'Automatic spacing', disabled: true },
    ] },
    { type: 'separator' },
    { label: 'Close view preferences', onSelect: () => setOpen(false) },
  ];
  return <div style={{ display: 'flex', flexDirection: 'column', gap: 20, width: '100%' }}>
    <DropdownMenu trigger={<Button variant="secondary">View preferences</Button>} items={items} open={open} onOpenChange={setOpen} />
    <p role="status">{labels ? 'Labels visible' : 'Labels hidden'} · {spacing === 'compact' ? 'Compact spacing' : 'Comfortable spacing'}</p>
  </div>;
}