Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

2.8 KB
Raw Blame
import { useState, useRef, useId } from 'react';
import AppIcon, { iconNames } from './AppIcon';

export default function IconPicker({ value, onChange }) {
  const [isOpen, setIsOpen] = useState(false);

  const triggerRef = useRef(null);
  const choicesRef = useRef(null);
  const choicesId = useId();

  const close = () => {
    if (choicesRef.current?.contains(document.activeElement)) {
      triggerRef.current?.focus();
    }
    setIsOpen(false);
  };

  const handleKeyDown = (event) => {
    if (isOpen && event.key === 'Escape') {
      event.preventDefault();
      event.stopPropagation();
      close();
    }
  };

  return (
    <div className="relative" onKeyDown={handleKeyDown}>
      <span className="block text-sm text-gray-400 mb-1">Icon</span>
      <button
        type="button"
        ref={triggerRef}
        aria-label="Icon picker"
        aria-expanded={isOpen}
        aria-controls={isOpen ? choicesId : undefined}
        onClick={() => isOpen ? close() : setIsOpen(true)}
        className="flex items-center gap-3 w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white hover:border-port-accent/50 focus:border-port-accent focus:outline-hidden transition-colors"
      >
        <div className="w-8 h-8 rounded bg-port-border flex items-center justify-center text-port-accent">
          <AppIcon icon={value || 'package'} size={20} />
        </div>
        <span className="text-sm text-gray-300">{value || 'package'}</span>
        <span className="ml-auto text-gray-500">▼</span>
      </button>

      {isOpen && (
        <>
          <div
            className="fixed inset-0 z-40"
            onClick={close}
            aria-hidden="true"
          />
          <div ref={choicesRef} id={choicesId} className="absolute top-full left-0 mt-1 w-full bg-port-card border border-port-border rounded-lg shadow-xl z-50 p-2 max-h-64 overflow-auto">
            <div className="grid grid-cols-3 gap-1 sm:grid-cols-5">
              {iconNames.map(name => (
                <button
                  key={name}
                  type="button"
                  onClick={() => {
                    onChange(name);
                    close();
                  }}
                  className={`p-2 rounded-lg flex flex-col items-center gap-1 transition-colors ${
                    value === name
                      ? 'bg-port-accent/20 text-port-accent'
                      : 'hover:bg-port-border text-gray-400 hover:text-white'
                  }`}
                  title={name}
                >
                  <AppIcon icon={name} size={20} />
                  <span className="text-[10px] truncate w-full text-center">{name}</span>
                </button>
              ))}
            </div>
          </div>
        </>
      )}
    </div>
  );
}