Skip to content

ComponentsMessage components

SourceSource

Entity Select

The picker for a server's own members, roles and channels, with chips when more than one may be chosen.

The picker for the things a server already knows about: its members, its roles, its channels, or all of them at once. It is a separate control from the string select, because the rows are not strings. A member arrives with a face, a role with a colour and a headcount, and a channel with the glyph of its kind.

The panel is built on Radix Popover in React and Reka UI Popover in Vue. The primitive owns the placement, the portal, Escape and the press outside. The listbox inside the panel is newt/ui’s own, because Radix has no listbox that takes more than one value.

Root class newt-entity-select
"use client"

import * as React from "react"

import { EntitySelect } from "@/registry/bases/newt/ui/entity-select"

/* A face, drawn rather than fetched, so the demo holds up offline. */
function avatar(hue: number) {
  return (
    <span
      className="h-full w-full rounded-full"
      style={{ background: `hsl(${hue} 60% 45%)` }}
    />
  )
}

/* The glyph a text channel is known by. */
const hash = (
  <svg viewBox="0 0 24 24" className="h-full w-full text-newt-text-muted">
    <path
      fill="currentColor"
      d="M10.2 3a1 1 0 0 1 1 1.15L10.83 7h3.98l.43-2.85a1 1 0 1 1 1.98.3L16.83 7H19a1 1 0 1 1 0 2h-2.47l-.6 4H18a1 1 0 1 1 0 2h-2.37l-.44 2.85a1 1 0 1 1-1.98-.3l.39-2.55h-3.98l-.43 2.85a1 1 0 1 1-1.98-.3L7.6 15H5a1 1 0 1 1 0-2h2.9l.6-4H6a1 1 0 0 1 0-2h2.8l.43-2.85A1 1 0 0 1 10.2 3Zm-.7 6-.6 4h3.98l.6-4H9.5Z"
    />
  </svg>
)

export default function EntitySelectDemo() {
  const [chosen, setChosen] = React.useState<readonly string[]>([])

  return (
    <div className="flex w-full max-w-[420px] flex-col gap-3">
      <EntitySelect
        kind="user"
        label="Choose a user"
        placeholder="Select a user"
        value={chosen}
        onValueChange={setChosen}
        options={[
          { value: "1", label: "someone", icon: avatar(210) },
          { value: "2", label: "a helper", icon: avatar(280), badge: "App" },
          { value: "3", label: "another", icon: avatar(140) },
        ]}
      />
      <EntitySelect
        kind="channel"
        label="Which text channel?"
        placeholder="Which text channel?"
        options={[
          { value: "general", label: "general", icon: hash },
          { value: "dev-chat", label: "dev-chat", icon: hash },
          { value: "playtesting", label: "playtesting", icon: hash },
        ]}
      />
    </div>
  )
}
<script setup lang="ts">
import { h, markRaw, ref } from "vue"
import { EntitySelect } from "~~/registry/bases/newt/ui/entity-select"

/* A face, drawn rather than fetched, so the demo holds up offline. */
function avatar(hue: number) {
  return markRaw({
    render: () =>
      h("span", {
        class: "h-full w-full rounded-full",
        style: { background: `hsl(${hue} 60% 45%)` },
      }),
  })
}

/* The glyph a text channel is known by. */
const Hash = markRaw({
  render: () =>
    h(
      "svg",
      { viewBox: "0 0 24 24", class: "h-full w-full text-newt-text-muted" },
      [
        h("path", {
          fill: "currentColor",
          d: "M10.2 3a1 1 0 0 1 1 1.15L10.83 7h3.98l.43-2.85a1 1 0 1 1 1.98.3L16.83 7H19a1 1 0 1 1 0 2h-2.47l-.6 4H18a1 1 0 1 1 0 2h-2.37l-.44 2.85a1 1 0 1 1-1.98-.3l.39-2.55h-3.98l-.43 2.85a1 1 0 1 1-1.98-.3L7.6 15H5a1 1 0 1 1 0-2h2.9l.6-4H6a1 1 0 0 1 0-2h2.8l.43-2.85A1 1 0 0 1 10.2 3Zm-.7 6-.6 4h3.98l.6-4H9.5Z",
        }),
      ]
    ),
})

const chosen = ref<readonly string[]>([])

const people = [
  { value: "1", label: "someone", icon: avatar(210) },
  { value: "2", label: "a helper", icon: avatar(280), badge: "App" },
  { value: "3", label: "another", icon: avatar(140) },
]

const channels = [
  { value: "general", label: "general", icon: Hash },
  { value: "dev-chat", label: "dev-chat", icon: Hash },
  { value: "playtesting", label: "playtesting", icon: Hash },
]
</script>

<template>
  <div class="flex w-full max-w-[420px] flex-col gap-3">
    <EntitySelect
      v-model="chosen"
      kind="user"
      label="Choose a user"
      placeholder="Select a user"
      :options="people"
    />
    <EntitySelect
      kind="channel"
      label="Which text channel?"
      placeholder="Which text channel?"
      :options="channels"
    />
  </div>
</template>

More than one

max above one turns the control into chips. The choices leave the list and sit in the trigger, each with a cross. Every row shows a box and not a tick: the shape says that a new choice keeps the earlier ones. Backspace removes the last chip.

At the limit the list stays open and takes no more choices. It does not drop the oldest choice, because somebody picked it.

Root class newt-entity-select
"use client"

import * as React from "react"

import { EntitySelect } from "@/registry/bases/newt/ui/entity-select"

/*
 * More than one may be chosen, so the choices leave the list and sit in the
 * control as chips — and each row grows a box instead of a tick.
 */
export default function EntitySelectRolesDemo() {
  const [roles, setRoles] = React.useState<readonly string[]>(["mod"])

  return (
    <EntitySelect
      kind="role"
      max={3}
      label="Which roles?"
      placeholder="Which roles?"
      value={roles}
      onValueChange={setRoles}
      options={[
        { value: "mod", label: "Moderator", color: "#f0b232", count: 4 },
        { value: "dev", label: "Contributor", color: "#3ba55d", count: 12 },
        { value: "player", label: "Player", color: "#5865f2", count: 128 },
        { value: "muted", label: "Muted", color: "#949ba4", count: 0 },
      ]}
    />
  )
}
<script setup lang="ts">
import { ref } from "vue"
import { EntitySelect } from "~~/registry/bases/newt/ui/entity-select"

/*
 * More than one may be chosen, so the choices leave the list and sit in the
 * control as chips — and each row grows a box instead of a tick.
 */
const roles = ref<readonly string[]>(["mod"])

const options = [
  { value: "mod", label: "Moderator", color: "#f0b232", count: 4 },
  { value: "dev", label: "Contributor", color: "#3ba55d", count: 12 },
  { value: "player", label: "Player", color: "#5865f2", count: 128 },
  { value: "muted", label: "Muted", color: "#949ba4", count: 0 },
]
</script>

<template>
  <EntitySelect
    v-model="roles"
    kind="role"
    :max="3"
    label="Which roles?"
    placeholder="Which roles?"
    :options="options"
  />
</template>

Installation

npx newtui@latest add entity-selectnpx shadcn@latest add @newtui/entity-selectnpx shadcn-vue@latest add @newtui/entity-selectpnpm dlx newtui@latest add entity-selectpnpm dlx shadcn@latest add @newtui/entity-selectpnpm dlx shadcn-vue@latest add @newtui/entity-selectyarn dlx newtui@latest add entity-selectyarn dlx shadcn@latest add @newtui/entity-selectyarn dlx shadcn-vue@latest add @newtui/entity-selectbunx newtui@latest add entity-selectbunx shadcn@latest add @newtui/entity-selectbunx shadcn-vue@latest add @newtui/entity-selectdeno run -A npm:newtui@latest add entity-selectdeno run -A npm:shadcn@latest add @newtui/entity-selectdeno run -A npm:shadcn-vue@latest add @newtui/entity-selectvlx newtui@latest add entity-selectvlx shadcn@latest add @newtui/entity-selectvlx shadcn-vue@latest add @newtui/entity-selectvp dlx newtui@latest add entity-selectvp dlx shadcn@latest add @newtui/entity-selectvp dlx shadcn-vue@latest add @newtui/entity-selectnubx newtui@latest add entity-selectnubx shadcn@latest add @newtui/entity-selectnubx shadcn-vue@latest add @newtui/entity-selectnlx newtui@latest add entity-selectnlx shadcn@latest add @newtui/entity-selectnlx shadcn-vue@latest add @newtui/entity-selectupx newtui@latest add entity-selectupx shadcn@latest add @newtui/entity-selectupx shadcn-vue@latest add @newtui/entity-select

The shadcn CLI needs the @newtui registry in your components.json. Set it up.

Install the dependencies

npm install radix-uipnpm add radix-uiyarn add radix-uibun add radix-uideno add npm:radix-uivlt install radix-uivp add radix-uinub add radix-uini radix-uiupm add radix-ui
npm install reka-uipnpm add reka-uiyarn add reka-uibun add reka-uideno add npm:reka-uivlt install reka-uivp add reka-uinub add reka-uini reka-uiupm add reka-ui

Copy the source into your project

"use client"

import { Popover as PopoverPrimitive } from "radix-ui"
import * as React from "react"

import { cn } from "@/lib/utils"

/*
 * The picker for things the server already knows about: its members, its
 * roles, its channels, or all of them at once.
 *
 * It is a separate control from the string select rather than a mode of it,
 * because the rows are not strings. A member arrives with a face, a role with
 * a colour and a headcount, a channel with the glyph that says what kind of
 * channel it is — and when more than one can be chosen, the choices come back
 * out of the list and sit in the control as chips, which a string select
 * never does.
 */

/** What the list is drawn from. Mentionable mixes members and roles. */
export type EntityKind = "user" | "role" | "channel" | "mentionable"

export interface EntityOption {
  value: string
  label: string
  /** A member's avatar, or the glyph a role or channel is known by. */
  icon?: React.ReactNode
  /** Members carrying this role — shown beside the name, as the client does. */
  count?: number
  /** A short tag after the name. An app is the one that always has one. */
  badge?: string
  /** A role's colour, which its name takes in the list and in its chip. */
  color?: string
  disabled?: boolean
}

/**
 * What one of these may carry. The platform takes at most this many either
 * way — as the list it offers, and as the selection it accepts back.
 */
export const ENTITY_SELECT_MAX = 25

export interface EntitySelectProps extends Omit<
  React.HTMLAttributes<HTMLDivElement>,
  "onSelect" | "defaultValue"
> {
  options: readonly EntityOption[]
  /** Which of the four this is. Only the empty-list wording depends on it. */
  kind?: EntityKind
  /** Controlled selection. Leave undefined to let the control own it. */
  value?: readonly string[]
  defaultValue?: readonly string[]
  onValueChange?: (value: readonly string[]) => void
  /** How many may be chosen. More than one turns the control into chips. */
  max?: number
  placeholder?: string
  disabled?: boolean
  /** Accessible name of the trigger and the listbox. */
  label?: string
  /** Shown when the list is empty. Defaults to a line for each `kind`. */
  emptyLabel?: string
  /**
   * The element the panel is portalled into. Defaults to `document.body`;
   * pass the enclosing native `<dialog>` or a themed wrapper to keep it there.
   */
  container?: React.ComponentProps<typeof PopoverPrimitive.Portal>["container"]
}

const EMPTY = {
  user: "No members to show",
  role: "No roles to show",
  channel: "No channels to show",
  mentionable: "Nothing to show",
} as const satisfies Record<EntityKind, string>

/** The headcount beside a role, which is a number and the glyph for people. */
function Headcount({ count }: { readonly count: number }) {
  return (
    <span className="flex shrink-0 items-center gap-1 text-xs tabular-nums text-newt-text-muted">
      <svg viewBox="0 0 24 24" aria-hidden="true" className="h-3.5 w-3.5">
        <path
          fill="currentColor"
          d="M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8Zm0 2c-3.9 0-7 2-7 4.5V20h14v-1.5c0-2.5-3.1-4.5-7-4.5Z"
        />
      </svg>
      {count}
    </span>
  )
}

/*
 * Radix Popover owns the panel: outside press, Escape, collision-aware
 * placement and the portal. The listbox inside it stays hand-written, because
 * Radix has no listbox that takes more than one value: focus never leaves the
 * control, and `aria-activedescendant` names the row the keyboard is on.
 */
const EntitySelect = React.forwardRef<HTMLDivElement, EntitySelectProps>(
  (
    {
      className,
      options: given,
      kind = "user",
      value,
      defaultValue = [],
      onValueChange,
      max = 1,
      placeholder = "Make a selection",
      disabled = false,
      label = "Select an option",
      emptyLabel,
      container,
      ...props
    },
    ref
  ) => {
    /* The cap is applied here: the keyboard and the option ids count rows, and
     * a row the platform would reject should not be one of them. */
    const options = React.useMemo(
      () => given.slice(0, ENTITY_SELECT_MAX),
      [given]
    )
    const limit = Math.min(max, ENTITY_SELECT_MAX)
    const multiple = limit > 1

    const id = React.useId()
    const listboxId = `${id}-listbox`
    const optionId = (index: number) => `${id}-option-${index}`

    const triggerRef = React.useRef<HTMLButtonElement>(null)
    const listRef = React.useRef<HTMLUListElement>(null)

    const [open, setOpen] = React.useState(false)
    const [active, setActive] = React.useState(-1)
    const [uncontrolled, setUncontrolled] =
      React.useState<readonly string[]>(defaultValue)
    const selected = value ?? uncontrolled

    const chosen = React.useMemo(
      () => options.filter((option) => selected.includes(option.value)),
      [options, selected]
    )

    const commit = React.useCallback(
      (next: readonly string[]) => {
        if (value === undefined) setUncontrolled(next)
        onValueChange?.(next)
      },
      [value, onValueChange]
    )

    const selectable = React.useCallback(
      (index: number) => Boolean(options[index]) && !options[index].disabled,
      [options]
    )

    const firstSelectable = React.useCallback(
      () => options.findIndex((option) => !option.disabled),
      [options]
    )

    const lastSelectable = React.useCallback(() => {
      for (let i = options.length - 1; i >= 0; i--) {
        if (!options[i].disabled) return i
      }
      return -1
    }, [options])

    const step = React.useCallback(
      (from: number, direction: 1 | -1) => {
        const count = options.length
        for (let i = 1; i <= count; i++) {
          const index = (from + direction * i + count * i) % count
          if (selectable(index)) return index
        }
        return from
      },
      [options.length, selectable]
    )

    const scrollToActive = (index: number) => {
      listRef.current
        ?.querySelector(`#${CSS.escape(optionId(index))}`)
        ?.scrollIntoView({ block: "nearest" })
    }

    React.useEffect(() => {
      if (active < 0) return
      scrollToActive(active)
      // eslint-disable-next-line react-hooks/exhaustive-deps -- optionId is derived from a stable id
    }, [active])

    const openMenu = () => {
      if (disabled || open) return
      setOpen(true)
      const current = options.findIndex((option) =>
        selected.includes(option.value)
      )
      setActive(
        current >= 0 && selectable(current) ? current : firstSelectable()
      )
    }

    const closeMenu = () => {
      setOpen(false)
      setActive(-1)
    }

    const toggle = (index: number) => {
      const option = options[index]
      if (!option || option.disabled) return
      if (!multiple) {
        commit([option.value])
        closeMenu()
        triggerRef.current?.focus()
        return
      }
      /*
       * At the limit the list stays open and stops taking more, rather than
       * silently dropping the oldest choice: the reader picked those, and a
       * control that quietly un-picks one is a control that lies.
       */
      const already = selected.includes(option.value)
      if (!already && selected.length >= limit) return
      commit(
        already
          ? selected.filter((each) => each !== option.value)
          : [...selected, option.value]
      )
    }

    const remove = (entity: string) => {
      commit(selected.filter((each) => each !== entity))
    }

    const onKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
      switch (event.key) {
        case "ArrowDown":
        case "ArrowUp": {
          event.preventDefault()
          if (!open) {
            openMenu()
            return
          }
          setActive((current) =>
            step(current, event.key === "ArrowDown" ? 1 : -1)
          )
          return
        }
        case "Home":
          if (!open) return
          event.preventDefault()
          setActive(firstSelectable())
          return
        case "End":
          if (!open) return
          event.preventDefault()
          setActive(lastSelectable())
          return
        case "Enter":
        case " ": {
          event.preventDefault()
          if (!open) {
            openMenu()
            return
          }
          if (active >= 0) toggle(active)
          return
        }
        case "Backspace": {
          /* The chip nearest the caret goes first, which is the last one. */
          if (!multiple || open || chosen.length === 0) return
          event.preventDefault()
          const last = chosen[chosen.length - 1]
          if (last) remove(last.value)
          return
        }
        default:
      }
    }

    return (
      <PopoverPrimitive.Root
        open={open}
        onOpenChange={(next) => (next ? openMenu() : closeMenu())}
      >
        <div
          ref={ref}
          className={cn("w-full max-w-[420px]", className)}
          {...props}
        >
          {/*
           * An anchor rather than a trigger: a popover trigger announces a
           * dialog, and this control is a combobox that opens a listbox.
           */}
          <PopoverPrimitive.Anchor asChild>
            <button
              ref={triggerRef}
              type="button"
              role="combobox"
              aria-haspopup="listbox"
              aria-expanded={open}
              aria-controls={open ? listboxId : undefined}
              aria-activedescendant={
                open && active >= 0 ? optionId(active) : undefined
              }
              aria-label={label}
              disabled={disabled}
              onClick={() => (open ? closeMenu() : openMenu())}
              onKeyDown={onKeyDown}
              className={cn(
                "flex w-full items-center gap-2 rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-base px-3 py-2 text-start text-sm text-newt-text-secondary",
                "transition-colors duration-fast ease-newt hover:border-newt-text-muted",
                "focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link",
                open && "border-newt-text-link",
                disabled && "cursor-not-allowed opacity-50"
              )}
            >
              <span className="flex min-w-0 flex-1 flex-wrap items-center gap-1.5">
                {chosen.length === 0 ? (
                  <span className="text-newt-text-muted">{placeholder}</span>
                ) : multiple ? (
                  chosen.map((option) => (
                    /*
                     * A chip is not a button: it sits inside one, and nesting
                     * two would be markup a keyboard cannot untangle. Its
                     * cross is decorative, and Backspace is the way in from
                     * the keyboard.
                     */
                    <span
                      key={option.value}
                      onMouseDown={(event) => {
                        event.preventDefault()
                        event.stopPropagation()
                        remove(option.value)
                      }}
                      className="flex items-center gap-1.5 rounded-[var(--newt-radius-sm)] bg-newt-bg-elevated px-1.5 py-1 text-xs text-newt-text-primary"
                    >
                      {option.icon ? (
                        <span
                          aria-hidden="true"
                          className="flex h-4 w-4 shrink-0"
                        >
                          {option.icon}
                        </span>
                      ) : null}
                      <span
                        style={
                          option.color ? { color: option.color } : undefined
                        }
                      >
                        {option.label}
                      </span>
                      <svg
                        viewBox="0 0 24 24"
                        aria-hidden="true"
                        className="h-3 w-3"
                      >
                        <path
                          fill="currentColor"
                          d="M7.05 5.64a1 1 0 0 1 1.41 0L12 9.17l3.54-3.53a1 1 0 1 1 1.41 1.41L13.41 10.6l3.54 3.53a1 1 0 0 1-1.41 1.42L12 12.01l-3.54 3.54a1 1 0 0 1-1.41-1.42l3.53-3.53-3.53-3.54a1 1 0 0 1 0-1.42Z"
                        />
                      </svg>
                    </span>
                  ))
                ) : (
                  <span className="flex min-w-0 items-center gap-2">
                    {chosen[0]?.icon ? (
                      <span
                        aria-hidden="true"
                        className="flex h-5 w-5 shrink-0"
                      >
                        {chosen[0].icon}
                      </span>
                    ) : null}
                    <span
                      className="truncate text-newt-text-primary"
                      style={
                        chosen[0]?.color
                          ? { color: chosen[0].color }
                          : undefined
                      }
                    >
                      {chosen[0]?.label}
                    </span>
                  </span>
                )}
              </span>

              <svg
                viewBox="0 0 24 24"
                aria-hidden="true"
                className={cn(
                  "h-[18px] w-[18px] shrink-0",
                  open && "rotate-180"
                )}
              >
                <path
                  fill="currentColor"
                  d="M5.3 9.3a1 1 0 0 1 1.4 0l5.3 5.29 5.3-5.3a1 1 0 1 1 1.4 1.42l-6 6a1 1 0 0 1-1.4 0l-6-6a1 1 0 0 1 0-1.42Z"
                />
              </svg>
            </button>
          </PopoverPrimitive.Anchor>
        </div>

        <PopoverPrimitive.Portal container={container}>
          <PopoverPrimitive.Content
            /*
             * Not the dialog Radix makes of a popover: the control names the
             * listbox inside with `aria-controls`, and this is only its frame.
             */
            role="presentation"
            align="start"
            sideOffset={4}
            /*
             * Focus stays on the control: aria-activedescendant drives the
             * list, so the panel must never take it. The row in force is
             * brought into view here, once the list exists.
             */
            onOpenAutoFocus={(event) => {
              event.preventDefault()
              if (active >= 0) scrollToActive(active)
            }}
            /*
             * A press on the control is not a press outside: its own click
             * handler closes the list, and dismissing here as well would let
             * that click open it again.
             */
            onInteractOutside={(event) => {
              const target = event.target
              if (
                target instanceof Node &&
                triggerRef.current?.contains(target)
              ) {
                event.preventDefault()
              }
            }}
            onMouseDown={(event) => event.preventDefault()}
            className="z-[110] w-[var(--radix-popover-trigger-width)] rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-floating p-1.5 shadow-elevation-high outline-none"
          >
            <ul
              ref={listRef}
              id={listboxId}
              role="listbox"
              aria-label={label}
              aria-multiselectable={multiple || undefined}
              /*
               * The scrollbar shows rather than hiding until the pointer
               * moves: the list is capped at 25, so the bar is what says
               * there is more of it below the fold.
               */
              className={cn(
                "m-0 max-h-[300px] list-none overflow-y-auto p-0",
                "[scrollbar-width:thin] [scrollbar-color:var(--newt-bg-active)_transparent]",
                "[&::-webkit-scrollbar]:w-2",
                "[&::-webkit-scrollbar-track]:bg-transparent",
                "[&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-solid [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-newt-bg-active [&::-webkit-scrollbar-thumb]:bg-clip-padding",
                "[&::-webkit-scrollbar-thumb:hover]:bg-newt-text-muted [&::-webkit-scrollbar-thumb:hover]:bg-clip-padding"
              )}
            >
              {options.length === 0 ? (
                <li className="px-2.5 py-2 text-sm text-newt-text-muted">
                  {emptyLabel ?? EMPTY[kind]}
                </li>
              ) : (
                options.map((option, index) => {
                  const picked = selected.includes(option.value)
                  const highlighted = index === active && !option.disabled
                  return (
                    <li
                      key={option.value}
                      id={optionId(index)}
                      role="option"
                      aria-selected={picked}
                      aria-disabled={option.disabled || undefined}
                      onMouseEnter={() => {
                        if (!option.disabled) setActive(index)
                      }}
                      onClick={() => toggle(index)}
                      className={cn(
                        "flex cursor-pointer items-center gap-2.5 rounded-[var(--newt-radius-sm)] px-2.5 py-2 text-sm text-newt-text-secondary",
                        highlighted && "bg-newt-bg-elevated",
                        picked && "text-newt-text-primary",
                        option.disabled &&
                          "cursor-not-allowed bg-transparent text-newt-text-muted opacity-50"
                      )}
                    >
                      {option.icon ? (
                        <span
                          aria-hidden="true"
                          className="flex h-6 w-6 shrink-0 items-center justify-center"
                        >
                          {option.icon}
                        </span>
                      ) : null}

                      <span
                        className="min-w-0 flex-1 truncate font-medium"
                        style={
                          option.color ? { color: option.color } : undefined
                        }
                      >
                        {option.label}
                      </span>

                      {option.count === undefined ? null : (
                        <Headcount count={option.count} />
                      )}

                      {option.badge === undefined ? null : (
                        <span className="shrink-0 rounded-[var(--newt-radius-sm)] bg-newt-brand px-1 py-0.5 text-[10px] font-bold uppercase leading-none text-newt-on-accent">
                          {option.badge}
                        </span>
                      )}

                      {/*
                       * A box rather than a tick, and only when more than one
                       * may be chosen: the shape says whether picking this one
                       * un-picks the last.
                       */}
                      {multiple ? (
                        <span
                          aria-hidden="true"
                          data-checked={picked || undefined}
                          className={cn(
                            "flex h-5 w-5 shrink-0 items-center justify-center rounded-[var(--newt-radius-sm)] border-2 border-newt-text-muted",
                            "data-[checked]:border-newt-brand data-[checked]:bg-newt-brand"
                          )}
                        >
                          {picked ? (
                            <svg viewBox="0 0 16 16" className="h-3.5 w-3.5">
                              <path
                                fill="#fff"
                                d="M6.2 11.6 3.1 8.5a1 1 0 0 1 1.4-1.4l1.7 1.7 4.3-4.3a1 1 0 1 1 1.4 1.4l-5 5a1 1 0 0 1-1.4 0Z"
                              />
                            </svg>
                          ) : null}
                        </span>
                      ) : null}
                    </li>
                  )
                })
              )}
            </ul>
          </PopoverPrimitive.Content>
        </PopoverPrimitive.Portal>
      </PopoverPrimitive.Root>
    )
  }
)
EntitySelect.displayName = "EntitySelect"

export { EntitySelect }
<script setup lang="ts">
import {
  PopoverAnchor,
  PopoverContent,
  PopoverPortal,
  PopoverRoot,
} from "reka-ui"
import type { HTMLAttributes } from "vue"
import { computed, nextTick, ref, useId, watch } from "vue"

import { cn } from "@/lib/utils"

import {
  ENTITY_SELECT_MAX,
  entitySelectEmpty,
  type EntityKind,
  type EntityOption,
} from "."

/*
 * The picker for things the server already knows about: its members, its
 * roles, its channels, or all of them at once.
 *
 * It is a separate control from the string select rather than a mode of it,
 * because the rows are not strings. A member arrives with a face, a role with
 * a colour and a headcount, a channel with the glyph that says what kind of
 * channel it is — and when more than one can be chosen, the choices come back
 * out of the list and sit in the control as chips, which a string select
 * never does.
 *
 * Reka Popover owns the panel: outside press, Escape, collision-aware
 * placement and the portal. The listbox inside it stays hand-written, because
 * the React side has no listbox that takes more than one value and both must
 * behave alike: focus never leaves the control, and `aria-activedescendant`
 * names the row the keyboard is on.
 */
defineOptions({ inheritAttrs: false })

const props = withDefaults(
  defineProps<{
    class?: HTMLAttributes["class"]
    options: readonly EntityOption[]
    /** Which of the four this is. Only the empty-list wording depends on it. */
    kind?: EntityKind
    /** How many may be chosen. More than one turns the control into chips. */
    max?: number
    placeholder?: string
    disabled?: boolean
    /** Accessible name of the trigger and the listbox. */
    label?: string
    /** Shown when the list is empty. Defaults to a line for each `kind`. */
    emptyLabel?: string
    /**
     * The element the panel is portalled into. Defaults to `body`; pass the
     * enclosing native `<dialog>` or a themed wrapper to keep it there.
     */
    container?: string | HTMLElement
  }>(),
  {
    kind: "user",
    max: 1,
    placeholder: "Make a selection",
    disabled: false,
    label: "Select an option",
  }
)

const selected = defineModel<readonly string[]>({ default: () => [] })

/* The cap is applied here: the keyboard and the option ids count rows, and a
 * row the platform would reject should not be one of them. */
const options = computed(() => props.options.slice(0, ENTITY_SELECT_MAX))
const limit = computed(() => Math.min(props.max, ENTITY_SELECT_MAX))
const multiple = computed(() => limit.value > 1)

const id = useId()
const listboxId = `${id}-listbox`
const optionId = (index: number) => `${id}-option-${index}`

const triggerRef = ref<HTMLButtonElement | null>(null)
const listRef = ref<HTMLUListElement | null>(null)

const open = ref(false)
const active = ref(-1)

const chosen = computed(() =>
  options.value.filter((option) => selected.value.includes(option.value))
)

const selectable = (index: number) => {
  const option = options.value[index]
  return Boolean(option) && !option?.disabled
}

const firstSelectable = () =>
  options.value.findIndex((option) => !option.disabled)

const lastSelectable = () => {
  for (let i = options.value.length - 1; i >= 0; i--) {
    if (!options.value[i]?.disabled) return i
  }
  return -1
}

const step = (from: number, direction: 1 | -1) => {
  const count = options.value.length
  for (let i = 1; i <= count; i++) {
    const index = (from + direction * i + count * i) % count
    if (selectable(index)) return index
  }
  return from
}

function scrollToActive() {
  if (active.value < 0) return
  listRef.value
    ?.querySelector(`#${CSS.escape(optionId(active.value))}`)
    ?.scrollIntoView({ block: "nearest" })
}

watch(active, async () => {
  await nextTick()
  scrollToActive()
})

/*
 * Focus stays on the control: aria-activedescendant drives the list, so the
 * panel must never take it. The row in force is brought into view here, once
 * the list exists.
 */
function onOpenAutoFocus(event: Event) {
  event.preventDefault()
  scrollToActive()
}

/*
 * A press on the control is not a press outside: its own click handler closes
 * the list, and dismissing here as well would let that click open it again.
 */
function onInteractOutside(event: Event) {
  const target = event.target
  if (target instanceof Node && triggerRef.value?.contains(target)) {
    event.preventDefault()
  }
}

function openMenu() {
  if (props.disabled || open.value) return
  open.value = true
  const current = options.value.findIndex((option) =>
    selected.value.includes(option.value)
  )
  active.value =
    current >= 0 && selectable(current) ? current : firstSelectable()
}

function closeMenu() {
  open.value = false
  active.value = -1
}

function toggle(index: number) {
  const option = options.value[index]
  if (!option || option.disabled) return
  if (!multiple.value) {
    selected.value = [option.value]
    closeMenu()
    triggerRef.value?.focus()
    return
  }
  /*
   * At the limit the list stays open and stops taking more, rather than
   * silently dropping the oldest choice: the reader picked those, and a
   * control that quietly un-picks one is a control that lies.
   */
  const already = selected.value.includes(option.value)
  if (!already && selected.value.length >= limit.value) return
  selected.value = already
    ? selected.value.filter((each) => each !== option.value)
    : [...selected.value, option.value]
}

function remove(entity: string) {
  selected.value = selected.value.filter((each) => each !== entity)
}

function onKeydown(event: KeyboardEvent) {
  switch (event.key) {
    case "ArrowDown":
    case "ArrowUp": {
      event.preventDefault()
      if (!open.value) {
        openMenu()
        return
      }
      active.value = step(active.value, event.key === "ArrowDown" ? 1 : -1)
      return
    }
    case "Home":
      if (!open.value) return
      event.preventDefault()
      active.value = firstSelectable()
      return
    case "End":
      if (!open.value) return
      event.preventDefault()
      active.value = lastSelectable()
      return
    case "Enter":
    case " ": {
      event.preventDefault()
      if (!open.value) {
        openMenu()
        return
      }
      if (active.value >= 0) toggle(active.value)
      return
    }
    case "Backspace": {
      /* The chip nearest the caret goes first, which is the last one. */
      if (!multiple.value || open.value || chosen.value.length === 0) return
      event.preventDefault()
      const last = chosen.value[chosen.value.length - 1]
      if (last) remove(last.value)
      return
    }
    default:
  }
}

function onChipMouseDown(event: MouseEvent, entity: string) {
  event.preventDefault()
  event.stopPropagation()
  remove(entity)
}
</script>

<template>
  <PopoverRoot
    :open="open"
    @update:open="(next) => (next ? openMenu() : closeMenu())"
  >
    <div v-bind="$attrs" :class="cn('w-full max-w-[420px]', props.class)">
      <!--
        An anchor rather than a trigger: a popover trigger announces a dialog,
        and this control is a combobox that opens a listbox.
      -->
      <PopoverAnchor as-child>
        <button
          ref="triggerRef"
          type="button"
          role="combobox"
          aria-haspopup="listbox"
          :aria-expanded="open"
          :aria-controls="open ? listboxId : undefined"
          :aria-activedescendant="
            open && active >= 0 ? optionId(active) : undefined
          "
          :aria-label="props.label"
          :disabled="props.disabled"
          :class="
            cn(
              'flex w-full items-center gap-2 rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-base px-3 py-2 text-start text-sm text-newt-text-secondary',
              'transition-colors duration-fast ease-newt hover:border-newt-text-muted',
              'focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link',
              open && 'border-newt-text-link',
              props.disabled && 'cursor-not-allowed opacity-50'
            )
          "
          @click="open ? closeMenu() : openMenu()"
          @keydown="onKeydown"
        >
          <span class="flex min-w-0 flex-1 flex-wrap items-center gap-1.5">
            <span v-if="chosen.length === 0" class="text-newt-text-muted">
              {{ props.placeholder }}
            </span>

            <!--
              A chip is not a button: it sits inside one, and nesting two would be
              markup a keyboard cannot untangle. Its cross is decorative, and
              Backspace is the way in from the keyboard.
            -->
            <template v-else-if="multiple">
              <span
                v-for="option of chosen"
                :key="option.value"
                class="flex items-center gap-1.5 rounded-[var(--newt-radius-sm)] bg-newt-bg-elevated px-1.5 py-1 text-xs text-newt-text-primary"
                @mousedown="onChipMouseDown($event, option.value)"
              >
                <span
                  v-if="option.icon"
                  aria-hidden="true"
                  class="flex h-4 w-4 shrink-0"
                >
                  <component :is="option.icon" />
                </span>
                <span
                  :style="option.color ? { color: option.color } : undefined"
                >
                  {{ option.label }}
                </span>
                <svg viewBox="0 0 24 24" aria-hidden="true" class="h-3 w-3">
                  <path
                    fill="currentColor"
                    d="M7.05 5.64a1 1 0 0 1 1.41 0L12 9.17l3.54-3.53a1 1 0 1 1 1.41 1.41L13.41 10.6l3.54 3.53a1 1 0 0 1-1.41 1.42L12 12.01l-3.54 3.54a1 1 0 0 1-1.41-1.42l3.53-3.53-3.53-3.54a1 1 0 0 1 0-1.42Z"
                  />
                </svg>
              </span>
            </template>

            <span v-else class="flex min-w-0 items-center gap-2">
              <span
                v-if="chosen[0]?.icon"
                aria-hidden="true"
                class="flex h-5 w-5 shrink-0"
              >
                <component :is="chosen[0].icon" />
              </span>
              <span
                class="truncate text-newt-text-primary"
                :style="
                  chosen[0]?.color ? { color: chosen[0].color } : undefined
                "
              >
                {{ chosen[0]?.label }}
              </span>
            </span>
          </span>

          <svg
            viewBox="0 0 24 24"
            aria-hidden="true"
            :class="cn('h-[18px] w-[18px] shrink-0', open && 'rotate-180')"
          >
            <path
              fill="currentColor"
              d="M5.3 9.3a1 1 0 0 1 1.4 0l5.3 5.29 5.3-5.3a1 1 0 1 1 1.4 1.42l-6 6a1 1 0 0 1-1.4 0l-6-6a1 1 0 0 1 0-1.42Z"
            />
          </svg>
        </button>
      </PopoverAnchor>
    </div>

    <!--
      The frame is not the dialog Reka makes of a popover: the control names
      the listbox inside with `aria-controls`. Under `as-child` the child's own
      attributes win, which is how `role` and `aria-labelledby` are replaced.
    -->
    <PopoverPortal :to="props.container">
      <PopoverContent
        as-child
        align="start"
        :side-offset="4"
        @open-auto-focus="onOpenAutoFocus"
        @interact-outside="onInteractOutside"
      >
        <div
          role="presentation"
          :aria-labelledby="undefined"
          class="z-[110] w-[var(--reka-popover-trigger-width)] rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-floating p-1.5 shadow-elevation-high outline-none"
          @mousedown.prevent
        >
          <!--
            The scrollbar shows rather than hiding until the pointer moves: the
            list is capped at 25, so the bar is what says there is more of it
            below the fold.
          -->
          <ul
            :id="listboxId"
            ref="listRef"
            role="listbox"
            :aria-label="props.label"
            :aria-multiselectable="multiple || undefined"
            :class="
              cn(
                'm-0 max-h-[300px] list-none overflow-y-auto p-0',
                '[scrollbar-width:thin] [scrollbar-color:var(--newt-bg-active)_transparent]',
                '[&::-webkit-scrollbar]:w-2',
                '[&::-webkit-scrollbar-track]:bg-transparent',
                '[&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-solid [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-newt-bg-active [&::-webkit-scrollbar-thumb]:bg-clip-padding',
                '[&::-webkit-scrollbar-thumb:hover]:bg-newt-text-muted [&::-webkit-scrollbar-thumb:hover]:bg-clip-padding'
              )
            "
          >
            <li
              v-if="options.length === 0"
              class="px-2.5 py-2 text-sm text-newt-text-muted"
            >
              {{ props.emptyLabel ?? entitySelectEmpty[props.kind] }}
            </li>

            <li
              v-for="(option, index) of options"
              v-else
              :id="optionId(index)"
              :key="option.value"
              role="option"
              :aria-selected="selected.includes(option.value)"
              :aria-disabled="option.disabled || undefined"
              :class="
                cn(
                  'flex cursor-pointer items-center gap-2.5 rounded-[var(--newt-radius-sm)] px-2.5 py-2 text-sm text-newt-text-secondary',
                  index === active && !option.disabled && 'bg-newt-bg-elevated',
                  selected.includes(option.value) && 'text-newt-text-primary',
                  option.disabled &&
                    'cursor-not-allowed bg-transparent text-newt-text-muted opacity-50'
                )
              "
              @mouseenter="option.disabled ? undefined : (active = index)"
              @click="toggle(index)"
            >
              <span
                v-if="option.icon"
                aria-hidden="true"
                class="flex h-6 w-6 shrink-0 items-center justify-center"
              >
                <component :is="option.icon" />
              </span>

              <span
                class="min-w-0 flex-1 truncate font-medium"
                :style="option.color ? { color: option.color } : undefined"
              >
                {{ option.label }}
              </span>

              <!-- The headcount beside a role: a number and the glyph for people. -->
              <span
                v-if="option.count !== undefined"
                class="flex shrink-0 items-center gap-1 text-xs tabular-nums text-newt-text-muted"
              >
                <svg viewBox="0 0 24 24" aria-hidden="true" class="h-3.5 w-3.5">
                  <path
                    fill="currentColor"
                    d="M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8Zm0 2c-3.9 0-7 2-7 4.5V20h14v-1.5c0-2.5-3.1-4.5-7-4.5Z"
                  />
                </svg>
                {{ option.count }}
              </span>

              <span
                v-if="option.badge"
                class="shrink-0 rounded-[var(--newt-radius-sm)] bg-newt-brand px-1 py-0.5 text-[10px] font-bold uppercase leading-none text-newt-on-accent"
              >
                {{ option.badge }}
              </span>

              <!--
                A box rather than a tick, and only when more than one may be
                chosen: the shape says whether picking this one un-picks the last.
              -->
              <span
                v-if="multiple"
                aria-hidden="true"
                :data-checked="
                  selected.includes(option.value) ? true : undefined
                "
                :class="
                  cn(
                    'flex h-5 w-5 shrink-0 items-center justify-center rounded-[var(--newt-radius-sm)] border-2 border-newt-text-muted',
                    'data-[checked]:border-newt-brand data-[checked]:bg-newt-brand'
                  )
                "
              >
                <svg
                  v-if="selected.includes(option.value)"
                  viewBox="0 0 16 16"
                  class="h-3.5 w-3.5"
                >
                  <path
                    fill="#fff"
                    d="M6.2 11.6 3.1 8.5a1 1 0 0 1 1.4-1.4l1.7 1.7 4.3-4.3a1 1 0 1 1 1.4 1.4l-5 5a1 1 0 0 1-1.4 0Z"
                  />
                </svg>
              </span>
            </li>
          </ul>
        </div>
      </PopoverContent>
    </PopoverPortal>
  </PopoverRoot>
</template>
import type { Component } from "vue"

export { default as EntitySelect } from "./EntitySelect.vue"

/** What the list is drawn from. Mentionable mixes members and roles. */
export type EntityKind = "user" | "role" | "channel" | "mentionable"

export interface EntityOption {
  value: string
  label: string
  /**
   * A member's avatar, or the glyph a role or channel is known by. The Vue
   * analogue of React's node: pass a component, and the control sizes it.
   */
  icon?: Component
  /** Members carrying this role — shown beside the name, as the client does. */
  count?: number
  /** A short tag after the name. An app is the one that always has one. */
  badge?: string
  /** A role's colour, which its name takes in the list and in its chip. */
  color?: string
  disabled?: boolean
}

/**
 * What one of these may carry. The platform takes at most this many either
 * way — as the list it offers, and as the selection it accepts back.
 */
export const ENTITY_SELECT_MAX = 25

/** The line shown when the server has nothing of this kind to offer. */
export const entitySelectEmpty = {
  user: "No members to show",
  role: "No roles to show",
  channel: "No channels to show",
  mentionable: "Nothing to show",
} as const satisfies Record<EntityKind, string>

Update the import paths

The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Entity Select to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Entity Select to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.

Usage

import { EntitySelect } from "@/components/ui/entity-select"

export function Example() {
  return <EntitySelect />
}
<script setup lang="ts">
import { EntitySelect } from "@/components/ui/entity-select"
</script>

<template>
  <EntitySelect />
</template>

Pass the entities as options. The value is always an array, also when max is one. In React, control it with value and onValueChange, or pass defaultValue. In Vue, bind v-model.

API reference

EntitySelect

PropType
options?EntityOption[]

The entities: `{ value, label, icon?, count?, badge?, color?, disabled? }`. The list is capped at 25.

TypeEntityOption[]
kind?"user" | "role" | "channel" | "mentionable"

What the list holds. It sets the text shown when `options` is empty.

Type"user" | "role" | "channel" | "mentionable"
Default"user"
value / v-model?string[]

The selected values, when you control them.

Typestring[]
defaultValue?string[]

React only. The values selected on first render.

Typestring[]
Default[]
onValueChange?(value: string[]) => void

React only. Called with the new selection; Vue emits `update:modelValue`.

Type(value: string[]) => void
max?number

How many entities can be chosen, up to 25. A value above one shows the choices as chips.

Typenumber
Default1
placeholder?string

Shown on the trigger while nothing is selected.

Typestring
Default"Make a selection"
label?string

The accessible name of the trigger and the listbox.

Typestring
Default"Select an option"
disabled?boolean

Makes the trigger inert.

Typeboolean
Defaultfalse
container?HTMLElement

Element the panel is portalled into. Pass the enclosing native `<dialog>` or a themed wrapper to keep it there.

TypeHTMLElement
Defaultdocument.body
emptyLabel?string | undefined

Shown when the list is empty. Defaults to a line for each `kind`, such as `No members to show`.

Typestring | undefined

In React, icon is a node. In Vue, icon is a component. The root also accepts the native attributes of a <div>, and a className (class in Vue) that is merged with the defaults.

Accessibility

The trigger is a combobox with aria-haspopup="listbox" and aria-expanded. While the panel is open, aria-controls points at the listbox, which carries aria-multiselectable when max is above one. Both take their name from label. Each option carries aria-selected.

The empty list takes its line from emptyLabel. Translating labels lists every English default.

Focus stays on the trigger while the list is open. aria-activedescendant names the option the keyboard is on. Enter, Space, Up and Down open the list. Up and Down move through the options and skip disabled ones. Home and End jump to the ends. Enter or Space picks the active option. With max at one the list then closes. Escape closes the list, and so does a press outside. Inside a drawer or a modal, Escape closes the list first and the dialog on the next press.

A chip is not a button, because it sits inside the trigger. A pointer removes a chip with a press on it. The keyboard removes the last chip with Backspace while the list is closed. A role’s colour is decoration: the name of the role is always shown as text.

Tokens

Entity Select reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-entity-select.

  • --newt-text-secondary
  • --newt-bg-base
  • --newt-border
  • --newt-radius-md
  • --newt-duration-fast
  • --newt-ease
  • --newt-text-muted
  • --newt-text-link
  • --newt-text-primary
  • --newt-bg-elevated
  • --newt-radius-sm
  • --newt-bg-floating
  • --newt-shadow-elevation-high
  • --newt-bg-active
  • --newt-on-accent
  • --newt-brand