Combobox example

Keyboard focus and popup completion.

Keep typing focus, scroll long results and close options before a parent dialog.

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

Interactive example

Keyboard focus and popup completion

Keep typing focus, scroll long results and close options before a parent dialog.

Keyboard

  • ArrowDown highlights an option while focus remains in the search input.
  • Tab and Shift+Tab leave the search naturally; Escape restores the committed label.
  • Inside the dialog, Escape closes options first and the dialog second.

States covered

  • controlled popup
  • completion callbacks
  • interrupted motion
  • long results
  • dialog ownership
  • native focus

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-combobox-focus-lifecycle.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 { useRef, useState } from 'react';
import { Button, Combobox, Dialog, DialogClose, UIProvider, type Option } from '@bld8/ui';
import '@bld8/ui/styles.css';
const rooms: Option[] = Array.from({ length: 36 }, (_, index) => ({ value: 'room-' + (index + 1), label: 'Review room ' + String(index + 1).padStart(2, '0') }));
export default function ComboboxFocusLifecycleExample() {
  const [value, setValue] = useState<string | null>('room-1');
  const [open, setOpen] = useState(false);
  const [completed, setCompleted] = useState<string[]>([]);
  const [outside, setOutside] = useState(0);
  const input = useRef<HTMLInputElement>(null);
  return <UIProvider><div style={{ display: 'grid', gap: 16, maxWidth: 440 }}>
    <Button variant="secondary">Before room search</Button>
    <Combobox label="Room search lifecycle" options={rooms} value={value} onValueChange={setValue} inputRef={input} open={open} onOpenChange={setOpen}
      onOpenChangeComplete={next => setCompleted(previous => [...previous, next ? 'open' : 'closed'])} clearable />
    <Button variant="secondary" onClick={() => setOutside(count => count + 1)}>After room search</Button>
    <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}><Button onClick={() => { input.current?.focus(); setOpen(true); }}>Open room choices</Button><Button variant="secondary" onClick={() => setOpen(false)}>Close room choices</Button><Button variant="ghost" onClick={() => setCompleted([])}>Clear room completions</Button></div>
    <output aria-label="Room popup completions">{completed.join(', ')}</output>
    <output aria-label="Room outside actions">{outside}</output>
    <Dialog trigger={<Button variant="secondary">Open room dialog</Button>} title="Review room dialog" description="The search belongs to this dialog.">
      <div style={{ display: 'grid', gap: 16 }}><Combobox label="Dialog review room" options={rooms} defaultValue="room-1" /><DialogClose>Finish room selection</DialogClose></div>
    </Dialog>
  </div></UIProvider>;
}