Skip to content
Skillpedia
Websites & UI (frontend)7.8k · #13078

react-localstorage-keyboard-nav

What it does

This skill shows how to create React components that remember their state and work with the keyboard. It covers menus and command palette style lists. It is for developers building React interfaces.

When to use it

  • ✓When you want a React menu or list that can be used with arrow keys and Enter.
  • ✓When you want a component to remember a choice after the page reloads.
  • ✓When you are building a command palette or a search popup in React.

Skill link

https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/react-localstorage-keyboard-nav/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/react-localstorage-keyboard-nav/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“Can you make a React command menu I can use with arrow keys that remembers my last choice?”

5 new skills a week. 1 minute to read.

How it works

A small custom helper saves values in local storage. Another helper listens for arrow keys and Enter, so the user can move through the list and pick an item.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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
FAQ

Does it need a specific React version?

The examples use standard React hooks, so they fit most modern React projects.

Where is the state saved?

It is saved in the browser's local storage on the same device.

Can I use it for a search popup?

Yes. The skill describes modal and command palette patterns that fit a search popup.

Details

Original description by the author

Create React components with localStorage persistence and keyboard navigation support

repo: HKUDS/AgentPilot

Popularity

GitHub stars
7.8k
Ranking position
#13078
License
MIT
Updated
11/1/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills