Todae

Todae

Combobox

패키지 네임스페이스가 아니라 레시피입니다. useCombobox가 열림 상태, 값, 입력, 하이라이트, id를 맡습니다. 외형은 컴포넌트가 맡습니다. Combobox.* export는 없습니다. 루트 하나에는 인터랙티브 역할 하나만 둡니다.

이 페이지의 내용

표시 방식

useCombobox({ presentation: 'auto' })는 useSheetPresentation을 통해 거친 포인터나 좁은 뷰포트에서는 시트를, 그 밖에는 팝오버를 고릅니다. 서버와 첫 페인트에서는 항상 팝오버입니다. 문서 스크린샷이 고정되어야 할 때는 popover나 sheet를 강제하세요.

목록이 열린 동안 표시 방식이 바뀌면 훅은 blur로 닫습니다.

배지는 useSheetPresentation이 여기서 고른 값을 보여 줍니다. isSheet는 isTouch가 아닙니다.

import { Text, useSheetPresentation } from '@tounsoo/todae';

export default function PresentationBadge() {
  const sheet = useSheetPresentation();
  return (
    <Text.Root>
      {sheet.presentation} · isSheet={String(sheet.isSheet)}
    </Text.Root>
  );
}

popover · isSheet=false

Popover · 단일

형제 Label.Root + Field.Root id. 입력이 combobox입니다. 목록은 Popover.Root 안에 있습니다.

import { ChevronDown } from 'lucide-react';
import {
  Field,
  Label,
  OptionList,
  Popover,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const FRAMEWORKS: ComboboxOption[] = [
  { value: 'react', label: 'React' },
  { value: 'redux', label: 'Redux' },
  { value: 'svelte', label: 'Svelte' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],
  getOptionProps: ReturnType<typeof useCombobox>['getOptionProps'],

Popover · 다중

getChipInputProps로 Field.ChipInput을 씁니다. 칩은 레이블입니다. 값은 중복이 제거됩니다.

import { ChevronDown } from 'lucide-react';
import {
  Field,
  Label,
  OptionList,
  Popover,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const TYPES: ComboboxOption[] = [
  { value: 'design', label: 'design' },
  { value: 'tokens', label: 'tokens' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],
  getOptionProps: ReturnType<typeof useCombobox>['getOptionProps'],
) {
design

시트

닫힌 컨트롤은 Button.Root입니다. 검색 Field는 시트 안에 있습니다. inputValue와 목록은 팝오버와 같습니다.

import { ChevronDown, Search } from 'lucide-react';
import {
  BottomSheet,
  Button,
  Field,
  Label,
  OptionList,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const CITIES: ComboboxOption[] = [
  { value: 'sf', label: 'San Francisco' },
  { value: 'sj', label: 'San Jose' },
  { value: 'sc', label: 'Santa Clara' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],

자동

훅은 하나입니다. 트리는 표시 방식에 따라 바뀝니다. 고정된 화면이 필요하면 presentation을 강제하세요.

import {
  BottomSheet,
  Button,
  Field,
  Label,
  OptionList,
  Popover,
  Stack,
  Text,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const FRAMEWORKS: ComboboxOption[] = [
  { value: 'react', label: 'React' },
  { value: 'redux', label: 'Redux' },
  { value: 'svelte', label: 'Svelte' },
];

presentation: popover

비동기

loadOptions(query, signal)이 앞뒤 공백을 뺀 텍스트에 맞는 옵션을 불러오고, 반환한 그대로 보여 줍니다. 그래서 일치 여부는 로더가 판단하며, 함께 넘긴 filterOptions는 무시되고 options는 시작 값처럼 레이블만 빌려줍니다. 새 쿼리는 이전 컨트롤러를 중단하고 오래된 결과를 버립니다. 훅은 loading이 true인 동안 이전 목록을 유지합니다. 디바운스, 로딩, 다시 시도 외형은 레시피에 둡니다. 훅은 디바운스하지 않습니다.

fail을 입력하면 오류 행을 볼 수 있습니다.

import { ChevronDown } from 'lucide-react';
import {
  Button,
  Field,
  Label,
  OptionList,
  Popover,
  Text,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const PACKAGES: ComboboxOption[] = [
  { value: 'todae', label: 'todae' },
  { value: 'tokens', label: 'tokens' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],

빈 결과와 생성

일치하는 항목이 없으면 상태 행을 렌더링합니다. allowCreate는 쿼리와 대소문자 구분 없이 일치하는 레이블이 없을 때 같은 OptionList에 Create "{query}" 옵션 하나를 더합니다. 클릭하거나 Enter를 누르면 확정됩니다. 단일 선택은 선택하고 닫습니다. 다중 선택은 칩을 더하고 열린 채로 둡니다. onCreate는 정규화된 값을 반환할 수 있으며, 목록이 선택된 상태로 보여 줄 수 있도록 새 옵션을 options에 추가해야 합니다. 프로미스를 반환해도 됩니다. 그 프로미스가 끝날 때까지 creating이 true입니다. onCreate가 던지거나 거부한 이유는 텍스트가 여전히 그 레이블을 가리키는 동안 createError에 들어갑니다. 첫 번째 생성 데모처럼 둘 다 생성 행 옆의 상태 요소에 보여 주세요. 그 데모에서 fail을 입력하면 볼 수 있습니다.

import { ChevronDown } from 'lucide-react';
import {
  Field,
  Label,
  OptionList,
  Popover,
  Text,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const PACKAGES: ComboboxOption[] = [
  { value: 'todae', label: 'todae' },
  { value: 'tokens', label: 'tokens' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],
  getOptionProps: ReturnType<typeof useCombobox>['getOptionProps'],
import { ChevronDown } from 'lucide-react';
import { useLayoutEffect, useState } from 'react';
import {
  Field,
  Label,
  OptionList,
  Popover,
  Text,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const PACKAGES: ComboboxOption[] = [
  { value: 'todae', label: 'todae' },
  { value: 'tokens', label: 'tokens' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],
import { ChevronDown } from 'lucide-react';
import { useLayoutEffect, useState } from 'react';
import {
  Field,
  Label,
  OptionList,
  Popover,
  useCombobox,
  useHighlight,
  type ComboboxOption,
} from '@tounsoo/todae';

const PACKAGES: ComboboxOption[] = [
  { value: 'todae', label: 'todae' },
  { value: 'tokens', label: 'tokens' },
];

function optionNodes(
  items: ReturnType<typeof useCombobox>['items'],
  getOptionProps: ReturnType<typeof useCombobox>['getOptionProps'],

일치 항목 강조

이 페이지의 모든 데모는 useHighlight로 옵션에서 입력한 텍스트를 표시합니다. 이 훅은 옵션의 마크업을 바꾸지 않고 칠하므로 각 옵션은 접근 가능한 이름을 그대로 유지합니다. 훅의 query(입력에 선택한 레이블이 보이는 동안은 비어 있으므로 그때는 아무것도 표시하지 않음)를 넘기고 ref를 목록에 두세요.

const found = useHighlight({ name: 'combobox-match', query: cb.query });

<OptionList.Root ref={found.ref} {...cb.getListboxProps()}>

같은 이름을 쓰는 훅들은 함께 칠하므로, 모든 콤보박스에 같은 이름을 주고 스타일은 한 번만 주세요.

::highlight(combobox-match) {
  background-color: var(--todae-color-status-warning-bg);
  color: var(--todae-color-fg-default);
}

loadOptions를 쓰면 로더가 일치 여부를 정하므로, 로더가 돌려준 행에 입력한 텍스트가 없으면 표시도 없습니다.

만들기 행의 레이블은 입력한 텍스트를 자신의 단어 안에 담으므로, 목록 전체를 검색하면 "Create" 같은 단어의 글자까지 표시됩니다. 만들기 데모는 훅의 ref를 옵션만 감싸는 role="none" 래퍼에 두고, 같은 이름을 쓰는 두 번째 훅으로 만들기 행을 표시합니다. 이 훅의 ranges는 레이블의 따옴표 사이 텍스트만 덮습니다.

조합 규칙

  • 형제 Label + Field.Root id. Field.Label은 없습니다.
  • getAnchorProps 옆에 usePopover의 getTriggerProps를 펼치지 마세요. 이중 연결은 QA 실패입니다.
  • OptionList가 오버레이 목록입니다. 같은 값에 Radio나 Checkbox를 함께 렌더링하지 마세요.
  • combobox를 Field에 내장하지 마세요. Combobox.* 외형을 export하지 마세요.
  • 시트 트리거는 aria-haspopup="dialog"를 씁니다. 팝오버의 Field 외형에는 aria-haspopup이 없습니다.

접근성을 지키는 조합

Todae가 맡는 것: useCombobox는 입력을 role="combobox"로 만들어 aria-controls로 listbox를, aria-activedescendant로 활성 옵션을 가리키게 하고, 화살표 키, Home, End, Enter, Escape, Tab을 처리합니다. OptionList는 role="listbox"와 aria-selected, aria-disabled가 있는 role="option" 행을 렌더링하며, 다중 모드에서는 aria-multiselectable을 줍니다. Popover와 BottomSheet는 Escape로 닫히고 포커스를 돌려줍니다. 다중 모드에서 Field.ChipInput은 각 칩의 제거 버튼에 이름을 붙입니다.

직접 제공할 것: 레이블을 주세요. Label에 {...cb.getLabelProps()}를 주면 입력을, 시트 모드에서는 트리거를 가리키고, listbox가 이름을 가져오는 id를 받습니다. 시트 안에서는 검색 필드에도 이름을 주세요. 예를 들어 그 Field.Root에 aria-label을 줍니다. 보이는 레이블이 없으면 getListboxProps()에 aria-label을 넘기세요. 시트 모드에서는 트리거 텍스트가 값을 보여 줘야 합니다. 로딩, 오류, 생성 실패, 빈 결과 텍스트는 role="status" 요소에 두세요. 방금 나타난 영역의 텍스트는 늘 읽히지는 않으므로, 그 요소는 마운트된 채로 두고 텍스트만 바꾸세요. Retry는 비동기 데모처럼 실제 버튼이어야 합니다.

주의할 점: 비어 있거나 로딩 중일 때 OptionList.Root를 마운트 해제하지 마세요. aria-controls가 계속 가리키도록 hidden을 지정하세요. getter 스프레드 뒤에 직접 onKeyDown, onFocus, onBlur를 주면 훅의 핸들러를 대체합니다. 일치 항목 강조만으로 알리지 마세요. 강조는 시각적일 뿐입니다.

언제 쓰나요

언제: 국가, 담당자, 태그처럼 목록이 길어서 입력해서 찾는 값을 하나 이상 고를 때.

쓰지 않을 때: 페이지에 다 들어가는 몇 가지 선택지 → Radio나 Checkbox. 폼에서 짧은 목록 중 값 하나 → Field.Select. 입력이 필요 없는 짧은 오버레이 목록 → usePopover와 OptionList. 동작 실행 → Menu, 동작을 검색하려면 Command palette 레시피. 목록 없는 자유 텍스트 → Field 텍스트 입력.

훅

언제 쓰는지와 props는 useCombobox와 useSheetPresentation에 있습니다.