Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.9 KB
Raw Blame
import MagnifyingGlassIcon from '@icon/system/regular/magnifying-glass.svg';
import XIcon from '@icon/system/regular/x.svg';
import { TextField } from '@kobalte/core/text-field';
import { mergeRefs } from '@solid-primitives/refs';
import { Button, cn, Hotkey, Surface } from '@ui';
import type { JSX, JSXElement } from 'solid-js';
import { Show, splitProps } from 'solid-js';

export type SearchBarProps = Omit<
  JSX.InputHTMLAttributes<HTMLInputElement>,
  'children' | 'class' | 'onInput' | 'size' | 'type' | 'value'
> & {
  label: string;
  value: string;
  onValueChange: (value: string) => void;
  hotkey?: string;
  /**
   * Called after Escape blurs the field without clearing its value. The view
   * can restore keyboard focus to its list.
   */
  onEscape?: () => void;
  /** Replaces the clear action with a close action in collapsible search fields. */
  onClose?: () => void;
  /** Controls shown after the input and before its clear or close action. */
  actions?: JSXElement;
  size?: 'sm' | 'md';
  class?: string;
  inputClass?: string;
};

export function SearchBar(props: SearchBarProps) {
  const [local, inputProps] = splitProps(props, [
    'label',
    'value',
    'onValueChange',
    'hotkey',
    'onEscape',
    'onClose',
    'actions',
    'size',
    'class',
    'inputClass',
    'disabled',
    'id',
    'name',
    'readOnly',
    'ref',
    'required',
  ]);

  let input: HTMLInputElement | undefined;
  const clear = () => {
    local.onValueChange('');
    queueMicrotask(() => input?.focus());
  };

  const onKeyDown = (event: KeyboardEvent) => {
    if (event.key !== 'Escape' || event.defaultPrevented) return;

    event.preventDefault();
    input?.blur();
    local.onEscape?.();
  };

  return (
    <Surface
      depth={2}
      hideBorder
      class={cn(
        'w-full min-w-0 rounded-full border border-edge-button bg-control not-touch:elevated text-ink has-[input:focus-visible]:ring-2 has-[input:focus-visible]:ring-edge-muted',
        local.size === 'sm' ? 'h-8' : 'h-[var(--view-search-height,2.5rem)]',
        local.class
      )}
      data-search-bar=""
    >
      <TextField
        id={local.id}
        name={local.name}
        value={local.value}
        onChange={local.onValueChange}
        disabled={local.disabled}
        readOnly={local.readOnly}
        required={local.required}
        class={cn(
          'group flex size-full min-w-0 items-center',
          local.size === 'sm' ? 'gap-1.5 px-2' : 'gap-2 px-3'
        )}
        onKeyDown={onKeyDown}
      >
        <TextField.Label class="sr-only">{local.label}</TextField.Label>
        <MagnifyingGlassIcon
          aria-hidden="true"
          class={cn(
            'shrink-0 text-ink-extra-muted',
            local.size === 'sm' ? 'size-3.5' : 'size-4'
          )}
        />
        <div class="relative min-w-0 flex-1">
          <TextField.Input
            {...inputProps}
            ref={mergeRefs((element) => (input = element), local.ref)}
            type="search"
            class={cn(
              'w-full min-w-0 border-0 bg-transparent text-sm text-ink outline-none placeholder:text-ink-placeholder focus:outline-none focus:ring-0 [&::-webkit-search-cancel-button]:hidden',
              local.hotkey &&
                inputProps.placeholder &&
                'placeholder:text-transparent group-focus-within:placeholder:text-ink-placeholder',
              local.inputClass
            )}
          />
          <Show when={!local.value && local.hotkey && inputProps.placeholder}>
            <div
              aria-hidden="true"
              class="pointer-events-none absolute inset-0 flex items-center gap-2 overflow-hidden whitespace-nowrap text-sm text-ink-placeholder group-focus-within:hidden"
            >
              <span class="truncate">{inputProps.placeholder}</span>
              <Hotkey
                shortcut={local.hotkey}
                theme="subtle"
                class="shrink-0 touch:hidden"
              />
            </div>
          </Show>
        </div>
        <Show when={!inputProps.placeholder && !local.value && local.hotkey}>
          {(hotkey) => (
            <Hotkey
              shortcut={hotkey()}
              theme="subtle"
              class="shrink-0 group-focus-within:hidden touch:hidden"
            />
          )}
        </Show>
        {local.actions}
        <Show
          when={
            local.onClose || (local.value && !local.disabled && !local.readOnly)
          }
        >
          <Button
            aria-label={local.onClose ? 'Close search' : 'Clear search'}
            tooltip={local.onClose ? 'Close search' : 'Clear search'}
            type="button"
            variant="ghost"
            size="icon-xs"
            onPointerDown={(event) => event.preventDefault()}
            onClick={() => (local.onClose ?? clear)()}
          >
            <XIcon class="size-3.5 touch:size-6 shrink-0 pointer-events-none" />
          </Button>
        </Show>
      </TextField>
    </Surface>
  );
}