monochrome 0.2.0 → 0.4.0

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.
@@ -1,230 +0,0 @@
1
- "use client"
2
-
3
- import { createContext, useContext, useId, useRef } from "react"
4
- import type { BaseProps } from "./shared.js"
5
-
6
- type MenuContextValue = {
7
- id: string
8
- root?: boolean
9
- menubar?: boolean
10
- submenu?: boolean
11
- first?: boolean
12
- }
13
- type MenuPopupContextValue = { claimFirst: () => boolean }
14
-
15
- const MenuContext = createContext<MenuContextValue | null>(null)
16
- const MenuPopupContext = createContext<MenuPopupContextValue | null>(null)
17
-
18
- function useMenuContext() {
19
- const context = useContext(MenuContext)
20
- if (!context) throw new Error("Menu components must be used within Menu.Root")
21
- return context
22
- }
23
-
24
- function useMenuPopupContext() {
25
- const context = useContext(MenuPopupContext)
26
- if (!context) throw new Error("Menu components must be used within Menu.Popover")
27
- return context
28
- }
29
-
30
- function Root({ children, menubar, ...props }: BaseProps & { menubar?: boolean }) {
31
- const id = useId()
32
- return (
33
- <MenuContext.Provider value={{ id, root: true, menubar }}>
34
- <div {...props} id={`mcr:menu:${id}`}>
35
- {children}
36
- </div>
37
- </MenuContext.Provider>
38
- )
39
- }
40
-
41
- function Trigger({ children, ...props }: BaseProps) {
42
- const context = useMenuContext()
43
- return (
44
- <button
45
- {...props}
46
- type="button"
47
- id={`mct:menu:${context.id}`}
48
- aria-controls={`mcc:menu:${context.id}`}
49
- aria-expanded="false"
50
- aria-haspopup="menu"
51
- tabIndex={context.root || context.first ? 0 : -1}
52
- role={context.submenu ? "menuitem" : "button"}
53
- >
54
- {children}
55
- </button>
56
- )
57
- }
58
-
59
- function Popover({ children, ...props }: BaseProps) {
60
- const context = useMenuContext()
61
- const claimed = useRef(false)
62
- claimed.current = false
63
- const inner = (
64
- <MenuPopupContext.Provider
65
- value={{
66
- claimFirst: () => {
67
- if (!claimed.current) {
68
- claimed.current = true
69
- return true
70
- }
71
- return false
72
- },
73
- }}
74
- >
75
- {children}
76
- </MenuPopupContext.Provider>
77
- )
78
- return context.menubar ? (
79
- // biome-ignore lint/a11y/noNoninteractiveElementToInteractiveRole: WAI-ARIA menubar
80
- <ul {...props} role="menubar">
81
- {inner}
82
- </ul>
83
- ) : (
84
- <ul
85
- {...props}
86
- // biome-ignore lint/a11y/noNoninteractiveElementToInteractiveRole: WAI-ARIA menu
87
- role="menu"
88
- id={`mcc:menu:${context.id}`}
89
- aria-labelledby={`mct:menu:${context.id}`}
90
- aria-hidden="true"
91
- popover="manual"
92
- >
93
- {inner}
94
- </ul>
95
- )
96
- }
97
-
98
- function Item({
99
- children,
100
- disabled,
101
- href,
102
- ...props
103
- }: BaseProps & { disabled?: boolean; href?: string }) {
104
- return (
105
- <li role="none">
106
- {disabled ? (
107
- <span {...props} role="menuitem" aria-disabled="true" tabIndex={-1}>
108
- {children}
109
- </span>
110
- ) : href ? (
111
- <a {...props} role="menuitem" href={href} tabIndex={-1}>
112
- {children}
113
- </a>
114
- ) : (
115
- <button {...props} type="button" role="menuitem" tabIndex={-1}>
116
- {children}
117
- </button>
118
- )}
119
- </li>
120
- )
121
- }
122
-
123
- function CheckboxItem({
124
- children,
125
- checked,
126
- disabled,
127
- ...props
128
- }: BaseProps & { checked?: boolean; disabled?: boolean }) {
129
- return (
130
- <li role="none">
131
- {disabled ? (
132
- <span
133
- {...props}
134
- role="menuitemcheckbox"
135
- aria-checked={checked ?? false}
136
- aria-disabled="true"
137
- tabIndex={-1}
138
- >
139
- {children}
140
- </span>
141
- ) : (
142
- <button
143
- {...props}
144
- type="button"
145
- role="menuitemcheckbox"
146
- aria-checked={checked ?? false}
147
- tabIndex={-1}
148
- >
149
- {children}
150
- </button>
151
- )}
152
- </li>
153
- )
154
- }
155
-
156
- function RadioItem({
157
- children,
158
- checked,
159
- disabled,
160
- ...props
161
- }: BaseProps & { checked?: boolean; disabled?: boolean }) {
162
- return (
163
- <li role="none">
164
- {disabled ? (
165
- <span
166
- {...props}
167
- role="menuitemradio"
168
- aria-checked={checked ?? false}
169
- aria-disabled="true"
170
- tabIndex={-1}
171
- >
172
- {children}
173
- </span>
174
- ) : (
175
- <button
176
- {...props}
177
- type="button"
178
- role="menuitemradio"
179
- aria-checked={checked ?? false}
180
- tabIndex={-1}
181
- >
182
- {children}
183
- </button>
184
- )}
185
- </li>
186
- )
187
- }
188
-
189
- function Label({ children, ...props }: BaseProps) {
190
- return (
191
- <li {...props} role="presentation">
192
- {children}
193
- </li>
194
- )
195
- }
196
-
197
- function Separator(props: Omit<BaseProps, "children">) {
198
- // biome-ignore lint/a11y/noNoninteractiveElementToInteractiveRole: WAI-ARIA menu separator
199
- // biome-ignore lint/a11y/useSemanticElements: separator must be <li> inside menu <ul>
200
- // biome-ignore lint/a11y/useFocusableInteractive: non-focusable separator per WAI-ARIA
201
- // biome-ignore lint/a11y/useAriaPropsForRole: static separator, not interactive range
202
- return <li {...props} role="separator" />
203
- }
204
-
205
- function Group({ children, ...props }: BaseProps) {
206
- const parentContext = useMenuContext()
207
- const popupContext = useMenuPopupContext()
208
- const isFirst = popupContext.claimFirst()
209
- const id = useId()
210
- const isFirstInMenubar = isFirst && parentContext.menubar && !parentContext.submenu
211
- return (
212
- <MenuContext.Provider value={{ id, submenu: true, first: isFirstInMenubar }}>
213
- <li {...props} role="none">
214
- {children}
215
- </li>
216
- </MenuContext.Provider>
217
- )
218
- }
219
-
220
- export const Menu = {
221
- Root,
222
- Trigger,
223
- Popover,
224
- Item,
225
- CheckboxItem,
226
- RadioItem,
227
- Label,
228
- Separator,
229
- Group,
230
- }
@@ -1,16 +0,0 @@
1
- "use client"
2
-
3
- import type { HTMLAttributes, ReactNode } from "react"
4
-
5
- export type BaseProps = HTMLAttributes<HTMLElement> & { children: ReactNode }
6
-
7
- export const buildId = (base: string, id?: string) => (id ? `${base}:${id}` : base)
8
-
9
- export const HiddenUntilFound = () => (
10
- <script
11
- // biome-ignore lint/security/noDangerouslySetInnerHtml: SSR hydration
12
- dangerouslySetInnerHTML={{
13
- __html: "document.currentScript.parentNode.setAttribute('hidden','until-found')",
14
- }}
15
- />
16
- )
@@ -1,116 +0,0 @@
1
- "use client"
2
-
3
- import { createContext, isValidElement, useContext, useId } from "react"
4
- import { type BaseProps, buildId, HiddenUntilFound } from "./shared.js"
5
-
6
- type TabsContextValue = {
7
- baseId: string
8
- selected?: string
9
- orientation: "horizontal" | "vertical"
10
- }
11
- const TabsContext = createContext<TabsContextValue | null>(null)
12
-
13
- function useTabsContext() {
14
- const context = useContext(TabsContext)
15
- if (!context) throw new Error("Tabs components must be used within Tabs.Root")
16
- return context
17
- }
18
-
19
- function findFirstValue(node: unknown): string | undefined {
20
- if (!node || typeof node !== "object") return
21
- if (isValidElement(node)) {
22
- const nodeProps = node.props as { value?: string; children?: unknown }
23
- if (nodeProps.value !== undefined) return nodeProps.value
24
- return findFirstValue(nodeProps.children)
25
- }
26
- if (Array.isArray(node))
27
- for (const child of node) {
28
- const value = findFirstValue(child)
29
- if (value !== undefined) return value
30
- }
31
- }
32
-
33
- function Root({
34
- children,
35
- defaultValue,
36
- orientation,
37
- ...props
38
- }: BaseProps & { defaultValue?: string; orientation?: "horizontal" | "vertical" }) {
39
- const baseId = useId()
40
- const dir = orientation ?? "horizontal"
41
- return (
42
- <TabsContext.Provider
43
- value={{ baseId, selected: defaultValue ?? findFirstValue(children), orientation: dir }}
44
- >
45
- <div {...props} data-orientation={dir} id={`mcr:tabs:${baseId}`}>
46
- {children}
47
- </div>
48
- </TabsContext.Provider>
49
- )
50
- }
51
-
52
- function List({ children, ...props }: BaseProps) {
53
- const context = useTabsContext()
54
- return (
55
- <div {...props} role="tablist" aria-orientation={context.orientation}>
56
- {children}
57
- </div>
58
- )
59
- }
60
-
61
- function Tab({
62
- children,
63
- value,
64
- id,
65
- selected,
66
- disabled,
67
- ...props
68
- }: BaseProps & { value?: string; id?: string; selected?: boolean; disabled?: boolean }) {
69
- const context = useTabsContext()
70
- const fullId = buildId(context.baseId, id ?? value)
71
- const isSelected = selected ?? value === context.selected
72
- return (
73
- <button
74
- {...props}
75
- type="button"
76
- role="tab"
77
- id={`mct:tabs:${fullId}`}
78
- aria-selected={isSelected}
79
- aria-controls={`mcc:tabs:${fullId}`}
80
- tabIndex={isSelected ? 0 : -1}
81
- aria-disabled={disabled || undefined}
82
- >
83
- {children}
84
- </button>
85
- )
86
- }
87
-
88
- function Panel({
89
- children,
90
- value,
91
- id,
92
- selected,
93
- focusable = true,
94
- ...props
95
- }: BaseProps & { value?: string; id?: string; selected?: boolean; focusable?: boolean }) {
96
- const context = useTabsContext()
97
- const fullId = buildId(context.baseId, id ?? value)
98
- const isSelected = selected ?? value === context.selected
99
- return (
100
- <div
101
- {...props}
102
- role="tabpanel"
103
- id={`mcc:tabs:${fullId}`}
104
- aria-labelledby={`mct:tabs:${fullId}`}
105
- aria-hidden={!isSelected}
106
- hidden={isSelected ? undefined : true}
107
- tabIndex={focusable ? (isSelected ? 0 : -1) : undefined}
108
- data-orientation={context.orientation}
109
- >
110
- {!isSelected && <HiddenUntilFound />}
111
- {children}
112
- </div>
113
- )
114
- }
115
-
116
- export const Tabs = { Root, List, Tab, Panel }