|
1 | 1 | import { useDefault } from '@siberiacancode/reactuse'; |
| 2 | +import { ChevronDownIcon, RotateCcwIcon } from 'lucide-react'; |
| 3 | + |
| 4 | +interface ProjectSettings { |
| 5 | + framework: string; |
| 6 | + name: string; |
| 7 | + packageManager: string; |
| 8 | + styling: string; |
| 9 | +} |
| 10 | + |
| 11 | +const DEFAULT_SETTINGS: ProjectSettings = { |
| 12 | + name: 'my-awesome-app', |
| 13 | + framework: 'next', |
| 14 | + packageManager: 'npm', |
| 15 | + styling: 'tailwind' |
| 16 | +}; |
| 17 | + |
| 18 | +const FRAMEWORKS = [ |
| 19 | + { value: 'next', label: 'Next.js' }, |
| 20 | + { value: 'vite', label: 'Vite' }, |
| 21 | + { value: 'remix', label: 'Remix' }, |
| 22 | + { value: 'astro', label: 'Astro' } |
| 23 | +]; |
| 24 | + |
| 25 | +const PACKAGE_MANAGERS = [ |
| 26 | + { value: 'npm', label: 'npm' }, |
| 27 | + { value: 'pnpm', label: 'pnpm' }, |
| 28 | + { value: 'yarn', label: 'yarn' }, |
| 29 | + { value: 'bun', label: 'bun' } |
| 30 | +]; |
| 31 | + |
| 32 | +const STYLING = [ |
| 33 | + { value: 'tailwind', label: 'Tailwind CSS' }, |
| 34 | + { value: 'css-modules', label: 'CSS Modules' }, |
| 35 | + { value: 'styled', label: 'styled-components' }, |
| 36 | + { value: 'vanilla', label: 'Vanilla CSS' } |
| 37 | +]; |
2 | 38 |
|
3 | 39 | const Demo = () => { |
4 | | - const initialUser = { name: 'Dima' }; |
5 | | - const defaultUser = { name: 'Danila' }; |
6 | | - const [user, setUser] = useDefault(initialUser, defaultUser); |
| 40 | + const [settings, setSettings] = useDefault<ProjectSettings>(DEFAULT_SETTINGS, DEFAULT_SETTINGS); |
| 41 | + |
| 42 | + const update = (key: keyof ProjectSettings, value: string) => |
| 43 | + setSettings({ ...settings, [key]: value }); |
| 44 | + |
| 45 | + const isDefault = |
| 46 | + settings.name === DEFAULT_SETTINGS.name && |
| 47 | + settings.framework === DEFAULT_SETTINGS.framework && |
| 48 | + settings.packageManager === DEFAULT_SETTINGS.packageManager && |
| 49 | + settings.styling === DEFAULT_SETTINGS.styling; |
7 | 50 |
|
8 | 51 | return ( |
9 | | - <div> |
10 | | - <p> |
11 | | - User: <code>{user.name}</code> |
12 | | - </p> |
13 | | - <input value={user.name} onChange={(event) => setUser({ name: event.target.value })} /> |
14 | | - <button type='button' onClick={() => setUser(null)}> |
15 | | - Clear user |
16 | | - </button> |
17 | | - </div> |
| 52 | + <section className='flex w-full max-w-md flex-col gap-4 p-4'> |
| 53 | + <div className='flex items-start justify-between gap-4'> |
| 54 | + <div className='flex flex-col gap-1'> |
| 55 | + <h3>Project settings</h3> |
| 56 | + <p className='text-muted-foreground text-sm'>Configure your project the way you like.</p> |
| 57 | + </div> |
| 58 | + |
| 59 | + <button |
| 60 | + aria-label='Reset to defaults' |
| 61 | + className='size-9! p-0!' |
| 62 | + data-variant='outline' |
| 63 | + disabled={isDefault} |
| 64 | + type='button' |
| 65 | + onClick={() => setSettings(null)} |
| 66 | + > |
| 67 | + <RotateCcwIcon className='size-4' /> |
| 68 | + </button> |
| 69 | + </div> |
| 70 | + |
| 71 | + <div className='flex flex-col divide-y rounded-xl border'> |
| 72 | + <div className='flex items-center justify-between gap-4 px-4 py-3'> |
| 73 | + <label className='text-sm font-medium' htmlFor='project-name'> |
| 74 | + Name |
| 75 | + </label> |
| 76 | + <input |
| 77 | + className='max-w-44' |
| 78 | + id='project-name' |
| 79 | + type='text' |
| 80 | + value={settings.name} |
| 81 | + onChange={(event) => update('name', event.target.value)} |
| 82 | + /> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className='flex items-center justify-between gap-4 px-4 py-3'> |
| 86 | + <label className='text-sm font-medium' htmlFor='framework'> |
| 87 | + Framework |
| 88 | + </label> |
| 89 | + <div className='relative'> |
| 90 | + <select |
| 91 | + id='framework' |
| 92 | + value={settings.framework} |
| 93 | + onChange={(event) => update('framework', event.target.value)} |
| 94 | + > |
| 95 | + {FRAMEWORKS.map((option) => ( |
| 96 | + <option key={option.value} value={option.value}> |
| 97 | + {option.label} |
| 98 | + </option> |
| 99 | + ))} |
| 100 | + </select> |
| 101 | + <ChevronDownIcon className='text-muted-foreground pointer-events-none absolute top-1/2 right-2 size-4 -translate-y-1/2' /> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div className='flex items-center justify-between gap-4 px-4 py-3'> |
| 106 | + <label className='text-sm font-medium' htmlFor='package-manager'> |
| 107 | + Package manager |
| 108 | + </label> |
| 109 | + <div className='relative'> |
| 110 | + <select |
| 111 | + id='package-manager' |
| 112 | + value={settings.packageManager} |
| 113 | + onChange={(event) => update('packageManager', event.target.value)} |
| 114 | + > |
| 115 | + {PACKAGE_MANAGERS.map((option) => ( |
| 116 | + <option key={option.value} value={option.value}> |
| 117 | + {option.label} |
| 118 | + </option> |
| 119 | + ))} |
| 120 | + </select> |
| 121 | + <ChevronDownIcon className='text-muted-foreground pointer-events-none absolute top-1/2 right-2 size-4 -translate-y-1/2' /> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + |
| 125 | + <div className='flex items-center justify-between gap-4 px-4 py-3'> |
| 126 | + <label className='text-sm font-medium' htmlFor='styling'> |
| 127 | + Styling |
| 128 | + </label> |
| 129 | + <div className='relative'> |
| 130 | + <select |
| 131 | + id='styling' |
| 132 | + value={settings.styling} |
| 133 | + onChange={(event) => update('styling', event.target.value)} |
| 134 | + > |
| 135 | + {STYLING.map((option) => ( |
| 136 | + <option key={option.value} value={option.value}> |
| 137 | + {option.label} |
| 138 | + </option> |
| 139 | + ))} |
| 140 | + </select> |
| 141 | + <ChevronDownIcon className='text-muted-foreground pointer-events-none absolute top-1/2 right-2 size-4 -translate-y-1/2' /> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + </section> |
18 | 146 | ); |
19 | 147 | }; |
20 | 148 |
|
|
0 commit comments