0.04K

Autocomplete input and command palette with a list of suggestions.

import { fn } from '@ember/helper';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { eq } from 'ember-truth-helpers';

import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';
import { cn } from '@/lib/utils';

import CheckIcon from '~icons/lucide/check';
import ChevronsUpDownIcon from '~icons/lucide/chevrons-up-down';

const frameworks = [
  { value: 'next.js', label: 'Next.js' },
  { value: 'sveltekit', label: 'SvelteKit' },
  { value: 'nuxt.js', label: 'Nuxt.js' },
  { value: 'remix', label: 'Remix' },
  { value: 'astro', label: 'Astro' },
  { value: 'ember', label: 'Ember' },
];

export default class ComboboxDemo extends Component {
  @tracked open = false;
  @tracked value = '';

  get selectedLabel() {
    const framework = frameworks.find((f) => f.value === this.value);
    return framework?.label || 'Select framework...';
  }

  handleSelect = (currentValue: string) => {
    this.value = currentValue === this.value ? '' : currentValue;
    this.open = false;
  };

  <template>
    <Popover @onOpenChange={{fn (mut this.open)}} @open={{this.open}}>
      <PopoverTrigger>
        {{! template-lint-disable require-mandatory-role-attributes }}
        <Button
          @class="w-[200px] justify-between"
          @variant="outline"
          aria-expanded={{this.open}}
          role="combobox"
        >
          {{this.selectedLabel}}
          <ChevronsUpDownIcon class="opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent @class="w-[200px] p-0">
        <Command>
          <CommandInput @placeholder="Search framework..." />
          <CommandList>
            <CommandEmpty>No framework found.</CommandEmpty>
            <CommandGroup>
              {{#each frameworks as |framework|}}
                <CommandItem
                  @onSelect={{this.handleSelect}}
                  @value={{framework.value}}
                >
                  {{framework.label}}
                  <CheckIcon
                    class={{cn
                      "ml-auto size-4"
                      (if
                        (eq this.value framework.value)
                        "opacity-100"
                        "opacity-0"
                      )
                    }}
                  />
                </CommandItem>
              {{/each}}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  </template>
}

Installation

The Combobox is built using a composition of the <Popover /> and the <Command /> components.

See installation instructions for the Popover and the Command components.

Usage

import { tracked } from '@glimmer/tracking';
import Component from '@glimmer/component';
import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';

const frameworks = [
  { value: 'next.js', label: 'Next.js' },
  { value: 'sveltekit', label: 'SvelteKit' },
  { value: 'nuxt.js', label: 'Nuxt.js' },
  { value: 'remix', label: 'Remix' },
  { value: 'astro', label: 'Astro' },
  { value: 'ember', label: 'Ember' },
];

export default class ExampleCombobox extends Component {
  @tracked open = false;
  @tracked value = '';

  get selectedLabel() {
    const framework = frameworks.find((f) => f.value === this.value);
    return framework?.label || 'Select framework...';
  }

  handleSelect = (currentValue: string) => {
    this.value = currentValue === this.value ? '' : currentValue;
    this.open = false;
  };

  <template>
    <Popover @open={{this.open}} @onOpenChange={{(fn (mut this.open))}}>
      <PopoverTrigger>
        <Button
          @variant="outline"
          role="combobox"
          aria-expanded={{this.open}}
          @class="w-[200px] justify-between"
        >
          {{this.selectedLabel}}
          <ChevronsUpDownIcon class="opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent @class="w-[200px] p-0">
        <Command>
          <CommandInput @placeholder="Search framework..." />
          <CommandList>
            <CommandEmpty>No framework found.</CommandEmpty>
            <CommandGroup>
              {{#each frameworks as |framework|}}
                <CommandItem
                  @value={{framework.value}}
                  @onSelect={{this.handleSelect}}
                >
                  <CheckIcon
                    class={{cn
                      "mr-2 size-4"
                      (if (eq this.value framework.value) "opacity-100" "opacity-0")
                    }}
                  />
                  {{framework.label}}
                </CommandItem>
              {{/each}}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  </template>
}

Examples

Combobox

import { fn } from '@ember/helper';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { eq } from 'ember-truth-helpers';

import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';
import { cn } from '@/lib/utils';

import CheckIcon from '~icons/lucide/check';
import ChevronsUpDownIcon from '~icons/lucide/chevrons-up-down';

const frameworks = [
  { value: 'next.js', label: 'Next.js' },
  { value: 'sveltekit', label: 'SvelteKit' },
  { value: 'nuxt.js', label: 'Nuxt.js' },
  { value: 'remix', label: 'Remix' },
  { value: 'astro', label: 'Astro' },
  { value: 'ember', label: 'Ember' },
];

export default class ComboboxDemo extends Component {
  @tracked open = false;
  @tracked value = '';

  get selectedLabel() {
    const framework = frameworks.find((f) => f.value === this.value);
    return framework?.label || 'Select framework...';
  }

  handleSelect = (currentValue: string) => {
    this.value = currentValue === this.value ? '' : currentValue;
    this.open = false;
  };

  <template>
    <Popover @onOpenChange={{fn (mut this.open)}} @open={{this.open}}>
      <PopoverTrigger>
        {{! template-lint-disable require-mandatory-role-attributes }}
        <Button
          @class="w-[200px] justify-between"
          @variant="outline"
          aria-expanded={{this.open}}
          role="combobox"
        >
          {{this.selectedLabel}}
          <ChevronsUpDownIcon class="opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent @class="w-[200px] p-0">
        <Command>
          <CommandInput @placeholder="Search framework..." />
          <CommandList>
            <CommandEmpty>No framework found.</CommandEmpty>
            <CommandGroup>
              {{#each frameworks as |framework|}}
                <CommandItem
                  @onSelect={{this.handleSelect}}
                  @value={{framework.value}}
                >
                  {{framework.label}}
                  <CheckIcon
                    class={{cn
                      "ml-auto size-4"
                      (if
                        (eq this.value framework.value)
                        "opacity-100"
                        "opacity-0"
                      )
                    }}
                  />
                </CommandItem>
              {{/each}}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  </template>
}

Popover

Status

import { fn } from '@ember/helper';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';

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

const statuses: Status[] = [
  { value: 'backlog', label: 'Backlog' },
  { value: 'todo', label: 'Todo' },
  { value: 'in progress', label: 'In Progress' },
  { value: 'done', label: 'Done' },
  { value: 'canceled', label: 'Canceled' },
];

export default class ComboboxPopover extends Component {
  @tracked open = false;
  @tracked selectedStatus: Status | null = null;

  handleSelect = (value: string) => {
    this.selectedStatus =
      statuses.find((status) => status.value === value) || null;
    this.open = false;
  };

  <template>
    <div class="flex items-center space-x-4">
      <p class="text-muted-foreground text-sm">Status</p>
      <Popover @onOpenChange={{fn (mut this.open)}} @open={{this.open}}>
        <PopoverTrigger>
          <Button @class="w-[150px] justify-start" @variant="outline">
            {{#if this.selectedStatus}}
              {{this.selectedStatus.label}}
            {{else}}
              + Set status
            {{/if}}
          </Button>
        </PopoverTrigger>
        <PopoverContent @align="start" @class="p-0" @side="right">
          <Command>
            <CommandInput @placeholder="Change status..." />
            <CommandList>
              <CommandEmpty>No results found.</CommandEmpty>
              <CommandGroup>
                {{#each statuses as |status|}}
                  <CommandItem
                    @onSelect={{this.handleSelect}}
                    @value={{status.value}}
                  >
                    {{status.label}}
                  </CommandItem>
                {{/each}}
              </CommandGroup>
            </CommandList>
          </Command>
        </PopoverContent>
      </Popover>
    </div>
  </template>
}

feature Create a new project

import { fn } from '@ember/helper';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuShortcut,
  DropdownMenuSub,
  DropdownMenuSubContent,
  DropdownMenuSubTrigger,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';

import MoreHorizontalIcon from '~icons/lucide/more-horizontal';

const labels = [
  'feature',
  'bug',
  'enhancement',
  'documentation',
  'design',
  'question',
  'maintenance',
];

export default class ComboboxDropdownMenu extends Component {
  @tracked label = 'feature';
  @tracked open = false;

  handleLabelSelect = (value: string) => {
    this.label = value;
    this.open = false;
  };

  <template>
    <div
      class="flex w-full flex-col items-start justify-between rounded-md border px-4 py-3 sm:flex-row sm:items-center"
    >
      <p class="text-sm leading-none font-medium">
        <span
          class="bg-primary text-primary-foreground mr-2 rounded-lg px-2 py-1 text-xs"
        >
          {{this.label}}
        </span>
        <span class="text-muted-foreground">Create a new project</span>
      </p>
      <DropdownMenu @onOpenChange={{fn (mut this.open)}} @open={{this.open}}>
        <DropdownMenuTrigger>
          <Button @size="sm" @variant="ghost">
            <MoreHorizontalIcon />
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent @align="end" @class="w-[200px]">
          <DropdownMenuLabel>Actions</DropdownMenuLabel>
          <DropdownMenuGroup>
            <DropdownMenuItem>Assign to...</DropdownMenuItem>
            <DropdownMenuItem>Set due date...</DropdownMenuItem>
            <DropdownMenuSeparator />
            <DropdownMenuSub>
              <DropdownMenuSubTrigger>Apply label</DropdownMenuSubTrigger>
              <DropdownMenuSubContent @class="p-0">
                <Command>
                  <CommandInput @placeholder="Filter label..." />
                  <CommandList>
                    <CommandEmpty>No label found.</CommandEmpty>
                    <CommandGroup>
                      {{#each labels as |label|}}
                        <CommandItem
                          @onSelect={{this.handleLabelSelect}}
                          @value={{label}}
                        >
                          {{label}}
                        </CommandItem>
                      {{/each}}
                    </CommandGroup>
                  </CommandList>
                </Command>
              </DropdownMenuSubContent>
            </DropdownMenuSub>
            <DropdownMenuSeparator />
            <DropdownMenuItem @class="text-red-600">
              Delete
              <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
            </DropdownMenuItem>
          </DropdownMenuGroup>
        </DropdownMenuContent>
      </DropdownMenu>
    </div>
  </template>
}

Responsive

You can create a responsive combobox by using the <Popover /> on desktop and the <Sheet /> components on mobile.

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { modifier } from 'ember-modifier';

import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';
import { Sheet, SheetContent, SheetTrigger } from '@/components/ui/sheet';

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

const statuses: Status[] = [
  { value: 'backlog', label: 'Backlog' },
  { value: 'todo', label: 'Todo' },
  { value: 'in progress', label: 'In Progress' },
  { value: 'done', label: 'Done' },
  { value: 'canceled', label: 'Canceled' },
];

const mediaQuery = modifier(
  (
    _element: HTMLElement,
    _positional: [],
    { onChange }: { onChange: (matches: boolean) => void }
  ) => {
    const mediaQueryList = window.matchMedia('(min-width: 768px)');

    const handler = (event: MediaQueryListEvent) => {
      onChange(event.matches);
    };

    onChange(mediaQueryList.matches);

    mediaQueryList.addEventListener('change', handler);

    return () => {
      mediaQueryList.removeEventListener('change', handler);
    };
  }
);

interface StatusListSignature {
  Args: {
    setOpen: (open: boolean) => void;
    setSelectedStatus: (status: Status | null) => void;
  };
}

class StatusList extends Component<StatusListSignature> {
  handleSelect = (value: string) => {
    const status = statuses.find((s) => s.value === value) || null;
    this.args.setSelectedStatus(status);
    this.args.setOpen(false);
  };

  <template>
    <Command>
      <CommandInput @placeholder="Filter status..." />
      <CommandList>
        <CommandEmpty>No results found.</CommandEmpty>
        <CommandGroup>
          {{#each statuses as |status|}}
            <CommandItem
              @onSelect={{this.handleSelect}}
              @value={{status.value}}
            >
              {{status.label}}
            </CommandItem>
          {{/each}}
        </CommandGroup>
      </CommandList>
    </Command>
  </template>
}

export default class ComboboxResponsive extends Component {
  @tracked open = false;
  @tracked isDesktop = false;
  @tracked selectedStatus: Status | null = null;

  get buttonLabel() {
    return this.selectedStatus ? this.selectedStatus.label : '+ Set status';
  }

  setOpen = (open: boolean) => {
    this.open = open;
  };

  setSelectedStatus = (status: Status | null) => {
    this.selectedStatus = status;
  };

  handleMediaQueryChange = (matches: boolean) => {
    this.isDesktop = matches;
  };

  <template>
    <div {{mediaQuery onChange=this.handleMediaQueryChange}}>
      {{#if this.isDesktop}}
        <Popover @onOpenChange={{this.setOpen}} @open={{this.open}}>
          <PopoverTrigger>
            <Button @class="w-[150px] justify-start" @variant="outline">
              {{this.buttonLabel}}
            </Button>
          </PopoverTrigger>
          <PopoverContent @align="start" @class="w-[200px] p-0">
            <StatusList
              @setOpen={{this.setOpen}}
              @setSelectedStatus={{this.setSelectedStatus}}
            />
          </PopoverContent>
        </Popover>
      {{else}}
        <Sheet @onOpenChange={{this.setOpen}} @open={{this.open}}>
          <SheetTrigger @asChild={{true}}>
            <Button @class="w-[150px] justify-start" @variant="outline">
              {{this.buttonLabel}}
            </Button>
          </SheetTrigger>
          <SheetContent @side="bottom">
            <div class="mt-4 border-t">
              <StatusList
                @setOpen={{this.setOpen}}
                @setSelectedStatus={{this.setSelectedStatus}}
              />
            </div>
          </SheetContent>
        </Sheet>
      {{/if}}
    </div>
  </template>
}