Skip to content

High performance self-hosted photo and video management solution.

12.3 KB
Raw Blame
<script lang="ts" module>
  import { get } from 'svelte/store';

  export type ComboBoxOption = {
    id?: string;
    label: string;
    value: string;
  };

  export const asComboboxOptions = (values: string[]) =>
    values.map((value) => {
      if (value === '') {
        return { label: get(t)('unknown'), value: '' };
      }

      return { label: value, value };
    });

  export const asSelectedOption = (value?: string) => (value === undefined ? undefined : asComboboxOptions([value])[0]);
</script>

<script lang="ts">
  import { focusOutside } from '#lib/actions/focus-outside.ts';
  import { shortcuts } from '#lib/actions/shortcut.ts';
  import { Icon, IconButton, Label } from '@immich/ui';
  import { mdiChevronDown, mdiClose, mdiMagnify } from '@mdi/js';
  import { onMount, tick } from 'svelte';
  import { t } from 'svelte-i18n';
  import type { FormEventHandler } from 'svelte/elements';
  import { fly } from 'svelte/transition';

  interface Props {
    label: string;
    disabled?: boolean;
    hideLabel?: boolean;
    options?: ComboBoxOption[];
    selectedOption?: ComboBoxOption | undefined;
    placeholder?: string;
    /** whether creating new items is allowed. */
    allowCreate?: boolean;
    /** select first matching option on enter key. */
    defaultFirstOption?: boolean;
    onSelect?: (option: ComboBoxOption | undefined) => void;
    forceFocus?: boolean;
  }

  let {
    label,
    hideLabel = false,
    disabled = false,
    options = [],
    selectedOption = $bindable(),
    placeholder = '',
    allowCreate = false,
    defaultFirstOption = false,
    onSelect = () => {},
    forceFocus = false,
  }: Props = $props();

  /** Indicates whether or not the dropdown autocomplete list should be visible. */
  let isOpen = $state(false);
  /** Keeps track of whether the combobox is actively being used. */
  let isActive = $state(false);
  let searchQuery = $derived(selectedOption?.label || '');
  let selectedIndex: number | undefined = $state();
  let optionRefs: HTMLElement[] = $state([]);
  let input = $state<HTMLInputElement>();
  let bounds: DOMRect | undefined = $state();

  const id = $props.id();
  const inputId = `combobox-${id}`;
  const listboxId = `listbox-${id}`;
  /** Buffer distance between the dropdown and top/bottom of the viewport. */
  const dropdownOffset = 15;
  /** Minimum space required for the dropdown to be displayed at the bottom of the input. */
  const bottomBreakpoint = 225;
  const observer = new IntersectionObserver(
    (entries) => {
      const inputEntry = entries[0];
      if (inputEntry.intersectionRatio < 1) {
        isOpen = false;
      }
    },
    { threshold: 0.5 },
  );

  onMount(() => {
    if (!input) {
      return;
    }
    observer.observe(input);
    const scrollableAncestor = input?.closest('.overflow-y-auto, .overflow-y-scroll');
    scrollableAncestor?.addEventListener('scroll', onPositionChange, { passive: true });
    window.visualViewport?.addEventListener('resize', onPositionChange, { passive: true });
    window.visualViewport?.addEventListener('scroll', onPositionChange, { passive: true });

    return () => {
      observer.disconnect();
      scrollableAncestor?.removeEventListener('scroll', onPositionChange);
      window.visualViewport?.removeEventListener('resize', onPositionChange);
      window.visualViewport?.removeEventListener('scroll', onPositionChange);
    };
  });

  const forceFocusInput = (el: HTMLDivElement) => {
    if (forceFocus) {
      el.focus();
    }
  };

  const activate = () => {
    isActive = true;
    searchQuery = '';
    openDropdown();
  };

  const deactivate = () => {
    searchQuery = selectedOption ? selectedOption.label : '';
    isActive = false;
    closeDropdown();
  };

  const openDropdown = () => {
    isOpen = true;
    bounds = getInputPosition();
  };

  const closeDropdown = () => {
    isOpen = false;
    selectedIndex = undefined;
  };

  const incrementSelectedIndex = async (increment: number) => {
    if (filteredOptions.length === 0) {
      selectedIndex = 0;
    } else if (selectedIndex === undefined) {
      selectedIndex = increment === 1 ? 0 : filteredOptions.length - 1;
    } else {
      selectedIndex = (selectedIndex + increment + filteredOptions.length) % filteredOptions.length;
    }
    await tick();
    optionRefs[selectedIndex]?.scrollIntoView({ block: 'nearest' });
  };

  const onInput: FormEventHandler<HTMLInputElement> = (event) => {
    openDropdown();
    searchQuery = event.currentTarget.value;
    selectedIndex = defaultFirstOption ? 0 : undefined;
    optionRefs[0]?.scrollIntoView({ block: 'nearest' });
  };

  let handleSelect = (option: ComboBoxOption) => {
    selectedOption = option;
    searchQuery = option.label;
    onSelect(option);
    closeDropdown();
  };

  const onClear = () => {
    input?.focus();
    selectedIndex = undefined;
    selectedOption = undefined;
    searchQuery = '';
    onSelect(selectedOption);
  };

  // TODO: move this combobox component into @immich/ui
  // Bits UI dialogs use `contain: layout` so fixed descendants are positioned in dialog space
  const getModalBounds = () => {
    const modalRoot = input?.closest('[data-dialog-content]');
    if (!modalRoot || !getComputedStyle(modalRoot).contain.includes('layout')) {
      return;
    }

    return modalRoot.getBoundingClientRect();
  };

  const calculatePosition = (boundary: DOMRect | undefined) => {
    const visualViewport = window.visualViewport;

    if (!boundary) {
      return;
    }

    const modalBounds = getModalBounds();
    const offsetTop = modalBounds?.top || 0;
    const offsetLeft = modalBounds?.left || 0;
    const rootHeight = modalBounds?.height || window.innerHeight;

    const top = boundary.top - offsetTop;
    const bottom = boundary.bottom - offsetTop;
    const left = boundary.left - offsetLeft;

    if (dropdownDirection === 'top') {
      return {
        bottom: `${rootHeight - top}px`,
        left: `${left}px`,
        width: `${boundary.width}px`,
        maxHeight: maxHeight(boundary.top - dropdownOffset),
      };
    }

    const viewportHeight = visualViewport?.height || window.innerHeight;
    const availableHeight = viewportHeight - boundary.bottom;
    return {
      top: `${bottom}px`,
      left: `${left}px`,
      width: `${boundary.width}px`,
      maxHeight: maxHeight(availableHeight - dropdownOffset),
    };
  };

  const maxHeight = (size: number) => `min(${Math.max(size, 0)}px,18rem)`;

  const onPositionChange = () => {
    if (!isOpen) {
      return;
    }
    bounds = getInputPosition();
  };

  const getComboboxDirection = (
    boundary: DOMRect | undefined,
    visualViewport: VisualViewport | null,
  ): 'bottom' | 'top' => {
    if (!boundary) {
      return 'bottom';
    }

    const visualHeight = visualViewport?.height || 0;
    const heightBelow = visualHeight - boundary.bottom;
    const heightAbove = boundary.top;

    const isViewportScaled = visualHeight && Math.floor(visualHeight) !== Math.floor(window.innerHeight);

    return heightBelow <= bottomBreakpoint && heightAbove > heightBelow && !isViewportScaled ? 'top' : 'bottom';
  };

  const getInputPosition = () => input?.getBoundingClientRect();

  let filteredOptions = $derived.by(() => {
    const _options = options.filter((option) => option.label.toLowerCase().includes(searchQuery.toLowerCase()));

    if (allowCreate && searchQuery !== '' && _options.filter((option) => option.label === searchQuery).length === 0) {
      _options.unshift({ label: searchQuery, value: searchQuery });
    }

    return _options;
  });
  let position = $derived(calculatePosition(bounds));
  let dropdownDirection: 'bottom' | 'top' = $derived(getComboboxDirection(bounds, visualViewport));
</script>

<svelte:window onresize={onPositionChange} />
{#if !hideLabel}
  <Label class="mb-1 block text-xs font-light text-neutral-500" for={inputId}>{label}</Label>
{/if}
<div
  class="relative w-full text-base text-gray-700 dark:text-gray-300"
  use:focusOutside={{ onFocusOut: deactivate }}
  use:shortcuts={[
    {
      shortcut: { key: 'Escape' },
      onShortcut: (event) => {
        event.stopPropagation();
        closeDropdown();
      },
    },
  ]}
>
  <div>
    {#if isActive}
      <div class="absolute inset-y-0 inset-s-0 flex items-center ps-3">
        <div class="dark:text-immich-dark-fg/75">
          <Icon icon={mdiMagnify} aria-hidden />
        </div>
      </div>
    {/if}

    <input
      {placeholder}
      {disabled}
      aria-activedescendant={selectedIndex || selectedIndex === 0 ? `${listboxId}-${selectedIndex}` : ''}
      aria-autocomplete="list"
      aria-controls={listboxId}
      aria-expanded={isOpen}
      autocomplete="off"
      bind:this={input}
      class:ps-8!={isActive}
      class:rounded-b-none!={isOpen && dropdownDirection === 'bottom'}
      class:rounded-t-none!={isOpen && dropdownDirection === 'top'}
      class:cursor-pointer={!isActive}
      class="immich-form-input w-full pe-12! text-sm transition-all"
      id={inputId}
      onfocus={activate}
      oninput={onInput}
      role="combobox"
      type="text"
      value={searchQuery}
      use:forceFocusInput
      use:shortcuts={[
        {
          shortcut: { key: 'ArrowUp' },
          onShortcut: () => {
            openDropdown();
            void incrementSelectedIndex(-1);
          },
        },
        {
          shortcut: { key: 'ArrowDown' },
          onShortcut: () => {
            openDropdown();
            void incrementSelectedIndex(1);
          },
        },
        {
          shortcut: { key: 'ArrowDown', alt: true },
          onShortcut: () => {
            openDropdown();
          },
        },
        {
          shortcut: { key: 'Enter' },
          onShortcut: () => {
            if (selectedIndex !== undefined && filteredOptions.length > 0) {
              handleSelect(filteredOptions[selectedIndex]);
            }
            closeDropdown();
          },
        },
        {
          shortcut: { key: 'Escape' },
          onShortcut: (event) => {
            if (!isOpen) {
              return;
            }

            event.stopPropagation();
            closeDropdown();
          },
        },
      ]}
    />

    <div
      class="absolute inset-e-0 top-0 flex h-full content-between items-center justify-center px-4"
      class:pe-2={selectedOption}
      class:pointer-events-none={!selectedOption}
    >
      {#if selectedOption}
        <IconButton
          shape="round"
          color="secondary"
          variant="ghost"
          onclick={onClear}
          aria-label={$t('clear_value')}
          icon={mdiClose}
          size="small"
        />
      {:else if !isOpen}
        <Icon icon={mdiChevronDown} aria-hidden />
      {/if}
    </div>
  </div>

  <ul
    role="listbox"
    id={listboxId}
    in:fly={{ duration: 250 }}
    class="fixed z-1 w-full overflow-y-auto border-gray-300 bg-white text-start text-sm dark:border-gray-900 dark:bg-gray-800"
    class:rounded-b-xl={dropdownDirection === 'bottom'}
    class:rounded-t-xl={dropdownDirection === 'top'}
    class:shadow-sm={dropdownDirection === 'bottom'}
    class:border={isOpen}
    style:top={position?.top}
    style:bottom={position?.bottom}
    style:left={position?.left}
    style:width={position?.width}
    style:max-height={position?.maxHeight}
    tabindex="-1"
  >
    {#if isOpen}
      {#if filteredOptions.length === 0}
        <!-- svelte-ignore a11y_click_events_have_key_events -->
        <li
          role="option"
          aria-selected={selectedIndex === 0}
          aria-disabled={true}
          class="w-full cursor-default px-4 py-2 text-start hover:bg-gray-200 aria-selected:bg-gray-200 dark:hover:bg-gray-700 aria-selected:dark:bg-gray-700"
          id={`${listboxId}-0`}
          onclick={closeDropdown}
        >
          {allowCreate ? searchQuery : $t('no_results')}
        </li>
      {/if}
      {#each filteredOptions as option, index (option.id || option.label)}
        <!-- svelte-ignore a11y_click_events_have_key_events -->
        <li
          aria-selected={index === selectedIndex}
          bind:this={optionRefs[index]}
          class="wrap-break-words w-full cursor-pointer px-4 py-2 text-start transition-all hover:bg-gray-200 aria-selected:bg-gray-200 dark:hover:bg-gray-700 aria-selected:dark:bg-gray-700"
          id={`${listboxId}-${index}`}
          onclick={() => handleSelect(option)}
          role="option"
        >
          {option.label}
        </li>
      {/each}
    {/if}
  </ul>
</div>