react-localstorage-keyboard-nav
Create React components with localStorage persistence and keyboard navigation support
- License 许可
- MIT
- 最近更新
- 2026-04-16
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- react-localstorage-keyboard-nav
- description
- Create React components with localStorage persistence and keyboard navigation support
02 资源文件 1
- 其他.skill_id45 B
03 SKILL.md 正文
React Components with localStorage Persistence & Keyboard Navigation
This skill guides you through creating reusable React components that:
- Persist state to localStorage
- Implement keyboard navigation
- 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
-
Create Component Scaffolding
mkdir -p src/components/{Modal,CommandPalette} touch src/components/Modal/SettingsModal.tsx touch src/components/CommandPalette/CommandPalette.tsx -
Implement localStorage Persistence
// SettingsModal.tsx const SettingsModal = () => { const [settings, setSettings] = useLocalStorage('app-settings', DEFAULT_SETTINGS); // ... modal implementation }; -
Add Keyboard Navigation
// CommandPalette.tsx const CommandPalette = ({ commands }) => { const { selectedIndex } = useKeyboardNav(commands, (cmd) => cmd.action()); // ... render commands with highlighted selectedIndex }; -
Export Components
// src/components/index.ts export * from './Modal/SettingsModal'; export * from './CommandPalette/CommandPalette';
Best Practices
- Create a
settings-keys.tsfile to manage localStorage keys - Use TypeScript interfaces for settings/commands
- Implement proper focus management for accessibility
04 同仓库更多 Skills
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.
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.
write-special-chars-heredoc
Workaround for write_file failures caused by special characters (apostrophes, backticks, template literals) by using a Python heredoc script executed via shell.
worldmonitor-reference
Curated architecture index of the WorldMonitor open-source dashboard. Reference for extracting patterns — vanilla TypeScript, Panel class hierarchy, service layer, CSS grid, API edge functions, refresh scheduler, and 60+ data source integrations.
virtual-list
Implement efficient virtual scrolling for rendering large lists with DOM recycling, chunk-based rendering, and performance optimizations
verify-directory-structure
Diagnose and resolve file access issues by first verifying the directory structure.