|
| 1 | +import type { Meta, StoryObj } from '@storybook/react'; |
| 2 | +import { Calendar, Home, Settings, User, FileText, Search, Plus, Bell } from 'lucide-react'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { CommandPalette } from './CommandPalette'; |
| 5 | +import { CommandPaletteProvider } from './CommandPaletteContext'; |
| 6 | +import { useCommandPalette } from './useCommandPalette'; |
| 7 | +import type { CommandGroup } from './types'; |
| 8 | + |
| 9 | +const sampleGroups: CommandGroup[] = [ |
| 10 | + { |
| 11 | + id: 'navigation', |
| 12 | + label: 'Navigation', |
| 13 | + commands: [ |
| 14 | + { id: 'home', label: 'Go to Home', icon: <Home className="w-4 h-4" />, shortcut: ['⌘', 'H'], action: () => alert('Home') }, |
| 15 | + { id: 'events', label: 'Go to Events', icon: <Calendar className="w-4 h-4" />, shortcut: ['⌘', 'E'], action: () => alert('Events') }, |
| 16 | + { id: 'profile', label: 'Go to Profile', icon: <User className="w-4 h-4" />, shortcut: ['⌘', 'P'], action: () => alert('Profile') }, |
| 17 | + ], |
| 18 | + }, |
| 19 | + { |
| 20 | + id: 'actions', |
| 21 | + label: 'Actions', |
| 22 | + commands: [ |
| 23 | + { id: 'new-event', label: 'Create New Event', icon: <Plus className="w-4 h-4" />, shortcut: ['⌘', 'N'], action: () => alert('New Event'), keywords: ['create', 'add'] }, |
| 24 | + { id: 'notifications', label: 'View Notifications', icon: <Bell className="w-4 h-4" />, action: () => alert('Notifications') }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + { |
| 28 | + id: 'settings', |
| 29 | + label: 'Settings', |
| 30 | + commands: [ |
| 31 | + { id: 'settings', label: 'Open Settings', icon: <Settings className="w-4 h-4" />, shortcut: ['⌘', ','], action: () => alert('Settings') }, |
| 32 | + ], |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +const meta: Meta<typeof CommandPalette> = { |
| 37 | + title: 'Design System/Atoms/CommandPalette', |
| 38 | + component: CommandPalette, |
| 39 | + tags: ['autodocs'], |
| 40 | + parameters: { |
| 41 | + layout: 'fullscreen', |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +export default meta; |
| 46 | + |
| 47 | +type Story = StoryObj<typeof CommandPalette>; |
| 48 | + |
| 49 | +const Wrapper = (args: React.ComponentProps<typeof CommandPalette>) => { |
| 50 | + const [isOpen, setIsOpen] = useState(true); |
| 51 | + return ( |
| 52 | + <div> |
| 53 | + <button |
| 54 | + onClick={() => setIsOpen(true)} |
| 55 | + className="px-4 py-2 bg-[var(--color-primary)] text-white rounded-lg text-sm" |
| 56 | + > |
| 57 | + Open Command Palette (⌘K) |
| 58 | + </button> |
| 59 | + <CommandPalette {...args} isOpen={isOpen} onClose={() => setIsOpen(false)} /> |
| 60 | + </div> |
| 61 | + ); |
| 62 | +}; |
| 63 | + |
| 64 | +export const Default: Story = { |
| 65 | + render: () => <Wrapper groups={sampleGroups} />, |
| 66 | +}; |
| 67 | + |
| 68 | +export const WithContext: Story = { |
| 69 | + render: () => ( |
| 70 | + <CommandPaletteProvider initialGroups={sampleGroups}> |
| 71 | + <ContextDemo /> |
| 72 | + </CommandPaletteProvider> |
| 73 | + ), |
| 74 | +}; |
| 75 | + |
| 76 | +function ContextDemo() { |
| 77 | + const { isOpen, open, close } = useCommandPalette(); |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <button |
| 81 | + onClick={open} |
| 82 | + className="px-4 py-2 bg-[var(--color-primary)] text-white rounded-lg text-sm" |
| 83 | + > |
| 84 | + Open Command Palette (⌘K) |
| 85 | + </button> |
| 86 | + <CommandPalette isOpen={isOpen} onClose={close} /> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +export const Empty: Story = { |
| 92 | + render: () => <Wrapper groups={[]} emptyMessage="No commands available." />, |
| 93 | +}; |
| 94 | + |
| 95 | +export const SingleGroup: Story = { |
| 96 | + render: () => ( |
| 97 | + <Wrapper |
| 98 | + groups={[ |
| 99 | + { |
| 100 | + id: 'search', |
| 101 | + label: 'Search', |
| 102 | + commands: [ |
| 103 | + { id: 'search', label: 'Search Events', icon: <Search className="w-4 h-4" />, action: () => alert('Search'), keywords: ['find', 'lookup'] }, |
| 104 | + { id: 'docs', label: 'View Documentation', icon: <FileText className="w-4 h-4" />, action: () => alert('Docs'), keywords: ['help', 'read'] }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + ]} |
| 108 | + /> |
| 109 | + ), |
| 110 | +}; |
0 commit comments