{
  "name": "select-menu",
  "type": "registry:ui",
  "framework": "react",
  "title": "Select Menu",
  "description": "A Discord-inspired string select: combobox trigger, keyboard-driven listbox and a portalled panel.",
  "dependencies": [
    "radix-ui"
  ],
  "files": [
    {
      "path": "ui/select-menu.tsx",
      "content": "\"use client\"\n\nimport { Select as SelectPrimitive } from \"radix-ui\"\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface SelectMenuOption {\n  value: string\n  label: string\n  /** Secondary line under the label. */\n  description?: string\n  /**\n   * Rendered before the label, in the row and on the trigger once chosen —\n   * what the client's device pickers use to tell a microphone from a monitor\n   * at a glance. Sized by the menu, so pass the glyph and nothing else.\n   */\n  icon?: React.ReactNode\n  /**\n   * A unicode emoji drawn in the place of `icon`, for an option that has no\n   * node to pass. `icon` wins when an option carries both.\n   */\n  emoji?: string\n  /**\n   * A muted aside after the label on the trigger, for what the option resolves\n   * to. It is the first thing allowed to truncate.\n   */\n  note?: string\n  disabled?: boolean\n}\n\n/**\n * What a select attached to a message may carry. The platform rejects a\n * payload with more, so a menu that rendered them would be showing something\n * that cannot exist.\n */\nexport const SELECT_MENU_MAX_OPTIONS = 25\n\n/**\n * Where the menu is: in a message, or in a settings panel. The two are not the\n * same control — a message select caps its list, shows the scrollbar it needs\n * to say the list continues, and marks nothing as chosen, because the choice\n * is submitted rather than kept. A settings select has no cap and keeps a tick\n * against the row that is in force.\n */\nexport type SelectMenuVariant = \"settings\" | \"message\"\n\n/**\n * The side of the trigger the panel opens on. The panel flips to the opposite\n * side when the preferred one has no room.\n */\nexport type SelectMenuSide = \"top\" | \"right\" | \"bottom\" | \"left\"\n\n/** How the panel lines up with the trigger along its side. */\nexport type SelectMenuAlign = \"start\" | \"center\" | \"end\"\n\n/** What a row render prop is told about the option it draws. */\nexport interface SelectMenuItemState {\n  index: number\n  selected: boolean\n}\n\nexport interface SelectMenuProps extends Omit<\n  React.HTMLAttributes<HTMLDivElement>,\n  \"onSelect\" | \"defaultValue\"\n> {\n  options: readonly SelectMenuOption[]\n  variant?: SelectMenuVariant\n  /** Controlled selection. Leave undefined to let the menu own it. */\n  value?: string\n  defaultValue?: string\n  onValueChange?: (value: string) => void\n  placeholder?: string\n  disabled?: boolean\n  /** Accessible name of the trigger and the listbox. */\n  label?: string\n  /**\n   * The element the panel is portalled into. Defaults to `document.body`;\n   * pass the enclosing native `<dialog>` or a themed wrapper to keep it there.\n   */\n  container?: React.ComponentProps<typeof SelectPrimitive.Portal>[\"container\"]\n  /**\n   * The side of the trigger the panel opens on. It flips to the opposite side\n   * when the preferred one has no room.\n   */\n  side?: SelectMenuSide\n  /** How the panel lines up with the trigger along that side. */\n  align?: SelectMenuAlign\n  /** Classes merged onto the trigger button, to size or restyle it. */\n  triggerClassName?: string\n  /** Classes merged onto the panel. */\n  contentClassName?: string\n  /**\n   * Replaces what a row draws: the icon or emoji, the label and the\n   * description. The row itself and its tick stay the menu's. The Vue port\n   * takes the same content through its `#item` slot.\n   */\n  renderItem?: (\n    option: SelectMenuOption,\n    state: SelectMenuItemState\n  ) => React.ReactNode\n  /**\n   * Replaces the text of the description of an option that has one. The Vue\n   * port takes the same content through its `#item-description` slot.\n   */\n  renderItemDescription?: (\n    option: SelectMenuOption,\n    state: SelectMenuItemState\n  ) => React.ReactNode\n}\n\n/*\n * Radix Select owns the behaviour: open state, Escape and outside press, focus\n * moving through the rows and back to the trigger, typeahead, and\n * collision-aware placement. Only the look, the option model and the cap are\n * newt/ui's.\n */\nconst SelectMenu = React.forwardRef<HTMLDivElement, SelectMenuProps>(\n  (\n    {\n      className,\n      options: given,\n      variant = \"settings\",\n      value,\n      defaultValue,\n      onValueChange,\n      placeholder = \"Make a selection\",\n      disabled = false,\n      label = \"Select an option\",\n      container,\n      side = \"bottom\",\n      align = \"start\",\n      triggerClassName,\n      contentClassName,\n      renderItem,\n      renderItemDescription,\n      ...props\n    },\n    ref\n  ) => {\n    /*\n     * The cap is applied here rather than left to the caller: a row the\n     * platform would reject should not be one the keyboard can reach.\n     */\n    const options = React.useMemo(\n      () =>\n        variant === \"message\" ? given.slice(0, SELECT_MENU_MAX_OPTIONS) : given,\n      [given, variant]\n    )\n\n    const id = React.useId()\n\n    /*\n     * The selection is mirrored here because the trigger draws more than the\n     * label of the chosen row: its icon and its note come from the option.\n     */\n    const [uncontrolled, setUncontrolled] = React.useState(defaultValue)\n    const selected = value ?? uncontrolled\n    const selectedOption = options.find((option) => option.value === selected)\n\n    /*\n     * While the list is open, Radix hides everything outside it from assistive\n     * technology, the trigger included. A hidden element that still takes Tab\n     * is what an accessibility audit reports, so the trigger leaves the tab\n     * order until the list closes. Focus is held in the list meanwhile, and it\n     * returns to the trigger by script, which a negative tabindex allows.\n     */\n    const [open, setOpen] = React.useState(false)\n\n    return (\n      <SelectPrimitive.Root\n        /* An empty string is how Radix spells \"nothing chosen\". */\n        value={selected ?? \"\"}\n        onValueChange={(next) => {\n          if (value === undefined) setUncontrolled(next)\n          onValueChange?.(next)\n        }}\n        onOpenChange={setOpen}\n        disabled={disabled}\n      >\n        <div\n          ref={ref}\n          className={cn(\"relative w-full max-w-[400px]\", className)}\n          {...props}\n        >\n          <SelectPrimitive.Trigger\n            data-slot=\"select-menu-trigger\"\n            aria-label={label}\n            tabIndex={open ? -1 : undefined}\n            className={cn(\n              \"group flex min-h-9 w-full cursor-pointer items-center justify-between gap-2 rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input px-2 font-[family-name:var(--newt-font-sans)] text-sm font-medium text-newt-text-primary hover:border-newt-bg-base focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link disabled:cursor-not-allowed disabled:opacity-50\",\n              triggerClassName\n            )}\n          >\n            <span className=\"flex min-w-0 flex-1 items-center gap-2\">\n              {/* An icon wins over an emoji when an option carries both. */}\n              {selectedOption?.icon || selectedOption?.emoji ? (\n                <span\n                  aria-hidden=\"true\"\n                  className=\"flex h-[18px] w-[18px] shrink-0 items-center justify-center text-base leading-none text-newt-text-secondary\"\n                >\n                  {selectedOption.icon || selectedOption.emoji}\n                </span>\n              ) : null}\n              <span\n                className={cn(\n                  \"truncate\",\n                  !selectedOption && \"text-newt-text-muted\"\n                )}\n              >\n                {selectedOption?.label ?? placeholder}\n              </span>\n              {selectedOption?.note ? (\n                <span className=\"truncate font-normal text-newt-text-muted\">\n                  {selectedOption.note}\n                </span>\n              ) : null}\n            </span>\n            {/* Points down when closed, flips when the listbox opens. */}\n            <svg\n              viewBox=\"0 0 24 24\"\n              aria-hidden=\"true\"\n              focusable=\"false\"\n              className=\"h-[18px] w-[18px] shrink-0 group-data-[state=open]:rotate-180\"\n            >\n              <path\n                fill=\"currentColor\"\n                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\"\n              />\n            </svg>\n          </SelectPrimitive.Trigger>\n        </div>\n\n        <SelectPrimitive.Portal container={container}>\n          <SelectPrimitive.Content\n            data-slot=\"select-menu-content\"\n            position=\"popper\"\n            side={side}\n            align={align}\n            sideOffset={4}\n            aria-label={label}\n            className={cn(\n              \"z-[110] w-[var(--radix-select-trigger-width)] rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-floating p-1.5 shadow-elevation-high\",\n              contentClassName\n            )}\n          >\n            <SelectPrimitive.Viewport\n              className={cn(\n                /*\n                 * A message select shows its scrollbar: the list is capped, so\n                 * the bar is what says there is more of it below the fold.\n                 * Radix hides the viewport's scrollbar with an attribute\n                 * selector, so the two rules that bring it back repeat that\n                 * attribute to outrank it.\n                 */\n                variant === \"message\"\n                  ? cn(\n                      \"max-h-[300px]\",\n                      \"[&[data-radix-select-viewport]]:[scrollbar-width:thin] [scrollbar-color:var(--newt-bg-active)_transparent]\",\n                      \"[&[data-radix-select-viewport]::-webkit-scrollbar]:block [&::-webkit-scrollbar]:w-2\",\n                      \"[&::-webkit-scrollbar-track]:bg-transparent\",\n                      \"[&::-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\",\n                      \"[&::-webkit-scrollbar-thumb:hover]:bg-newt-text-muted [&::-webkit-scrollbar-thumb:hover]:bg-clip-padding\"\n                    )\n                  : \"max-h-80\"\n              )}\n            >\n              {options.map((option, index) => {\n                const descriptionId = `${id}-description-${index}`\n                const state = { index, selected: option.value === selected }\n                return (\n                  <SelectPrimitive.Item\n                    key={option.value}\n                    value={option.value}\n                    disabled={option.disabled}\n                    /* Typeahead matches the label, not the description. */\n                    textValue={option.label}\n                    aria-describedby={\n                      option.description && !renderItem\n                        ? descriptionId\n                        : undefined\n                    }\n                    className={cn(\n                      \"group/item flex cursor-pointer select-none items-center gap-2.5 rounded-[var(--newt-radius-sm)] px-2.5 py-2 text-sm text-newt-text-secondary outline-none\",\n                      \"data-[state=checked]:text-newt-text-primary\",\n                      \"data-[highlighted]:bg-newt-brand data-[highlighted]:data-[state=unchecked]:text-newt-on-accent\",\n                      \"data-[disabled]:cursor-not-allowed data-[disabled]:text-newt-text-muted data-[disabled]:opacity-50\"\n                    )}\n                  >\n                    {renderItem ? (\n                      /*\n                       * A row drawn by the caller is the option's name as a\n                       * whole: the text of everything in it, with no\n                       * description id left dangling.\n                       */\n                      <SelectPrimitive.ItemText asChild>\n                        <div className=\"flex min-w-0 flex-1 items-center gap-2.5\">\n                          {renderItem(option, state)}\n                        </div>\n                      </SelectPrimitive.ItemText>\n                    ) : (\n                      <>\n                        {option.icon || option.emoji ? (\n                          <span\n                            aria-hidden=\"true\"\n                            className=\"flex h-[18px] w-[18px] shrink-0 items-center justify-center text-base leading-none text-newt-text-secondary group-data-[highlighted]/item:text-newt-on-accent\"\n                          >\n                            {option.icon || option.emoji}\n                          </span>\n                        ) : null}\n                        <span className=\"flex min-w-0 flex-col\">\n                          <SelectPrimitive.ItemText className=\"truncate\">\n                            {option.label}\n                          </SelectPrimitive.ItemText>\n                          {option.description ? (\n                            <span\n                              id={descriptionId}\n                              className=\"truncate text-xs text-newt-text-muted group-data-[highlighted]/item:text-newt-on-accent\"\n                            >\n                              {renderItemDescription\n                                ? renderItemDescription(option, state)\n                                : option.description}\n                            </span>\n                          ) : null}\n                        </span>\n                      </>\n                    )}\n                    {/*\n                     * Only the settings menu ticks a row. In a message the\n                     * choice is submitted rather than kept, so there is no\n                     * standing selection for a tick to be about.\n                     */}\n                    {variant === \"settings\" ? (\n                      <SelectPrimitive.ItemIndicator\n                        className={cn(\n                          \"ms-auto flex h-[18px] w-[18px] shrink-0 items-center justify-center rounded-full bg-newt-brand text-newt-on-accent\",\n                          // On the highlighted row the disc would dissolve into\n                          // the background, so it swaps with the tick.\n                          \"group-data-[highlighted]/item:bg-newt-on-accent group-data-[highlighted]/item:text-newt-brand\"\n                        )}\n                      >\n                        <svg viewBox=\"0 0 24 24\" className=\"h-3.5 w-3.5\">\n                          <path\n                            fill=\"none\"\n                            stroke=\"currentColor\"\n                            strokeWidth=\"3\"\n                            strokeLinecap=\"round\"\n                            strokeLinejoin=\"round\"\n                            d=\"m6 12.5 4 4 8-9\"\n                          />\n                        </svg>\n                      </SelectPrimitive.ItemIndicator>\n                    ) : null}\n                  </SelectPrimitive.Item>\n                )\n              })}\n            </SelectPrimitive.Viewport>\n          </SelectPrimitive.Content>\n        </SelectPrimitive.Portal>\n      </SelectPrimitive.Root>\n    )\n  }\n)\nSelectMenu.displayName = \"SelectMenu\"\n\nexport { SelectMenu }\n",
      "type": "registry:ui"
    }
  ]
}