-
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathSelect.tsx
More file actions
86 lines (83 loc) · 2.4 KB
/
Copy pathSelect.tsx
File metadata and controls
86 lines (83 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
Listbox,
ListboxButton,
ListboxOption,
ListboxOptions,
} from '@headlessui/react';
import cn from 'clsx';
import { CheckIcon } from 'nextra/icons';
import type { ReactElement } from 'react';
interface MenuOption {
key: string;
name: ReactElement | string;
}
interface MenuProps {
selected: MenuOption;
onChange: (option: MenuOption) => void;
options: MenuOption[];
title?: string;
className?: string;
}
export function Select({
options,
selected,
onChange,
title,
className,
}: MenuProps): ReactElement {
return (
<Listbox value={selected} onChange={onChange}>
<ListboxButton
title={title}
className={({ hover, open, focus }) =>
cn(
'h-7 rounded-md px-2 text-xs font-medium transition-colors',
open
? 'bg-gray-200 text-gray-900 dark:bg-primary-100/10 dark:text-gray-50'
: hover
? 'bg-gray-100 text-gray-900 dark:bg-primary-100/5 dark:text-gray-50'
: 'text-gray-600 dark:text-gray-400',
focus && 'nextra-focusable',
className,
)
}
>
{selected.name}
</ListboxButton>
<ListboxOptions
as="ul"
transition
anchor={{ to: 'top start', gap: 10 }}
className={({ open }) =>
cn(
'nextra-focus',
open ? 'opacity-100' : 'opacity-0',
'z-20 max-h-64 min-w-[--button-width] rounded-md border border-black/5 bg-[rgb(var(--nextra-bg),.8)] py-1 text-sm shadow-lg backdrop-blur-lg transition-opacity motion-reduce:transition-none dark:border-white/20',
)
}
>
{options.map((option) => (
<ListboxOption
key={option.key}
value={option}
as="li"
className={({ focus }) =>
cn(
focus
? 'bg-primary-50 text-primary-600 dark:bg-primary-500/10'
: 'text-gray-800 dark:text-gray-100',
'cursor-pointer whitespace-nowrap px-3 py-1.5',
'transition-colors',
option.key === selected.key &&
'flex items-center justify-between gap-3',
)
}
>
{option.name}
{option.key === selected.key && <CheckIcon height="16" />}
</ListboxOption>
))}
</ListboxOptions>
</Listbox>
);
}