Create React components with localStorage persistence and keyboard navigation support
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-react-localstorage-keyboard-nav-c10cc660df5c ,按照其中的说明把「react-localstorage-keyboard-nav」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
This skill guides you through creating reusable React components that:
// 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 };
};
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';
settings-keys.ts file to manage localStorage keys