AGENT SKILL

react-localstorage-keyboard-nav

Create React components with localStorage persistence and keyboard navigation support

使用场景前端开发
License 许可
MIT
所属仓库
HKUDS/AgentPilot
最近更新
2026-11-12
Stars
★ 8.0k
Forks
944

01 前言 Front Matter

name
react-localstorage-keyboard-nav
description
Create React components with localStorage persistence and keyboard navigation support

02 资源文件 1

03 SKILL.md 正文

React Components with localStorage Persistence & Keyboard Navigation

This skill guides you through creating reusable React components that:

  1. Persist state to localStorage
  2. Implement keyboard navigation
  3. Follow modal/command palette patterns

Core Pattern

// 1. Define localStorage hooks
const useLocalStorage = (key: string, initialValue: any) => {
  const [value, setValue] = useState(() => {
    const stored = localStorage.getItem(key);
    return stored ? JSON.parse(stored) : initialValue;
  });

  useEffect(() => {
    localStorage.setItem(key, JSON.stringify(value));
  }, [key, value]);

  return [value, setValue];
};

// 2. Implement keyboard navigation
const useKeyboardNav = (items: any[], onSelect: (item: any) => void) => {
  const [selectedIndex, setSelectedIndex] = useState(0);

  const handleKeyDown = (e: KeyboardEvent) => {
    if (e.key === 'ArrowDown') {
      setSelectedIndex(prev => Math.min(prev + 1, items.length - 1));
    } else if (e.key === 'ArrowUp') {
      setSelectedIndex(prev => Math.max(prev - 1, 0));
    } else if (e.key === 'Enter') {
      onSelect(items[selectedIndex]);
    }
  };

  useEffect(() => {
    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [selectedIndex, items]);

  return { selectedIndex };
};

Implementation Steps

  1. Create Component Scaffolding

    mkdir -p src/components/{Modal,CommandPalette}
    touch src/components/Modal/SettingsModal.tsx
    touch src/components/CommandPalette/CommandPalette.tsx
    
  2. Implement localStorage Persistence

    // SettingsModal.tsx
    const SettingsModal = () => {
      const [settings, setSettings] = useLocalStorage('app-settings', DEFAULT_SETTINGS);
      // ... modal implementation
    };
    
  3. Add Keyboard Navigation

    // CommandPalette.tsx
    const CommandPalette = ({ commands }) => {
      const { selectedIndex } = useKeyboardNav(commands, (cmd) => cmd.action());
      // ... render commands with highlighted selectedIndex
    };
    
  4. Export Components

    // src/components/index.ts
    export * from './Modal/SettingsModal';
    export * from './CommandPalette/CommandPalette';
    

Best Practices

  • Create a settings-keys.ts file to manage localStorage keys
  • Use TypeScript interfaces for settings/commands
  • Implement proper focus management for accessibility

04 同仓库更多 Skills

HKUDS
HKUDS/AgentPilot

agentpilot

Set up AgentPilot and use its single entry to prepare missing inputs and run configured RSI experiments. Use when the user requests AgentPilot installation or work with AgentPilot.

★ 8.0k
HKUDS
HKUDS/AgentPilot

remember

Review auto-memory entries and propose promotions to OPENSPACE.md, OPENSPACE.local.md, or retained auto-memory topics; detect duplicates, stale facts, conflicts, and ambiguous entries before making changes.

★ 8.0k
HKUDS
HKUDS/AgentPilot

skill-discovery

Search reusable OpenSpace skills locally, and browse cloud packages step by step when a cloud skill may help. Reusing proven skills saves tokens, improves reliability, and extends your capabilities beyond built-in tools.

Skill与智能体开发
★ 8.0k
HKUDS
HKUDS/AgentPilot

delegate-task

Delegate tasks to OpenSpace — a full-stack autonomous worker for coding, DevOps, web research, and desktop automation, backed by an extensive MCP tool and skill library. Skills auto-improve through use, reducing token consumption over time. A cloud community lets agents share and collectively evolve reusable skills.

Skill与智能体开发
★ 8.0k
HKUDS
HKUDS/AgentPilot

write-special-chars-ts-check

Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, plus a reliable TypeScript type-checking step that filters output to only the newly-written files to avoid being misled by pre-existing errors.

★ 8.0k
HKUDS
HKUDS/AgentPilot

write-special-chars-readback

Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) using a Python heredoc script, with mandatory post-write verification via read_file and cleanup of /tmp helper scripts.

★ 8.0k