본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

오토컴플리트

원문 보기 위키 갱신

오토컴플리트 (Autocomplete)

입력하면서 옵션을 제안하는 검색창을 만드는 고품질의 비스타일(unstyled) React autocomplete 컴포넌트예요. 사용자가 입력한 값을 필터로 삼아, 일치하는 옵션들을 목록으로 보여주고 선택할 수 있게 해줘요.

출처: Base UI Autocomplete

본문

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';

export default function ExampleAutocomplete() {
  return (
    <Autocomplete.Root items={tags}>
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Search tags
        <Autocomplete.Input
          placeholder="e.g. feature"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:opacity-100 placeholder:text-neutral-500 placeholder:[-webkit-text-fill-color:var(--color-neutral-500)] dark:placeholder:text-neutral-400 dark:placeholder:[-webkit-text-fill-color:var(--color-neutral-400)] focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.Empty>
              <div className="py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                No tags found.
              </div>
            </Autocomplete.Empty>
            <Autocomplete.List className="outline-0 overflow-y-auto scroll-py-[0.25rem] py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0">
              {(tag: Tag) => (
                <Autocomplete.Item
                  key={tag.id}
                  className="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                  value={tag}
                >
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 0.25rem;
  scroll-padding-block: 0.25rem;
  outline: 0;
  max-height: min(22.5rem, var(--available-height));

  &[data-empty] {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Separator {
  margin: 0.375rem 1rem;
  height: 1px;
  background-color: oklch(97% 0 0deg);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(26.9% 0 0deg);
  }
}

.Empty {
  box-sizing: border-box;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

export default function ExampleAutocomplete() {
  return (
    <Autocomplete.Root items={tags}>
      <label className={styles.Label}>
        Search tags
        <Autocomplete.Input placeholder="e.g. feature" className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.Empty>
              <div className={styles.Empty}>No tags found.</div>
            </Autocomplete.Empty>
            <Autocomplete.List className={styles.List}>
              {(tag: Tag) => (
                <Autocomplete.Item key={tag.id} className={styles.Item} value={tag}>
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

사용 가이드라인

  • 선택 상태가 필요할 때는 피하세요: 선택을 기억해야 하고 입력 값을 사용자 지정할 수 없다면 Autocomplete 대신 Combobox를 사용하세요. Combobox와 달리 Autocomplete의 입력은 자유 형식 텍스트를 담을 수 있어요. 제안이 텍스트를 선택적으로만 자동 완성하기 때문이에요.
  • 필터 가능한 명령 선택기로 쓸 수 있어요: 팝업 안에 렌더링될 때 입력을 클릭하면 동작하는 명령 항목의 필터로 사용할 수 있어요.
  • 폼 컨트롤에는 접근 가능한 이름이 필요해요: <label> 요소나 Field 컴포넌트로 만들 수 있어요. forms 가이드를 참고하세요.

애너토미 (Anatomy)

컴포넌트를 가져와 함께 배치하세요:

import { Autocomplete } from '@base-ui/react/autocomplete';

<Autocomplete.Root>
  <Autocomplete.InputGroup>
    <Autocomplete.Input />
    <Autocomplete.Trigger />
    <Autocomplete.Icon />
    <Autocomplete.Clear />
    <Autocomplete.Value />
  </Autocomplete.InputGroup>

  <Autocomplete.Portal>
    <Autocomplete.Backdrop />
    <Autocomplete.Positioner>
      <Autocomplete.Popup>
        <Autocomplete.Arrow />

        <Autocomplete.Status />
        <Autocomplete.Empty />

        <Autocomplete.List>
          <Autocomplete.Row>
            <Autocomplete.Item />
          </Autocomplete.Row>

          <Autocomplete.Separator />

          <Autocomplete.Group>
            <Autocomplete.GroupLabel />
          </Autocomplete.Group>

          <Autocomplete.Collection />
        </Autocomplete.List>
      </Autocomplete.Popup>
    </Autocomplete.Positioner>
  </Autocomplete.Portal>
</Autocomplete.Root>;

항목 값 (Item values)

각 <Autocomplete.Item>은 자신을 식별하는 value prop을 받아요. 렌더링할 항목을 전달해서 itemToStringValue 같은 prop이 그 값을 받도록 해요.

예시 (Examples)

비동기 검색

입력하는 동안 항목을 비동기로 로드하고 사용자 지정 상태 콘텐츠를 렌더링해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';

export default function ExampleAsyncAutocomplete() {
  const [searchValue, setSearchValue] = React.useState('');
  const [searchResults, setSearchResults] = React.useState<Movie[]>([]);
  const [error, setError] = React.useState<string | null>(null);

  const [isPending, startTransition] = React.useTransition();

  const { contains } = Autocomplete.useFilter();

  const abortControllerRef = React.useRef<AbortController | null>(null);

  function getStatus(): React.ReactNode | null {
    if (isPending) {
      return (
        <React.Fragment>
          <span
            className="inline-block size-3 animate-spin rounded-full border border-current border-r-transparent rtl:border-r-current rtl:border-l-transparent"
            aria-hidden
          />
          Searching…
        </React.Fragment>
      );
    }

    if (error) {
      return error;
    }

    if (searchValue === '') {
      return null;
    }

    if (searchResults.length === 0) {
      return `Movie or year "${searchValue}" does not exist in the Top 100 IMDb movies`;
    }

    return `${searchResults.length} result${searchResults.length === 1 ? '' : 's'} found`;
  }

  const status = getStatus();

  return (
    <Autocomplete.Root
      items={searchResults}
      value={searchValue}
      onValueChange={(nextSearchValue) => {
        setSearchValue(nextSearchValue);

        const controller = new AbortController();
        abortControllerRef.current?.abort();
        abortControllerRef.current = controller;

        if (nextSearchValue === '') {
          setSearchResults([]);
          setError(null);
          return;
        }

        startTransition(async () => {
          setError(null);

          const result = await searchMovies(nextSearchValue, contains);
          if (controller.signal.aborted) {
            return;
          }

          startTransition(() => {
            setSearchResults(result.movies);
            setError(result.error);
          });
        });
      }}
      itemToStringValue={(item) => item.title}
      filter={null}
    >
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Search movies by name or year
        <Autocomplete.Input
          placeholder="e.g. Pulp Fiction or 1994"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal hidden={!status}>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4} align="start">
          <Autocomplete.Popup
            className="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
            aria-busy={isPending || undefined}
          >
            <div className="max-h-[min(var(--available-height),22.5rem)] overflow-y-auto overscroll-contain py-1 scroll-pt-1 scroll-pb-1">
              <Autocomplete.Status>
                {status && (
                  <div className="flex items-center gap-2 py-1 pr-8 pl-2 text-sm text-neutral-500 dark:text-neutral-400">
                    {status}
                  </div>
                )}
              </Autocomplete.Status>
              <Autocomplete.List>
                {(movie: Movie) => (
                  <Autocomplete.Item
                    key={movie.id}
                    className="group flex cursor-default py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                    value={movie}
                  >
                    <span className="flex w-full flex-col gap-1">
                      <span className="font-bold leading-5">{movie.title}</span>
                      <span className="text-sm leading-4 text-neutral-500 dark:text-neutral-400 group-data-highlighted:text-neutral-300 dark:group-data-highlighted:text-neutral-500">
                        {movie.year}
                      </span>
                    </span>
                  </Autocomplete.Item>
                )}
              </Autocomplete.List>
            </div>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

async function searchMovies(
  query: string,
  filter: (item: string, query: string) => boolean,
): Promise<{ movies: Movie[]; error: string | null }> {
  // Simulate network delay
  await new Promise((resolve) => {
    setTimeout(resolve, Math.random() * 500 + 100);
  });

  // Simulate occasional network errors (1% chance)
  if (Math.random() < 0.01 || query === 'will_error') {
    return {
      movies: [],
      error: 'Failed to fetch movies. Please try again.',
    };
  }

  const movies = top100Movies.filter(
    (movie) => filter(movie.title, query) || filter(movie.year.toString(), query),
  );

  return {
    movies,
    error: null,
  };
}

interface Movie {
  id: string;
  title: string;
  year: number;
}

const top100Movies: Movie[] = [
  { id: '1', title: 'The Shawshank Redemption', year: 1994 },
  { id: '2', title: 'The Godfather', year: 1972 },
  { id: '3', title: 'The Dark Knight', year: 2008 },
  { id: '4', title: 'The Godfather Part II', year: 1974 },
  { id: '5', title: '12 Angry Men', year: 1957 },
  { id: '6', title: 'The Lord of the Rings: The Return of the King', year: 2003 },
  { id: '7', title: "Schindler's List", year: 1993 },
  { id: '8', title: 'Pulp Fiction', year: 1994 },
  { id: '9', title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
  { id: '10', title: 'The Good, the Bad and the Ugly', year: 1966 },
  { id: '11', title: 'Forrest Gump', year: 1994 },
  { id: '12', title: 'The Lord of the Rings: The Two Towers', year: 2002 },
  { id: '13', title: 'Fight Club', year: 1999 },
  { id: '14', title: 'Inception', year: 2010 },
  { id: '15', title: 'Star Wars: Episode V – The Empire Strikes Back', year: 1980 },
  { id: '16', title: 'The Matrix', year: 1999 },
  { id: '17', title: 'Goodfellas', year: 1990 },
  { id: '18', title: 'Interstellar', year: 2014 },
  { id: '19', title: "One Flew Over the Cuckoo's Nest", year: 1975 },
  { id: '20', title: 'Se7en', year: 1995 },
  { id: '21', title: "It's a Wonderful Life", year: 1946 },
  { id: '22', title: 'The Silence of the Lambs', year: 1991 },
  { id: '23', title: 'Seven Samurai', year: 1954 },
  { id: '24', title: 'Saving Private Ryan', year: 1998 },
  { id: '25', title: 'City of God', year: 2002 },
  { id: '26', title: 'Life Is Beautiful', year: 1997 },
  { id: '27', title: 'The Green Mile', year: 1999 },
  { id: '28', title: 'Star Wars: Episode IV – A New Hope', year: 1977 },
  { id: '29', title: 'Terminator 2: Judgment Day', year: 1991 },
  { id: '30', title: 'Back to the Future', year: 1985 },
  { id: '31', title: 'Spirited Away', year: 2001 },
  { id: '32', title: 'The Pianist', year: 2002 },
  { id: '33', title: 'Psycho', year: 1960 },
  { id: '34', title: 'Parasite', year: 2019 },
  { id: '35', title: 'Gladiator', year: 2000 },
  { id: '36', title: 'Léon: The Professional', year: 1994 },
  { id: '37', title: 'American History X', year: 1998 },
  { id: '38', title: 'The Departed', year: 2006 },
  { id: '39', title: 'Whiplash', year: 2014 },
  { id: '40', title: 'The Prestige', year: 2006 },
  { id: '41', title: 'Grave of the Fireflies', year: 1988 },
  { id: '42', title: 'The Usual Suspects', year: 1995 },
  { id: '43', title: 'Casablanca', year: 1942 },
  { id: '44', title: 'Harakiri', year: 1962 },
  { id: '45', title: 'The Lion King', year: 1994 },
  { id: '46', title: 'The Intouchables', year: 2011 },
  { id: '47', title: 'Modern Times', year: 1936 },
  { id: '48', title: 'The Lives of Others', year: 2006 },
  { id: '49', title: 'Once Upon a Time in the West', year: 1968 },
  { id: '50', title: 'Rear Window', year: 1954 },
  { id: '51', title: 'Alien', year: 1979 },
  { id: '52', title: 'City Lights', year: 1931 },
  { id: '53', title: 'The Shining', year: 1980 },
  { id: '54', title: 'Cinema Paradiso', year: 1988 },
  { id: '55', title: 'Avengers: Infinity War', year: 2018 },
  { id: '56', title: 'Paths of Glory', year: 1957 },
  { id: '57', title: 'Django Unchained', year: 2012 },
  { id: '58', title: 'WALL·E', year: 2008 },
  { id: '59', title: 'Sunset Boulevard', year: 1950 },
  { id: '60', title: 'The Great Dictator', year: 1940 },
  { id: '61', title: 'The Dark Knight Rises', year: 2012 },
  { id: '62', title: 'Princess Mononoke', year: 1997 },
  { id: '63', title: 'Witness for the Prosecution', year: 1957 },
  { id: '64', title: 'Oldboy', year: 2003 },
  { id: '65', title: 'Aliens', year: 1986 },
  { id: '66', title: 'Once Upon a Time in America', year: 1984 },
  { id: '67', title: 'Coco', year: 2017 },
  { id: '68', title: 'Your Name.', year: 2016 },
  { id: '69', title: 'American Beauty', year: 1999 },
  { id: '70', title: 'Braveheart', year: 1995 },
  { id: '71', title: 'Das Boot', year: 1981 },
  { id: '72', title: '3 Idiots', year: 2009 },
  { id: '73', title: 'Toy Story', year: 1995 },
  { id: '74', title: 'Inglourious Basterds', year: 2009 },
  { id: '75', title: 'High and Low', year: 1963 },
  { id: '76', title: 'Amadeus', year: 1984 },
  { id: '77', title: 'Good Will Hunting', year: 1997 },
  { id: '78', title: 'Star Wars: Episode VI – Return of the Jedi', year: 1983 },
  { id: '79', title: 'The Hunt', year: 2012 },
  { id: '80', title: 'Capharnaüm', year: 2018 },
  { id: '81', title: 'Reservoir Dogs', year: 1992 },
  { id: '82', title: 'Eternal Sunshine of the Spotless Mind', year: 2004 },
  { id: '83', title: 'Requiem for a Dream', year: 2000 },
  { id: '84', title: 'Come and See', year: 1985 },
  { id: '85', title: 'Ikiru', year: 1952 },
  { id: '86', title: 'Vertigo', year: 1958 },
  { id: '87', title: 'Lawrence of Arabia', year: 1962 },
  { id: '88', title: 'Citizen Kane', year: 1941 },
  { id: '89', title: 'Memento', year: 2000 },
  { id: '90', title: 'North by Northwest', year: 1959 },
  { id: '91', title: 'Star Wars: Episode III – Revenge of the Sith', year: 2005 },
  { id: '92', title: '2001: A Space Odyssey', year: 1968 },
  { id: '93', title: 'Amélie', year: 2001 },
  { id: '94', title: "Singin' in the Rain", year: 1952 },
  { id: '95', title: 'Apocalypse Now', year: 1979 },
  { id: '96', title: 'Taxi Driver', year: 1976 },
  { id: '97', title: 'Downfall', year: 2004 },
  { id: '98', title: 'The Wolf of Wall Street', year: 2013 },
  { id: '99', title: 'A Clockwork Orange', year: 1971 },
  { id: '100', title: 'Double Indemnity', year: 1944 },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.Viewport {
  box-sizing: border-box;
  max-height: min(var(--available-height), 22.5rem);
  padding-block: 0.25rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.MovieItem {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  width: 100%;
}

.MovieName {
  font-weight: 700;
  line-height: 1.25rem;
}

.MovieYear {
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }

  .Item[data-highlighted] & {
    color: oklch(87% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(55.6% 0 0deg);
    }
  }
}

.Status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-block: 0.25rem;
  padding-left: 0.5rem;
  padding-right: 2rem;
  font-size: 0.875rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Spinner {
  box-sizing: border-box;
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  border: 1px solid currentColor;
  border-right-color: transparent;
  animation: autocompleteSpinner 0.75s linear infinite;
}

@keyframes autocompleteSpinner {
  100% {
    transform: rotate(360deg);
  }
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

export default function ExampleAsyncAutocomplete() {
  const [searchValue, setSearchValue] = React.useState('');
  const [searchResults, setSearchResults] = React.useState<Movie[]>([]);
  const [error, setError] = React.useState<string | null>(null);

  const [isPending, startTransition] = React.useTransition();

  const { contains } = Autocomplete.useFilter();

  const abortControllerRef = React.useRef<AbortController | null>(null);

  function getStatus(): React.ReactNode | null {
    if (isPending) {
      return (
        <React.Fragment>
          <span className={styles.Spinner} aria-hidden />
          Searching…
        </React.Fragment>
      );
    }

    if (error) {
      return error;
    }

    if (searchValue === '') {
      return null;
    }

    if (searchResults.length === 0) {
      return `Movie or year "${searchValue}" does not exist in the Top 100 IMDb movies`;
    }

    return `${searchResults.length} result${searchResults.length === 1 ? '' : 's'} found`;
  }

  const status = getStatus();

  return (
    <Autocomplete.Root
      items={searchResults}
      value={searchValue}
      onValueChange={(nextSearchValue) => {
        setSearchValue(nextSearchValue);

        const controller = new AbortController();
        abortControllerRef.current?.abort();
        abortControllerRef.current = controller;

        if (nextSearchValue === '') {
          setSearchResults([]);
          setError(null);
          return;
        }

        startTransition(async () => {
          setError(null);

          const result = await searchMovies(nextSearchValue, contains);
          if (controller.signal.aborted) {
            return;
          }

          startTransition(() => {
            setSearchResults(result.movies);
            setError(result.error);
          });
        });
      }}
      itemToStringValue={(item) => item.title}
      filter={null}
    >
      <label className={styles.Label}>
        Search movies by name or year
        <Autocomplete.Input placeholder="e.g. Pulp Fiction or 1994" className={styles.Input} />
      </label>

      <Autocomplete.Portal hidden={!status}>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4} align="start">
          <Autocomplete.Popup className={styles.Popup} aria-busy={isPending || undefined}>
            <div className={styles.Viewport}>
              <Autocomplete.Status>
                {status && <div className={styles.Status}>{status}</div>}
              </Autocomplete.Status>
              <Autocomplete.List>
                {(movie: Movie) => (
                  <Autocomplete.Item key={movie.id} className={styles.Item} value={movie}>
                    <span className={styles.MovieItem}>
                      <span className={styles.MovieName}>{movie.title}</span>
                      <span className={styles.MovieYear}>{movie.year}</span>
                    </span>
                  </Autocomplete.Item>
                )}
              </Autocomplete.List>
            </div>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

async function searchMovies(
  query: string,
  filter: (item: string, query: string) => boolean,
): Promise<{ movies: Movie[]; error: string | null }> {
  // Simulate network delay
  await new Promise((resolve) => {
    setTimeout(resolve, Math.random() * 500 + 100);
  });

  // Simulate occasional network errors (1% chance)
  if (Math.random() < 0.01 || query === 'will_error') {
    return {
      movies: [],
      error: 'Failed to fetch movies. Please try again.',
    };
  }

  const movies = top100Movies.filter(
    (movie) => filter(movie.title, query) || filter(movie.year.toString(), query),
  );

  return {
    movies,
    error: null,
  };
}

interface Movie {
  id: string;
  title: string;
  year: number;
}

const top100Movies: Movie[] = [
  { id: '1', title: 'The Shawshank Redemption', year: 1994 },
  { id: '2', title: 'The Godfather', year: 1972 },
  { id: '3', title: 'The Dark Knight', year: 2008 },
  { id: '4', title: 'The Godfather Part II', year: 1974 },
  { id: '5', title: '12 Angry Men', year: 1957 },
  { id: '6', title: 'The Lord of the Rings: The Return of the King', year: 2003 },
  { id: '7', title: "Schindler's List", year: 1993 },
  { id: '8', title: 'Pulp Fiction', year: 1994 },
  { id: '9', title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
  { id: '10', title: 'The Good, the Bad and the Ugly', year: 1966 },
  { id: '11', title: 'Forrest Gump', year: 1994 },
  { id: '12', title: 'The Lord of the Rings: The Two Towers', year: 2002 },
  { id: '13', title: 'Fight Club', year: 1999 },
  { id: '14', title: 'Inception', year: 2010 },
  { id: '15', title: 'Star Wars: Episode V – The Empire Strikes Back', year: 1980 },
  { id: '16', title: 'The Matrix', year: 1999 },
  { id: '17', title: 'Goodfellas', year: 1990 },
  { id: '18', title: 'Interstellar', year: 2014 },
  { id: '19', title: "One Flew Over the Cuckoo's Nest", year: 1975 },
  { id: '20', title: 'Se7en', year: 1995 },
  { id: '21', title: "It's a Wonderful Life", year: 1946 },
  { id: '22', title: 'The Silence of the Lambs', year: 1991 },
  { id: '23', title: 'Seven Samurai', year: 1954 },
  { id: '24', title: 'Saving Private Ryan', year: 1998 },
  { id: '25', title: 'City of God', year: 2002 },
  { id: '26', title: 'Life Is Beautiful', year: 1997 },
  { id: '27', title: 'The Green Mile', year: 1999 },
  { id: '28', title: 'Star Wars: Episode IV – A New Hope', year: 1977 },
  { id: '29', title: 'Terminator 2: Judgment Day', year: 1991 },
  { id: '30', title: 'Back to the Future', year: 1985 },
  { id: '31', title: 'Spirited Away', year: 2001 },
  { id: '32', title: 'The Pianist', year: 2002 },
  { id: '33', title: 'Psycho', year: 1960 },
  { id: '34', title: 'Parasite', year: 2019 },
  { id: '35', title: 'Gladiator', year: 2000 },
  { id: '36', title: 'Léon: The Professional', year: 1994 },
  { id: '37', title: 'American History X', year: 1998 },
  { id: '38', title: 'The Departed', year: 2006 },
  { id: '39', title: 'Whiplash', year: 2014 },
  { id: '40', title: 'The Prestige', year: 2006 },
  { id: '41', title: 'Grave of the Fireflies', year: 1988 },
  { id: '42', title: 'The Usual Suspects', year: 1995 },
  { id: '43', title: 'Casablanca', year: 1942 },
  { id: '44', title: 'Harakiri', year: 1962 },
  { id: '45', title: 'The Lion King', year: 1994 },
  { id: '46', title: 'The Intouchables', year: 2011 },
  { id: '47', title: 'Modern Times', year: 1936 },
  { id: '48', title: 'The Lives of Others', year: 2006 },
  { id: '49', title: 'Once Upon a Time in the West', year: 1968 },
  { id: '50', title: 'Rear Window', year: 1954 },
  { id: '51', title: 'Alien', year: 1979 },
  { id: '52', title: 'City Lights', year: 1931 },
  { id: '53', title: 'The Shining', year: 1980 },
  { id: '54', title: 'Cinema Paradiso', year: 1988 },
  { id: '55', title: 'Avengers: Infinity War', year: 2018 },
  { id: '56', title: 'Paths of Glory', year: 1957 },
  { id: '57', title: 'Django Unchained', year: 2012 },
  { id: '58', title: 'WALL·E', year: 2008 },
  { id: '59', title: 'Sunset Boulevard', year: 1950 },
  { id: '60', title: 'The Great Dictator', year: 1940 },
  { id: '61', title: 'The Dark Knight Rises', year: 2012 },
  { id: '62', title: 'Princess Mononoke', year: 1997 },
  { id: '63', title: 'Witness for the Prosecution', year: 1957 },
  { id: '64', title: 'Oldboy', year: 2003 },
  { id: '65', title: 'Aliens', year: 1986 },
  { id: '66', title: 'Once Upon a Time in America', year: 1984 },
  { id: '67', title: 'Coco', year: 2017 },
  { id: '68', title: 'Your Name.', year: 2016 },
  { id: '69', title: 'American Beauty', year: 1999 },
  { id: '70', title: 'Braveheart', year: 1995 },
  { id: '71', title: 'Das Boot', year: 1981 },
  { id: '72', title: '3 Idiots', year: 2009 },
  { id: '73', title: 'Toy Story', year: 1995 },
  { id: '74', title: 'Inglourious Basterds', year: 2009 },
  { id: '75', title: 'High and Low', year: 1963 },
  { id: '76', title: 'Amadeus', year: 1984 },
  { id: '77', title: 'Good Will Hunting', year: 1997 },
  { id: '78', title: 'Star Wars: Episode VI – Return of the Jedi', year: 1983 },
  { id: '79', title: 'The Hunt', year: 2012 },
  { id: '80', title: 'Capharnaüm', year: 2018 },
  { id: '81', title: 'Reservoir Dogs', year: 1992 },
  { id: '82', title: 'Eternal Sunshine of the Spotless Mind', year: 2004 },
  { id: '83', title: 'Requiem for a Dream', year: 2000 },
  { id: '84', title: 'Come and See', year: 1985 },
  { id: '85', title: 'Ikiru', year: 1952 },
  { id: '86', title: 'Vertigo', year: 1958 },
  { id: '87', title: 'Lawrence of Arabia', year: 1962 },
  { id: '88', title: 'Citizen Kane', year: 1941 },
  { id: '89', title: 'Memento', year: 2000 },
  { id: '90', title: 'North by Northwest', year: 1959 },
  { id: '91', title: 'Star Wars: Episode III – Revenge of the Sith', year: 2005 },
  { id: '92', title: '2001: A Space Odyssey', year: 1968 },
  { id: '93', title: 'Amélie', year: 2001 },
  { id: '94', title: "Singin' in the Rain", year: 1952 },
  { id: '95', title: 'Apocalypse Now', year: 1979 },
  { id: '96', title: 'Taxi Driver', year: 1976 },
  { id: '97', title: 'Downfall', year: 2004 },
  { id: '98', title: 'The Wolf of Wall Street', year: 2013 },
  { id: '99', title: 'A Clockwork Orange', year: 1971 },
  { id: '100', title: 'Double Indemnity', year: 1944 },
];

인라인 자동완성

mode prop을 사용해 화살표 키로 탐색하는 동안 강조된 항목으로 입력을 자동 채움해요. aria-autocomplete 값인 list, both, inline, none을 받아요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';

export default function ExampleAutocompleteInline() {
  return (
    <Autocomplete.Root items={tags} mode="both">
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Search tags
        <Autocomplete.Input
          placeholder="e.g. feature"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden data-empty:hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.List className="outline-0 overflow-y-auto scroll-py-[0.25rem] py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0">
              {(tag: Tag) => (
                <Autocomplete.Item
                  key={tag.id}
                  className="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                  value={tag}
                >
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Container {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;

  &[data-empty] {
    display: none;
  }
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 0.25rem;
  scroll-padding-block: 0.25rem;
  outline: 0;
  max-height: min(22.5rem, var(--available-height));

  &[data-empty] {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}
/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

export default function ExampleAutocompleteInline() {
  return (
    <Autocomplete.Root items={tags} mode="both">
      <label className={styles.Label}>
        Search tags
        <Autocomplete.Input placeholder="e.g. feature" className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.List className={styles.List}>
              {(tag: Tag) => (
                <Autocomplete.Item key={tag.id} className={styles.Item} value={tag}>
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

그룹화

<Autocomplete.Group>과 <Autocomplete.GroupLabel>로 관련 옵션을 정리해 팝업 안에 섹션 제목을 추가해요.

그룹은 items 속성을 가진 객체 배열로 표현돼요. items 자체는 각 그룹의 개별 항목 배열이에요. 그룹 라벨을 렌더링할 때 value 같은 추가 속성으로 제목 텍스트를 제공할 수 있어요.

interface ProduceGroupItem {
  value: string;
  // @highlight
  items: string[];
}

const groups: ProduceGroupItem[] = [
  {
    value: 'Fruits',
    // @highlight
    items: ['Apple', 'Banana', 'Orange'],
  },
  {
    value: 'Vegetables',
    // @highlight
    items: ['Carrot', 'Lettuce', 'Spinach'],
  },
];

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';

export default function ExampleGroupAutocomplete() {
  return (
    <Autocomplete.Root items={groupedTags}>
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Select a tag
        <Autocomplete.Input
          placeholder="e.g. feature"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.Empty>
              <div className="py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                No tags found.
              </div>
            </Autocomplete.Empty>
            <Autocomplete.List className="outline-0 overflow-y-auto py-1 scroll-py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0">
              {(group: TagGroup) => (
                <Autocomplete.Group
                  key={group.value}
                  items={group.items}
                  className="block pb-2 last:pb-0"
                >
                  <Autocomplete.GroupLabel className="p-2 text-sm leading-4 text-neutral-500 select-none dark:text-neutral-400">
                    {group.value}
                  </Autocomplete.GroupLabel>
                  <Autocomplete.Collection>
                    {(tag: Tag) => (
                      <Autocomplete.Item
                        key={tag.id}
                        className="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                        value={tag}
                      >
                        {tag.label}
                      </Autocomplete.Item>
                    )}
                  </Autocomplete.Collection>
                </Autocomplete.Group>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  label: string;
  group: 'Type' | 'Component';
}

interface TagGroup {
  value: string;
  items: Tag[];
}

const tagsData: Tag[] = [
  { id: 't1', label: 'feature', group: 'Type' },
  { id: 't2', label: 'fix', group: 'Type' },
  { id: 't3', label: 'bug', group: 'Type' },
  { id: 't4', label: 'docs', group: 'Type' },
  { id: 't5', label: 'internal', group: 'Type' },
  { id: 't6', label: 'mobile', group: 'Type' },
  { id: 'c-accordion', label: 'component: accordion', group: 'Component' },
  { id: 'c-alert-dialog', label: 'component: alert dialog', group: 'Component' },
  { id: 'c-autocomplete', label: 'component: autocomplete', group: 'Component' },
  { id: 'c-avatar', label: 'component: avatar', group: 'Component' },
  { id: 'c-checkbox', label: 'component: checkbox', group: 'Component' },
  { id: 'c-checkbox-group', label: 'component: checkbox group', group: 'Component' },
  { id: 'c-collapsible', label: 'component: collapsible', group: 'Component' },
  { id: 'c-combobox', label: 'component: combobox', group: 'Component' },
  { id: 'c-context-menu', label: 'component: context menu', group: 'Component' },
  { id: 'c-dialog', label: 'component: dialog', group: 'Component' },
  { id: 'c-field', label: 'component: field', group: 'Component' },
  { id: 'c-fieldset', label: 'component: fieldset', group: 'Component' },
  { id: 'c-filterable-menu', label: 'component: filterable menu', group: 'Component' },
  { id: 'c-form', label: 'component: form', group: 'Component' },
  { id: 'c-input', label: 'component: input', group: 'Component' },
  { id: 'c-menu', label: 'component: menu', group: 'Component' },
  { id: 'c-menubar', label: 'component: menubar', group: 'Component' },
  { id: 'c-meter', label: 'component: meter', group: 'Component' },
  { id: 'c-navigation-menu', label: 'component: navigation menu', group: 'Component' },
  { id: 'c-number-field', label: 'component: number field', group: 'Component' },
  { id: 'c-popover', label: 'component: popover', group: 'Component' },
  { id: 'c-preview-card', label: 'component: preview card', group: 'Component' },
  { id: 'c-progress', label: 'component: progress', group: 'Component' },
  { id: 'c-radio', label: 'component: radio', group: 'Component' },
  { id: 'c-scroll-area', label: 'component: scroll area', group: 'Component' },
  { id: 'c-select', label: 'component: select', group: 'Component' },
  { id: 'c-separator', label: 'component: separator', group: 'Component' },
  { id: 'c-slider', label: 'component: slider', group: 'Component' },
  { id: 'c-switch', label: 'component: switch', group: 'Component' },
  { id: 'c-tabs', label: 'component: tabs', group: 'Component' },
  { id: 'c-toast', label: 'component: toast', group: 'Component' },
  { id: 'c-toggle', label: 'component: toggle', group: 'Component' },
  { id: 'c-toggle-group', label: 'component: toggle group', group: 'Component' },
  { id: 'c-toolbar', label: 'component: toolbar', group: 'Component' },
  { id: 'c-tooltip', label: 'component: tooltip', group: 'Component' },
];

function groupTags(tags: Tag[]): TagGroup[] {
  const groups: { [key: string]: Tag[] } = {};
  tags.forEach((t) => {
    (groups[t.group] ??= []).push(t);
  });
  const order = ['Type', 'Component'];
  return order.map((value) => ({ value, items: groups[value] ?? [] }));
}

const groupedTags: TagGroup[] = groupTags(tagsData);

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  padding-block: 0.25rem;
  scroll-padding-top: 0.25rem;
  scroll-padding-bottom: 0.25rem;
  overscroll-behavior: contain;
  max-height: min(22.5rem, var(--available-height));
  outline: 0;

  &[data-empty] {
    padding: 0;
  }
}

.Group {
  display: block;
  padding-bottom: 0.5rem;

  &:last-child {
    padding-bottom: 0;
  }
}

.GroupLabel {
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  padding: 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Separator {
  margin: 0.375rem 1rem;
  height: 1px;
  background-color: oklch(97% 0 0deg);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(26.9% 0 0deg);
  }
}

.Empty {
  box-sizing: border-box;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

export default function ExampleGroupAutocomplete() {
  return (
    <Autocomplete.Root items={groupedTags}>
      <label className={styles.Label}>
        Select a tag
        <Autocomplete.Input placeholder="e.g. feature" className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.Empty>
              <div className={styles.Empty}>No tags found.</div>
            </Autocomplete.Empty>
            <Autocomplete.List className={styles.List}>
              {(group: TagGroup) => (
                <Autocomplete.Group key={group.value} items={group.items} className={styles.Group}>
                  <Autocomplete.GroupLabel className={styles.GroupLabel}>
                    {group.value}
                  </Autocomplete.GroupLabel>
                  <Autocomplete.Collection>
                    {(tag: Tag) => (
                      <Autocomplete.Item key={tag.id} className={styles.Item} value={tag}>
                        {tag.label}
                      </Autocomplete.Item>
                    )}
                  </Autocomplete.Collection>
                </Autocomplete.Group>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  label: string;
  group: 'Type' | 'Component';
}

interface TagGroup {
  value: string;
  items: Tag[];
}

const tagsData: Tag[] = [
  { id: 't1', label: 'feature', group: 'Type' },
  { id: 't2', label: 'fix', group: 'Type' },
  { id: 't3', label: 'bug', group: 'Type' },
  { id: 't4', label: 'docs', group: 'Type' },
  { id: 't5', label: 'internal', group: 'Type' },
  { id: 't6', label: 'mobile', group: 'Type' },
  { id: 'c-accordion', label: 'component: accordion', group: 'Component' },
  { id: 'c-alert-dialog', label: 'component: alert dialog', group: 'Component' },
  { id: 'c-autocomplete', label: 'component: autocomplete', group: 'Component' },
  { id: 'c-avatar', label: 'component: avatar', group: 'Component' },
  { id: 'c-checkbox', label: 'component: checkbox', group: 'Component' },
  { id: 'c-checkbox-group', label: 'component: checkbox group', group: 'Component' },
  { id: 'c-collapsible', label: 'component: collapsible', group: 'Component' },
  { id: 'c-combobox', label: 'component: combobox', group: 'Component' },
  { id: 'c-context-menu', label: 'component: context menu', group: 'Component' },
  { id: 'c-dialog', label: 'component: dialog', group: 'Component' },
  { id: 'c-field', label: 'component: field', group: 'Component' },
  { id: 'c-fieldset', label: 'component: fieldset', group: 'Component' },
  { id: 'c-filterable-menu', label: 'component: filterable menu', group: 'Component' },
  { id: 'c-form', label: 'component: form', group: 'Component' },
  { id: 'c-input', label: 'component: input', group: 'Component' },
  { id: 'c-menu', label: 'component: menu', group: 'Component' },
  { id: 'c-menubar', label: 'component: menubar', group: 'Component' },
  { id: 'c-meter', label: 'component: meter', group: 'Component' },
  { id: 'c-navigation-menu', label: 'component: navigation menu', group: 'Component' },
  { id: 'c-number-field', label: 'component: number field', group: 'Component' },
  { id: 'c-popover', label: 'component: popover', group: 'Component' },
  { id: 'c-preview-card', label: 'component: preview card', group: 'Component' },
  { id: 'c-progress', label: 'component: progress', group: 'Component' },
  { id: 'c-radio', label: 'component: radio', group: 'Component' },
  { id: 'c-scroll-area', label: 'component: scroll area', group: 'Component' },
  { id: 'c-select', label: 'component: select', group: 'Component' },
  { id: 'c-separator', label: 'component: separator', group: 'Component' },
  { id: 'c-slider', label: 'component: slider', group: 'Component' },
  { id: 'c-switch', label: 'component: switch', group: 'Component' },
  { id: 'c-tabs', label: 'component: tabs', group: 'Component' },
  { id: 'c-toast', label: 'component: toast', group: 'Component' },
  { id: 'c-toggle', label: 'component: toggle', group: 'Component' },
  { id: 'c-toggle-group', label: 'component: toggle group', group: 'Component' },
  { id: 'c-toolbar', label: 'component: toolbar', group: 'Component' },
  { id: 'c-tooltip', label: 'component: tooltip', group: 'Component' },
];

function groupTags(tags: Tag[]): TagGroup[] {
  const groups: { [key: string]: Tag[] } = {};
  tags.forEach((t) => {
    (groups[t.group] ??= []).push(t);
  });
  const order = ['Type', 'Component'];
  return order.map((value) => ({ value, items: groups[value] ?? [] }));
}

const groupedTags: TagGroup[] = groupTags(tagsData);

퍼지 매칭

쿼리가 항목 텍스트와 정확히 일치하지 않아도 관련 결과를 찾도록 퍼지 매칭(fuzzy matching)을 사용해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { matchSorter } from 'match-sorter';

export default function ExampleFuzzyMatchingAutocomplete() {
  return (
    <Autocomplete.Root
      items={fuzzyItems}
      filter={fuzzyFilter}
      itemToStringValue={(item) => item.title}
    >
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Fuzzy search documentation
        <Autocomplete.Input
          placeholder="e.g. React"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-(--anchor-width) max-w-(--available-width) border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.Empty>
              <div className="py-3 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                No results found for "{<Autocomplete.Value />}"
              </div>
            </Autocomplete.Empty>

            <Autocomplete.List className="flex max-h-[min(var(--available-height),28rem)] flex-col overflow-y-auto overscroll-contain py-1 scroll-pt-1 scroll-pb-1 empty:p-0">
              {(item: FuzzyItem) => (
                <Autocomplete.Item
                  key={item.title}
                  value={item}
                  className="flex cursor-default py-3 pr-2 pl-2 text-sm leading-6 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-100 dark:data-highlighted:before:bg-neutral-800"
                >
                  <Autocomplete.Value>
                    {(value) => (
                      <span className="flex w-full flex-col gap-1">
                        <span className="flex items-center justify-between gap-3">
                          <span className="flex-1 font-bold leading-5">
                            {highlightText(item.title, value)}
                          </span>
                        </span>
                        <span className="text-sm text-neutral-500 dark:text-neutral-400">
                          {highlightText(item.description, value)}
                        </span>
                      </span>
                    )}
                  </Autocomplete.Value>
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

function highlightText(text: string, query: string): React.ReactNode {
  const trimmed = query.trim();
  if (!trimmed) {
    return text;
  }

  const limited = trimmed.slice(0, 100);
  const escaped = limited.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  const regex = new RegExp(`(${escaped})`, 'gi');

  return text.split(regex).map((part, idx) =>
    regex.test(part) ? (
      <mark key={idx} className="bg-transparent font-bold text-blue-800 dark:text-blue-500">
        {part}
      </mark>
    ) : (
      part
    ),
  );
}

function fuzzyFilter(item: FuzzyItem, query: string): boolean {
  if (!query) {
    return true;
  }

  const results = matchSorter([item], query, {
    keys: [
      'title',
      'description',
      'category',
      { key: 'title', threshold: matchSorter.rankings.CONTAINS },
      { key: 'description', threshold: matchSorter.rankings.WORD_STARTS_WITH },
    ],
  });

  return results.length > 0;
}

interface FuzzyItem {
  title: string;
  description: string;
  category: string;
}

const fuzzyItems: FuzzyItem[] = [
  {
    title: 'React Hooks Guide',
    description: 'Learn how to use React Hooks like useState, useEffect, and custom hooks',
    category: 'React',
  },
  {
    title: 'JavaScript Array Methods',
    description: 'Master array methods like map, filter, reduce, and forEach in JavaScript',
    category: 'JavaScript',
  },
  {
    title: 'CSS Flexbox Layout',
    description: 'Complete guide to CSS Flexbox for responsive web design',
    category: 'CSS',
  },
  {
    title: 'TypeScript Interfaces',
    description: 'Understanding TypeScript interfaces and type definitions',
    category: 'TypeScript',
  },
  {
    title: 'React Performance Optimization',
    description: 'Tips and techniques for optimizing React application performance',
    category: 'React',
  },
  {
    title: 'HTML Semantic Elements',
    description: 'Using semantic HTML elements for better accessibility and SEO',
    category: 'HTML',
  },
  {
    title: 'Node.js Express Server',
    description: 'Building RESTful APIs with Node.js and Express framework',
    category: 'Node.js',
  },
  {
    title: 'Vue Composition API',
    description: 'Modern Vue.js development using the Composition API',
    category: 'Vue.js',
  },
  {
    title: 'Angular Components',
    description: 'Creating reusable Angular components with TypeScript',
    category: 'Angular',
  },
  {
    title: 'Python Django Framework',
    description: 'Web development with Python Django framework',
    category: 'Python',
  },
  {
    title: 'CSS Grid Layout',
    description: 'Advanced CSS Grid techniques for complex layouts',
    category: 'CSS',
  },
  {
    title: 'React Testing Library',
    description: 'Testing React components with React Testing Library',
    category: 'React',
  },
  {
    title: 'MongoDB Queries',
    description: 'Advanced MongoDB queries and aggregation pipelines',
    category: 'Database',
  },
  {
    title: 'Webpack Configuration',
    description: 'Optimizing webpack configuration for production builds',
    category: 'Build Tools',
  },
  {
    title: 'SASS/SCSS Guide',
    description: 'Writing maintainable CSS with SASS and SCSS',
    category: 'CSS',
  },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  padding-block: 0.25rem;
  max-height: min(var(--available-height), 28rem);
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;

  &:empty {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding: 0.75rem 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1.5rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }
}

.ItemContent {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  width: 100%;
}

.ItemHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.ItemTitle {
  font-weight: 700;
  line-height: 1.25rem;
  flex: 1;
}

.ItemDescription {
  font-size: 0.875rem;
  color: oklch(55.6% 0 0deg);
  line-height: 1.25rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Empty {
  box-sizing: border-box;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);
  padding: 0.75rem 0.5rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Item mark {
  background-color: transparent;
  color: oklch(42.4% 0.199 265.638deg);
  font-weight: 700;

  @media (prefers-color-scheme: dark) {
    color: oklch(62.3% 0.214 259.815deg);
  }
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { matchSorter } from 'match-sorter';
import styles from './index.module.css';

export default function ExampleFuzzyMatchingAutocomplete() {
  return (
    <Autocomplete.Root
      items={fuzzyItems}
      filter={fuzzyFilter}
      itemToStringValue={(item) => item.title}
    >
      <label className={styles.Label}>
        Fuzzy search documentation
        <Autocomplete.Input placeholder="e.g. React" className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.Empty>
              <div className={styles.Empty}>No results found for "{<Autocomplete.Value />}"</div>
            </Autocomplete.Empty>

            <Autocomplete.List className={styles.List}>
              {(item: FuzzyItem) => (
                <Autocomplete.Item key={item.title} value={item} className={styles.Item}>
                  <Autocomplete.Value>
                    {(value) => (
                      <span className={styles.ItemContent}>
                        <span className={styles.ItemHeader}>
                          <span className={styles.ItemTitle}>
                            {highlightText(item.title, value)}
                          </span>
                        </span>
                        <span className={styles.ItemDescription}>
                          {highlightText(item.description, value)}
                        </span>
                      </span>
                    )}
                  </Autocomplete.Value>
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

function highlightText(text: string, query: string): React.ReactNode {
  const trimmed = query.trim();
  if (!trimmed) {
    return text;
  }

  const limited = trimmed.slice(0, 100);
  const escaped = limited.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  const regex = new RegExp(`(${escaped})`, 'gi');

  return text
    .split(regex)
    .map((part, idx) => (regex.test(part) ? <mark key={idx}>{part}</mark> : part));
}

function fuzzyFilter(item: FuzzyItem, query: string): boolean {
  if (!query) {
    return true;
  }

  const results = matchSorter([item], query, {
    keys: [
      'title',
      'description',
      'category',
      { key: 'title', threshold: matchSorter.rankings.CONTAINS },
      { key: 'description', threshold: matchSorter.rankings.WORD_STARTS_WITH },
    ],
  });

  return results.length > 0;
}

interface FuzzyItem {
  title: string;
  description: string;
  category: string;
}

const fuzzyItems: FuzzyItem[] = [
  {
    title: 'React Hooks Guide',
    description: 'Learn how to use React Hooks like useState, useEffect, and custom hooks',
    category: 'React',
  },
  {
    title: 'JavaScript Array Methods',
    description: 'Master array methods like map, filter, reduce, and forEach in JavaScript',
    category: 'JavaScript',
  },
  {
    title: 'CSS Flexbox Layout',
    description: 'Complete guide to CSS Flexbox for responsive web design',
    category: 'CSS',
  },
  {
    title: 'TypeScript Interfaces',
    description: 'Understanding TypeScript interfaces and type definitions',
    category: 'TypeScript',
  },
  {
    title: 'React Performance Optimization',
    description: 'Tips and techniques for optimizing React application performance',
    category: 'React',
  },
  {
    title: 'HTML Semantic Elements',
    description: 'Using semantic HTML elements for better accessibility and SEO',
    category: 'HTML',
  },
  {
    title: 'Node.js Express Server',
    description: 'Building RESTful APIs with Node.js and Express framework',
    category: 'Node.js',
  },
  {
    title: 'Vue Composition API',
    description: 'Modern Vue.js development using the Composition API',
    category: 'Vue.js',
  },
  {
    title: 'Angular Components',
    description: 'Creating reusable Angular components with TypeScript',
    category: 'Angular',
  },
  {
    title: 'Python Django Framework',
    description: 'Web development with Python Django framework',
    category: 'Python',
  },
  {
    title: 'CSS Grid Layout',
    description: 'Advanced CSS Grid techniques for complex layouts',
    category: 'CSS',
  },
  {
    title: 'React Testing Library',
    description: 'Testing React components with React Testing Library',
    category: 'React',
  },
  {
    title: 'MongoDB Queries',
    description: 'Advanced MongoDB queries and aggregation pipelines',
    category: 'Database',
  },
  {
    title: 'Webpack Configuration',
    description: 'Optimizing webpack configuration for production builds',
    category: 'Build Tools',
  },
  {
    title: 'SASS/SCSS Guide',
    description: 'Writing maintainable CSS with SASS and SCSS',
    category: 'CSS',
  },
];

결과 제한

limit prop으로 보이는 항목 수를 제한하고 <Autocomplete.Status>로 사용자가 쿼리를 다듬도록 안내해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';

const limit = 8;

export default function ExampleAutocompleteLimit() {
  const [value, setValue] = React.useState('');

  const { contains } = Autocomplete.useFilter({ sensitivity: 'base' });

  const totalMatches = React.useMemo(() => {
    const trimmed = value.trim();
    if (!trimmed) {
      return tags.length;
    }
    return tags.filter((t) => contains(t.value, trimmed)).length;
  }, [value, contains]);

  const moreCount = Math.max(0, totalMatches - limit);

  return (
    <Autocomplete.Root items={tags} value={value} onValueChange={setValue} limit={limit}>
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Limit results to 8
        <Autocomplete.Input
          placeholder="e.g. component"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-[var(--anchor-width)] max-h-[min(var(--available-height),22.5rem)] max-w-[var(--available-width)] overflow-y-auto scroll-pt-1 scroll-pb-1 overscroll-contain border border-neutral-950 bg-white py-1 text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.Empty>
              <div className="py-2 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                No results found for "{value}"
              </div>
            </Autocomplete.Empty>

            <Autocomplete.List>
              {(tag: Tag) => (
                <Autocomplete.Item
                  key={tag.id}
                  className="flex cursor-default py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                  value={tag}
                >
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>

            <Autocomplete.Status>
              {moreCount > 0 ? (
                <div className="py-2 pr-4 pl-2 text-sm text-neutral-500 dark:text-neutral-400">
                  {`Hiding ${moreCount} results (type a more specific query to narrow results)`}
                </div>
              ) : null}
            </Autocomplete.Status>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

// Larger dataset to make the limit visible.
const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 't7', value: 'frontend' },
  { id: 't8', value: 'backend' },
  { id: 't9', value: 'performance' },
  { id: 't10', value: 'accessibility' },
  { id: 't11', value: 'design' },
  { id: 't12', value: 'research' },
  { id: 't13', value: 'testing' },
  { id: 't14', value: 'infrastructure' },
  { id: 't15', value: 'documentation' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  padding-block: 0.25rem;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-height: min(var(--available-height), 22.5rem);
  max-width: var(--available-width);
  overflow-y: auto;
  scroll-padding-block: 0.25rem;
  overscroll-behavior: contain;

  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Empty {
  box-sizing: border-box;
  padding: 0.5rem 1rem 0.5rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Status {
  box-sizing: border-box;
  padding: 0.5rem 1rem 0.5rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

const limit = 8;

export default function ExampleAutocompleteLimit() {
  const [value, setValue] = React.useState('');

  const { contains } = Autocomplete.useFilter({ sensitivity: 'base' });

  const totalMatches = React.useMemo(() => {
    const trimmed = value.trim();
    if (!trimmed) {
      return tags.length;
    }
    return tags.filter((t) => contains(t.value, trimmed)).length;
  }, [value, contains]);

  const moreCount = Math.max(0, totalMatches - limit);

  return (
    <Autocomplete.Root items={tags} value={value} onValueChange={setValue} limit={limit}>
      <label className={styles.Label}>
        Limit results to 8
        <Autocomplete.Input placeholder="e.g. component" className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.Empty>
              <div className={styles.Empty}>No results found for "{value}"</div>
            </Autocomplete.Empty>

            <Autocomplete.List>
              {(tag: Tag) => (
                <Autocomplete.Item key={tag.id} className={styles.Item} value={tag}>
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>

            <Autocomplete.Status>
              {moreCount > 0 ? (
                <div className={styles.Status}>
                  {`Hiding ${moreCount} results (type a more specific query to narrow results)`}
                </div>
              ) : null}
            </Autocomplete.Status>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

// Larger dataset to make the limit visible.
const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 't7', value: 'frontend' },
  { id: 't8', value: 'backend' },
  { id: 't9', value: 'performance' },
  { id: 't10', value: 'accessibility' },
  { id: 't11', value: 'design' },
  { id: 't12', value: 'research' },
  { id: 't13', value: 'testing' },
  { id: 't14', value: 'infrastructure' },
  { id: 't15', value: 'documentation' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

자동 강조

<Autocomplete.Root>에서 autoHighlight prop을 지정하면 사용자가 입력하는 동안 첫 번째 일치 항목을 자동으로 강조할 수 있어요. 목록이 다이얼로그 안에 인라인으로 렌더링되는 경우처럼 강조가 항상 필요하다면 prop 값을 "always"로 설정하세요.

이 prop은 keepHighlight, highlightItemOnHover prop과 함께 사용해 마우스 상호작용 중 강조가 어떻게 동작할지 설정할 수 있어요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';

export default function ExampleAutocompleteAutoHighlight() {
  return (
    <Autocomplete.Root items={tags} autoHighlight>
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Auto highlight on type
        <Autocomplete.Input
          placeholder="e.g. feature"
          className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
        />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.Empty>
              <div className="py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                No tags found.
              </div>
            </Autocomplete.Empty>
            <Autocomplete.List className="outline-0 overflow-y-auto scroll-py-[0.25rem] py-1 overscroll-contain max-h-[min(22.5rem,var(--available-height))] data-empty:p-0">
              {(tag: Tag) => (
                <Autocomplete.Item
                  key={tag.id}
                  className="flex cursor-default items-center gap-2 py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
                  value={tag}
                >
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.List {
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-block: 0.25rem;
  scroll-padding-block: 0.25rem;
  outline: 0;
  max-height: min(22.5rem, var(--available-height));

  &[data-empty] {
    padding: 0;
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Empty {
  box-sizing: border-box;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
'use client';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

export default function ExampleAutocompleteAutoHighlight() {
  return (
    <Autocomplete.Root items={tags} autoHighlight>
      <label className={styles.Label}>
        Auto highlight on type
        <Autocomplete.Input placeholder="e.g. feature" className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.Empty>
              <div className={styles.Empty}>No tags found.</div>
            </Autocomplete.Empty>
            <Autocomplete.List className={styles.List}>
              {(tag: Tag) => (
                <Autocomplete.Item key={tag.id} className={styles.Item} value={tag}>
                  {tag.value}
                </Autocomplete.Item>
              )}
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

interface Tag {
  id: string;
  value: string;
}

const tags: Tag[] = [
  { id: 't1', value: 'feature' },
  { id: 't2', value: 'fix' },
  { id: 't3', value: 'bug' },
  { id: 't4', value: 'docs' },
  { id: 't5', value: 'internal' },
  { id: 't6', value: 'mobile' },
  { id: 'c-accordion', value: 'component: accordion' },
  { id: 'c-alert-dialog', value: 'component: alert dialog' },
  { id: 'c-autocomplete', value: 'component: autocomplete' },
  { id: 'c-avatar', value: 'component: avatar' },
  { id: 'c-checkbox', value: 'component: checkbox' },
  { id: 'c-checkbox-group', value: 'component: checkbox group' },
  { id: 'c-collapsible', value: 'component: collapsible' },
  { id: 'c-combobox', value: 'component: combobox' },
  { id: 'c-context-menu', value: 'component: context menu' },
  { id: 'c-dialog', value: 'component: dialog' },
  { id: 'c-field', value: 'component: field' },
  { id: 'c-fieldset', value: 'component: fieldset' },
  { id: 'c-filterable-menu', value: 'component: filterable menu' },
  { id: 'c-form', value: 'component: form' },
  { id: 'c-input', value: 'component: input' },
  { id: 'c-menu', value: 'component: menu' },
  { id: 'c-menubar', value: 'component: menubar' },
  { id: 'c-meter', value: 'component: meter' },
  { id: 'c-navigation-menu', value: 'component: navigation menu' },
  { id: 'c-number-field', value: 'component: number field' },
  { id: 'c-popover', value: 'component: popover' },
  { id: 'c-preview-card', value: 'component: preview card' },
  { id: 'c-progress', value: 'component: progress' },
  { id: 'c-radio', value: 'component: radio' },
  { id: 'c-scroll-area', value: 'component: scroll area' },
  { id: 'c-select', value: 'component: select' },
  { id: 'c-separator', value: 'component: separator' },
  { id: 'c-slider', value: 'component: slider' },
  { id: 'c-switch', value: 'component: switch' },
  { id: 'c-tabs', value: 'component: tabs' },
  { id: 'c-toast', value: 'component: toast' },
  { id: 'c-toggle', value: 'component: toggle' },
  { id: 'c-toggle-group', value: 'component: toggle group' },
  { id: 'c-toolbar', value: 'component: toolbar' },
  { id: 'c-tooltip', value: 'component: tooltip' },
];

명령 팔레트

클릭하면 동작하는 명령 항목 목록을 필터링하는 데 자동완성 입력을 사용해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { Dialog } from '@base-ui/react/dialog';
import { ScrollArea } from '@base-ui/react/scroll-area';

export default function ExampleAutocompleteCommandPalette() {
  const [open, setOpen] = React.useState(false);
  const shortcutsDescriptionId = React.useId();

  function handleItemClick() {
    setOpen(false);
  }

  return (
    <Dialog.Root open={open} onOpenChange={setOpen}>
      <Dialog.Trigger className="flex h-8 cursor-default items-center justify-center gap-2 border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700">
        Open command palette
      </Dialog.Trigger>
      <Dialog.Portal>
        <Dialog.Backdrop className="fixed inset-0 bg-black opacity-20 transition-opacity duration-150 ease-[cubic-bezier(0.45,1.005,0,1.005)] data-ending-style:opacity-0 data-starting-style:opacity-0 dark:opacity-70 supports-[-webkit-touch-callout:none]:absolute" />
        <Dialog.Viewport className="fixed inset-0 flex items-start justify-center overflow-hidden px-2 pt-18 pb-2">
          <Dialog.Popup
            className="relative flex max-h-[min(36rem,calc(100dvh-5rem))] w-[calc(100vw-1rem)] max-w-md flex-col border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[translate,scale,opacity] duration-150 data-ending-style:-translate-y-4 data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:-translate-y-4 data-starting-style:scale-95 data-starting-style:opacity-0 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
            aria-label="Command palette"
          >
            <Autocomplete.Root
              open
              inline
              items={groupedItems}
              autoHighlight="always"
              keepHighlight
            >
              <Autocomplete.InputGroup className="relative z-1 flex cursor-text items-center gap-2 bg-white pl-3 focus-within:outline-2 focus-within:outline-solid focus-within:-outline-offset-1 focus-within:outline-neutral-950 dark:bg-neutral-950 dark:focus-within:outline-white">
                <MagnifyingGlassIcon
                  className="h-4 w-4 shrink-0 text-neutral-500 dark:text-neutral-400"
                  aria-hidden
                />
                <Autocomplete.Input
                  className="h-10 w-full border-0 bg-transparent pr-3 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 outline-none placeholder:text-neutral-500 dark:text-white dark:placeholder:text-neutral-400"
                  aria-label="Search commands"
                  aria-describedby={shortcutsDescriptionId}
                  placeholder="Search for apps and commands…"
                />
              </Autocomplete.InputGroup>
              <Dialog.Close className="sr-only">Close command palette</Dialog.Close>

              <ScrollArea.Root className="relative flex max-h-[min(60dvh,24rem)] min-h-0 flex-[0_1_auto] overflow-hidden border-t border-neutral-950 dark:border-t-white">
                <ScrollArea.Viewport className="min-h-0 flex-1 overscroll-contain [scroll-padding-block:0.25rem] focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">
                  <ScrollArea.Content style={{ minWidth: '100%' }}>
                    <Autocomplete.Empty>
                      <div className="flex min-h-32 items-center justify-start py-4 pr-4 pl-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                        No results found.
                      </div>
                    </Autocomplete.Empty>

                    <Autocomplete.List className="py-1">
                      {(group: Group) => (
                        <Autocomplete.Group
                          key={group.value}
                          items={group.items}
                          className="not-last:mb-1"
                        >
                          <Autocomplete.GroupLabel className="flex min-h-8 items-center pr-6 pl-3 text-sm leading-none font-normal text-neutral-500 select-none outline-none dark:text-neutral-400">
                            {group.value}
                          </Autocomplete.GroupLabel>
                          <Autocomplete.Collection>
                            {(item: Item) => (
                              <Autocomplete.Item
                                key={item.value}
                                value={item}
                                onClick={handleItemClick}
                                className="group grid min-h-8 cursor-default grid-cols-[minmax(0,1fr)_auto] items-center gap-2 px-6 text-sm font-normal leading-[1.25] outline-none select-none [scroll-margin-block:0.25rem] data-highlighted:bg-neutral-200 dark:data-highlighted:bg-neutral-700"
                              >
                                <span className="min-w-0 truncate font-normal">{item.label}</span>
                                <span className="shrink-0 whitespace-nowrap text-sm text-neutral-500 group-data-highlighted:text-neutral-700 dark:text-neutral-400 dark:group-data-highlighted:text-neutral-300">
                                  {group.value === 'Suggestions' ? 'Application' : 'Command'}
                                </span>
                              </Autocomplete.Item>
                            )}
                          </Autocomplete.Collection>
                        </Autocomplete.Group>
                      )}
                    </Autocomplete.List>
                  </ScrollArea.Content>
                </ScrollArea.Viewport>
                <ScrollArea.Scrollbar className="flex w-4 justify-center bg-black/12 dark:bg-white/12">
                  <ScrollArea.Thumb className="w-full bg-neutral-950 dark:bg-white" />
                </ScrollArea.Scrollbar>
              </ScrollArea.Root>

              <div className="flex items-center justify-between border-t border-neutral-950 bg-white px-3 py-2.5 text-xs text-neutral-600 dark:border-white dark:bg-neutral-950 dark:text-neutral-400">
                <span id={shortcutsDescriptionId} className="sr-only">
                  Use Enter to activate the highlighted item.
                </span>
                <div className="flex items-center gap-1">
                  <span>Activate</span>
                  <kbd className="inline-flex h-5 min-w-5 items-center justify-center border border-neutral-400 bg-neutral-100 px-1 font-mono text-[0.625rem] leading-none font-normal text-neutral-600 dark:border-neutral-600 dark:bg-neutral-900 dark:text-neutral-400">
                    Enter
                  </kbd>
                </div>
              </div>
            </Autocomplete.Root>
          </Dialog.Popup>
        </Dialog.Viewport>
      </Dialog.Portal>
    </Dialog.Root>
  );
}

function MagnifyingGlassIcon(props: React.ComponentProps<'svg'>) {
  return (
    <svg
      width="16"
      height="16"
      viewBox="0 0 16 16"
      fill="none"
      stroke="currentColor"
      strokeLinecap="square"
      strokeLinejoin="round"
      {...props}
      style={{ display: 'block', ...props.style }}
    >
      <path d="m11 11 3.5 3.5" />
      <circle cx="7" cy="7" r="5.5" />
    </svg>
  );
}

interface Item {
  value: string;
  label: string;
}

interface Group {
  value: string;
  items: Item[];
}

const suggestions: Item[] = [
  { value: 'linear', label: 'Linear' },
  { value: 'figma', label: 'Figma' },
  { value: 'slack', label: 'Slack' },
  { value: 'youtube', label: 'YouTube' },
  { value: 'raycast', label: 'Raycast' },
  { value: 'notion', label: 'Notion' },
  { value: 'github', label: 'GitHub' },
  { value: 'jira', label: 'Jira' },
  { value: 'calendar', label: 'Google Calendar' },
  { value: 'chrome', label: 'Google Chrome' },
  { value: 'mail', label: 'Apple Mail' },
  { value: 'terminal', label: 'Terminal' },
];

const commands: Item[] = [
  { value: 'clipboard-history', label: 'Clipboard History' },
  { value: 'import-extension', label: 'Import Extension' },
  { value: 'create-snippet', label: 'Create Snippet' },
  { value: 'system-preferences', label: 'System Preferences' },
  { value: 'window-management', label: 'Window Management' },
  { value: 'toggle-dark-mode', label: 'Toggle Dark Mode' },
  { value: 'new-window', label: 'New Window' },
  { value: 'new-tab', label: 'New Tab' },
  { value: 'search-docs', label: 'Search Documentation' },
  { value: 'capture-screen', label: 'Capture Screenshot' },
  { value: 'close-sidebar', label: 'Toggle Sidebar' },
  { value: 'toggle-terminal', label: 'Toggle Integrated Terminal' },
  { value: 'run-script', label: 'Run Script' },
];

const groupedItems: Group[] = [
  { value: 'Suggestions', items: suggestions },
  { value: 'Commands', items: commands },
];

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;
  cursor: default;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Backdrop {
  position: fixed;
  inset: 0;
  background-color: black;
  opacity: 0.2;
  transition: opacity 150ms cubic-bezier(0.45, 1.005, 0, 1.005);

  @media (prefers-color-scheme: dark) {
    opacity: 0.7;
  }

  &[data-starting-style],
  &[data-ending-style] {
    opacity: 0;
  }

  @supports (-webkit-touch-callout: none) {
    position: absolute;
  }
}

.Viewport {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 4.5rem 0.5rem 0.5rem;
  overflow: hidden;
}

.Popup {
  box-sizing: border-box;
  position: relative;
  width: calc(100vw - 1rem);
  max-width: 28rem;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  transition:
    transform 150ms,
    opacity 150ms;
  max-height: min(36rem, calc(100dvh - 5rem));
  display: flex;
  flex-direction: column;
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }

  &[data-starting-style],
  &[data-ending-style] {
    opacity: 0;
    transform: translateY(-1rem) scale(0.95);
  }
}

.InputGroup {
  box-sizing: border-box;
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-left: 0.75rem;
  background-color: white;
  cursor: text;

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
  }

  &:focus-within {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.InputIcon {
  flex-shrink: 0;
  width: 1rem;
  height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.75rem 0 0;
  margin: 0;
  border: none;
  width: 100%;
  height: 2.5rem;
  border-radius: 0;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: transparent;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }
}

.ListArea {
  position: relative;
  display: flex;
  flex: 0 1 auto;
  min-height: 0;
  max-height: min(60dvh, 24rem);
  overflow: hidden;
  border-top: 1px solid oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    border-color: white;
  }
}

.ListViewport {
  box-sizing: border-box;
  flex: 1 1 auto;
  min-height: 0;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.List {
  box-sizing: border-box;
  padding: 0.25rem 0;

  &:empty {
    padding: 0;
  }
}

.ListContent {
  min-width: 100%;
}

.Group:not(:last-child) {
  margin-block-end: 0.25rem;
}

.GroupLabel {
  outline: 0;
  -webkit-user-select: none;
  user-select: none;
  display: flex;
  align-items: center;
  min-height: 2rem;
  margin: 0;
  padding: 0 1.5rem 0 0.75rem;
  font-size: 0.875rem;
  line-height: 1;
  font-weight: 400;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  scroll-margin-block: 0.25rem;
  min-height: 2rem;
  padding: 0 1.5rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.5rem;
  align-items: center;
  font-size: 0.875rem;
  line-height: 1.25;
  font-weight: 400;

  &[data-highlighted] {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }
}

.ItemLabel {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 400;
}

.ItemType {
  font-size: 0.875rem;
  color: oklch(55.6% 0 0deg);
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }

  [data-highlighted] & {
    color: oklch(37.1% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(87% 0 0deg);
    }
  }
}

.Empty {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 1rem 1rem 1rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);
  min-height: 8rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Scrollbar {
  display: flex;
  justify-content: center;
  background-color: rgb(0 0 0 / 12%);
  width: 1rem;
  transition: opacity 150ms;

  @media (prefers-color-scheme: dark) {
    background-color: rgb(255 255 255 / 12%);
  }
}

.ScrollbarThumb {
  width: 100%;
  background-color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    background-color: white;
  }
}

.Footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.625rem 0.75rem;
  border-top: 1px solid oklch(14.5% 0 0deg);
  font-size: 0.75rem;
  color: oklch(43.9% 0 0deg);
  background-color: white;

  @media (prefers-color-scheme: dark) {
    border-top: 1px solid white;
    color: oklch(70.8% 0 0deg);
    background-color: oklch(14.5% 0 0deg);
  }
}

.FooterLeft,
.FooterRight {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.Kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 0.25rem;
  font-size: 0.625rem;
  font-family:
    'Paper Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono',
    'Courier New', monospace;
  font-weight: 400;
  line-height: 1;
  color: oklch(43.9% 0 0deg);
  background-color: oklch(97% 0 0deg);
  border: 1px solid oklch(70.8% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
    background-color: oklch(20.5% 0 0deg);
    border: 1px solid oklch(43.9% 0 0deg);
  }
}

.VisuallyHidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Dialog } from '@base-ui/react/dialog';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { ScrollArea } from '@base-ui/react/scroll-area';
import styles from './index.module.css';

export default function ExampleAutocompleteCommandPalette() {
  const [open, setOpen] = React.useState(false);
  const shortcutsDescriptionId = React.useId();

  function handleItemClick() {
    setOpen(false);
  }

  return (
    <Dialog.Root open={open} onOpenChange={setOpen}>
      <Dialog.Trigger className={styles.Button}>Open command palette</Dialog.Trigger>
      <Dialog.Portal>
        <Dialog.Backdrop className={styles.Backdrop} />
        <Dialog.Viewport className={styles.Viewport}>
          <Dialog.Popup className={styles.Popup} aria-label="Command palette">
            <Autocomplete.Root
              open
              inline
              items={groupedItems}
              autoHighlight="always"
              keepHighlight
            >
              <Autocomplete.InputGroup className={styles.InputGroup}>
                <MagnifyingGlassIcon className={styles.InputIcon} aria-hidden />
                <Autocomplete.Input
                  className={styles.Input}
                  aria-label="Search commands"
                  aria-describedby={shortcutsDescriptionId}
                  placeholder="Search for apps and commands…"
                />
              </Autocomplete.InputGroup>
              <Dialog.Close className={styles.VisuallyHidden}>Close command palette</Dialog.Close>

              <ScrollArea.Root className={styles.ListArea}>
                <ScrollArea.Viewport className={styles.ListViewport}>
                  <ScrollArea.Content className={styles.ListContent}>
                    <Autocomplete.Empty>
                      <div className={styles.Empty}>No results found.</div>
                    </Autocomplete.Empty>

                    <Autocomplete.List className={styles.List}>
                      {(group: Group) => (
                        <Autocomplete.Group
                          key={group.value}
                          items={group.items}
                          className={styles.Group}
                        >
                          <Autocomplete.GroupLabel className={styles.GroupLabel}>
                            {group.value}
                          </Autocomplete.GroupLabel>
                          <Autocomplete.Collection>
                            {(item: Item) => (
                              <Autocomplete.Item
                                key={item.value}
                                value={item}
                                className={styles.Item}
                                onClick={handleItemClick}
                              >
                                <span className={styles.ItemLabel}>{item.label}</span>
                                <span className={styles.ItemType}>
                                  {group.value === 'Suggestions' ? 'Application' : 'Command'}
                                </span>
                              </Autocomplete.Item>
                            )}
                          </Autocomplete.Collection>
                        </Autocomplete.Group>
                      )}
                    </Autocomplete.List>
                  </ScrollArea.Content>
                </ScrollArea.Viewport>
                <ScrollArea.Scrollbar className={styles.Scrollbar}>
                  <ScrollArea.Thumb className={styles.ScrollbarThumb} />
                </ScrollArea.Scrollbar>
              </ScrollArea.Root>

              <div className={styles.Footer}>
                <span id={shortcutsDescriptionId} className={styles.VisuallyHidden}>
                  Use Enter to activate the highlighted item.
                </span>
                <div className={styles.FooterLeft}>
                  <span>Activate</span>
                  <kbd className={styles.Kbd}>Enter</kbd>
                </div>
              </div>
            </Autocomplete.Root>
          </Dialog.Popup>
        </Dialog.Viewport>
      </Dialog.Portal>
    </Dialog.Root>
  );
}

function MagnifyingGlassIcon(props: React.ComponentProps<'svg'>) {
  return (
    <svg
      width="16"
      height="16"
      viewBox="0 0 16 16"
      fill="none"
      stroke="currentColor"
      strokeLinecap="square"
      strokeLinejoin="round"
      {...props}
      style={{ display: 'block', ...props.style }}
    >
      <path d="m11 11 3.5 3.5" />
      <circle cx="7" cy="7" r="5.5" />
    </svg>
  );
}

interface Item {
  value: string;
  label: string;
}

interface Group {
  value: string;
  items: Item[];
}

const suggestions: Item[] = [
  { value: 'linear', label: 'Linear' },
  { value: 'figma', label: 'Figma' },
  { value: 'slack', label: 'Slack' },
  { value: 'youtube', label: 'YouTube' },
  { value: 'raycast', label: 'Raycast' },
  { value: 'notion', label: 'Notion' },
  { value: 'github', label: 'GitHub' },
  { value: 'jira', label: 'Jira' },
  { value: 'calendar', label: 'Google Calendar' },
  { value: 'chrome', label: 'Google Chrome' },
  { value: 'mail', label: 'Apple Mail' },
  { value: 'terminal', label: 'Terminal' },
];

const commands: Item[] = [
  { value: 'clipboard-history', label: 'Clipboard History' },
  { value: 'import-extension', label: 'Import Extension' },
  { value: 'create-snippet', label: 'Create Snippet' },
  { value: 'system-preferences', label: 'System Preferences' },
  { value: 'window-management', label: 'Window Management' },
  { value: 'toggle-dark-mode', label: 'Toggle Dark Mode' },
  { value: 'new-window', label: 'New Window' },
  { value: 'new-tab', label: 'New Tab' },
  { value: 'search-docs', label: 'Search Documentation' },
  { value: 'capture-screen', label: 'Capture Screenshot' },
  { value: 'close-sidebar', label: 'Toggle Sidebar' },
  { value: 'toggle-terminal', label: 'Toggle Integrated Terminal' },
  { value: 'run-script', label: 'Run Script' },
];

const groupedItems: Group[] = [
  { value: 'Suggestions', items: suggestions },
  { value: 'Commands', items: commands },
];

그리드 레이아웃

각 행을 <Autocomplete.Row> 컴포넌트로 감싸 항목을 그리드 레이아웃으로 표시해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';

export default function ExampleEmojiPicker() {
  const [pickerOpen, setPickerOpen] = React.useState(false);
  const [textValue, setTextValue] = React.useState('');
  const [searchValue, setSearchValue] = React.useState('');

  const textInputRef = React.useRef<HTMLInputElement | null>(null);

  function handleInsertEmoji(value: string | null) {
    if (!value || !textInputRef.current) {
      return;
    }

    const emoji = value;
    const start = textInputRef.current.selectionStart ?? textInputRef.current.value.length ?? 0;
    const end = textInputRef.current.selectionEnd ?? textInputRef.current.value.length ?? 0;

    setTextValue((prev) => prev.slice(0, start) + emoji + prev.slice(end));
    setPickerOpen(false);

    const input = textInputRef.current;
    if (input) {
      input.focus();
      const caretPos = start + emoji.length;
      input.setSelectionRange(caretPos, caretPos);
    }
  }

  return (
    <div className="mx-auto w-[16rem]">
      <div className="relative flex w-full">
        <input
          ref={textInputRef}
          type="text"
          aria-label="Message"
          className="-mr-px h-8 flex-1 border border-r-0 border-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 bg-white dark:bg-neutral-950 focus:relative focus:outline-2 focus:-outline-offset-1 focus:outline-solid focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white"
          placeholder="iMessage"
          value={textValue}
          onChange={(event) => setTextValue(event.target.value)}
        />

        <Autocomplete.Root
          items={emojiGroups}
          grid
          open={pickerOpen}
          onOpenChange={setPickerOpen}
          onOpenChangeComplete={() => setSearchValue('')}
          value={searchValue}
          onValueChange={(value, details) => {
            if (details.reason !== 'item-press') {
              setSearchValue(value);
            }
          }}
        >
          <Autocomplete.Trigger
            className="flex size-8 items-center justify-center border border-neutral-950 bg-transparent text-xl leading-none text-neutral-950 outline-none hover:bg-neutral-100 active:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-solid focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-pressed:bg-neutral-100 dark:border-white dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 dark:data-pressed:bg-neutral-800"
            aria-label="Choose emoji"
          >
            😀
          </Autocomplete.Trigger>
          <Autocomplete.Portal>
            <Autocomplete.Positioner className="outline-0" sideOffset={4} align="end">
              <Autocomplete.Popup
                className="[--input-container-height:2rem] max-h-[20.5rem] max-w-[var(--available-width)] origin-[var(--transform-origin)] bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[transform,scale,opacity] data-ending-style:scale-90 data-ending-style:opacity-0 data-starting-style:scale-90 data-starting-style:opacity-0 dark:bg-neutral-950 dark:text-white dark:shadow-none"
                aria-label="Select emoji"
              >
                <Autocomplete.Input
                  aria-label="Search emojis"
                  placeholder="Search emojis…"
                  className="h-8 w-64 max-w-full border border-neutral-950 bg-white px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-2 focus:outline-solid focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:bg-neutral-950 dark:text-white"
                />
                <div className="border border-t-0 border-neutral-950 dark:border-white">
                  <Autocomplete.Empty>
                    <div className="px-2 py-3 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                      No emojis found
                    </div>
                  </Autocomplete.Empty>
                  <Autocomplete.List
                    aria-label="Emoji results"
                    className="max-h-[min(calc(20.5rem-var(--input-container-height)-2px),calc(var(--available-height)-var(--input-container-height)-2px))] overflow-auto scroll-pt-1 scroll-pb-[0.35rem] overscroll-contain py-2 empty:p-0"
                  >
                    {(group: EmojiGroup) => (
                      <Autocomplete.Group key={group.value} items={group.items} className="block">
                        <Autocomplete.GroupLabel className="p-2 text-sm leading-4 text-neutral-500 select-none dark:text-neutral-400">
                          {group.label}
                        </Autocomplete.GroupLabel>
                        <div className="px-2 pb-1 pt-0" role="presentation">
                          {chunkArray(group.items, COLUMNS).map((row, rowIdx) => (
                            <Autocomplete.Row key={rowIdx} className="grid grid-cols-5">
                              {row.map((rowItem) => (
                                <Autocomplete.Item
                                  key={rowItem.emoji}
                                  value={rowItem}
                                  className="group flex h-10 min-w-[var(--anchor-width)] cursor-default flex-col items-center justify-center bg-transparent px-0.5 py-2 text-neutral-950 outline-0 select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-100 dark:text-white dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-neutral-800"
                                  onClick={() => {
                                    handleInsertEmoji(rowItem.emoji);
                                    setPickerOpen(false);
                                  }}
                                >
                                  <span className="text-2xl leading-none">{rowItem.emoji}</span>
                                </Autocomplete.Item>
                              ))}
                            </Autocomplete.Row>
                          ))}
                        </div>
                      </Autocomplete.Group>
                    )}
                  </Autocomplete.List>
                </div>
              </Autocomplete.Popup>
            </Autocomplete.Positioner>
          </Autocomplete.Portal>
        </Autocomplete.Root>
      </div>
    </div>
  );
}

const COLUMNS = 5;

function chunkArray<T>(array: T[], size: number): T[][] {
  const result: T[][] = [];
  for (let i = 0; i < array.length; i += size) {
    result.push(array.slice(i, i + size));
  }
  return result;
}

interface EmojiItem {
  emoji: string;
  value: string;
  name: string;
}

interface EmojiGroup {
  value: string;
  label: string;
  items: EmojiItem[];
}

export const emojiCategories = [
  {
    label: 'Smileys & Emotion',
    emojis: [
      { emoji: '😀', name: 'grinning face' },
      { emoji: '😃', name: 'grinning face with big eyes' },
      { emoji: '😄', name: 'grinning face with smiling eyes' },
      { emoji: '😁', name: 'beaming face with smiling eyes' },
      { emoji: '😆', name: 'grinning squinting face' },
      { emoji: '😅', name: 'grinning face with sweat' },
      { emoji: '🤣', name: 'rolling on the floor laughing' },
      { emoji: '😂', name: 'face with tears of joy' },
      { emoji: '🙂', name: 'slightly smiling face' },
      { emoji: '🙃', name: 'upside-down face' },
      { emoji: '😉', name: 'winking face' },
      { emoji: '😊', name: 'smiling face with smiling eyes' },
      { emoji: '😇', name: 'smiling face with halo' },
      { emoji: '🥰', name: 'smiling face with hearts' },
      { emoji: '😍', name: 'smiling face with heart-eyes' },
      { emoji: '🤩', name: 'star-struck' },
      { emoji: '😘', name: 'face blowing a kiss' },
      { emoji: '😗', name: 'kissing face' },
      { emoji: '☺️', name: 'smiling face' },
      { emoji: '😚', name: 'kissing face with closed eyes' },
      { emoji: '😙', name: 'kissing face with smiling eyes' },
      { emoji: '🥲', name: 'smiling face with tear' },
      { emoji: '😋', name: 'face savoring food' },
      { emoji: '😛', name: 'face with tongue' },
      { emoji: '😜', name: 'winking face with tongue' },
      { emoji: '🤪', name: 'zany face' },
      { emoji: '😝', name: 'squinting face with tongue' },
      { emoji: '🤑', name: 'money-mouth face' },
      { emoji: '🤗', name: 'hugging face' },
      { emoji: '🤭', name: 'face with hand over mouth' },
    ],
  },
  {
    label: 'Animals & Nature',
    emojis: [
      { emoji: '🐶', name: 'dog face' },
      { emoji: '🐱', name: 'cat face' },
      { emoji: '🐭', name: 'mouse face' },
      { emoji: '🐹', name: 'hamster' },
      { emoji: '🐰', name: 'rabbit face' },
      { emoji: '🦊', name: 'fox' },
      { emoji: '🐻', name: 'bear' },
      { emoji: '🐼', name: 'panda' },
      { emoji: '🐨', name: 'koala' },
      { emoji: '🐯', name: 'tiger face' },
      { emoji: '🦁', name: 'lion' },
      { emoji: '🐮', name: 'cow face' },
      { emoji: '🐷', name: 'pig face' },
      { emoji: '🐽', name: 'pig nose' },
      { emoji: '🐸', name: 'frog' },
      { emoji: '🐵', name: 'monkey face' },
      { emoji: '🙈', name: 'see-no-evil monkey' },
      { emoji: '🙉', name: 'hear-no-evil monkey' },
      { emoji: '🙊', name: 'speak-no-evil monkey' },
      { emoji: '🐒', name: 'monkey' },
      { emoji: '🐔', name: 'chicken' },
      { emoji: '🐧', name: 'penguin' },
      { emoji: '🐦', name: 'bird' },
      { emoji: '🐤', name: 'baby chick' },
      { emoji: '🐣', name: 'hatching chick' },
      { emoji: '🐥', name: 'front-facing baby chick' },
      { emoji: '🦆', name: 'duck' },
      { emoji: '🦅', name: 'eagle' },
      { emoji: '🦉', name: 'owl' },
      { emoji: '🦇', name: 'bat' },
    ],
  },
  {
    label: 'Food & Drink',
    emojis: [
      { emoji: '🍎', name: 'red apple' },
      { emoji: '🍏', name: 'green apple' },
      { emoji: '🍊', name: 'tangerine' },
      { emoji: '🍋', name: 'lemon' },
      { emoji: '🍌', name: 'banana' },
      { emoji: '🍉', name: 'watermelon' },
      { emoji: '🍇', name: 'grapes' },
      { emoji: '🍓', name: 'strawberry' },
      { emoji: '🫐', name: 'blueberries' },
      { emoji: '🍈', name: 'melon' },
      { emoji: '🍒', name: 'cherries' },
      { emoji: '🍑', name: 'peach' },
      { emoji: '🥭', name: 'mango' },
      { emoji: '🍍', name: 'pineapple' },
      { emoji: '🥥', name: 'coconut' },
      { emoji: '🥝', name: 'kiwi fruit' },
      { emoji: '🍅', name: 'tomato' },
      { emoji: '🍆', name: 'eggplant' },
      { emoji: '🥑', name: 'avocado' },
      { emoji: '🥦', name: 'broccoli' },
      { emoji: '🥬', name: 'leafy greens' },
      { emoji: '🥒', name: 'cucumber' },
      { emoji: '🌶️', name: 'hot pepper' },
      { emoji: '🫑', name: 'bell pepper' },
      { emoji: '🌽', name: 'ear of corn' },
      { emoji: '🥕', name: 'carrot' },
      { emoji: '🫒', name: 'olive' },
      { emoji: '🧄', name: 'garlic' },
      { emoji: '🧅', name: 'onion' },
      { emoji: '🥔', name: 'potato' },
    ],
  },
];

const emojiGroups: EmojiGroup[] = emojiCategories.map((category) => ({
  value: category.label,
  label: category.label,
  items: category.emojis.map((emoji) => ({
    ...emoji,
    value: emoji.name.toLowerCase(),
  })),
}));

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Container {
  width: 16rem;
  margin: 0 auto;
}

.InputGroup {
  position: relative;
  display: flex;
  width: 100%;
}

.TextInput {
  box-sizing: border-box;
  flex: 1;
  padding: 0 0.5rem;
  margin: 0;
  margin-right: -1px;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-right: 0;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  color: oklch(14.5% 0 0deg);
  outline: none;
  background-color: white;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    border-right: 0;
    color: white;
    background-color: oklch(14.5% 0 0deg);
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    position: relative;
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.EmojiButton {
  box-sizing: border-box;
  width: 2rem;
  height: 2rem;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  line-height: 1;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    color: white;
  }

  &:hover {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-pressed] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  margin: 0;
  width: 16rem;
  max-width: 100%;
  height: var(--input-container-height);
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border-color: white;
  }

  &::placeholder {
    color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
    }
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -2px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Viewport {
  border: 1px solid oklch(14.5% 0 0deg);
  border-top: none;

  @media (prefers-color-scheme: dark) {
    border-color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  --input-container-height: 2rem;
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  transform-origin: var(--transform-origin);
  transition:
    transform 150ms,
    opacity 150ms;
  max-width: var(--available-width);
  max-height: 20.5rem;
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-starting-style],
  &[data-ending-style] {
    opacity: 0;
    transform: scale(0.9);
  }
}

.List {
  overflow: auto;
  scroll-padding-top: 0.25rem;
  scroll-padding-bottom: 0.35rem;
  overscroll-behavior: contain;
  max-height: min(
    calc(20.5rem - var(--input-container-height) - 2px),
    calc(var(--available-height) - var(--input-container-height) - 2px)
  );
  padding-block: 0.5rem;

  &:empty {
    padding: 0;
  }
}

.GroupLabel {
  box-sizing: border-box;
  padding: 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Group {
  display: block;
}

.Grid {
  padding: 0 0.5rem 0.25rem;
}

.Row {
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), 1fr);
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: var(--anchor-width);
  height: 2.5rem;
  padding: 0.5rem 0.125rem;
  background: transparent;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }

    &::before {
      content: '';
      z-index: -1;
      position: absolute;
      inset: 0;
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }
}

.Emoji {
  font-size: 1.5rem;
  line-height: 1;
}

.Name {
  font-size: 0.625rem;
  text-align: center;
  opacity: 0.8;
  line-height: 1.2;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.Item[data-highlighted] .Name {
  opacity: 1;
}

.Empty {
  box-sizing: border-box;
  padding: 0.75rem 0.5rem;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import styles from './index.module.css';

export default function ExampleEmojiPicker() {
  const [pickerOpen, setPickerOpen] = React.useState(false);
  const [textValue, setTextValue] = React.useState('');
  const [searchValue, setSearchValue] = React.useState('');

  const textInputRef = React.useRef<HTMLInputElement | null>(null);

  function handleInsertEmoji(value: string | null) {
    if (!value || !textInputRef.current) {
      return;
    }

    const emoji = value;
    const start = textInputRef.current.selectionStart ?? textInputRef.current.value.length ?? 0;
    const end = textInputRef.current.selectionEnd ?? textInputRef.current.value.length ?? 0;

    setTextValue((prev) => prev.slice(0, start) + emoji + prev.slice(end));
    setPickerOpen(false);

    const input = textInputRef.current;
    if (input) {
      input.focus();
      const caretPos = start + emoji.length;
      input.setSelectionRange(caretPos, caretPos);
    }
  }

  return (
    <div className={styles.Container}>
      <div className={styles.InputGroup}>
        <input
          ref={textInputRef}
          type="text"
          aria-label="Message"
          className={styles.TextInput}
          placeholder="iMessage"
          value={textValue}
          onChange={(event) => setTextValue(event.target.value)}
        />

        <Autocomplete.Root
          items={emojiGroups}
          grid
          open={pickerOpen}
          onOpenChange={setPickerOpen}
          onOpenChangeComplete={() => setSearchValue('')}
          value={searchValue}
          onValueChange={(value, details) => {
            if (details.reason !== 'item-press') {
              setSearchValue(value);
            }
          }}
        >
          <Autocomplete.Trigger className={styles.EmojiButton} aria-label="Choose emoji">
            😀
          </Autocomplete.Trigger>
          <Autocomplete.Portal>
            <Autocomplete.Positioner className={styles.Positioner} sideOffset={4} align="end">
              <Autocomplete.Popup className={styles.Popup} aria-label="Select emoji">
                <Autocomplete.Input
                  aria-label="Search emojis"
                  placeholder="Search emojis…"
                  className={styles.Input}
                />
                <div className={styles.Viewport}>
                  <Autocomplete.Empty>
                    <div className={styles.Empty}>No emojis found</div>
                  </Autocomplete.Empty>
                  <Autocomplete.List
                    aria-label="Emoji results"
                    className={styles.List}
                    style={{ '--cols': COLUMNS } as React.CSSProperties}
                  >
                    {(group: EmojiGroup) => (
                      <Autocomplete.Group
                        key={group.value}
                        items={group.items}
                        className={styles.Group}
                      >
                        <Autocomplete.GroupLabel className={styles.GroupLabel}>
                          {group.label}
                        </Autocomplete.GroupLabel>
                        <div className={styles.Grid} role="presentation">
                          {chunkArray(group.items, COLUMNS).map((row, rowIdx) => (
                            <Autocomplete.Row key={rowIdx} className={styles.Row}>
                              {row.map((rowItem) => (
                                <Autocomplete.Item
                                  key={rowItem.emoji}
                                  value={rowItem}
                                  className={styles.Item}
                                  onClick={() => {
                                    handleInsertEmoji(rowItem.emoji);
                                    setPickerOpen(false);
                                  }}
                                >
                                  <span className={styles.Emoji}>{rowItem.emoji}</span>
                                </Autocomplete.Item>
                              ))}
                            </Autocomplete.Row>
                          ))}
                        </div>
                      </Autocomplete.Group>
                    )}
                  </Autocomplete.List>
                </div>
              </Autocomplete.Popup>
            </Autocomplete.Positioner>
          </Autocomplete.Portal>
        </Autocomplete.Root>
      </div>
    </div>
  );
}

const COLUMNS = 5;

function chunkArray<T>(array: T[], size: number): T[][] {
  const result: T[][] = [];
  for (let i = 0; i < array.length; i += size) {
    result.push(array.slice(i, i + size));
  }
  return result;
}

interface EmojiItem {
  emoji: string;
  value: string;
  name: string;
}

interface EmojiGroup {
  value: string;
  label: string;
  items: EmojiItem[];
}

export const emojiCategories = [
  {
    label: 'Smileys & Emotion',
    emojis: [
      { emoji: '😀', name: 'grinning face' },
      { emoji: '😃', name: 'grinning face with big eyes' },
      { emoji: '😄', name: 'grinning face with smiling eyes' },
      { emoji: '😁', name: 'beaming face with smiling eyes' },
      { emoji: '😆', name: 'grinning squinting face' },
      { emoji: '😅', name: 'grinning face with sweat' },
      { emoji: '🤣', name: 'rolling on the floor laughing' },
      { emoji: '😂', name: 'face with tears of joy' },
      { emoji: '🙂', name: 'slightly smiling face' },
      { emoji: '🙃', name: 'upside-down face' },
      { emoji: '😉', name: 'winking face' },
      { emoji: '😊', name: 'smiling face with smiling eyes' },
      { emoji: '😇', name: 'smiling face with halo' },
      { emoji: '🥰', name: 'smiling face with hearts' },
      { emoji: '😍', name: 'smiling face with heart-eyes' },
      { emoji: '🤩', name: 'star-struck' },
      { emoji: '😘', name: 'face blowing a kiss' },
      { emoji: '😗', name: 'kissing face' },
      { emoji: '☺️', name: 'smiling face' },
      { emoji: '😚', name: 'kissing face with closed eyes' },
      { emoji: '😙', name: 'kissing face with smiling eyes' },
      { emoji: '🥲', name: 'smiling face with tear' },
      { emoji: '😋', name: 'face savoring food' },
      { emoji: '😛', name: 'face with tongue' },
      { emoji: '😜', name: 'winking face with tongue' },
      { emoji: '🤪', name: 'zany face' },
      { emoji: '😝', name: 'squinting face with tongue' },
      { emoji: '🤑', name: 'money-mouth face' },
      { emoji: '🤗', name: 'hugging face' },
      { emoji: '🤭', name: 'face with hand over mouth' },
    ],
  },
  {
    label: 'Animals & Nature',
    emojis: [
      { emoji: '🐶', name: 'dog face' },
      { emoji: '🐱', name: 'cat face' },
      { emoji: '🐭', name: 'mouse face' },
      { emoji: '🐹', name: 'hamster' },
      { emoji: '🐰', name: 'rabbit face' },
      { emoji: '🦊', name: 'fox' },
      { emoji: '🐻', name: 'bear' },
      { emoji: '🐼', name: 'panda' },
      { emoji: '🐨', name: 'koala' },
      { emoji: '🐯', name: 'tiger face' },
      { emoji: '🦁', name: 'lion' },
      { emoji: '🐮', name: 'cow face' },
      { emoji: '🐷', name: 'pig face' },
      { emoji: '🐽', name: 'pig nose' },
      { emoji: '🐸', name: 'frog' },
      { emoji: '🐵', name: 'monkey face' },
      { emoji: '🙈', name: 'see-no-evil monkey' },
      { emoji: '🙉', name: 'hear-no-evil monkey' },
      { emoji: '🙊', name: 'speak-no-evil monkey' },
      { emoji: '🐒', name: 'monkey' },
      { emoji: '🐔', name: 'chicken' },
      { emoji: '🐧', name: 'penguin' },
      { emoji: '🐦', name: 'bird' },
      { emoji: '🐤', name: 'baby chick' },
      { emoji: '🐣', name: 'hatching chick' },
      { emoji: '🐥', name: 'front-facing baby chick' },
      { emoji: '🦆', name: 'duck' },
      { emoji: '🦅', name: 'eagle' },
      { emoji: '🦉', name: 'owl' },
      { emoji: '🦇', name: 'bat' },
    ],
  },
  {
    label: 'Food & Drink',
    emojis: [
      { emoji: '🍎', name: 'red apple' },
      { emoji: '🍏', name: 'green apple' },
      { emoji: '🍊', name: 'tangerine' },
      { emoji: '🍋', name: 'lemon' },
      { emoji: '🍌', name: 'banana' },
      { emoji: '🍉', name: 'watermelon' },
      { emoji: '🍇', name: 'grapes' },
      { emoji: '🍓', name: 'strawberry' },
      { emoji: '🫐', name: 'blueberries' },
      { emoji: '🍈', name: 'melon' },
      { emoji: '🍒', name: 'cherries' },
      { emoji: '🍑', name: 'peach' },
      { emoji: '🥭', name: 'mango' },
      { emoji: '🍍', name: 'pineapple' },
      { emoji: '🥥', name: 'coconut' },
      { emoji: '🥝', name: 'kiwi fruit' },
      { emoji: '🍅', name: 'tomato' },
      { emoji: '🍆', name: 'eggplant' },
      { emoji: '🥑', name: 'avocado' },
      { emoji: '🥦', name: 'broccoli' },
      { emoji: '🥬', name: 'leafy greens' },
      { emoji: '🥒', name: 'cucumber' },
      { emoji: '🌶️', name: 'hot pepper' },
      { emoji: '🫑', name: 'bell pepper' },
      { emoji: '🌽', name: 'ear of corn' },
      { emoji: '🥕', name: 'carrot' },
      { emoji: '🫒', name: 'olive' },
      { emoji: '🧄', name: 'garlic' },
      { emoji: '🧅', name: 'onion' },
      { emoji: '🥔', name: 'potato' },
    ],
  },
];

const emojiGroups: EmojiGroup[] = emojiCategories.map((category) => ({
  value: category.label,
  label: category.label,
  items: category.emojis.map((emoji) => ({
    ...emoji,
    value: emoji.name.toLowerCase(),
  })),
}));

가상화

@tanstack/react-virtual 같은 가상화 라이브러리로 대용량 데이터를 효율적으로 처리해요.

데모 (Demo)

Tailwind

이 예시는 Tailwind CSS를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { useVirtualizer } from '@tanstack/react-virtual';

export default function ExampleVirtualizedAutocomplete() {
  const virtualizerRef = React.useRef<Virtualizer | null>(null);

  return (
    <Autocomplete.Root
      virtualized
      items={virtualizedItems}
      openOnInputClick
      itemToStringValue={getItemLabel}
      onItemHighlighted={(item, { reason, index }) => {
        const virtualizer = virtualizerRef.current;

        if (!item || !virtualizer) {
          return;
        }

        const isStart = index === 0;
        const isEnd = index === virtualizer.options.count - 1;
        const shouldScroll = reason === 'none' || (reason === 'keyboard' && (isStart || isEnd));

        if (shouldScroll) {
          queueMicrotask(() => {
            virtualizer.scrollToIndex(index, { align: isEnd ? 'start' : 'end' });
          });
        }
      }}
    >
      <label className="flex flex-col gap-1 text-sm font-bold text-neutral-950 dark:text-white">
        Search 10,000 items
        <Autocomplete.Input className="h-8 w-[16rem] border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white" />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className="outline-hidden" sideOffset={4}>
          <Autocomplete.Popup className="w-[var(--anchor-width)] max-w-[var(--available-width)] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
            <Autocomplete.Empty>
              <div className="py-3 px-2 text-sm leading-4 text-neutral-500 dark:text-neutral-400">
                No items found.
              </div>
            </Autocomplete.Empty>
            <Autocomplete.List className="p-0">
              <VirtualizedList virtualizerRef={virtualizerRef} />
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

function VirtualizedList({
  virtualizerRef,
}: {
  virtualizerRef: React.RefObject<Virtualizer | null>;
}) {
  const filteredItems = Autocomplete.useFilteredItems<VirtualizedItem>();

  const scrollElementRef = React.useRef<HTMLDivElement | null>(null);

  const virtualizer = useVirtualizer({
    count: filteredItems.length,
    getScrollElement: () => scrollElementRef.current,
    estimateSize: () => 32,
    overscan: 20,
    paddingStart: 4,
    paddingEnd: 4,
    scrollPaddingEnd: 4,
    scrollPaddingStart: 4,
  });

  React.useImperativeHandle(virtualizerRef, () => virtualizer);

  const handleScrollElementRef = React.useCallback(
    (element: HTMLDivElement | null) => {
      scrollElementRef.current = element;
      if (element) {
        virtualizer.measure();
      }
    },
    [virtualizer],
  );

  const totalSize = virtualizer.getTotalSize();

  if (!filteredItems.length) {
    return null;
  }

  return (
    <div
      role="presentation"
      ref={handleScrollElementRef}
      className="h-[min(22.5rem,var(--total-size))] max-h-[var(--available-height)] overflow-auto overscroll-contain scroll-py-1"
      style={{ '--total-size': `${totalSize}px` } as React.CSSProperties}
    >
      <div role="presentation" className="relative w-full" style={{ height: totalSize }}>
        {virtualizer.getVirtualItems().map((virtualItem) => {
          const item = filteredItems[virtualItem.index];
          if (!item) {
            return null;
          }

          return (
            <Autocomplete.Item
              key={virtualItem.key}
              index={virtualItem.index}
              data-index={virtualItem.index}
              ref={virtualizer.measureElement}
              value={item}
              className="flex cursor-default py-2 pr-2 pl-2 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-0 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white"
              aria-setsize={filteredItems.length}
              aria-posinset={virtualItem.index + 1}
              style={{
                position: 'absolute',
                top: 0,
                left: 0,
                width: '100%',
                height: virtualItem.size,
                transform: `translateY(${virtualItem.start}px)`,
              }}
            >
              {item.name}
            </Autocomplete.Item>
          );
        })}
      </div>
    </div>
  );
}

interface VirtualizedItem {
  id: string;
  name: string;
}

function getItemLabel(item: VirtualizedItem | null) {
  return item ? item.name : '';
}

const virtualizedItems: VirtualizedItem[] = Array.from({ length: 10000 }, (_, index) => {
  const id = String(index + 1);
  const indexLabel = id.padStart(4, '0');
  return { id, name: `Item ${indexLabel}` };
});

type Virtualizer = ReturnType<typeof useVirtualizer<HTMLDivElement, Element>>;

CSS Modules

이 예시는 CSS Modules를 사용해 컴포넌트를 구현하는 방법을 보여줄게요.

/* index.module.css */
.Input {
  box-sizing: border-box;
  padding: 0 0.5rem;
  margin: 0;
  border-radius: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  width: 16rem;
  height: 2rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  outline: none;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &:focus {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Positioner {
  outline: 0;
}

.Popup {
  box-sizing: border-box;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  width: var(--anchor-width);
  max-width: var(--available-width);
  border: 1px solid oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
    color: white;
    border: 1px solid white;
    box-shadow: none;
  }
}

.Scroller {
  box-sizing: border-box;
  height: min(22.5rem, var(--total-size));
  max-height: var(--available-height);
  overflow: auto;
  overscroll-behavior: contain;
  scroll-padding-block: 0.25rem;
}

.VirtualizedPlaceholder {
  width: 100%;
  position: relative;
}

.List {
  padding: 0;
}

.Item {
  box-sizing: border-box;
  outline: 0;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  padding-block: 0.5rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  display: flex;
  font-size: 0.875rem;
  line-height: 1rem;

  &[data-highlighted] {
    z-index: 0;
    position: relative;
    color: white;

    @media (prefers-color-scheme: dark) {
      color: oklch(14.5% 0 0deg);
    }
  }

  &[data-highlighted]::before {
    content: '';
    z-index: -1;
    position: absolute;
    inset-block: 0;
    inset-inline: 0;
    background-color: oklch(14.5% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: white;
    }
  }
}

.Empty {
  box-sizing: border-box;
  font-size: 0.875rem;
  line-height: 1rem;
  color: oklch(55.6% 0 0deg);
  padding: 0.75rem 0.5rem;

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}
/* index.tsx */
'use client';
import * as React from 'react';
import { Autocomplete } from '@base-ui/react/autocomplete';
import { useVirtualizer } from '@tanstack/react-virtual';
import styles from './index.module.css';

export default function ExampleVirtualizedAutocomplete() {
  const virtualizerRef = React.useRef<Virtualizer | null>(null);

  return (
    <Autocomplete.Root
      virtualized
      items={virtualizedItems}
      openOnInputClick
      itemToStringValue={getItemLabel}
      onItemHighlighted={(item, { reason, index }) => {
        const virtualizer = virtualizerRef.current;

        if (!item || !virtualizer) {
          return;
        }

        const isStart = index === 0;
        const isEnd = index === virtualizer.options.count - 1;
        const shouldScroll = reason === 'none' || (reason === 'keyboard' && (isStart || isEnd));

        if (shouldScroll) {
          queueMicrotask(() => {
            virtualizer.scrollToIndex(index, { align: isEnd ? 'start' : 'end' });
          });
        }
      }}
    >
      <label className={styles.Label}>
        Search 10,000 items
        <Autocomplete.Input className={styles.Input} />
      </label>

      <Autocomplete.Portal>
        <Autocomplete.Positioner className={styles.Positioner} sideOffset={4}>
          <Autocomplete.Popup className={styles.Popup}>
            <Autocomplete.Empty>
              <div className={styles.Empty}>No items found.</div>
            </Autocomplete.Empty>
            <Autocomplete.List className={styles.List}>
              <VirtualizedList virtualizerRef={virtualizerRef} />
            </Autocomplete.List>
          </Autocomplete.Popup>
        </Autocomplete.Positioner>
      </Autocomplete.Portal>
    </Autocomplete.Root>
  );
}

function VirtualizedList({
  virtualizerRef,
}: {
  virtualizerRef: React.RefObject<Virtualizer | null>;
}) {
  const filteredItems = Autocomplete.useFilteredItems<VirtualizedItem>();

  const scrollElementRef = React.useRef<HTMLDivElement | null>(null);

  const virtualizer = useVirtualizer({
    count: filteredItems.length,
    getScrollElement: () => scrollElementRef.current,
    estimateSize: () => 32,
    overscan: 20,
    paddingStart: 4,
    paddingEnd: 4,
    scrollPaddingEnd: 4,
    scrollPaddingStart: 4,
  });

  React.useImperativeHandle(virtualizerRef, () => virtualizer);

  const handleScrollElementRef = React.useCallback(
    (element: HTMLDivElement | null) => {
      scrollElementRef.current = element;
      if (element) {
        virtualizer.measure();
      }
    },
    [virtualizer],
  );

  const totalSize = virtualizer.getTotalSize();

  if (!filteredItems.length) {
    return null;
  }

  return (
    <div
      role="presentation"
      ref={handleScrollElementRef}
      className={styles.Scroller}
      style={{ '--total-size': `${totalSize}px` } as React.CSSProperties}
    >
      <div
        role="presentation"
        className={styles.VirtualizedPlaceholder}
        style={{ height: totalSize }}
      >
        {virtualizer.getVirtualItems().map((virtualItem) => {
          const item = filteredItems[virtualItem.index];
          if (!item) {
            return null;
          }

          return (
            <Autocomplete.Item
              key={virtualItem.key}
              index={virtualItem.index}
              data-index={virtualItem.index}
              ref={virtualizer.measureElement}
              value={item}
              className={styles.Item}
              aria-setsize={filteredItems.length}
              aria-posinset={virtualItem.index + 1}
              style={{
                position: 'absolute',
                top: 0,
                left: 0,
                width: '100%',
                height: virtualItem.size,
                transform: `translateY(${virtualItem.start}px)`,
              }}
            >
              {item.name}
            </Autocomplete.Item>
          );
        })}
      </div>
    </div>
  );
}

interface VirtualizedItem {
  id: string;
  name: string;
}

function getItemLabel(item: VirtualizedItem | null) {
  return item ? item.name : '';
}

const virtualizedItems: VirtualizedItem[] = Array.from({ length: 10000 }, (_, index) => {
  const id = String(index + 1);
  const indexLabel = id.padStart(4, '0');
  return { id, name: `Item ${indexLabel}` };
});

type Virtualizer = ReturnType<typeof useVirtualizer<HTMLDivElement, Element>>;

항목 메모이제이션

약 1,000개까지의 데이터셋에서는 각 항목을 메모이제이션하는 것이 가상화보다 더 간단한 대안이에요. 각 항목을 React.memo로 감싸고 항목을 prop으로 전달하면 변경되지 않은 항목이 재렌더링을 건너뛰어요. 메모이제이션은 입력 속도를 높이지만 열기 속도는 높이지 못해요. 항목 수가 충분히 많아지면 마운트 비용이 지배적이므로, 저사양 기기에서 여는 상호작용을 빠르게 유지하려면 가상화가 필요해요.

interface Suggestion {
  id: string;
  label: string;
  description: string;
}

const SuggestionItem = React.memo(function SuggestionItem({ item }: { item: Suggestion }) {
  return (
    <Autocomplete.Item value={item}>
      <span>{item.label}</span>
      <span>{item.description}</span>
    </Autocomplete.Item>
  );
});

<Autocomplete.List>
  {(item: Suggestion) => <SuggestionItem key={item.id} item={item} />}
</Autocomplete.List>;

API 레퍼런스 (API reference)

Root

자동완성의 모든 부분을 묶어요. 자체 HTML 요소를 렌더링하지 않아요.

Root Props:

Prop Type Default Description
name string - Identifies the field when a form is submitted.
defaultValue string | number | string[] - The uncontrolled input value of the autocomplete when it's initially rendered. To render a controlled autocomplete, use the value prop instead.
value string | string[] | number - The input value of the autocomplete. Use when controlled.
onValueChange ((value: string, eventDetails: Autocomplete.Root.ChangeEventDetails) => void) - Event handler called when the input value of the autocomplete changes.
defaultOpen boolean false Whether the popup is initially open. To render a controlled popup, use the open prop instead.
open boolean - Whether the popup is currently open. Use when controlled.
onOpenChange ((open: boolean, eventDetails: Autocomplete.Root.ChangeEventDetails) => void) - Event handler called when the popup is opened or closed.
autoHighlight boolean | 'always' false Whether the first matching item is highlighted automatically. true: highlight after the user types and keep the highlight while the query changes.'always': always highlight the first item.
keepHighlight boolean false Whether the highlighted item should be preserved when the pointer leaves the list.
highlightItemOnHover boolean true Whether moving the pointer over items should highlight them. Disabling this prop allows CSS :hover to be differentiated from the :focus (data-highlighted) state.
actionsRef React.RefObject<Autocomplete.Root.Actions | null> - A ref to imperative actions. unmount: Manually unmounts the autocomplete. Call this after any externally controlled closing animation finishes.
filter ((item: ItemValue, query: string, itemToString?: ((item: ItemValue) => string)) => boolean) | null - AutocompleteFilter function used to match items against the input query.
filteredItems any[] | Group<any>[] | ItemValue[] | Group<ItemValue>[] - Filtered items to display in the list. When provided, the list uses these items instead of filtering the items prop internally. When items is also provided, this array must preserve its flat or grouped structure. Nullish entries are not supported, as in items. Use when you want to control filtering logic externally with the useFilter() hook.
form string - Identifies the form that owns the internal input. Useful when the autocomplete is rendered outside the form.
grid boolean false Whether list items are presented in a grid layout. When enabled, arrow keys navigate across rows and columns inferred from DOM rows.
inline boolean false Whether the list is rendered inline without using the component's own popup. Specify open unconditionally in conjunction with this prop so the list is considered visible: <Autocomplete.Root inline open>
itemToStringValue ((itemValue: ItemValue) => string) - When the item values are objects (<Autocomplete.Item value={object}>), this function converts the object value to a string representation for both display in the input and form submission. If the shape of the object is { value, label }, the label will be used automatically without needing to specify this prop.
items ({ items: any[] })[] | ItemValue[] - The items to be displayed in the list. Can be either a flat array of items or an array of groups with items. Nullish entries are not supported: remove them from the data before passing it.
limit number -1 The maximum number of items to display in the list.
locale Intl.LocalesArgument - The locale to use for string comparison. Defaults to the user's runtime locale.
loopFocus boolean true Whether to loop keyboard focus back to the input when the end of the list is reached while using the arrow keys. The first item can then be reached by pressing ArrowDown again from the input, or the last item can be reached by pressing ArrowUp from the input. The input is always included in the focus loop per ARIA Authoring Practices. When disabled, focus does not move when on the last element and the user presses ArrowDown, or when on the first element and the user presses ArrowUp.
modal boolean false Determines if the popup enters a modal state when open. true: user interaction is limited to the popup: document page scroll is locked and pointer interactions on outside elements are disabled.false: user interaction with the rest of the document is allowed. On touch devices, a true modal blocks outside taps but leaves the page scrollable unless the popup spans nearly the full viewport width, matching native iOS behavior.
mode 'list' | 'both' | 'inline' | 'none' 'list' Controls how the autocomplete behaves with respect to list filtering and inline autocompletion. list (default): items are dynamically filtered based on the input value. The input value does not change based on the active item.both: items are dynamically filtered based on the input value, which will temporarily change based on the active item (inline autocompletion).inline: items are static (not filtered), and the input value will temporarily change based on the active item (inline autocompletion).none: items are static (not filtered), and the input value will not change based on the active item.
onItemHighlighted ((highlightedValue: ItemValue | undefined, eventDetails: Autocomplete.Root.HighlightEventDetails) => void) - Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item value (or undefined if no item is highlighted) and event details with a reason property describing why the highlight changed. The reason can be: 'keyboard': the highlight changed due to keyboard navigation.'pointer': the highlight changed due to pointer hovering.'none': the highlight changed programmatically.
onOpenChangeComplete ((open: boolean) => void) - Event handler called after any animations complete when the popup is opened or closed.
openOnInputClick boolean false Whether the popup opens when clicking the input.
submitOnItemClick boolean false Whether clicking an item should submit the autocomplete's owning form. By default, clicking an item via a pointer or Enter key does not submit the owning form. Useful when the autocomplete is used as a single-field form search input.
virtualized boolean false Whether the items are being externally virtualized.
disabled boolean false Whether the component should ignore user interaction.
readOnly boolean false Whether the user should be unable to choose a different option from the popup.
required boolean false Whether the user must choose a value before submitting a form.
inputRef React.Ref<HTMLInputElement> - A ref to the hidden input element.
id string - The id of the component.
children React.ReactNode - -

Root.Props

Re-export of Root props.

Root.State

type AutocompleteRootState = {};

Root.Actions

type AutocompleteRootActions = { unmount: () => void };

Root.ChangeEventReason

type AutocompleteRootChangeEventReason =
  | 'trigger-press'
  | 'input-press'
  | 'outside-press'
  | 'item-press'
  | 'close-press'
  | 'escape-key'
  | 'list-navigation'
  | 'focus-out'
  | 'input-change'
  | 'input-clear'
  | 'clear-press'
  | 'chip-remove-press'
  | 'cancel-open'
  | 'none';

Root.ChangeEventDetails

type AutocompleteRootChangeEventDetails = (
  | { reason: 'trigger-press'; event: MouseEvent | PointerEvent | TouchEvent | KeyboardEvent }
  | { reason: 'input-press'; event: MouseEvent | PointerEvent | TouchEvent | KeyboardEvent }
  | { reason: 'outside-press'; event: MouseEvent | PointerEvent | TouchEvent }
  | { reason: 'item-press'; event: MouseEvent | PointerEvent | KeyboardEvent }
  | { reason: 'close-press'; event: MouseEvent | PointerEvent | KeyboardEvent }
  | { reason: 'escape-key'; event: KeyboardEvent }
  | { reason: 'list-navigation'; event: KeyboardEvent }
  | { reason: 'focus-out'; event: KeyboardEvent | FocusEvent }
  | { reason: 'input-change'; event: Event | InputEvent }
  | { reason: 'input-clear'; event: Event | FocusEvent | InputEvent }
  | { reason: 'clear-press'; event: MouseEvent | PointerEvent | KeyboardEvent }
  | { reason: 'chip-remove-press'; event: MouseEvent | PointerEvent | KeyboardEvent }
  | { reason: 'cancel-open'; event: MouseEvent }
  | { reason: 'none'; event: Event }
) & {
  /** Cancels Base UI from handling the event. */
  cancel: () => void;
  /** Allows the event to propagate in cases where Base UI will stop the propagation. */
  allowPropagation: () => void;
  /** Indicates whether the event has been canceled. */
  isCanceled: boolean;
  /** Indicates whether the event is allowed to propagate. */
  isPropagationAllowed: boolean;
  /** The element that triggered the event, if applicable. */
  trigger: Element | undefined;
};

Root.HighlightEventReason

type AutocompleteRootHighlightEventReason = 'keyboard' | 'pointer' | 'none';

Root.HighlightEventDetails

type AutocompleteRootHighlightEventDetails =
  | { reason: 'none'; event: Event; index: number }
  | { reason: 'keyboard'; event: KeyboardEvent; index: number }
  | { reason: 'pointer'; event: PointerEvent; index: number };

Trigger

팝업을 여는 버튼이에요. <button> 요소로 렌더링해요.

Trigger Props:

Prop Type Default Description
nativeButton boolean true Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
disabled boolean false Whether the component should ignore user interaction.
className string | ((state: Autocomplete.Trigger.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Trigger.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Trigger.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Trigger Data Attributes:

Attribute Type Description
data-popup-open - Present when the corresponding popup is open.
data-popup-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | null Indicates which side the corresponding popup is positioned relative to its anchor.
data-list-empty - Present when the corresponding items list is empty.
data-pressed - Present when the trigger is pressed.
data-disabled - Present when the component is disabled.
data-readonly - Present when the component is readonly.
data-required - Present when the component is required.
data-valid - Present when the component is in a valid state (when wrapped in Field.Root).
data-invalid - Present when the component is in an invalid state (when wrapped in Field.Root).
data-dirty - Present when the component's value has changed (when wrapped in Field.Root).
data-touched - Present when the component has been touched (when wrapped in Field.Root).
data-filled - Present when the component has a value (when wrapped in Field.Root).
data-focused - Present when the trigger is focused (when wrapped in Field.Root).

Trigger.Props

Re-export of Trigger props.

Trigger.State

type AutocompleteTriggerState = {
  /** Whether the popup is open. */
  open: boolean;
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
  /** Indicates which side the corresponding popup is positioned relative to its anchor. */
  popupSide: Side | null;
  /** Present when the corresponding items list is empty. */
  listEmpty: boolean;
  /** Whether the component should ignore user edits. */
  readOnly: boolean;
  /** Whether the field has been touched. */
  touched: boolean;
  /** Whether the field value has changed from its initial value. */
  dirty: boolean;
  /** Whether the field is valid. */
  valid: boolean | null;
  /** Whether the field has a value. */
  filled: boolean;
  /** Whether the field is focused. */
  focused: boolean;
};

Value

자동완성의 현재 값이에요. 자체 HTML 요소를 렌더링하지 않아요.

Value Props:

Prop Type Default Description
children React.ReactNode | ((value: string) => React.ReactNode) - -

Value.Props

Re-export of Value props.

Value.State

type AutocompleteValueState = {};

Input

목록에서 항목을 검색하는 텍스트 입력이에요. <input> 요소로 렌더링해요.

Input Props:

Prop Type Default Description
disabled boolean false Whether the component should ignore user interaction.
className string | ((state: Autocomplete.Input.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Input.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Input.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Input Data Attributes:

Attribute Type Description
data-popup-open - Present when the corresponding popup is open.
data-popup-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | null Indicates which side the corresponding popup is positioned relative to its anchor.
data-list-empty - Present when the corresponding items list is empty.
data-pressed - Present when the input is pressed.
data-disabled - Present when the component is disabled.
data-readonly - Present when the component is readonly.
data-required - Present when the component is required.
data-valid - Present when the component is in a valid state (when wrapped in Field.Root).
data-invalid - Present when the component is in an invalid state (when wrapped in Field.Root).
data-dirty - Present when the component's value has changed (when wrapped in Field.Root).
data-touched - Present when the component has been touched (when wrapped in Field.Root).
data-filled - Present when the component has a value (when wrapped in Field.Root).
data-focused - Present when the input is focused (when wrapped in Field.Root).

Input.Props

Re-export of Input props.

Input.State

type AutocompleteInputState = {
  /** Whether the corresponding popup is open. */
  open: boolean;
  /** Indicates which side the corresponding popup is positioned relative to its anchor. */
  popupSide: Side | null;
  /** Present when the corresponding items list is empty. */
  listEmpty: boolean;
  /** Whether the component should ignore user edits. */
  readOnly: boolean;
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
  /** Whether the field has been touched. */
  touched: boolean;
  /** Whether the field value has changed from its initial value. */
  dirty: boolean;
  /** Whether the field is valid. */
  valid: boolean | null;
  /** Whether the field has a value. */
  filled: boolean;
  /** Whether the field is focused. */
  focused: boolean;
};

Icon

트리거 버튼이 팝업을 연다는 것을 나타내는 아이콘이에요. <span> 요소로 렌더링해요.

Icon Props:

Prop Type Default Description
className string | ((state: Autocomplete.Icon.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Icon.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Icon.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Icon.Props

Re-export of Icon props.

Icon.State

type AutocompleteIconState = {};

Clear

클릭하면 값을 지워요. <button> 요소로 렌더링해요.

Clear Props:

Prop Type Default Description
nativeButton boolean true Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
disabled boolean false Whether the component should ignore user interaction.
className string | ((state: Autocomplete.Clear.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Clear.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
keepMounted boolean false Whether the component should remain mounted in the DOM when not visible.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Clear.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Clear Data Attributes:

Attribute Type Description
data-popup-open - Present when the corresponding popup is open.
data-disabled - Present when the button is disabled.
data-visible - Present when the clear button is visible.
data-starting-style - Present when the button begins animating in.
data-ending-style - Present when the button is animating out.

Clear.Props

Re-export of Clear props.

Clear.State

type AutocompleteClearState = {
  /** Whether the popup is open. */
  open: boolean;
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
  /** Whether the clear button should be visible. */
  visible: boolean;
  /** The transition status of the component. */
  transitionStatus: TransitionStatus;
};

List

항목을 담는 목록 컨테이너예요. <div> 요소로 렌더링해요.

List Props:

Prop Type Default Description
children React.ReactNode | ((item: any, index: number) => React.ReactNode) - -
className string | ((state: Autocomplete.List.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.List.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.List.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

List.Props

Re-export of List props.

List.State

type AutocompleteListState = {
  /** Whether the list is empty. */
  empty: boolean;
};

Portal

팝업을 DOM의 다른 부분으로 옮기는 포털 요소예요. 기본적으로 포털 요소는 <body>에 추가돼요. <div> 요소로 렌더링해요.

Portal Props:

Prop Type Default Description
container HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null - A parent element to render the portal element into.
className string | ((state: Autocomplete.Portal.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Portal.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
keepMounted boolean false Whether to keep the portal mounted in the DOM while the popup is hidden.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Portal.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Portal.Props

Re-export of Portal props.

Portal.State

type AutocompletePortalState = {};

Backdrop

팝업 아래에 표시되는 오버레이예요. <div> 요소로 렌더링해요.

Backdrop Props:

Prop Type Default Description
className string | ((state: Autocomplete.Backdrop.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Backdrop.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Backdrop.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Backdrop Data Attributes:

Attribute Type Description
data-open - Present when the popup is open.
data-closed - Present when the popup is closed.
data-starting-style - Present when the popup begins animating in.
data-ending-style - Present when the popup is animating out.

Backdrop.Props

Re-export of Backdrop props.

Backdrop.State

type AutocompleteBackdropState = {
  /** Whether the popup is currently open. */
  open: boolean;
  /** The transition status of the component. */
  transitionStatus: TransitionStatus;
};

Positioner

트리거를 기준으로 팝업을 배치해요. <div> 요소로 렌더링해요.

Positioner Props:

Prop Type Default Description
disableAnchorTracking boolean false Whether to disable the popup from tracking any layout shift of its positioning anchor.
align Align 'center' How to align the popup relative to the specified side.
alignOffset number | OffsetFunction 0 Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment. The function takes a data object parameter with the following properties: data.anchor: the dimensions of the anchor element with properties width and height.data.positioner: the dimensions of the positioner element with properties width and height.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side.
side Side 'bottom' Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
sideOffset number | OffsetFunction 0 Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment. The function takes a data object parameter with the following properties: data.anchor: the dimensions of the anchor element with properties width and height.data.positioner: the dimensions of the positioner element with properties width and height.data.side: which side of the anchor element the positioner is aligned against.data.align: how the positioner is aligned relative to the specified side.
arrowPadding number 5 Minimum distance to maintain between the arrow and the edges of the popup. Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
anchor Element | VirtualElement | React.RefObject<Element | null> | (() => Element | VirtualElement | null) | null - An element to position the popup against. By default, the popup will be positioned against the trigger.
collisionAvoidance CollisionAvoidance - Determines how to handle collisions when positioning the popup. side controls overflow on the preferred placement axis (top/bottom or left/right): 'flip': keep the requested side when it fits; otherwise try the opposite side (top and bottom, or left and right).'shift': never change side; keep the requested side and move the popup within the clipping boundary so it stays visible.'none': do not correct side-axis overflow. align controls overflow on the alignment axis (start/center/end): 'flip': keep side, but swap start and end when the requested alignment overflows.'shift': keep side and requested alignment, then nudge the popup along the alignment axis to fit.'none': do not correct alignment-axis overflow. fallbackAxisSide controls fallback behavior on the perpendicular axis when the preferred axis cannot fit: 'start': allow perpendicular fallback and try the logical start side first (top before bottom, or left before right in LTR).'end': allow perpendicular fallback and try the logical end side first (bottom before top, or right before left in LTR).'none': do not fallback to the perpendicular axis. When side is 'shift', explicitly setting align only supports 'shift' or 'none'. If align is omitted, it defaults to 'flip'.
collisionBoundary Boundary 'clipping-ancestors' An element or a rectangle that delimits the area that the popup is confined to.
collisionPadding Padding 5 Additional space to maintain from the edge of the collision boundary.
sticky boolean false Whether to maintain the popup in the viewport after the anchor element was scrolled out of view.
positionMethod 'absolute' | 'fixed' 'absolute' Determines which CSS position property to use.
className string | ((state: Autocomplete.Positioner.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Positioner.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Positioner.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

alignOffset Prop Example:

<Positioner
  alignOffset={({ side, align, anchor, positioner }) => {
    return side === 'top' || side === 'bottom' ? anchor.width : anchor.height;
  }}
/>

sideOffset Prop Example:

<Positioner
  sideOffset={({ side, align, anchor, positioner }) => {
    return side === 'top' || side === 'bottom' ? anchor.height : anchor.width;
  }}
/>

collisionAvoidance Prop Example:

<Positioner
  collisionAvoidance={{
    side: 'shift',
    align: 'shift',
    fallbackAxisSide: 'none',
  }}
/>

Positioner Data Attributes:

Attribute Type Description
data-open - Present when the popup is open.
data-closed - Present when the popup is closed.
data-anchor-hidden - Present when the anchor is hidden.
data-align 'start' | 'center' | 'end' Indicates how the popup is aligned relative to specified side.
data-empty - Present when the items list is empty.
data-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' Indicates which side the popup is positioned relative to the trigger.

Positioner CSS Variables:

Variable Type Description
--anchor-height number The anchor's height.
--anchor-width number The anchor's width.
--available-height number The available height between the trigger and the edge of the viewport.
--available-width number The available width between the trigger and the edge of the viewport.
--transform-origin string The coordinates that this element is anchored to. Used for animations and transitions.

Positioner.Props

Re-export of Positioner props.

Positioner.State

type AutocompletePositionerState = {
  /** Whether the popup is currently open. */
  open: boolean;
  /** The side of the anchor the component is placed on. */
  side: Side;
  /** The alignment of the component relative to the anchor. */
  align: Align;
  /** Whether the anchor element is hidden. */
  anchorHidden: boolean;
  /** Whether there are no items to display. */
  empty: boolean;
};

목록을 담는 컨테이너예요. <div> 요소로 렌더링해요.

Popup Props:

Prop Type Default Description
initialFocus boolean | React.RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | void | HTMLElement | null) - Determines the element to focus when the popup is opened. false: Do not move focus.true: Move focus based on the default behavior (first tabbable element or popup).RefObject: Move focus to the ref element.function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, or false/undefined to do nothing.
finalFocus boolean | React.RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null) - Determines the element to focus when the popup is closed. false: Do not move focus.true: Move focus based on the default behavior (trigger or previously focused element).RefObject: Move focus to the ref element.function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, or false/undefined to do nothing.
className string | ((state: Autocomplete.Popup.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Popup.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Popup.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Popup Data Attributes:

Attribute Type Description
data-open - Present when the popup is open.
data-closed - Present when the popup is closed.
data-anchor-hidden - Present when the anchor is hidden.
data-align 'start' | 'center' | 'end' Indicates how the popup is aligned relative to specified side.
data-empty - Present when the items list is empty.
data-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' Indicates which side the popup is positioned relative to the trigger.
data-starting-style - Present when the popup begins animating in.
data-ending-style - Present when the popup is animating out.

Popup.Props

Re-export of Popup props.

Popup.State

type AutocompletePopupState = {
  /** Whether the component is open. */
  open: boolean;
  /** The side of the anchor the component is placed on. */
  side: Side;
  /** The alignment of the component relative to the anchor. */
  align: Align;
  /** Whether the anchor element is hidden. */
  anchorHidden: boolean;
  /** The transition status of the component. */
  transitionStatus: TransitionStatus;
  /** Whether there are no items to display. */
  empty: boolean;
};

Arrow

앵커를 기준으로 배치된 요소를 표시해요. <div> 요소로 렌더링해요.

Arrow Props:

Prop Type Default Description
className string | ((state: Autocomplete.Arrow.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Arrow.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Arrow.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Arrow Data Attributes:

Attribute Type Description
data-open - Present when the popup is open.
data-closed - Present when the popup is closed.
data-uncentered - Present when the arrow is uncentered.
data-align 'start' | 'center' | 'end' Indicates how the popup is aligned relative to specified side.
data-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' Indicates which side the popup is positioned relative to the trigger.

Arrow.Props

Re-export of Arrow props.

Arrow.State

type AutocompleteArrowState = {
  /** Whether the popup is currently open. */
  open: boolean;
  /** The side of the anchor the component is placed on. */
  side: Side;
  /** The alignment of the component relative to the anchor. */
  align: Align;
  /** Whether the arrow cannot be centered on the anchor. */
  uncentered: boolean;
};

Item

목록에 있는 개별 항목이에요. <div> 요소로 렌더링해요.

Item Props:

Prop Type Default Description
value any null A unique value that identifies this item.
onClick ((event: BaseUIEvent<React.MouseEvent<HTMLDivElement, MouseEvent>>) => void) - An optional click handler for the item when selected. It fires when clicking the item with the pointer, as well as when pressing Enter with the keyboard if the item is highlighted when the Input or List element has focus.
index number - The index of the item in the list. Improves performance when specified by avoiding the need to calculate the index automatically from the DOM.
nativeButton boolean false Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
disabled boolean false Whether the component should ignore user interaction.
children React.ReactNode - -
className string | ((state: Autocomplete.Item.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Item.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Item.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Item Data Attributes:

Attribute Type Description
data-highlighted - Present when the item is highlighted.
data-disabled - Present when the item is disabled.

Item.Props

Re-export of Item props.

Item.State

type AutocompleteItemState = {
  /** Whether the item should ignore user interaction. */
  disabled: boolean;
  /** Whether the item is highlighted. */
  highlighted: boolean;
};

Group

관련 항목을 해당 라벨과 함께 묶어요. <div> 요소로 렌더링해요.

Group Props:

Prop Type Default Description
items any[] - Items to be rendered within this group. When provided, child Collection components will use these items.
className string | ((state: Autocomplete.Group.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Group.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Group.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Group Data Attributes:

Attribute Type Description
data-popup-open - Present when the corresponding popup is open.
data-popup-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | null Indicates which side the corresponding popup is positioned relative to its anchor.
data-list-empty - Present when the corresponding items list is empty.
data-pressed - Present when the input group is pressed.
data-disabled - Present when the component is disabled.
data-readonly - Present when the component is readonly.
data-valid - Present when the component is in a valid state (when wrapped in Field.Root).
data-invalid - Present when the component is in an invalid state (when wrapped in Field.Root).
data-dirty - Present when the component's value has changed (when wrapped in Field.Root).
data-touched - Present when the component has been touched (when wrapped in Field.Root).
data-filled - Present when the component has a value (when wrapped in Field.Root).
data-focused - Present when the component is focused (when wrapped in Field.Root).

Group.Props

Re-export of Group props.

Group.State

type AutocompleteGroupState = {};

GroupLabel

부모 그룹에 자동으로 연결되는 접근 가능한 라벨이에요. <div> 요소로 렌더링해요.

GroupLabel Props:

Prop Type Default Description
className string | ((state: Autocomplete.GroupLabel.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.GroupLabel.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.GroupLabel.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

GroupLabel.Props

Re-export of GroupLabel props.

GroupLabel.State

type AutocompleteGroupLabelState = {};

Separator

항목 또는 그룹 사이의 시각적 구분선이에요. <div> 요소로 렌더링해요.

Separator Props:

Prop Type Default Description
orientation Orientation 'horizontal' The orientation of the separator.
className string | ((state: Autocomplete.Separator.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Separator.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Separator.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Separator.Props

Re-export of Separator props.

Separator.State

type AutocompleteSeparatorState = {
  /** The orientation of the separator. */
  orientation: Orientation;
};

Status

내용 변경이 스크린 리더에 정중하게 알려지는 상태 메시지를 표시해요. 비동기로 로드된 목록의 상태를 전달하는 데 유용해요. 이 컴포넌트의 루트 요소는 변경 사항을 스크린 리더에 일관되게 전달하기 위해 DOM에 마운트된 상태를 유지해야 해요. display: none, hidden, aria-hidden, 조건부 렌더링 등으로 컴포넌트 자체를 숨기거나 제거하지 마세요. 대신 자식을 업데이트하거나 조건부로 렌더링하세요. <div> 요소로 렌더링해요.

Status Props:

Prop Type Default Description
className string | ((state: Autocomplete.Status.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Status.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Status.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Status.Props

Re-export of Status props.

Status.State

type AutocompleteStatusState = {};

Empty

목록이 비어 있을 때만 자식을 렌더링해요. 루트 컴포넌트의 items prop이 필요해요. 변경 사항을 스크린 리더에 정중하게 알려요. 이 컴포넌트의 루트 요소는 변경 사항을 스크린 리더에 일관되게 전달하기 위해 DOM에 마운트된 상태를 유지해야 해요. display: none, hidden, aria-hidden, 조건부 렌더링 등으로 컴포넌트 자체를 숨기거나 제거하지 마세요. 대신 자식을 업데이트하거나 조건부로 렌더링하세요. <div> 요소로 렌더링해요.

Empty Props:

Prop Type Default Description
className string | ((state: Autocomplete.Empty.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Empty.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Empty.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Empty.Props

Re-export of Empty props.

Empty.State

type AutocompleteEmptyState = {};

Collection

필터링된 목록 항목을 렌더링해요. 자체 HTML 요소를 렌더링하지 않아요.

평평한 목록을 렌더링한다면 대신 List 컴포넌트에 함수 자식을 전달하세요. 그러면 암시적으로 감싸줘요.

Collection Props:

Prop Type Default Description
children* ((item: any, index: number) => React.ReactNode) - -

Collection.Props

Re-export of Collection props.

Collection.State

type AutocompleteCollectionState = {};

Row

그리드 목록에서 항목의 단일 행을 표시해요. 루트 컴포넌트에서 grid를 활성화하면 listbox를 그리드로 바꿔요. <div> 요소로 렌더링해요.

Row Props:

Prop Type Default Description
className string | ((state: Autocomplete.Row.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.Row.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.Row.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

Row.Props

Re-export of Row props.

Row.State

type AutocompleteRowState = {};

InputGroup

입력과 관련 컨트롤을 감싸는 래퍼예요. <div> 요소로 렌더링해요.

InputGroup Props:

Prop Type Default Description
className string | ((state: Autocomplete.InputGroup.State) => string | undefined) - CSS class applied to the element, or a function that returns a class based on the component's state.
style React.CSSProperties | ((state: Autocomplete.InputGroup.State) => React.CSSProperties | undefined) - Style applied to the element, or a function that returns a style object based on the component's state.
render ReactElement | ((props: HTMLProps, state: Autocomplete.InputGroup.State) => ReactElement) - Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.

InputGroup Data Attributes:

Attribute Type Description
data-popup-open - Present when the corresponding popup is open.
data-popup-side 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start' | null Indicates which side the corresponding popup is positioned relative to its anchor.
data-list-empty - Present when the corresponding items list is empty.
data-pressed - Present when the input group is pressed.
data-disabled - Present when the component is disabled.
data-readonly - Present when the component is readonly.
data-valid - Present when the component is in a valid state (when wrapped in Field.Root).
data-invalid - Present when the component is in an invalid state (when wrapped in Field.Root).
data-dirty - Present when the component's value has changed (when wrapped in Field.Root).
data-touched - Present when the component has been touched (when wrapped in Field.Root).
data-filled - Present when the component has a value (when wrapped in Field.Root).
data-focused - Present when the component is focused (when wrapped in Field.Root).

InputGroup.Props

Re-export of InputGroup props.

InputGroup.State

type AutocompleteInputGroupState = {
  /** Whether the corresponding popup is open. */
  open: boolean;
  /** Whether the component should ignore user interaction. */
  disabled: boolean;
  /** Whether the component should ignore user edits. */
  readOnly: boolean;
  /** Indicates which side the corresponding popup is positioned relative to its anchor. */
  popupSide: Side | null;
  /** Present when the corresponding items list is empty. */
  listEmpty: boolean;
  /** Whether the field has been touched. */
  touched: boolean;
  /** Whether the field value has changed from its initial value. */
  dirty: boolean;
  /** Whether the field is valid. */
  valid: boolean | null;
  /** Whether the field has a value. */
  filled: boolean;
  /** Whether the field is focused. */
  focused: boolean;
};

useFilter

Intl.Collator로 항목을 쿼리와 대조해 견고한 문자열 매칭을 수행해요.

Parameters:

Parameter Type Default Description
options? AutocompleteFilterOptions {} -

Return Value:

type ReturnValue = AutocompleteFilter;

useFilteredItems

내부적으로 필터링된 항목을 반환해요. 결과를 읽기 전용으로 취급해요. 내부 상태이며 공유된 동결(frozen) 배열일 수 있어요.

Return Value:

type ReturnValue = T[];

추가 타입

AutocompleteFilter

type AutocompleteFilter = {
  /** Returns whether the item matches the query anywhere. */
  contains: <Item>(item: Item, query: string, itemToString?: (item: Item) => string) => boolean;
  /** Returns whether the item starts with the query. */
  startsWith: <Item>(item: Item, query: string, itemToString?: (item: Item) => string) => boolean;
  /** Returns whether the item ends with the query. */
  endsWith: <Item>(item: Item, query: string, itemToString?: (item: Item) => string) => boolean;
};

AutocompleteFilterOptions

type AutocompleteFilterOptions = {
  /**
   * The locale to use for string comparison.
   * Defaults to the user's runtime locale.
   */
  locale?: Intl.LocalesArgument;
};

외부 타입

Side

type Side = 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start';

Align

type Align = 'start' | 'center' | 'end';

OffsetFunction

type OffsetFunction = (data: {
  side: 'top' | 'bottom' | 'left' | 'right' | 'inline-end' | 'inline-start';
  align: 'start' | 'center' | 'end';
  anchor: { width: number; height: number };
  positioner: { width: number; height: number };
}) => number;

InteractionType

type InteractionType = 'mouse' | 'touch' | 'pen' | 'keyboard' | '';

Orientation

type Orientation = 'horizontal' | 'vertical';

내보내기 그룹

  • Autocomplete.Root: Autocomplete.Root, Autocomplete.Root.Props, Autocomplete.Root.State, Autocomplete.Root.Actions, Autocomplete.Root.ChangeEventReason, Autocomplete.Root.ChangeEventDetails, Autocomplete.Root.HighlightEventReason, Autocomplete.Root.HighlightEventDetails
  • Autocomplete.Value: Autocomplete.Value, Autocomplete.Value.State, Autocomplete.Value.Props
  • Autocomplete.Trigger: Autocomplete.Trigger, Autocomplete.Trigger.State, Autocomplete.Trigger.Props
  • Autocomplete.Input: Autocomplete.Input, Autocomplete.Input.State, Autocomplete.Input.Props
  • Autocomplete.InputGroup: Autocomplete.InputGroup, Autocomplete.InputGroup.State, Autocomplete.InputGroup.Props
  • Autocomplete.Icon: Autocomplete.Icon, Autocomplete.Icon.State, Autocomplete.Icon.Props
  • Autocomplete.Clear: Autocomplete.Clear, Autocomplete.Clear.State, Autocomplete.Clear.Props
  • Autocomplete.List: Autocomplete.List, Autocomplete.List.State, Autocomplete.List.Props
  • Autocomplete.Status: Autocomplete.Status, Autocomplete.Status.State, Autocomplete.Status.Props
  • Autocomplete.Portal: Autocomplete.Portal, Autocomplete.Portal.State, Autocomplete.Portal.Props
  • Autocomplete.Backdrop: Autocomplete.Backdrop, Autocomplete.Backdrop.Props, Autocomplete.Backdrop.State
  • Autocomplete.Positioner: Autocomplete.Positioner, Autocomplete.Positioner.State, Autocomplete.Positioner.Props
  • Autocomplete.Popup: Autocomplete.Popup, Autocomplete.Popup.State, Autocomplete.Popup.Props
  • Autocomplete.Arrow: Autocomplete.Arrow, Autocomplete.Arrow.State, Autocomplete.Arrow.Props
  • Autocomplete.Group: Autocomplete.Group, Autocomplete.Group.State, Autocomplete.Group.Props
  • Autocomplete.GroupLabel: Autocomplete.GroupLabel, Autocomplete.GroupLabel.State, Autocomplete.GroupLabel.Props
  • Autocomplete.Item: Autocomplete.Item, Autocomplete.Item.State, Autocomplete.Item.Props
  • Autocomplete.Row: Autocomplete.Row, Autocomplete.Row.State, Autocomplete.Row.Props
  • Autocomplete.Collection: Autocomplete.Collection, Autocomplete.Collection.State, Autocomplete.Collection.Props
  • Autocomplete.Empty: Autocomplete.Empty, Autocomplete.Empty.State, Autocomplete.Empty.Props
  • Autocomplete.Separator: Autocomplete.Separator, Autocomplete.Separator.Props, Autocomplete.Separator.State
  • Autocomplete.useFilter
  • Autocomplete.useFilteredItems
  • Default: AutocompleteSeparatorProps, AutocompleteSeparatorState, AutocompleteInputProps, AutocompleteInputState, AutocompleteIconProps, AutocompleteIconState, AutocompleteClearProps, AutocompleteClearState, AutocompletePopupProps, AutocompletePopupState, AutocompletePositionerProps, AutocompletePositionerState, AutocompleteListProps, AutocompleteListState, AutocompleteRowProps, AutocompleteRowState, AutocompleteArrowProps, AutocompleteArrowState, AutocompleteBackdropProps, AutocompleteBackdropState, AutocompletePortalProps, AutocompletePortalState, AutocompleteGroupProps, AutocompleteGroupState, AutocompleteGroupLabelProps, AutocompleteGroupLabelState, AutocompleteEmptyProps, AutocompleteEmptyState, AutocompleteStatusProps, AutocompleteStatusState, AutocompleteCollectionState, AutocompleteCollectionProps, AutocompleteFilter, AutocompleteFilterOptions, AutocompleteRootState, AutocompleteRootActions, AutocompleteRootChangeEventReason, AutocompleteRootChangeEventDetails, AutocompleteRootHighlightEventReason, AutocompleteRootHighlightEventDetails, AutocompleteRootProps, AutocompleteTriggerState, AutocompleteTriggerProps, AutocompleteInputGroupState, AutocompleteInputGroupProps, AutocompleteItemState, AutocompleteItemProps, AutocompleteValueState, AutocompleteValueProps

Canonical 타입

Canonical: Alias 매핑 — 네임스페이스가 이미 import되어 있으면 Canonical을, 그렇지 않으면 Alias를 사용하세요.

  • Autocomplete.Root.Props: AutocompleteRootProps
  • Autocomplete.Root.State: AutocompleteRootState
  • Autocomplete.Root.Actions: AutocompleteRootActions
  • Autocomplete.Root.ChangeEventReason: AutocompleteRootChangeEventReason
  • Autocomplete.Root.ChangeEventDetails: AutocompleteRootChangeEventDetails
  • Autocomplete.Root.HighlightEventReason: AutocompleteRootHighlightEventReason
  • Autocomplete.Root.HighlightEventDetails: AutocompleteRootHighlightEventDetails
  • Autocomplete.Value.State: AutocompleteValueState
  • Autocomplete.Value.Props: AutocompleteValueProps
  • Autocomplete.Trigger.State: AutocompleteTriggerState
  • Autocomplete.Trigger.Props: AutocompleteTriggerProps
  • Autocomplete.Input.State: AutocompleteInputState
  • Autocomplete.Input.Props: AutocompleteInputProps
  • Autocomplete.InputGroup.State: AutocompleteInputGroupState
  • Autocomplete.InputGroup.Props: AutocompleteInputGroupProps
  • Autocomplete.Icon.State: AutocompleteIconState
  • Autocomplete.Icon.Props: AutocompleteIconProps
  • Autocomplete.Clear.State: AutocompleteClearState
  • Autocomplete.Clear.Props: AutocompleteClearProps
  • Autocomplete.List.State: AutocompleteListState
  • Autocomplete.List.Props: AutocompleteListProps
  • Autocomplete.Status.State: AutocompleteStatusState
  • Autocomplete.Status.Props: AutocompleteStatusProps
  • Autocomplete.Portal.State: AutocompletePortalState
  • Autocomplete.Portal.Props: AutocompletePortalProps
  • Autocomplete.Backdrop.Props: AutocompleteBackdropProps
  • Autocomplete.Backdrop.State: AutocompleteBackdropState
  • Autocomplete.Positioner.State: AutocompletePositionerState
  • Autocomplete.Positioner.Props: AutocompletePositionerProps
  • Autocomplete.Popup.State: AutocompletePopupState
  • Autocomplete.Popup.Props: AutocompletePopupProps
  • Autocomplete.Arrow.State: AutocompleteArrowState
  • Autocomplete.Arrow.Props: AutocompleteArrowProps
  • Autocomplete.Group.State: AutocompleteGroupState
  • Autocomplete.Group.Props: AutocompleteGroupProps
  • Autocomplete.GroupLabel.State: AutocompleteGroupLabelState
  • Autocomplete.GroupLabel.Props: AutocompleteGroupLabelProps
  • Autocomplete.Item.State: AutocompleteItemState
  • Autocomplete.Item.Props: AutocompleteItemProps
  • Autocomplete.Row.State: AutocompleteRowState
  • Autocomplete.Row.Props: AutocompleteRowProps
  • Autocomplete.Collection.State: AutocompleteCollectionState
  • Autocomplete.Collection.Props: AutocompleteCollectionProps
  • Autocomplete.Empty.State: AutocompleteEmptyState
  • Autocomplete.Empty.Props: AutocompleteEmptyProps
  • Autocomplete.Separator.Props: AutocompleteSeparatorProps
  • Autocomplete.Separator.State: AutocompleteSeparatorState

useFilter

Intl.Collator로 항목을 쿼리와 대조해 견고한 문자열 매칭을 수행해요. 이 훅은 항목을 외부에서 필터링할 때 사용돼요.

useFilteredItems

<Autocomplete.Root> 안에서 호출하면 내부적으로 필터링된 항목을 반환해요.

더 알아보기 (Learn more)