@luxfi/ui 5.6.0 → 6.0.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.
Files changed (125) hide show
  1. package/README.md +109 -0
  2. package/package.json +81 -278
  3. package/dist/accordion.cjs +0 -213
  4. package/dist/accordion.js +0 -186
  5. package/dist/alert.cjs +0 -553
  6. package/dist/alert.js +0 -531
  7. package/dist/avatar.cjs +0 -149
  8. package/dist/avatar.js +0 -125
  9. package/dist/badge.cjs +0 -611
  10. package/dist/badge.js +0 -589
  11. package/dist/button.cjs +0 -689
  12. package/dist/button.js +0 -664
  13. package/dist/checkbox.cjs +0 -265
  14. package/dist/checkbox.js +0 -241
  15. package/dist/close-button.cjs +0 -73
  16. package/dist/close-button.js +0 -51
  17. package/dist/collapsible.cjs +0 -702
  18. package/dist/collapsible.js +0 -679
  19. package/dist/color-mode.cjs +0 -96
  20. package/dist/color-mode.js +0 -72
  21. package/dist/dialog.cjs +0 -279
  22. package/dist/dialog.js +0 -246
  23. package/dist/drawer.cjs +0 -207
  24. package/dist/drawer.js +0 -175
  25. package/dist/empty-state.cjs +0 -93
  26. package/dist/empty-state.js +0 -71
  27. package/dist/field.cjs +0 -183
  28. package/dist/field.js +0 -160
  29. package/dist/heading.cjs +0 -46
  30. package/dist/heading.js +0 -40
  31. package/dist/icon-button.cjs +0 -491
  32. package/dist/icon-button.js +0 -470
  33. package/dist/image.cjs +0 -572
  34. package/dist/image.js +0 -551
  35. package/dist/index.cjs +0 -5779
  36. package/dist/index.js +0 -5619
  37. package/dist/input-group.cjs +0 -155
  38. package/dist/input-group.js +0 -133
  39. package/dist/input.cjs +0 -65
  40. package/dist/input.js +0 -59
  41. package/dist/link.cjs +0 -630
  42. package/dist/link.js +0 -606
  43. package/dist/menu.cjs +0 -305
  44. package/dist/menu.js +0 -269
  45. package/dist/pin-input.cjs +0 -182
  46. package/dist/pin-input.js +0 -160
  47. package/dist/popover.cjs +0 -327
  48. package/dist/popover.js +0 -294
  49. package/dist/progress-circle.cjs +0 -152
  50. package/dist/progress-circle.js +0 -128
  51. package/dist/progress.cjs +0 -117
  52. package/dist/progress.js +0 -94
  53. package/dist/provider.cjs +0 -62
  54. package/dist/provider.js +0 -40
  55. package/dist/radio.cjs +0 -177
  56. package/dist/radio.js +0 -153
  57. package/dist/rating.cjs +0 -80
  58. package/dist/rating.js +0 -58
  59. package/dist/select.cjs +0 -791
  60. package/dist/select.js +0 -757
  61. package/dist/separator.cjs +0 -57
  62. package/dist/separator.js +0 -51
  63. package/dist/skeleton.cjs +0 -370
  64. package/dist/skeleton.js +0 -346
  65. package/dist/slider.cjs +0 -138
  66. package/dist/slider.js +0 -115
  67. package/dist/switch.cjs +0 -163
  68. package/dist/switch.js +0 -140
  69. package/dist/table.cjs +0 -1044
  70. package/dist/table.js +0 -1013
  71. package/dist/tabs.cjs +0 -240
  72. package/dist/tabs.js +0 -213
  73. package/dist/tag.cjs +0 -651
  74. package/dist/tag.js +0 -628
  75. package/dist/textarea.cjs +0 -65
  76. package/dist/textarea.js +0 -59
  77. package/dist/toaster.cjs +0 -99
  78. package/dist/toaster.js +0 -96
  79. package/dist/tooltip.cjs +0 -171
  80. package/dist/tooltip.js +0 -148
  81. package/dist/utils.cjs +0 -11
  82. package/dist/utils.js +0 -9
  83. package/src/accordion.tsx +0 -285
  84. package/src/alert.tsx +0 -221
  85. package/src/avatar.tsx +0 -174
  86. package/src/badge.tsx +0 -158
  87. package/src/button.tsx +0 -411
  88. package/src/checkbox.tsx +0 -307
  89. package/src/close-button.tsx +0 -51
  90. package/src/collapsible.tsx +0 -126
  91. package/src/color-mode.tsx +0 -125
  92. package/src/dialog.tsx +0 -356
  93. package/src/drawer.tsx +0 -186
  94. package/src/empty-state.tsx +0 -97
  95. package/src/field.tsx +0 -202
  96. package/src/heading.tsx +0 -55
  97. package/src/icon-button.tsx +0 -192
  98. package/src/image.tsx +0 -280
  99. package/src/index.ts +0 -192
  100. package/src/input-group.tsx +0 -159
  101. package/src/input.tsx +0 -60
  102. package/src/link.tsx +0 -326
  103. package/src/menu.tsx +0 -471
  104. package/src/pin-input.tsx +0 -187
  105. package/src/popover.tsx +0 -400
  106. package/src/progress-circle.tsx +0 -180
  107. package/src/progress.tsx +0 -109
  108. package/src/provider.tsx +0 -12
  109. package/src/radio.tsx +0 -175
  110. package/src/rating.tsx +0 -79
  111. package/src/select.tsx +0 -696
  112. package/src/separator.tsx +0 -59
  113. package/src/skeleton.tsx +0 -302
  114. package/src/slider.tsx +0 -152
  115. package/src/switch.tsx +0 -158
  116. package/src/table.tsx +0 -621
  117. package/src/tabs.tsx +0 -354
  118. package/src/tag.tsx +0 -159
  119. package/src/textarea.tsx +0 -60
  120. package/src/toaster.tsx +0 -117
  121. package/src/tokens.css +0 -438
  122. package/src/tooltip.tsx +0 -184
  123. package/src/utils/cn.ts +0 -7
  124. package/src/utils.ts +0 -6
  125. package/tokens.css +0 -438
package/src/checkbox.tsx DELETED
@@ -1,307 +0,0 @@
1
- import * as RadixCheckbox from '@radix-ui/react-checkbox';
2
- // chakra() HOC removed — pure Radix + Tailwind
3
- import * as React from 'react';
4
-
5
- import { cn } from './utils';
6
-
7
- const NOOP = () => { /* noop */ };
8
-
9
- // ─── CheckboxGroup context ───────────────────────────────────────────
10
- interface CheckboxGroupContextValue {
11
- value: Array<string>;
12
- toggle: (itemValue: string) => void;
13
- }
14
-
15
- const CheckboxGroupContext = React.createContext<CheckboxGroupContextValue | null>(null);
16
-
17
- function useCheckboxGroupContext(): CheckboxGroupContextValue | null {
18
- return React.useContext(CheckboxGroupContext);
19
- }
20
-
21
- // ─── CheckboxGroup ───────────────────────────────────────────────────
22
- export interface CheckboxGroupProps extends React.ComponentPropsWithoutRef<'div'> {
23
- value?: Array<string>;
24
- defaultValue?: Array<string>;
25
- onValueChange?: (value: Array<string>) => void;
26
- orientation?: 'vertical' | 'horizontal';
27
- name?: string;
28
- disabled?: boolean;
29
- readOnly?: boolean;
30
- }
31
-
32
- const CheckboxGroupBase = React.forwardRef<HTMLDivElement, CheckboxGroupProps>(
33
- function CheckboxGroup(props, ref) {
34
- const {
35
- children,
36
- value: controlledValue,
37
- defaultValue,
38
- onValueChange,
39
- orientation = 'vertical',
40
- name: _name,
41
- className,
42
- ...rest
43
- } = props;
44
-
45
- const [ uncontrolledValue, setUncontrolledValue ] = React.useState<Array<string>>(defaultValue ?? []);
46
-
47
- const isControlled = controlledValue !== undefined;
48
- const value = isControlled ? controlledValue : uncontrolledValue;
49
-
50
- const toggle = React.useCallback(
51
- (itemValue: string) => {
52
- const next = value.includes(itemValue) ?
53
- value.filter((v) => v !== itemValue) :
54
- [ ...value, itemValue ];
55
-
56
- if (!isControlled) {
57
- setUncontrolledValue(next);
58
- }
59
- onValueChange?.(next);
60
- },
61
- [ value, isControlled, onValueChange ],
62
- );
63
-
64
- // Sync uncontrolled state when controlled value changes
65
- React.useEffect(() => {
66
- if (isControlled) {
67
- setUncontrolledValue(controlledValue);
68
- }
69
- }, [ isControlled, controlledValue ]);
70
-
71
- const ctx = React.useMemo(() => ({ value, toggle }), [ value, toggle ]);
72
-
73
- return (
74
- <CheckboxGroupContext.Provider value={ ctx }>
75
- <div
76
- ref={ ref }
77
- role="group"
78
- className={ cn(
79
- 'flex',
80
- orientation === 'vertical' ? 'flex-col gap-3' : 'flex-row gap-8',
81
- className,
82
- ) }
83
- { ...rest }
84
- >
85
- { children }
86
- </div>
87
- </CheckboxGroupContext.Provider>
88
- );
89
- },
90
- );
91
-
92
- export const CheckboxGroup = CheckboxGroupBase;
93
-
94
- // ─── Checkbox ────────────────────────────────────────────────────────
95
- export interface CheckboxProps extends Omit<React.ComponentPropsWithoutRef<'label'>, 'onChange'> {
96
- icon?: React.ReactNode;
97
- inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
98
- rootRef?: React.Ref<HTMLLabelElement>;
99
- checked?: boolean | 'indeterminate';
100
- defaultChecked?: boolean;
101
- onCheckedChange?: (checked: RadixCheckbox.CheckedState) => void;
102
- onChange?: React.FormEventHandler<HTMLLabelElement>;
103
- disabled?: boolean;
104
- readOnly?: boolean;
105
- value?: string;
106
- size?: 'sm' | 'md';
107
- name?: string;
108
- required?: boolean;
109
- }
110
-
111
- const SIZE_CLASSES = {
112
- sm: {
113
- root: 'gap-1.5',
114
- control: 'h-3.5 w-3.5 rounded-sm',
115
- label: 'text-xs',
116
- icon: 'h-3 w-3',
117
- },
118
- md: {
119
- root: 'gap-2',
120
- control: 'h-4 w-4 rounded',
121
- label: 'text-sm',
122
- icon: 'h-3.5 w-3.5',
123
- },
124
- } as const;
125
-
126
- const CheckIcon = ({ className }: { readonly className?: string }) => (
127
- <svg
128
- className={ className }
129
- viewBox="0 0 12 10"
130
- fill="none"
131
- xmlns="http://www.w3.org/2000/svg"
132
- >
133
- <path
134
- d="M10.59 0.59L4 7.17L1.41 4.59L0 6L4 10L12 2L10.59 0.59Z"
135
- fill="currentColor"
136
- />
137
- </svg>
138
- );
139
-
140
- const IndeterminateIcon = ({ className }: { readonly className?: string }) => (
141
- <svg
142
- className={ className }
143
- viewBox="0 0 12 2"
144
- fill="none"
145
- xmlns="http://www.w3.org/2000/svg"
146
- >
147
- <path d="M0 0H12V2H0V0Z" fill="currentColor"/>
148
- </svg>
149
- );
150
-
151
- const CheckboxBase = React.forwardRef<HTMLInputElement, CheckboxProps>(
152
- function Checkbox(props, ref) {
153
- const {
154
- icon,
155
- children,
156
- inputProps,
157
- rootRef,
158
- checked: checkedProp,
159
- defaultChecked,
160
- onCheckedChange,
161
- onChange,
162
- disabled = false,
163
- readOnly = false,
164
- value,
165
- size = 'md',
166
- name,
167
- required,
168
- className,
169
- ...rest
170
- } = props;
171
-
172
- const group = useCheckboxGroupContext();
173
- const hiddenInputRef = React.useRef<HTMLInputElement | null>(null);
174
-
175
- // Merge external ref with internal ref
176
- const setHiddenInputRef = React.useCallback(
177
- (node: HTMLInputElement | null) => {
178
- hiddenInputRef.current = node;
179
- if (typeof ref === 'function') {
180
- ref(node);
181
- } else if (ref) {
182
- (ref as React.MutableRefObject<HTMLInputElement | null>).current = node;
183
- }
184
- },
185
- [ ref ],
186
- );
187
-
188
- // Determine checked state
189
- const isInGroup = group !== null && value !== undefined;
190
- const groupChecked = isInGroup ? group.value.includes(value) : undefined;
191
-
192
- const [ internalChecked, setInternalChecked ] = React.useState<RadixCheckbox.CheckedState>(
193
- defaultChecked ?? false,
194
- );
195
-
196
- const isControlled = checkedProp !== undefined || isInGroup;
197
- let checkedState: RadixCheckbox.CheckedState;
198
- if (checkedProp !== undefined) {
199
- checkedState = checkedProp;
200
- } else if (isInGroup) {
201
- checkedState = groupChecked ?? false;
202
- } else {
203
- checkedState = internalChecked;
204
- }
205
-
206
- const handleCheckedChange = React.useCallback(
207
- (nextChecked: RadixCheckbox.CheckedState) => {
208
- if (readOnly) return;
209
-
210
- if (!isControlled) {
211
- setInternalChecked(nextChecked);
212
- }
213
-
214
- if (isInGroup) {
215
- group.toggle(value);
216
- }
217
-
218
- onCheckedChange?.(nextChecked);
219
-
220
- // Dispatch a native change event from the hidden input so that
221
- // onChange handlers on the label (which listen for bubbling change
222
- // events) continue to work as they did with the Chakra version.
223
- if (onChange && hiddenInputRef.current) {
224
- const input = hiddenInputRef.current;
225
- // Set the checked property to the new state before dispatching
226
- const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
227
- HTMLInputElement.prototype,
228
- 'checked',
229
- )?.set;
230
- nativeInputValueSetter?.call(input, nextChecked === true);
231
- const event = new Event('change', { bubbles: true });
232
- input.dispatchEvent(event);
233
- }
234
- },
235
- [ readOnly, isControlled, isInGroup, group, value, onCheckedChange, onChange ],
236
- );
237
-
238
- const sizeClasses = SIZE_CLASSES[size];
239
-
240
- return (
241
- <label
242
- ref={ rootRef }
243
- className={ cn(
244
- 'inline-flex items-center cursor-pointer select-none',
245
- sizeClasses.root,
246
- disabled && 'opacity-50 cursor-not-allowed',
247
- readOnly && 'cursor-default',
248
- className,
249
- ) }
250
- onChange={ onChange }
251
- data-disabled={ disabled || undefined }
252
- data-readonly={ readOnly || undefined }
253
- { ...rest }
254
- >
255
- <RadixCheckbox.Root
256
- checked={ checkedState }
257
- onCheckedChange={ handleCheckedChange }
258
- disabled={ disabled || readOnly }
259
- name={ name }
260
- value={ value }
261
- required={ required }
262
- className={ cn(
263
- 'inline-flex items-center justify-center shrink-0',
264
- 'border-2 border-current/30',
265
- 'data-[state=checked]:bg-gray-800 data-[state=checked]:text-white',
266
- 'dark:data-[state=checked]:bg-white dark:data-[state=checked]:text-black',
267
- 'data-[state=indeterminate]:bg-gray-800 data-[state=indeterminate]:text-white',
268
- 'dark:data-[state=indeterminate]:bg-white dark:data-[state=indeterminate]:text-black',
269
- 'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-500',
270
- 'transition-colors duration-150',
271
- sizeClasses.control,
272
- ) }
273
- >
274
- { icon ?? (
275
- <RadixCheckbox.Indicator className={ cn('flex items-center justify-center', sizeClasses.icon) }>
276
- { checkedState === 'indeterminate' ? (
277
- <IndeterminateIcon className={ sizeClasses.icon }/>
278
- ) : (
279
- <CheckIcon className={ sizeClasses.icon }/>
280
- ) }
281
- </RadixCheckbox.Indicator>
282
- ) }
283
- </RadixCheckbox.Root>
284
- { /* Hidden native input for form compatibility and ref forwarding */ }
285
- <input
286
- ref={ setHiddenInputRef }
287
- type="checkbox"
288
- className="sr-only"
289
- checked={ checkedState === true }
290
- disabled={ disabled }
291
- readOnly={ readOnly }
292
- name={ name }
293
- value={ value }
294
- tabIndex={ -1 }
295
- aria-hidden
296
- onChange={ NOOP }
297
- { ...inputProps }
298
- />
299
- { children != null && (
300
- <span className={ cn(sizeClasses.label) }>{ children }</span>
301
- ) }
302
- </label>
303
- );
304
- },
305
- );
306
-
307
- export const Checkbox = CheckboxBase;
@@ -1,51 +0,0 @@
1
- import * as React from 'react';
2
-
3
- import { cn } from './utils';
4
-
5
- // eslint-disable-next-line max-len
6
- const CLOSE_ICON_PATH = 'M9.44 8.035a.791.791 0 0 0 1.12 0l3.802-3.803a.791.791 0 0 1 1.119 0l.287.287a.79.79 0 0 1 0 1.119L11.965 9.44a.79.79 0 0 0 0 1.118l3.803 3.803a.791.791 0 0 1 0 1.119l-.287.287a.791.791 0 0 1-1.119 0l-3.803-3.803a.79.79 0 0 0-1.118 0l-3.803 3.803a.79.79 0 0 1-1.119 0l-.287-.287a.791.791 0 0 1 0-1.119l3.803-3.803a.791.791 0 0 0 0-1.118L4.232 5.638a.791.791 0 0 1 0-1.119l.287-.287a.791.791 0 0 1 1.119 0L9.44 8.035Z';
7
-
8
- export interface CloseButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
9
- readonly variant?: 'plain';
10
- readonly size?: 'md';
11
- }
12
-
13
- export const CloseButton = React.forwardRef<
14
- HTMLButtonElement,
15
- CloseButtonProps
16
- >(function CloseButton(props, ref) {
17
- const { variant: _variant, size: _size, className, children, ...rest } = props;
18
-
19
- return (
20
- <button
21
- type="button"
22
- aria-label="Close"
23
- ref={ ref }
24
- className={ cn(
25
- 'inline-flex items-center justify-center',
26
- 'size-5 min-w-0 shrink-0 p-0',
27
- 'rounded-sm border-0 overflow-hidden',
28
- 'bg-transparent text-[var(--closeButton-fg,currentColor)]',
29
- 'hover:bg-transparent hover:text-[var(--hover-color,currentColor)]',
30
- 'disabled:opacity-40',
31
- 'cursor-pointer',
32
- className,
33
- ) }
34
- { ...rest }
35
- >
36
- { children ?? (
37
- <svg
38
- className="size-5"
39
- viewBox="0 0 20 20"
40
- fill="none"
41
- aria-hidden="true"
42
- >
43
- <path
44
- d={ CLOSE_ICON_PATH }
45
- fill="currentColor"
46
- />
47
- </svg>
48
- ) }
49
- </button>
50
- );
51
- });
@@ -1,126 +0,0 @@
1
- import * as Collapsible from '@radix-ui/react-collapsible';
2
- import React from 'react';
3
-
4
- import { cn } from './utils';
5
-
6
- import type { LinkProps } from './link';
7
- import { Link } from './link';
8
-
9
- // Inline useUpdateEffect (effect that skips first mount)
10
- function useUpdateEffect(effect: React.EffectCallback, deps: React.DependencyList): void {
11
- const isFirstMount = React.useRef(true);
12
- React.useEffect(() => {
13
- if (isFirstMount.current) {
14
- isFirstMount.current = false;
15
- return;
16
- }
17
- return effect();
18
- // eslint-disable-next-line react-hooks/exhaustive-deps
19
- }, deps);
20
- }
21
-
22
- // Inline scroll-to helper (replaces react-scroll dependency)
23
- function scrollToElement(id: string): void {
24
- const el = document.getElementById(id);
25
- if (el) {
26
- el.scrollIntoView({ behavior: 'smooth', block: 'start' });
27
- }
28
- }
29
-
30
- export interface CollapsibleDetailsProps extends LinkProps {
31
- children: React.ReactNode;
32
- id?: string;
33
- isExpanded?: boolean;
34
- text?: [string, string];
35
- noScroll?: boolean;
36
- }
37
-
38
- const CUT_ID = 'CollapsibleDetails';
39
-
40
- export const CollapsibleDetails = (props: CollapsibleDetailsProps) => {
41
-
42
- const { children, id = CUT_ID, onClick, isExpanded: isExpandedProp = false, text: textProp, loading, noScroll, ...rest } = props;
43
-
44
- const [ isExpanded, setIsExpanded ] = React.useState(isExpandedProp);
45
-
46
- const handleClick = React.useCallback((event: React.MouseEvent<HTMLAnchorElement>) => {
47
- setIsExpanded((flag) => !flag);
48
- if (!noScroll) {
49
- scrollToElement(id);
50
- }
51
- onClick?.(event);
52
- }, [ id, noScroll, onClick ]);
53
-
54
- useUpdateEffect(() => {
55
- setIsExpanded(isExpandedProp);
56
- if (isExpandedProp && !noScroll) {
57
- scrollToElement(id);
58
- }
59
- }, [ isExpandedProp, id, noScroll ]);
60
-
61
- const text = isExpanded ? (textProp?.[1] ?? 'Hide details') : (textProp?.[0] ?? 'View details');
62
-
63
- return (
64
- <Collapsible.Root open={ isExpanded }>
65
- <Collapsible.Trigger asChild>
66
- <Link
67
- className="text-sm underline decoration-dashed w-fit"
68
- onClick={ handleClick }
69
- loading={ loading }
70
- { ...rest }
71
- >
72
- <span id={ id }>{ text }</span>
73
- </Link>
74
- </Collapsible.Trigger>
75
- <Collapsible.Content>
76
- { children }
77
- </Collapsible.Content>
78
- </Collapsible.Root>
79
- );
80
- };
81
-
82
- interface CollapsibleListProps<T> extends React.ComponentPropsWithoutRef<'div'> {
83
- items: Array<T>;
84
- renderItem: (item: T, index: number) => React.ReactNode;
85
- triggerProps?: LinkProps;
86
- cutLength?: number;
87
- text?: [React.ReactNode, React.ReactNode];
88
- defaultExpanded?: boolean;
89
- }
90
-
91
- export const CollapsibleList = <T,>(props: CollapsibleListProps<T>) => {
92
- const CUT_LENGTH = 3;
93
-
94
- const { items, renderItem, triggerProps, cutLength = CUT_LENGTH, text: textProp, defaultExpanded = false, className, ...rest } = props;
95
-
96
- const [ isExpanded, setIsExpanded ] = React.useState(defaultExpanded);
97
-
98
- React.useEffect(() => {
99
- setIsExpanded(defaultExpanded);
100
- }, [ defaultExpanded ]);
101
-
102
- const handleToggle = React.useCallback(() => {
103
- setIsExpanded((flag) => !flag);
104
- }, []);
105
-
106
- const text = isExpanded ? (textProp?.[1] ?? 'Hide') : (textProp?.[0] ?? 'Show all');
107
-
108
- return (
109
- <Collapsible.Root open={ isExpanded }>
110
- <div className={ cn('flex flex-col w-full', className) } { ...rest }>
111
- { items.slice(0, isExpanded ? undefined : cutLength).map(renderItem) }
112
- { items.length > cutLength && (
113
- <Collapsible.Trigger asChild>
114
- <Link
115
- className="text-sm underline decoration-dashed w-fit min-w-0"
116
- onClick={ handleToggle }
117
- { ...triggerProps }
118
- >
119
- { text }
120
- </Link>
121
- </Collapsible.Trigger>
122
- ) }
123
- </div>
124
- </Collapsible.Root>
125
- );
126
- };
@@ -1,125 +0,0 @@
1
- 'use client';
2
-
3
- import * as React from 'react';
4
-
5
- export type ColorMode = 'light' | 'dark';
6
-
7
- // ---------------------------------------------------------------------------
8
- // Context — framework-agnostic color mode via React context.
9
- // Consumers (Next.js apps, Vite apps, etc.) wrap with their own theme provider
10
- // and pass the resolved color mode + setter into this context.
11
- // ---------------------------------------------------------------------------
12
-
13
- interface ColorModeContextValue {
14
- readonly colorMode: ColorMode;
15
- readonly setColorMode: (mode: string) => void;
16
- readonly toggleColorMode: () => void;
17
- }
18
-
19
- const ColorModeContext = React.createContext<ColorModeContextValue | undefined>(undefined);
20
-
21
- // ---------------------------------------------------------------------------
22
- // Provider
23
- // ---------------------------------------------------------------------------
24
-
25
- export interface ColorModeProviderProps {
26
- readonly children?: React.ReactNode;
27
- /** Initial/default color mode. Defaults to 'light'. */
28
- readonly defaultTheme?: string;
29
- /** Controlled color mode value from an external theme provider. */
30
- readonly value?: string;
31
- /** Called when the color mode changes. */
32
- readonly onValueChange?: (mode: string) => void;
33
- /** Any extra props are accepted and ignored for compat with next-themes ThemeProviderProps. */
34
- readonly [key: string]: unknown;
35
- }
36
-
37
- export function ColorModeProvider(props: ColorModeProviderProps) {
38
- const { children, defaultTheme = 'light', value: controlledValue, onValueChange } = props;
39
-
40
- const [ internalMode, setInternalMode ] = React.useState<ColorMode>(() => {
41
- // SSR-safe: check document.documentElement for existing class
42
- if (controlledValue === 'dark' || controlledValue === 'light') {
43
- return controlledValue;
44
- }
45
- if (typeof document !== 'undefined') {
46
- if (document.documentElement.classList.contains('dark')) return 'dark';
47
- }
48
- return (defaultTheme === 'dark' ? 'dark' : 'light');
49
- });
50
-
51
- const colorMode = (controlledValue === 'dark' || controlledValue === 'light')
52
- ? controlledValue
53
- : internalMode;
54
-
55
- const setColorMode = React.useCallback((mode: string) => {
56
- const resolved = mode === 'dark' ? 'dark' : 'light';
57
- setInternalMode(resolved);
58
- onValueChange?.(resolved);
59
- // Sync class on documentElement
60
- if (typeof document !== 'undefined') {
61
- document.documentElement.classList.toggle('dark', resolved === 'dark');
62
- document.documentElement.classList.toggle('light', resolved === 'light');
63
- }
64
- }, [ onValueChange ]);
65
-
66
- const toggleColorMode = React.useCallback(() => {
67
- setColorMode(colorMode === 'dark' ? 'light' : 'dark');
68
- }, [ colorMode, setColorMode ]);
69
-
70
- const ctx = React.useMemo<ColorModeContextValue>(
71
- () => ({ colorMode, setColorMode, toggleColorMode }),
72
- [ colorMode, setColorMode, toggleColorMode ],
73
- );
74
-
75
- return (
76
- <ColorModeContext.Provider value={ ctx }>
77
- { children }
78
- </ColorModeContext.Provider>
79
- );
80
- }
81
-
82
- // ---------------------------------------------------------------------------
83
- // Hooks
84
- // ---------------------------------------------------------------------------
85
-
86
- export function useColorMode(): ColorModeContextValue {
87
- const ctx = React.useContext(ColorModeContext);
88
- if (!ctx) {
89
- // Fallback: if no provider is mounted, return a noop implementation
90
- // that reads from the document class. This allows components to work
91
- // without an explicit provider (e.g., in Storybook, tests).
92
- // eslint-disable-next-line react-hooks/rules-of-hooks
93
- const [ mode, setMode ] = React.useState<ColorMode>(() => {
94
- if (typeof document !== 'undefined' && document.documentElement.classList.contains('dark')) {
95
- return 'dark';
96
- }
97
- return 'light';
98
- });
99
- return {
100
- colorMode: mode,
101
- setColorMode: (m: string) => {
102
- const resolved = m === 'dark' ? 'dark' : 'light';
103
- setMode(resolved);
104
- if (typeof document !== 'undefined') {
105
- document.documentElement.classList.toggle('dark', resolved === 'dark');
106
- document.documentElement.classList.toggle('light', resolved === 'light');
107
- }
108
- },
109
- toggleColorMode: () => {
110
- const next = mode === 'dark' ? 'light' : 'dark';
111
- setMode(next);
112
- if (typeof document !== 'undefined') {
113
- document.documentElement.classList.toggle('dark', next === 'dark');
114
- document.documentElement.classList.toggle('light', next === 'light');
115
- }
116
- },
117
- };
118
- }
119
- return ctx;
120
- }
121
-
122
- export function useColorModeValue<T>(light: T, dark: T): T {
123
- const { colorMode } = useColorMode();
124
- return colorMode === 'dark' ? dark : light;
125
- }