@bwui/react 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/alert.d.ts +8 -0
- package/dist/alert.js +11 -0
- package/dist/badge.d.ts +6 -0
- package/dist/badge.js +5 -0
- package/dist/button.d.ts +16 -0
- package/dist/button.js +12 -0
- package/dist/card.d.ts +9 -0
- package/dist/card.js +20 -0
- package/dist/chrome.d.ts +59 -0
- package/dist/chrome.js +75 -0
- package/dist/cn.d.ts +6 -0
- package/dist/cn.js +8 -0
- package/dist/combobox.d.ts +20 -0
- package/dist/combobox.js +70 -0
- package/dist/command.d.ts +16 -0
- package/dist/command.js +74 -0
- package/dist/data-table.d.ts +17 -0
- package/dist/data-table.js +17 -0
- package/dist/date-picker.d.ts +30 -0
- package/dist/date-picker.js +146 -0
- package/dist/form.d.ts +31 -0
- package/dist/form.js +55 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +17 -0
- package/dist/kbd.d.ts +2 -0
- package/dist/kbd.js +5 -0
- package/dist/layout.d.ts +10 -0
- package/dist/layout.js +23 -0
- package/dist/modal.d.ts +11 -0
- package/dist/modal.js +18 -0
- package/dist/pages.d.ts +42 -0
- package/dist/pages.js +84 -0
- package/dist/toast.d.ts +18 -0
- package/dist/toast.js +24 -0
- package/package.json +43 -3
- package/src/alert.tsx +36 -0
- package/src/badge.tsx +24 -0
- package/src/button.tsx +66 -0
- package/src/card.tsx +34 -0
- package/src/chrome.tsx +263 -0
- package/src/cn.ts +8 -0
- package/src/combobox.tsx +162 -0
- package/src/command.tsx +145 -0
- package/src/data-table.tsx +114 -0
- package/src/date-picker.tsx +357 -0
- package/src/form.tsx +201 -0
- package/src/index.ts +90 -0
- package/src/kbd.tsx +9 -0
- package/src/layout.tsx +44 -0
- package/src/modal.tsx +70 -0
- package/src/pages.tsx +361 -0
- package/src/toast.tsx +84 -0
package/src/badge.tsx
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { cn } from './cn'
|
|
3
|
+
|
|
4
|
+
export type BadgeVariant =
|
|
5
|
+
| 'default'
|
|
6
|
+
| 'secondary'
|
|
7
|
+
| 'outline'
|
|
8
|
+
| 'success'
|
|
9
|
+
| 'warning'
|
|
10
|
+
| 'danger'
|
|
11
|
+
| 'info'
|
|
12
|
+
|
|
13
|
+
export interface BadgeProps extends React.HTMLAttributes<HTMLSpanElement> {
|
|
14
|
+
variant?: BadgeVariant
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function Badge({ className, variant = 'default', ...props }: BadgeProps) {
|
|
18
|
+
return (
|
|
19
|
+
<span
|
|
20
|
+
className={cn('bw-badge', `bw-badge--${variant}`, className)}
|
|
21
|
+
{...props}
|
|
22
|
+
/>
|
|
23
|
+
)
|
|
24
|
+
}
|
package/src/button.tsx
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { cn } from './cn'
|
|
3
|
+
|
|
4
|
+
export type ButtonVariant =
|
|
5
|
+
| 'primary'
|
|
6
|
+
| 'secondary'
|
|
7
|
+
| 'outline'
|
|
8
|
+
| 'ghost'
|
|
9
|
+
| 'danger'
|
|
10
|
+
| 'solid'
|
|
11
|
+
export type ButtonSize = 'sm' | 'md' | 'lg'
|
|
12
|
+
|
|
13
|
+
function buttonClasses(
|
|
14
|
+
variant: ButtonVariant,
|
|
15
|
+
size: ButtonSize,
|
|
16
|
+
className?: string,
|
|
17
|
+
) {
|
|
18
|
+
return cn(
|
|
19
|
+
'bw-btn',
|
|
20
|
+
`bw-btn--${variant}`,
|
|
21
|
+
size !== 'md' && `bw-btn--${size}`,
|
|
22
|
+
className,
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ButtonProps
|
|
27
|
+
extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
28
|
+
variant?: ButtonVariant
|
|
29
|
+
size?: ButtonSize
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
|
33
|
+
function Button(
|
|
34
|
+
{ className, variant = 'primary', size = 'md', type = 'button', ...props },
|
|
35
|
+
ref,
|
|
36
|
+
) {
|
|
37
|
+
return (
|
|
38
|
+
<button
|
|
39
|
+
ref={ref}
|
|
40
|
+
type={type}
|
|
41
|
+
className={buttonClasses(variant, size, className)}
|
|
42
|
+
{...props}
|
|
43
|
+
/>
|
|
44
|
+
)
|
|
45
|
+
},
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
export type ButtonLinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & {
|
|
49
|
+
variant?: ButtonVariant
|
|
50
|
+
size?: ButtonSize
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const ButtonLink = React.forwardRef<HTMLAnchorElement, ButtonLinkProps>(
|
|
54
|
+
function ButtonLink(
|
|
55
|
+
{ className, variant = 'primary', size = 'md', ...props },
|
|
56
|
+
ref,
|
|
57
|
+
) {
|
|
58
|
+
return (
|
|
59
|
+
<a
|
|
60
|
+
ref={ref}
|
|
61
|
+
className={buttonClasses(variant, size, className)}
|
|
62
|
+
{...props}
|
|
63
|
+
/>
|
|
64
|
+
)
|
|
65
|
+
},
|
|
66
|
+
)
|
package/src/card.tsx
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { cn } from './cn'
|
|
3
|
+
|
|
4
|
+
type DivProps = React.HTMLAttributes<HTMLDivElement>
|
|
5
|
+
|
|
6
|
+
export function Card({ className, ...props }: DivProps) {
|
|
7
|
+
return <div className={cn('bw-card', className)} {...props} />
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function CardHeader({ className, ...props }: DivProps) {
|
|
11
|
+
return <div className={cn('bw-card__header', className)} {...props} />
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function CardTitle({
|
|
15
|
+
className,
|
|
16
|
+
...props
|
|
17
|
+
}: React.HTMLAttributes<HTMLHeadingElement>) {
|
|
18
|
+
return <h3 className={cn('bw-card__title', className)} {...props} />
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function CardDescription({
|
|
22
|
+
className,
|
|
23
|
+
...props
|
|
24
|
+
}: React.HTMLAttributes<HTMLParagraphElement>) {
|
|
25
|
+
return <p className={cn('bw-card__description', className)} {...props} />
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function CardBody({ className, ...props }: DivProps) {
|
|
29
|
+
return <div className={cn('bw-card__body', className)} {...props} />
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function CardFooter({ className, ...props }: DivProps) {
|
|
33
|
+
return <div className={cn('bw-card__footer', className)} {...props} />
|
|
34
|
+
}
|
package/src/chrome.tsx
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { cn } from './cn'
|
|
5
|
+
|
|
6
|
+
const TabsContext = React.createContext<{
|
|
7
|
+
value: string
|
|
8
|
+
onValueChange: (value: string) => void
|
|
9
|
+
} | null>(null)
|
|
10
|
+
|
|
11
|
+
function useTabs() {
|
|
12
|
+
const ctx = React.useContext(TabsContext)
|
|
13
|
+
if (!ctx) throw new Error('Tabs parts must be used within <Tabs>')
|
|
14
|
+
return ctx
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function Tabs({
|
|
18
|
+
value,
|
|
19
|
+
onValueChange,
|
|
20
|
+
className,
|
|
21
|
+
children,
|
|
22
|
+
}: {
|
|
23
|
+
value: string
|
|
24
|
+
onValueChange: (value: string) => void
|
|
25
|
+
className?: string
|
|
26
|
+
children: React.ReactNode
|
|
27
|
+
}) {
|
|
28
|
+
return (
|
|
29
|
+
<TabsContext.Provider value={{ value, onValueChange }}>
|
|
30
|
+
<div className={cn('bw-tabs', className)} data-bw-tabs="">
|
|
31
|
+
{children}
|
|
32
|
+
</div>
|
|
33
|
+
</TabsContext.Provider>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function TabList({
|
|
38
|
+
className,
|
|
39
|
+
children,
|
|
40
|
+
}: {
|
|
41
|
+
className?: string
|
|
42
|
+
children: React.ReactNode
|
|
43
|
+
}) {
|
|
44
|
+
return (
|
|
45
|
+
<div className={cn('bw-tabs__list', className)} role="tablist">
|
|
46
|
+
{children}
|
|
47
|
+
</div>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function Tab({
|
|
52
|
+
value,
|
|
53
|
+
className,
|
|
54
|
+
children,
|
|
55
|
+
}: {
|
|
56
|
+
value: string
|
|
57
|
+
className?: string
|
|
58
|
+
children: React.ReactNode
|
|
59
|
+
}) {
|
|
60
|
+
const tabs = useTabs()
|
|
61
|
+
const selected = tabs.value === value
|
|
62
|
+
return (
|
|
63
|
+
<button
|
|
64
|
+
type="button"
|
|
65
|
+
role="tab"
|
|
66
|
+
className={cn('bw-tabs__tab', selected && 'bw-tabs__tab--active', className)}
|
|
67
|
+
aria-selected={selected}
|
|
68
|
+
{...(selected ? { 'data-bw-active': '' } : {})}
|
|
69
|
+
onClick={() => tabs.onValueChange(value)}
|
|
70
|
+
>
|
|
71
|
+
{children}
|
|
72
|
+
</button>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function TabPanel({
|
|
77
|
+
value,
|
|
78
|
+
className,
|
|
79
|
+
children,
|
|
80
|
+
}: {
|
|
81
|
+
value: string
|
|
82
|
+
className?: string
|
|
83
|
+
children: React.ReactNode
|
|
84
|
+
}) {
|
|
85
|
+
const tabs = useTabs()
|
|
86
|
+
if (tabs.value !== value) return null
|
|
87
|
+
return (
|
|
88
|
+
<div className={cn('bw-tabs__panel', className)} role="tabpanel">
|
|
89
|
+
{children}
|
|
90
|
+
</div>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function ThemeToggle({ className }: { className?: string }) {
|
|
95
|
+
const [theme, setTheme] = React.useState<'light' | 'dark'>('light')
|
|
96
|
+
|
|
97
|
+
React.useEffect(() => {
|
|
98
|
+
const stored =
|
|
99
|
+
(typeof document !== 'undefined' &&
|
|
100
|
+
(document.body.getAttribute('data-theme') as 'light' | 'dark' | null)) ||
|
|
101
|
+
'light'
|
|
102
|
+
setTheme(stored === 'dark' ? 'dark' : 'light')
|
|
103
|
+
}, [])
|
|
104
|
+
|
|
105
|
+
function toggle() {
|
|
106
|
+
const next = theme === 'dark' ? 'light' : 'dark'
|
|
107
|
+
setTheme(next)
|
|
108
|
+
document.body.setAttribute('data-theme', next)
|
|
109
|
+
document.documentElement.setAttribute('data-theme', next)
|
|
110
|
+
try {
|
|
111
|
+
localStorage.setItem('bw-theme', next)
|
|
112
|
+
} catch {
|
|
113
|
+
/* ignore quota / private mode */
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<button
|
|
119
|
+
type="button"
|
|
120
|
+
className={cn('bw-btn', 'bw-btn--ghost', className)}
|
|
121
|
+
data-bw-theme-toggle=""
|
|
122
|
+
aria-pressed={theme === 'dark'}
|
|
123
|
+
onClick={toggle}
|
|
124
|
+
>
|
|
125
|
+
{theme === 'dark' ? 'Light' : 'Dark'}
|
|
126
|
+
</button>
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function EmptyState({
|
|
131
|
+
title,
|
|
132
|
+
children,
|
|
133
|
+
className,
|
|
134
|
+
}: {
|
|
135
|
+
title: string
|
|
136
|
+
children?: React.ReactNode
|
|
137
|
+
className?: string
|
|
138
|
+
}) {
|
|
139
|
+
return (
|
|
140
|
+
<div className={cn('bw-empty', className)}>
|
|
141
|
+
<h2 className="bw-empty__title">{title}</h2>
|
|
142
|
+
{children ? <div className="bw-empty__body">{children}</div> : null}
|
|
143
|
+
</div>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function Spinner({ className }: { className?: string }) {
|
|
148
|
+
return (
|
|
149
|
+
<span className={cn('bw-spinner', className)} role="status" aria-label="Loading" />
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function Progress({
|
|
154
|
+
value,
|
|
155
|
+
className,
|
|
156
|
+
}: {
|
|
157
|
+
value: number
|
|
158
|
+
className?: string
|
|
159
|
+
}) {
|
|
160
|
+
const clamped = Math.max(0, Math.min(100, value))
|
|
161
|
+
return (
|
|
162
|
+
<div
|
|
163
|
+
className={cn('bw-progress', className)}
|
|
164
|
+
role="progressbar"
|
|
165
|
+
aria-valuemin={0}
|
|
166
|
+
aria-valuemax={100}
|
|
167
|
+
aria-valuenow={clamped}
|
|
168
|
+
>
|
|
169
|
+
<div className="bw-progress__bar" style={{ inlineSize: `${clamped}%` }} />
|
|
170
|
+
</div>
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function Skeleton({ className }: { className?: string }) {
|
|
175
|
+
return <div className={cn('bw-skeleton', className)} aria-hidden="true" />
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function Avatar({
|
|
179
|
+
initials,
|
|
180
|
+
src,
|
|
181
|
+
alt = '',
|
|
182
|
+
className,
|
|
183
|
+
}: {
|
|
184
|
+
initials?: string
|
|
185
|
+
src?: string
|
|
186
|
+
alt?: string
|
|
187
|
+
className?: string
|
|
188
|
+
}) {
|
|
189
|
+
return (
|
|
190
|
+
<span className={cn('bw-avatar', className)}>
|
|
191
|
+
{src ? <img src={src} alt={alt} /> : initials}
|
|
192
|
+
</span>
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function Chip({
|
|
197
|
+
className,
|
|
198
|
+
...props
|
|
199
|
+
}: React.HTMLAttributes<HTMLSpanElement>) {
|
|
200
|
+
return <span className={cn('bw-chip', className)} {...props} />
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function Breadcrumbs({
|
|
204
|
+
items,
|
|
205
|
+
className,
|
|
206
|
+
}: {
|
|
207
|
+
items: Array<{ href?: string; label: string }>
|
|
208
|
+
className?: string
|
|
209
|
+
}) {
|
|
210
|
+
return (
|
|
211
|
+
<nav className={cn('bw-breadcrumbs', className)} aria-label="Breadcrumb">
|
|
212
|
+
<ol>
|
|
213
|
+
{items.map((item, i) => (
|
|
214
|
+
<li key={item.label}>
|
|
215
|
+
{item.href && i < items.length - 1 ? (
|
|
216
|
+
<a href={item.href}>{item.label}</a>
|
|
217
|
+
) : (
|
|
218
|
+
<span aria-current={i === items.length - 1 ? 'page' : undefined}>
|
|
219
|
+
{item.label}
|
|
220
|
+
</span>
|
|
221
|
+
)}
|
|
222
|
+
</li>
|
|
223
|
+
))}
|
|
224
|
+
</ol>
|
|
225
|
+
</nav>
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function Pagination({
|
|
230
|
+
page,
|
|
231
|
+
pageCount,
|
|
232
|
+
onPageChange,
|
|
233
|
+
className,
|
|
234
|
+
}: {
|
|
235
|
+
page: number
|
|
236
|
+
pageCount: number
|
|
237
|
+
onPageChange: (page: number) => void
|
|
238
|
+
className?: string
|
|
239
|
+
}) {
|
|
240
|
+
return (
|
|
241
|
+
<nav className={cn('bw-pagination', className)} aria-label="Pagination">
|
|
242
|
+
<button
|
|
243
|
+
type="button"
|
|
244
|
+
className="bw-btn bw-btn--ghost bw-btn--sm"
|
|
245
|
+
disabled={page <= 1}
|
|
246
|
+
onClick={() => onPageChange(page - 1)}
|
|
247
|
+
>
|
|
248
|
+
Previous
|
|
249
|
+
</button>
|
|
250
|
+
<span className="bw-pagination__status">
|
|
251
|
+
{page} of {pageCount}
|
|
252
|
+
</span>
|
|
253
|
+
<button
|
|
254
|
+
type="button"
|
|
255
|
+
className="bw-btn bw-btn--ghost bw-btn--sm"
|
|
256
|
+
disabled={page >= pageCount}
|
|
257
|
+
onClick={() => onPageChange(page + 1)}
|
|
258
|
+
>
|
|
259
|
+
Next
|
|
260
|
+
</button>
|
|
261
|
+
</nav>
|
|
262
|
+
)
|
|
263
|
+
}
|
package/src/cn.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal class-name joiner. The React adapter deliberately avoids a runtime
|
|
3
|
+
* styling dependency (clsx/tailwind-merge) because all styling lives in the
|
|
4
|
+
* Brickwork CSS layer — components only ever toggle bw-* classes.
|
|
5
|
+
*/
|
|
6
|
+
export function cn(...parts: Array<string | false | null | undefined>): string {
|
|
7
|
+
return parts.filter(Boolean).join(' ')
|
|
8
|
+
}
|
package/src/combobox.tsx
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { cn } from './cn'
|
|
5
|
+
|
|
6
|
+
export type ComboboxOption = {
|
|
7
|
+
value: string
|
|
8
|
+
label: string
|
|
9
|
+
disabled?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type ComboboxProps = {
|
|
13
|
+
id?: string
|
|
14
|
+
name?: string
|
|
15
|
+
options: ComboboxOption[]
|
|
16
|
+
value?: string
|
|
17
|
+
defaultValue?: string
|
|
18
|
+
onValueChange?: (value: string) => void
|
|
19
|
+
placeholder?: string
|
|
20
|
+
disabled?: boolean
|
|
21
|
+
required?: boolean
|
|
22
|
+
className?: string
|
|
23
|
+
emptyLabel?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function Combobox({
|
|
27
|
+
id,
|
|
28
|
+
name,
|
|
29
|
+
options,
|
|
30
|
+
value,
|
|
31
|
+
defaultValue = '',
|
|
32
|
+
onValueChange,
|
|
33
|
+
placeholder = 'Search',
|
|
34
|
+
disabled,
|
|
35
|
+
required,
|
|
36
|
+
className,
|
|
37
|
+
emptyLabel = 'No matches',
|
|
38
|
+
}: ComboboxProps) {
|
|
39
|
+
const listboxId = React.useId()
|
|
40
|
+
const [open, setOpen] = React.useState(false)
|
|
41
|
+
const [query, setQuery] = React.useState('')
|
|
42
|
+
const [active, setActive] = React.useState(0)
|
|
43
|
+
const [uncontrolled, setUncontrolled] = React.useState(defaultValue)
|
|
44
|
+
const rootRef = React.useRef<HTMLDivElement>(null)
|
|
45
|
+
const selected = value ?? uncontrolled
|
|
46
|
+
|
|
47
|
+
const selectedOption = options.find((o) => o.value === selected)
|
|
48
|
+
const filtered = React.useMemo(() => {
|
|
49
|
+
const q = query.trim().toLowerCase()
|
|
50
|
+
if (!q) return options
|
|
51
|
+
return options.filter((o) => o.label.toLowerCase().includes(q))
|
|
52
|
+
}, [options, query])
|
|
53
|
+
|
|
54
|
+
React.useEffect(() => {
|
|
55
|
+
if (open) return
|
|
56
|
+
setQuery(selectedOption?.label ?? '')
|
|
57
|
+
}, [open, selectedOption])
|
|
58
|
+
|
|
59
|
+
React.useEffect(() => {
|
|
60
|
+
function onPointer(e: PointerEvent) {
|
|
61
|
+
if (!rootRef.current?.contains(e.target as Node)) setOpen(false)
|
|
62
|
+
}
|
|
63
|
+
document.addEventListener('pointerdown', onPointer)
|
|
64
|
+
return () => document.removeEventListener('pointerdown', onPointer)
|
|
65
|
+
}, [])
|
|
66
|
+
|
|
67
|
+
function commit(next: string) {
|
|
68
|
+
if (value === undefined) setUncontrolled(next)
|
|
69
|
+
onValueChange?.(next)
|
|
70
|
+
const label = options.find((o) => o.value === next)?.label ?? next
|
|
71
|
+
setQuery(label)
|
|
72
|
+
setOpen(false)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<div
|
|
77
|
+
ref={rootRef}
|
|
78
|
+
className={cn('bw-combobox', className)}
|
|
79
|
+
data-bw-combobox=""
|
|
80
|
+
{...(open ? { 'data-bw-open': '' } : {})}
|
|
81
|
+
>
|
|
82
|
+
<select
|
|
83
|
+
className="bw-input bw-combobox__floor"
|
|
84
|
+
id={id}
|
|
85
|
+
name={name}
|
|
86
|
+
value={selected}
|
|
87
|
+
required={required}
|
|
88
|
+
disabled={disabled}
|
|
89
|
+
tabIndex={-1}
|
|
90
|
+
aria-hidden="true"
|
|
91
|
+
hidden
|
|
92
|
+
onChange={(e) => commit(e.target.value)}
|
|
93
|
+
>
|
|
94
|
+
<option value="">Choose</option>
|
|
95
|
+
{options.map((opt) => (
|
|
96
|
+
<option key={opt.value} value={opt.value} disabled={opt.disabled}>
|
|
97
|
+
{opt.label}
|
|
98
|
+
</option>
|
|
99
|
+
))}
|
|
100
|
+
</select>
|
|
101
|
+
<div className="bw-combobox__field">
|
|
102
|
+
<input
|
|
103
|
+
className="bw-input bw-combobox__input"
|
|
104
|
+
role="combobox"
|
|
105
|
+
aria-expanded={open}
|
|
106
|
+
aria-controls={listboxId}
|
|
107
|
+
aria-autocomplete="list"
|
|
108
|
+
autoComplete="off"
|
|
109
|
+
disabled={disabled}
|
|
110
|
+
placeholder={placeholder}
|
|
111
|
+
value={open ? query : (selectedOption?.label ?? query)}
|
|
112
|
+
onChange={(e) => {
|
|
113
|
+
setQuery(e.target.value)
|
|
114
|
+
setOpen(true)
|
|
115
|
+
setActive(0)
|
|
116
|
+
}}
|
|
117
|
+
onFocus={() => setOpen(true)}
|
|
118
|
+
onKeyDown={(e) => {
|
|
119
|
+
if (e.key === 'ArrowDown') {
|
|
120
|
+
e.preventDefault()
|
|
121
|
+
setOpen(true)
|
|
122
|
+
setActive((i) => Math.min(i + 1, Math.max(filtered.length - 1, 0)))
|
|
123
|
+
} else if (e.key === 'ArrowUp') {
|
|
124
|
+
e.preventDefault()
|
|
125
|
+
setActive((i) => Math.max(i - 1, 0))
|
|
126
|
+
} else if (e.key === 'Enter') {
|
|
127
|
+
e.preventDefault()
|
|
128
|
+
const opt = filtered[active]
|
|
129
|
+
if (opt && !opt.disabled) commit(opt.value)
|
|
130
|
+
} else if (e.key === 'Escape') {
|
|
131
|
+
e.preventDefault()
|
|
132
|
+
setOpen(false)
|
|
133
|
+
}
|
|
134
|
+
}}
|
|
135
|
+
/>
|
|
136
|
+
<ul className="bw-combobox__listbox" role="listbox" id={listboxId}>
|
|
137
|
+
{filtered.map((opt, idx) => (
|
|
138
|
+
<li
|
|
139
|
+
key={opt.value}
|
|
140
|
+
className="bw-combobox__option"
|
|
141
|
+
role="option"
|
|
142
|
+
data-bw-value={opt.value}
|
|
143
|
+
aria-selected={opt.value === selected}
|
|
144
|
+
aria-disabled={opt.disabled || undefined}
|
|
145
|
+
{...(idx === active ? { 'data-bw-active': '' } : {})}
|
|
146
|
+
onMouseMove={() => setActive(idx)}
|
|
147
|
+
onMouseDown={(e) => {
|
|
148
|
+
e.preventDefault()
|
|
149
|
+
if (!opt.disabled) commit(opt.value)
|
|
150
|
+
}}
|
|
151
|
+
>
|
|
152
|
+
{opt.label}
|
|
153
|
+
</li>
|
|
154
|
+
))}
|
|
155
|
+
<li className="bw-combobox__empty" hidden={filtered.length > 0}>
|
|
156
|
+
{emptyLabel}
|
|
157
|
+
</li>
|
|
158
|
+
</ul>
|
|
159
|
+
</div>
|
|
160
|
+
</div>
|
|
161
|
+
)
|
|
162
|
+
}
|
package/src/command.tsx
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { cn } from './cn'
|
|
5
|
+
|
|
6
|
+
export type CommandItem = {
|
|
7
|
+
id: string
|
|
8
|
+
label: string
|
|
9
|
+
group?: string
|
|
10
|
+
href?: string
|
|
11
|
+
onSelect?: () => void
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type CommandPaletteProps = {
|
|
15
|
+
open: boolean
|
|
16
|
+
onOpenChange: (open: boolean) => void
|
|
17
|
+
items: CommandItem[]
|
|
18
|
+
placeholder?: string
|
|
19
|
+
className?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function CommandPalette({
|
|
23
|
+
open,
|
|
24
|
+
onOpenChange,
|
|
25
|
+
items,
|
|
26
|
+
placeholder = 'Type a command or search...',
|
|
27
|
+
className,
|
|
28
|
+
}: CommandPaletteProps) {
|
|
29
|
+
const [query, setQuery] = React.useState('')
|
|
30
|
+
const [active, setActive] = React.useState(0)
|
|
31
|
+
const inputRef = React.useRef<HTMLInputElement>(null)
|
|
32
|
+
|
|
33
|
+
const filtered = React.useMemo(() => {
|
|
34
|
+
const q = query.trim().toLowerCase()
|
|
35
|
+
if (!q) return items
|
|
36
|
+
return items.filter((i) => i.label.toLowerCase().includes(q))
|
|
37
|
+
}, [items, query])
|
|
38
|
+
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
if (open) {
|
|
41
|
+
setQuery('')
|
|
42
|
+
setActive(0)
|
|
43
|
+
queueMicrotask(() => inputRef.current?.focus())
|
|
44
|
+
}
|
|
45
|
+
}, [open])
|
|
46
|
+
|
|
47
|
+
React.useEffect(() => {
|
|
48
|
+
const onKey = (e: KeyboardEvent) => {
|
|
49
|
+
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
|
|
50
|
+
e.preventDefault()
|
|
51
|
+
onOpenChange(!open)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
document.addEventListener('keydown', onKey)
|
|
55
|
+
return () => document.removeEventListener('keydown', onKey)
|
|
56
|
+
}, [open, onOpenChange])
|
|
57
|
+
|
|
58
|
+
function run(item: CommandItem) {
|
|
59
|
+
onOpenChange(false)
|
|
60
|
+
item.onSelect?.()
|
|
61
|
+
if (item.href) window.location.href = item.href
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const groups = React.useMemo(() => {
|
|
65
|
+
const map = new Map<string, CommandItem[]>()
|
|
66
|
+
for (const item of filtered) {
|
|
67
|
+
const g = item.group || 'Commands'
|
|
68
|
+
if (!map.has(g)) map.set(g, [])
|
|
69
|
+
map.get(g)!.push(item)
|
|
70
|
+
}
|
|
71
|
+
return map
|
|
72
|
+
}, [filtered])
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div
|
|
76
|
+
className={cn('bw-cmdk', className)}
|
|
77
|
+
{...(open ? { 'data-bw-open': '' } : {})}
|
|
78
|
+
role="dialog"
|
|
79
|
+
aria-modal="true"
|
|
80
|
+
aria-label="Command palette"
|
|
81
|
+
aria-hidden={!open}
|
|
82
|
+
>
|
|
83
|
+
<div className="bw-cmdk__scrim" onClick={() => onOpenChange(false)} />
|
|
84
|
+
<div className="bw-cmdk__panel">
|
|
85
|
+
<div className="bw-cmdk__search">
|
|
86
|
+
<input
|
|
87
|
+
ref={inputRef}
|
|
88
|
+
className="bw-cmdk__input"
|
|
89
|
+
type="search"
|
|
90
|
+
placeholder={placeholder}
|
|
91
|
+
aria-label="Command search"
|
|
92
|
+
value={query}
|
|
93
|
+
onChange={(e) => {
|
|
94
|
+
setQuery(e.target.value)
|
|
95
|
+
setActive(0)
|
|
96
|
+
}}
|
|
97
|
+
onKeyDown={(e) => {
|
|
98
|
+
if (e.key === 'ArrowDown') {
|
|
99
|
+
e.preventDefault()
|
|
100
|
+
setActive((i) => Math.min(i + 1, filtered.length - 1))
|
|
101
|
+
} else if (e.key === 'ArrowUp') {
|
|
102
|
+
e.preventDefault()
|
|
103
|
+
setActive((i) => Math.max(i - 1, 0))
|
|
104
|
+
} else if (e.key === 'Enter') {
|
|
105
|
+
e.preventDefault()
|
|
106
|
+
const item = filtered[active]
|
|
107
|
+
if (item) run(item)
|
|
108
|
+
} else if (e.key === 'Escape') {
|
|
109
|
+
e.preventDefault()
|
|
110
|
+
onOpenChange(false)
|
|
111
|
+
}
|
|
112
|
+
}}
|
|
113
|
+
/>
|
|
114
|
+
<kbd className="bw-kbd">Esc</kbd>
|
|
115
|
+
</div>
|
|
116
|
+
<ul className="bw-cmdk__list" role="listbox">
|
|
117
|
+
{filtered.length === 0 ? (
|
|
118
|
+
<li className="bw-cmdk__empty">No results</li>
|
|
119
|
+
) : (
|
|
120
|
+
Array.from(groups.entries()).map(([group, groupItems]) => (
|
|
121
|
+
<React.Fragment key={group}>
|
|
122
|
+
<li className="bw-cmdk__group-label">{group}</li>
|
|
123
|
+
{groupItems.map((item) => {
|
|
124
|
+
const idx = filtered.indexOf(item)
|
|
125
|
+
return (
|
|
126
|
+
<li
|
|
127
|
+
key={item.id}
|
|
128
|
+
className="bw-cmdk__item"
|
|
129
|
+
role="option"
|
|
130
|
+
{...(idx === active ? { 'data-bw-active': '' } : {})}
|
|
131
|
+
onMouseMove={() => setActive(idx)}
|
|
132
|
+
onClick={() => run(item)}
|
|
133
|
+
>
|
|
134
|
+
<span>{item.label}</span>
|
|
135
|
+
</li>
|
|
136
|
+
)
|
|
137
|
+
})}
|
|
138
|
+
</React.Fragment>
|
|
139
|
+
))
|
|
140
|
+
)}
|
|
141
|
+
</ul>
|
|
142
|
+
</div>
|
|
143
|
+
</div>
|
|
144
|
+
)
|
|
145
|
+
}
|