@badnpm/ui 0.0.0-stage → 0.0.1
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 +193 -2
- package/dist/cjs/components/accordion.cjs +24 -0
- package/dist/cjs/components/alert-dialog.cjs +56 -0
- package/dist/cjs/components/alert.cjs +34 -0
- package/dist/cjs/components/aspect-ratio.cjs +12 -0
- package/dist/cjs/components/attachment.cjs +80 -0
- package/dist/cjs/components/avatar.cjs +31 -0
- package/dist/cjs/components/badge.cjs +38 -0
- package/dist/cjs/components/breadcrumb.cjs +46 -0
- package/dist/cjs/components/bubble.cjs +66 -0
- package/dist/cjs/components/button-group.cjs +43 -0
- package/dist/cjs/components/button.cjs +41 -0
- package/dist/cjs/components/calendar.cjs +100 -0
- package/dist/cjs/components/card.cjs +34 -0
- package/dist/cjs/components/carousel.cjs +113 -0
- package/dist/cjs/components/chart.cjs +160 -0
- package/dist/cjs/components/checkbox.cjs +13 -0
- package/dist/cjs/components/code-block.cjs +110 -0
- package/dist/cjs/components/collapsible.cjs +18 -0
- package/dist/cjs/components/combobox.cjs +96 -0
- package/dist/cjs/components/command.cjs +46 -0
- package/dist/cjs/components/context-menu.cjs +69 -0
- package/dist/cjs/components/dialog.cjs +50 -0
- package/dist/cjs/components/drawer.cjs +108 -0
- package/dist/cjs/components/dropdown-menu.cjs +68 -0
- package/dist/cjs/components/empty.cjs +42 -0
- package/dist/cjs/components/field.cjs +80 -0
- package/dist/cjs/components/hover-card.cjs +20 -0
- package/dist/cjs/components/input-group.cjs +66 -0
- package/dist/cjs/components/input-otp.cjs +46 -0
- package/dist/cjs/components/input.cjs +11 -0
- package/dist/cjs/components/item.cjs +91 -0
- package/dist/cjs/components/kbd.cjs +14 -0
- package/dist/cjs/components/label.cjs +11 -0
- package/dist/cjs/components/marker.cjs +41 -0
- package/dist/cjs/components/menubar.cjs +75 -0
- package/dist/cjs/components/message-scroller.cjs +45 -0
- package/dist/cjs/components/message.cjs +30 -0
- package/dist/cjs/components/native-select.cjs +19 -0
- package/dist/cjs/components/navigation-menu.cjs +43 -0
- package/dist/cjs/components/pagination.cjs +36 -0
- package/dist/cjs/components/popover.cjs +31 -0
- package/dist/cjs/components/progress.cjs +28 -0
- package/dist/cjs/components/questionnaire.cjs +70 -0
- package/dist/cjs/components/radio-group.cjs +16 -0
- package/dist/cjs/components/resizable.cjs +39 -0
- package/dist/cjs/components/scroll-area.cjs +15 -0
- package/dist/cjs/components/select.cjs +47 -0
- package/dist/cjs/components/separator.cjs +11 -0
- package/dist/cjs/components/sheet.cjs +47 -0
- package/dist/cjs/components/sidebar.cjs +301 -0
- package/dist/cjs/components/skeleton.cjs +10 -0
- package/dist/cjs/components/slider.cjs +16 -0
- package/dist/cjs/components/spinner.cjs +11 -0
- package/dist/cjs/components/switch.cjs +12 -0
- package/dist/cjs/components/table.cjs +38 -0
- package/dist/cjs/components/tabs.cjs +37 -0
- package/dist/cjs/components/textarea.cjs +10 -0
- package/dist/cjs/components/theme-toggle.cjs +19 -0
- package/dist/cjs/components/toast.cjs +82 -0
- package/dist/cjs/components/toggle-group.cjs +47 -0
- package/dist/cjs/components/toggle.cjs +30 -0
- package/dist/cjs/components/tooltip.cjs +24 -0
- package/dist/cjs/components/trace.cjs +40 -0
- package/dist/cjs/hooks/use-highlighted-code.cjs +73 -0
- package/dist/cjs/hooks/use-hydrated.cjs +15 -0
- package/dist/cjs/hooks/use-mobile.cjs +36 -0
- package/dist/cjs/index.cjs +497 -0
- package/dist/cjs/lib/highlighter.cjs +96 -0
- package/dist/cjs/lib/preference.cjs +40 -0
- package/dist/cjs/providers/preference-provider.cjs +127 -0
- package/dist/cjs/providers/theme-provider.cjs +181 -0
- package/dist/cjs/types/components/accordion.d.ts +9 -0
- package/dist/cjs/types/components/alert-dialog.d.ts +20 -0
- package/dist/cjs/types/components/alert.d.ts +13 -0
- package/dist/cjs/types/components/aspect-ratio.d.ts +7 -0
- package/dist/cjs/types/components/attachment.d.ts +26 -0
- package/dist/cjs/types/components/avatar.d.ts +13 -0
- package/dist/cjs/types/components/badge.d.ts +11 -0
- package/dist/cjs/types/components/breadcrumb.d.ts +12 -0
- package/dist/cjs/types/components/bubble.d.ts +19 -0
- package/dist/cjs/types/components/button-group.d.ts +14 -0
- package/dist/cjs/types/components/button.d.ts +12 -0
- package/dist/cjs/types/components/calendar.d.ts +12 -0
- package/dist/cjs/types/components/card.d.ts +13 -0
- package/dist/cjs/types/components/carousel.d.ts +31 -0
- package/dist/cjs/types/components/chart.d.ts +47 -0
- package/dist/cjs/types/components/checkbox.d.ts +6 -0
- package/dist/cjs/types/components/code-block.d.ts +29 -0
- package/dist/cjs/types/components/collapsible.d.ts +8 -0
- package/dist/cjs/types/components/combobox.d.ts +26 -0
- package/dist/cjs/types/components/command.d.ts +21 -0
- package/dist/cjs/types/components/context-menu.d.ts +31 -0
- package/dist/cjs/types/components/dialog.d.ts +19 -0
- package/dist/cjs/types/components/direction.d.ts +1 -0
- package/dist/cjs/types/components/drawer.d.ts +18 -0
- package/dist/cjs/types/components/dropdown-menu.d.ts +31 -0
- package/dist/cjs/types/components/empty.d.ts +15 -0
- package/dist/cjs/types/components/field.d.ts +28 -0
- package/dist/cjs/types/components/hover-card.d.ts +8 -0
- package/dist/cjs/types/components/index.d.ts +65 -0
- package/dist/cjs/types/components/input-group.d.ts +21 -0
- package/dist/cjs/types/components/input-otp.d.ts +13 -0
- package/dist/cjs/types/components/input.d.ts +5 -0
- package/dist/cjs/types/components/item.d.ts +25 -0
- package/dist/cjs/types/components/kbd.d.ts +6 -0
- package/dist/cjs/types/components/label.d.ts +5 -0
- package/dist/cjs/types/components/marker.d.ts +13 -0
- package/dist/cjs/types/components/menubar.d.ts +31 -0
- package/dist/cjs/types/components/message-scroller.d.ts +13 -0
- package/dist/cjs/types/components/message.d.ts +12 -0
- package/dist/cjs/types/components/native-select.d.ts +10 -0
- package/dist/cjs/types/components/navigation-menu.d.ts +15 -0
- package/dist/cjs/types/components/pagination.d.ts +19 -0
- package/dist/cjs/types/components/popover.d.ts +11 -0
- package/dist/cjs/types/components/progress.d.ts +10 -0
- package/dist/cjs/types/components/questionnaire.d.ts +21 -0
- package/dist/cjs/types/components/radio-group.d.ts +8 -0
- package/dist/cjs/types/components/resizable.d.ts +10 -0
- package/dist/cjs/types/components/scroll-area.d.ts +7 -0
- package/dist/cjs/types/components/select.d.ts +17 -0
- package/dist/cjs/types/components/separator.d.ts +6 -0
- package/dist/cjs/types/components/sheet.d.ts +16 -0
- package/dist/cjs/types/components/sidebar.d.ts +66 -0
- package/dist/cjs/types/components/skeleton.d.ts +5 -0
- package/dist/cjs/types/components/slider.d.ts +6 -0
- package/dist/cjs/types/components/spinner.d.ts +5 -0
- package/dist/cjs/types/components/switch.d.ts +8 -0
- package/dist/cjs/types/components/table.d.ts +12 -0
- package/dist/cjs/types/components/tabs.d.ts +14 -0
- package/dist/cjs/types/components/textarea.d.ts +5 -0
- package/dist/cjs/types/components/theme-toggle.d.ts +14 -0
- package/dist/cjs/types/components/toast.d.ts +19 -0
- package/dist/cjs/types/components/toggle-group.d.ts +13 -0
- package/dist/cjs/types/components/toggle.d.ts +12 -0
- package/dist/cjs/types/components/tooltip.d.ts +9 -0
- package/dist/cjs/types/components/trace.d.ts +20 -0
- package/dist/cjs/types/hooks/index.d.ts +3 -0
- package/dist/cjs/types/hooks/use-highlighted-code.d.ts +3 -0
- package/dist/cjs/types/hooks/use-hydrated.d.ts +4 -0
- package/dist/cjs/types/hooks/use-mobile.d.ts +3 -0
- package/dist/cjs/types/index.d.ts +73 -0
- package/dist/cjs/types/lib/highlighter.d.ts +11 -0
- package/dist/cjs/types/lib/index.d.ts +3 -0
- package/dist/cjs/types/lib/preference.d.ts +85 -0
- package/dist/cjs/types/lib/utils.d.ts +1 -0
- package/dist/cjs/types/package.json +1 -0
- package/dist/cjs/types/providers/index.d.ts +2 -0
- package/dist/cjs/types/providers/preference-provider.d.ts +20 -0
- package/dist/cjs/types/providers/theme-provider.d.ts +25 -0
- package/dist/esm/components/accordion.js +19 -0
- package/dist/esm/components/alert-dialog.js +43 -0
- package/dist/esm/components/alert.js +29 -0
- package/dist/esm/components/aspect-ratio.js +10 -0
- package/dist/esm/components/attachment.js +70 -0
- package/dist/esm/components/avatar.js +24 -0
- package/dist/esm/components/badge.js +35 -0
- package/dist/esm/components/breadcrumb.js +38 -0
- package/dist/esm/components/bubble.js +61 -0
- package/dist/esm/components/button-group.js +38 -0
- package/dist/esm/components/button.js +38 -0
- package/dist/esm/components/calendar.js +78 -0
- package/dist/esm/components/card.js +26 -0
- package/dist/esm/components/carousel.js +87 -0
- package/dist/esm/components/chart.js +133 -0
- package/dist/esm/components/checkbox.js +11 -0
- package/dist/esm/components/code-block.js +82 -0
- package/dist/esm/components/collapsible.js +14 -0
- package/dist/esm/components/combobox.js +60 -0
- package/dist/esm/components/command.js +36 -0
- package/dist/esm/components/context-menu.js +53 -0
- package/dist/esm/components/dialog.js +39 -0
- package/dist/esm/components/drawer.js +77 -0
- package/dist/esm/components/dropdown-menu.js +52 -0
- package/dist/esm/components/empty.js +35 -0
- package/dist/esm/components/field.js +69 -0
- package/dist/esm/components/hover-card.js +16 -0
- package/dist/esm/components/input-group.js +59 -0
- package/dist/esm/components/input-otp.js +22 -0
- package/dist/esm/components/input.js +9 -0
- package/dist/esm/components/item.js +80 -0
- package/dist/esm/components/kbd.js +11 -0
- package/dist/esm/components/label.js +9 -0
- package/dist/esm/components/marker.js +36 -0
- package/dist/esm/components/menubar.js +58 -0
- package/dist/esm/components/message-scroller.js +27 -0
- package/dist/esm/components/message.js +23 -0
- package/dist/esm/components/native-select.js +15 -0
- package/dist/esm/components/navigation-menu.js +33 -0
- package/dist/esm/components/pagination.js +28 -0
- package/dist/esm/components/popover.js +24 -0
- package/dist/esm/components/progress.js +22 -0
- package/dist/esm/components/questionnaire.js +54 -0
- package/dist/esm/components/radio-group.js +13 -0
- package/dist/esm/components/resizable.js +16 -0
- package/dist/esm/components/scroll-area.js +12 -0
- package/dist/esm/components/select.js +36 -0
- package/dist/esm/components/separator.js +9 -0
- package/dist/esm/components/sheet.js +38 -0
- package/dist/esm/components/sidebar.js +257 -0
- package/dist/esm/components/skeleton.js +8 -0
- package/dist/esm/components/slider.js +14 -0
- package/dist/esm/components/spinner.js +9 -0
- package/dist/esm/components/switch.js +10 -0
- package/dist/esm/components/table.js +29 -0
- package/dist/esm/components/tabs.js +31 -0
- package/dist/esm/components/textarea.js +8 -0
- package/dist/esm/components/theme-toggle.js +17 -0
- package/dist/esm/components/toast.js +68 -0
- package/dist/esm/components/toggle-group.js +25 -0
- package/dist/esm/components/toggle.js +27 -0
- package/dist/esm/components/tooltip.js +19 -0
- package/dist/esm/components/trace.js +31 -0
- package/dist/esm/hooks/use-highlighted-code.js +52 -0
- package/dist/esm/hooks/use-hydrated.js +13 -0
- package/dist/esm/hooks/use-mobile.js +15 -0
- package/dist/esm/index.js +73 -0
- package/dist/esm/lib/highlighter.js +93 -0
- package/dist/esm/lib/preference.js +35 -0
- package/dist/esm/providers/preference-provider.js +124 -0
- package/dist/esm/providers/theme-provider.js +178 -0
- package/dist/styles/custom.css +2 -0
- package/dist/styles/font/LICENSE +93 -0
- package/dist/styles/font/files/11944884.woff2 +0 -0
- package/dist/styles/font/files/3f081d19.woff2 +0 -0
- package/dist/styles/font/files/aa4aa22f.woff2 +0 -0
- package/dist/styles/font/files/ba1a9b92.woff2 +0 -0
- package/dist/styles/font/files/eadd8e6c.woff2 +0 -0
- package/dist/styles/font/geist.css +2 -0
- package/dist/styles/index.css +2 -0
- package/dist/types/components/accordion.d.ts +9 -0
- package/dist/types/components/alert-dialog.d.ts +20 -0
- package/dist/types/components/alert.d.ts +13 -0
- package/dist/types/components/aspect-ratio.d.ts +7 -0
- package/dist/types/components/attachment.d.ts +26 -0
- package/dist/types/components/avatar.d.ts +13 -0
- package/dist/types/components/badge.d.ts +11 -0
- package/dist/types/components/breadcrumb.d.ts +12 -0
- package/dist/types/components/bubble.d.ts +19 -0
- package/dist/types/components/button-group.d.ts +14 -0
- package/dist/types/components/button.d.ts +12 -0
- package/dist/types/components/calendar.d.ts +12 -0
- package/dist/types/components/card.d.ts +13 -0
- package/dist/types/components/carousel.d.ts +31 -0
- package/dist/types/components/chart.d.ts +47 -0
- package/dist/types/components/checkbox.d.ts +6 -0
- package/dist/types/components/code-block.d.ts +29 -0
- package/dist/types/components/collapsible.d.ts +8 -0
- package/dist/types/components/combobox.d.ts +26 -0
- package/dist/types/components/command.d.ts +21 -0
- package/dist/types/components/context-menu.d.ts +31 -0
- package/dist/types/components/dialog.d.ts +19 -0
- package/dist/types/components/direction.d.ts +1 -0
- package/dist/types/components/drawer.d.ts +18 -0
- package/dist/types/components/dropdown-menu.d.ts +31 -0
- package/dist/types/components/empty.d.ts +15 -0
- package/dist/types/components/field.d.ts +28 -0
- package/dist/types/components/hover-card.d.ts +8 -0
- package/dist/types/components/index.d.ts +65 -0
- package/dist/types/components/input-group.d.ts +21 -0
- package/dist/types/components/input-otp.d.ts +13 -0
- package/dist/types/components/input.d.ts +5 -0
- package/dist/types/components/item.d.ts +25 -0
- package/dist/types/components/kbd.d.ts +6 -0
- package/dist/types/components/label.d.ts +5 -0
- package/dist/types/components/marker.d.ts +13 -0
- package/dist/types/components/menubar.d.ts +31 -0
- package/dist/types/components/message-scroller.d.ts +13 -0
- package/dist/types/components/message.d.ts +12 -0
- package/dist/types/components/native-select.d.ts +10 -0
- package/dist/types/components/navigation-menu.d.ts +15 -0
- package/dist/types/components/pagination.d.ts +19 -0
- package/dist/types/components/popover.d.ts +11 -0
- package/dist/types/components/progress.d.ts +10 -0
- package/dist/types/components/questionnaire.d.ts +21 -0
- package/dist/types/components/radio-group.d.ts +8 -0
- package/dist/types/components/resizable.d.ts +10 -0
- package/dist/types/components/scroll-area.d.ts +7 -0
- package/dist/types/components/select.d.ts +17 -0
- package/dist/types/components/separator.d.ts +6 -0
- package/dist/types/components/sheet.d.ts +16 -0
- package/dist/types/components/sidebar.d.ts +66 -0
- package/dist/types/components/skeleton.d.ts +5 -0
- package/dist/types/components/slider.d.ts +6 -0
- package/dist/types/components/spinner.d.ts +5 -0
- package/dist/types/components/switch.d.ts +8 -0
- package/dist/types/components/table.d.ts +12 -0
- package/dist/types/components/tabs.d.ts +14 -0
- package/dist/types/components/textarea.d.ts +5 -0
- package/dist/types/components/theme-toggle.d.ts +14 -0
- package/dist/types/components/toast.d.ts +19 -0
- package/dist/types/components/toggle-group.d.ts +13 -0
- package/dist/types/components/toggle.d.ts +12 -0
- package/dist/types/components/tooltip.d.ts +9 -0
- package/dist/types/components/trace.d.ts +20 -0
- package/dist/types/hooks/index.d.ts +3 -0
- package/dist/types/hooks/use-highlighted-code.d.ts +3 -0
- package/dist/types/hooks/use-hydrated.d.ts +4 -0
- package/dist/types/hooks/use-mobile.d.ts +3 -0
- package/dist/types/index.d.ts +73 -0
- package/dist/types/lib/highlighter.d.ts +11 -0
- package/dist/types/lib/index.d.ts +3 -0
- package/dist/types/lib/preference.d.ts +85 -0
- package/dist/types/lib/utils.d.ts +1 -0
- package/dist/types/providers/index.d.ts +2 -0
- package/dist/types/providers/preference-provider.d.ts +20 -0
- package/dist/types/providers/theme-provider.d.ts +25 -0
- package/dist/umd/index.js +86818 -0
- package/package.json +116 -4
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { MoonIcon, SunIcon } from 'lucide-react';
|
|
4
|
+
import { Button } from './button.js';
|
|
5
|
+
import { useTheme } from '../providers/theme-provider.js';
|
|
6
|
+
|
|
7
|
+
function ThemeToggle({ darkLabel = '切换到深色主题', lightLabel = '切换到浅色主题', variant = 'outline', size = 'icon', title = '切换主题', onClick, ...props }) {
|
|
8
|
+
const { toggleTheme } = useTheme();
|
|
9
|
+
return (jsxs(Button, { type: "button", variant: variant, size: size, title: title, ...props, onClick: (event) => {
|
|
10
|
+
// 先执行使用方回调,允许通过 preventDefault() 取消主题切换。
|
|
11
|
+
onClick?.(event);
|
|
12
|
+
if (!event.defaultPrevented)
|
|
13
|
+
toggleTheme();
|
|
14
|
+
}, children: [jsx(MoonIcon, { "data-icon": "inline-start", "aria-hidden": "true", className: "animate-in duration-200 ease-out zoom-in-75 fade-in spin-in-90 motion-reduce:animate-none dark:hidden" }), jsx(SunIcon, { "data-icon": "inline-start", "aria-hidden": "true", className: "hidden animate-in duration-200 ease-out zoom-in-75 fade-in spin-in-90 motion-reduce:animate-none dark:block" }), jsx("span", { className: "sr-only dark:hidden", children: darkLabel }), jsx("span", { className: "sr-only hidden dark:block", children: lightLabel })] }));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { ThemeToggle };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { Toast as Toast$1 } from '@base-ui/react/toast';
|
|
4
|
+
import { cn } from 'cn';
|
|
5
|
+
import { Button } from './button.js';
|
|
6
|
+
import { XIcon, CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from 'lucide-react';
|
|
7
|
+
|
|
8
|
+
const toast = Toast$1.createToastManager();
|
|
9
|
+
function ToastProvider({ ...props }) {
|
|
10
|
+
return jsx(Toast$1.Provider, { ...props });
|
|
11
|
+
}
|
|
12
|
+
function ToastPortal({ ...props }) {
|
|
13
|
+
return jsx(Toast$1.Portal, { "data-slot": "toast-portal", ...props });
|
|
14
|
+
}
|
|
15
|
+
function ToastViewport({ className, ...props }) {
|
|
16
|
+
return (jsx(Toast$1.Viewport, { "data-slot": "toast-viewport", className: cn("pointer-events-none fixed inset-x-4 bottom-4 z-50 mx-auto w-auto max-w-sm outline-none sm:right-4 sm:left-auto sm:mx-0 sm:w-full", className), ...props }));
|
|
17
|
+
}
|
|
18
|
+
function Toast({ className, ...props }) {
|
|
19
|
+
return (jsx(Toast$1.Root, { "data-slot": "toast", className: cn("group/toast pointer-events-auto absolute right-0 bottom-0 z-[calc(1000-var(--toast-index))] w-full origin-bottom rounded-2xl border bg-popover text-popover-foreground shadow-lg will-change-transform outline-none select-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50", "[--gap:0.75rem] [--height:var(--toast-frontmost-height,var(--toast-height))] [--offset-y:calc(var(--toast-offset-y)*-1+calc(var(--toast-index)*var(--gap)*-1)+var(--toast-swipe-movement-y))] [--peek:0.75rem] [--scale:calc(max(0,1-(var(--toast-index)*0.1)))] [--shrink:calc(1-var(--scale))]", "h-(--height) [transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--toast-swipe-movement-y)-(var(--toast-index)*var(--peek))-(var(--shrink)*var(--height))))_scale(var(--scale))] [transition:transform_500ms_cubic-bezier(0.22,1,0.36,1),opacity_500ms,height_150ms]", "after:absolute after:top-full after:left-0 after:h-[calc(var(--gap)+1px)] after:w-full after:content-['']", "data-expanded:h-(--toast-height) data-expanded:[transform:translateX(var(--toast-swipe-movement-x))_translateY(var(--offset-y))]", "data-limited:opacity-0 data-starting-style:[transform:translateY(150%)]", "[&[data-ending-style]:not([data-limited]):not([data-swipe-direction])]:[transform:translateY(150%)]", "data-ending-style:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))]", "data-ending-style:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))]", "data-ending-style:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))]", "data-ending-style:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))]", "data-expanded:data-ending-style:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))]", "data-expanded:data-ending-style:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))]", "data-expanded:data-ending-style:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))]", "data-expanded:data-ending-style:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))]", className), ...props }));
|
|
20
|
+
}
|
|
21
|
+
function ToastContent({ className, ...props }) {
|
|
22
|
+
return (jsx(Toast$1.Content, { "data-slot": "toast-content", className: cn("flex h-full items-center gap-3 overflow-hidden p-4 transition-opacity duration-250 ease-[cubic-bezier(0.22,1,0.36,1)] data-behind:opacity-0 data-expanded:opacity-100", className), ...props }));
|
|
23
|
+
}
|
|
24
|
+
function ToastTitle({ className, ...props }) {
|
|
25
|
+
return (jsx(Toast$1.Title, { "data-slot": "toast-title", className: cn("text-sm font-medium", className), ...props }));
|
|
26
|
+
}
|
|
27
|
+
function ToastDescription({ className, ...props }) {
|
|
28
|
+
return (jsx(Toast$1.Description, { "data-slot": "toast-description", className: cn("text-sm text-muted-foreground", className), ...props }));
|
|
29
|
+
}
|
|
30
|
+
function ToastAction({ className, render = jsx(Button, { variant: "outline", size: "sm" }), ...props }) {
|
|
31
|
+
return (jsx(Toast$1.Action, { "data-slot": "toast-action", render: render, className: cn("shrink-0", className), ...props }));
|
|
32
|
+
}
|
|
33
|
+
function ToastClose({ className, children, render = jsx(Button, { variant: "ghost", size: "icon-sm" }), ...props }) {
|
|
34
|
+
return (jsx(Toast$1.Close, { "data-slot": "toast-close", "aria-label": "Close toast", render: render, className: cn("relative shrink-0 text-muted-foreground after:absolute after:-inset-2 after:content-[''] hover:text-foreground", className), ...props, children: children ?? (jsx(XIcon, { "aria-hidden": "true" })) }));
|
|
35
|
+
}
|
|
36
|
+
function ToastIcon({ type }) {
|
|
37
|
+
let icon = null;
|
|
38
|
+
if (type === "success") {
|
|
39
|
+
icon = (jsx(CircleCheckIcon, { "aria-hidden": "true" }));
|
|
40
|
+
}
|
|
41
|
+
if (type === "info") {
|
|
42
|
+
icon = (jsx(InfoIcon, { "aria-hidden": "true" }));
|
|
43
|
+
}
|
|
44
|
+
if (type === "warning") {
|
|
45
|
+
icon = (jsx(TriangleAlertIcon, { "aria-hidden": "true" }));
|
|
46
|
+
}
|
|
47
|
+
if (type === "error") {
|
|
48
|
+
icon = (jsx(OctagonXIcon, { className: "text-destructive", "aria-hidden": "true" }));
|
|
49
|
+
}
|
|
50
|
+
if (type === "loading") {
|
|
51
|
+
icon = (jsx(Loader2Icon, { className: "animate-spin", "aria-hidden": "true" }));
|
|
52
|
+
}
|
|
53
|
+
if (!icon) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return (jsx("span", { "data-slot": "toast-icon", className: "shrink-0 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4", children: icon }));
|
|
57
|
+
}
|
|
58
|
+
function ToastList() {
|
|
59
|
+
const { toasts } = Toast$1.useToastManager();
|
|
60
|
+
return toasts.map((toastItem) => (jsx(Toast, { toast: toastItem, children: jsxs(ToastContent, { children: [jsx(ToastIcon, { type: toastItem.type }), jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-1", children: [jsx(ToastTitle, {}), jsx(ToastDescription, {})] }), jsx(ToastAction, {}), jsx(ToastClose, {})] }) }, toastItem.id)));
|
|
61
|
+
}
|
|
62
|
+
function Toaster({ children, toastManager = toast, ...props }) {
|
|
63
|
+
return (jsxs(ToastProvider, { toastManager: toastManager, ...props, children: [children, jsx(ToastPortal, { children: jsx(ToastViewport, { children: jsx(ToastList, {}) }) })] }));
|
|
64
|
+
}
|
|
65
|
+
const createToastManager = Toast$1.createToastManager;
|
|
66
|
+
const useToastManager = Toast$1.useToastManager;
|
|
67
|
+
|
|
68
|
+
export { Toast, ToastAction, ToastClose, ToastContent, ToastDescription, ToastPortal, ToastProvider, ToastTitle, ToastViewport, Toaster, createToastManager, toast, useToastManager };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { Toggle } from '@base-ui/react/toggle';
|
|
4
|
+
import { ToggleGroup as ToggleGroup$1 } from '@base-ui/react/toggle-group';
|
|
5
|
+
import { cn } from 'cn';
|
|
6
|
+
import { toggleVariants } from './toggle.js';
|
|
7
|
+
|
|
8
|
+
const ToggleGroupContext = React.createContext({
|
|
9
|
+
size: "default",
|
|
10
|
+
variant: "default",
|
|
11
|
+
spacing: 2,
|
|
12
|
+
orientation: "horizontal",
|
|
13
|
+
});
|
|
14
|
+
function ToggleGroup({ className, variant, size, spacing = 2, orientation = "horizontal", children, ...props }) {
|
|
15
|
+
return (jsx(ToggleGroup$1, { "data-slot": "toggle-group", "data-variant": variant, "data-size": size, "data-spacing": spacing, "data-orientation": orientation, style: { "--gap": spacing }, className: cn("group/toggle-group flex w-fit flex-row items-center gap-[--spacing(var(--gap))] rounded-lg data-[size=sm]:rounded-[min(var(--radius-md),10px)] data-vertical:flex-col data-vertical:items-stretch", className), ...props, children: jsx(ToggleGroupContext.Provider, { value: { variant, size, spacing, orientation }, children: children }) }));
|
|
16
|
+
}
|
|
17
|
+
function ToggleGroupItem({ className, children, variant = "default", size = "default", ...props }) {
|
|
18
|
+
const context = React.useContext(ToggleGroupContext);
|
|
19
|
+
return (jsx(Toggle, { "data-slot": "toggle-group-item", "data-variant": context.variant || variant, "data-size": context.size || size, "data-spacing": context.spacing, className: cn("shrink-0 group-data-[spacing=0]/toggle-group:rounded-none group-data-[spacing=0]/toggle-group:px-2 focus:z-10 focus-visible:z-10 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-end]:pr-1.5 group-data-[spacing=0]/toggle-group:has-data-[icon=inline-start]:pl-1.5 group-data-horizontal/toggle-group:data-[spacing=0]:first:rounded-l-lg group-data-vertical/toggle-group:data-[spacing=0]:first:rounded-t-lg group-data-horizontal/toggle-group:data-[spacing=0]:last:rounded-r-lg group-data-vertical/toggle-group:data-[spacing=0]:last:rounded-b-lg group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:border-l-0 group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:border-t-0 group-data-horizontal/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-l group-data-vertical/toggle-group:data-[spacing=0]:data-[variant=outline]:first:border-t", toggleVariants({
|
|
20
|
+
variant: context.variant || variant,
|
|
21
|
+
size: context.size || size,
|
|
22
|
+
}), className), ...props, children: children }));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { ToggleGroup, ToggleGroupItem };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { Toggle as Toggle$1 } from '@base-ui/react/toggle';
|
|
3
|
+
import { cva } from 'class-variance-authority';
|
|
4
|
+
import { cn } from 'cn';
|
|
5
|
+
|
|
6
|
+
const toggleVariants = cva("group/toggle inline-flex items-center justify-center gap-1 rounded-lg text-sm font-medium whitespace-nowrap transition-all outline-none hover:bg-muted hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-pressed:bg-muted data-[state=on]:bg-muted dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", {
|
|
7
|
+
variants: {
|
|
8
|
+
variant: {
|
|
9
|
+
default: "bg-transparent",
|
|
10
|
+
outline: "border border-input bg-transparent hover:bg-muted",
|
|
11
|
+
},
|
|
12
|
+
size: {
|
|
13
|
+
default: "h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
14
|
+
sm: "h-7 min-w-7 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
|
|
15
|
+
lg: "h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
defaultVariants: {
|
|
19
|
+
variant: "default",
|
|
20
|
+
size: "default",
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
function Toggle({ className, variant = "default", size = "default", ...props }) {
|
|
24
|
+
return (jsx(Toggle$1, { "data-slot": "toggle", className: cn(toggleVariants({ variant, size, className })), ...props }));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { Toggle, toggleVariants };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { Tooltip as Tooltip$1 } from '@base-ui/react/tooltip';
|
|
4
|
+
import { cn } from 'cn';
|
|
5
|
+
|
|
6
|
+
function TooltipProvider({ delay = 0, ...props }) {
|
|
7
|
+
return (jsx(Tooltip$1.Provider, { "data-slot": "tooltip-provider", delay: delay, ...props }));
|
|
8
|
+
}
|
|
9
|
+
function Tooltip({ ...props }) {
|
|
10
|
+
return jsx(Tooltip$1.Root, { "data-slot": "tooltip", ...props });
|
|
11
|
+
}
|
|
12
|
+
function TooltipTrigger({ ...props }) {
|
|
13
|
+
return jsx(Tooltip$1.Trigger, { "data-slot": "tooltip-trigger", ...props });
|
|
14
|
+
}
|
|
15
|
+
function TooltipContent({ className, side = "top", sideOffset = 4, align = "center", alignOffset = 0, children, ...props }) {
|
|
16
|
+
return (jsx(Tooltip$1.Portal, { children: jsx(Tooltip$1.Positioner, { align: align, alignOffset: alignOffset, side: side, sideOffset: sideOffset, className: "isolate z-50", children: jsxs(Tooltip$1.Popup, { "data-slot": "tooltip-content", className: cn("z-50 inline-flex w-fit max-w-xs origin-(--transform-origin) items-center gap-1.5 rounded-md bg-foreground px-3 py-1.5 text-xs text-background has-data-[slot=kbd]:pr-1.5 data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 **:data-[slot=kbd]:relative **:data-[slot=kbd]:isolate **:data-[slot=kbd]:z-50 **:data-[slot=kbd]:rounded-sm data-[state=delayed-open]:animate-in data-[state=delayed-open]:fade-in-0 data-[state=delayed-open]:zoom-in-95 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className), ...props, children: [children, jsx(Tooltip$1.Arrow, { className: "z-50 size-2.5 translate-y-[calc(-50%-2px)] rotate-45 rounded-[2px] bg-foreground fill-foreground data-[side=bottom]:top-1 data-[side=inline-end]:top-1/2! data-[side=inline-end]:-left-1 data-[side=inline-end]:-translate-y-1/2 data-[side=inline-start]:top-1/2! data-[side=inline-start]:-right-1 data-[side=inline-start]:-translate-y-1/2 data-[side=left]:top-1/2! data-[side=left]:-right-1 data-[side=left]:-translate-y-1/2 data-[side=right]:top-1/2! data-[side=right]:-left-1 data-[side=right]:-translate-y-1/2 data-[side=top]:-bottom-2.5" })] }) }) }));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
|
+
import { cn } from 'cn';
|
|
3
|
+
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from './collapsible.js';
|
|
4
|
+
import { ChevronRightIcon } from 'lucide-react';
|
|
5
|
+
|
|
6
|
+
function Trace({ className, ...props }) {
|
|
7
|
+
return (jsx("div", { "data-slot": "trace", className: cn("flex w-full min-w-0 flex-col gap-2 *:has-[>[data-slot=trace-line]]:flex *:has-[>[data-slot=trace-line]]:w-fit *:has-[>[data-slot=trace-line]]:max-w-full *:has-[>[data-slot=trace-line]]:min-w-0 *:has-[>[data-slot=trace-line]]:rounded-md *:has-[>[data-slot=trace-line]]:text-left *:has-[>[data-slot=trace-line]]:outline-none *:has-[>[data-slot=trace-line]]:focus-visible:ring-3 *:has-[>[data-slot=trace-line]]:focus-visible:ring-ring/50", className), ...props }));
|
|
8
|
+
}
|
|
9
|
+
function TraceLine({ className, status, ...props }) {
|
|
10
|
+
return (jsx("span", { "data-slot": "trace-line", "data-status": status, className: cn("flex w-fit max-w-full min-w-0 items-center gap-2 text-sm text-muted-foreground transition-colors in-[a,button]:cursor-pointer in-[a:hover,button:hover]:text-foreground data-[status=error]:text-destructive data-[status=error]:in-[a:hover,button:hover]:text-destructive data-[status=running]:*:data-[slot=trace-title]:shimmer", className), ...props }));
|
|
11
|
+
}
|
|
12
|
+
function TraceIcon({ className, ...props }) {
|
|
13
|
+
return (jsx("span", { "data-slot": "trace-icon", "aria-hidden": "true", className: cn("flex size-4 shrink-0 items-center justify-center [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [[data-slot=trace-title]~&]:-ml-1", className), ...props }));
|
|
14
|
+
}
|
|
15
|
+
function TraceTitle({ className, ...props }) {
|
|
16
|
+
return (jsx("span", { "data-slot": "trace-title", className: cn("min-w-0 truncate motion-reduce:shimmer-none", className), ...props }));
|
|
17
|
+
}
|
|
18
|
+
function TraceDescription({ className, ...props }) {
|
|
19
|
+
return (jsx("span", { "data-slot": "trace-description", className: cn("shrink-0 text-muted-foreground tabular-nums", className), ...props }));
|
|
20
|
+
}
|
|
21
|
+
function TraceCollapsible({ className, ...props }) {
|
|
22
|
+
return (jsx(Collapsible, { "data-slot": "trace-collapsible", className: cn("flex w-full min-w-0 flex-col", className), ...props }));
|
|
23
|
+
}
|
|
24
|
+
function TraceCollapsibleTrigger({ className, children, ...props }) {
|
|
25
|
+
return (jsxs(CollapsibleTrigger, { "data-slot": "trace-collapsible-trigger", className: cn("group/trace-collapsible-trigger flex w-fit max-w-full min-w-0 cursor-pointer items-center gap-1 rounded-md text-left text-muted-foreground transition-colors outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50 has-[[data-status=error]]:text-destructive", className), ...props, children: [children, jsx(TraceIcon, { className: "transition-transform group-aria-expanded/trace-collapsible-trigger:rotate-90", children: jsx(ChevronRightIcon, {}) })] }));
|
|
26
|
+
}
|
|
27
|
+
function TraceCollapsibleContent({ className, children, ...props }) {
|
|
28
|
+
return (jsx(CollapsibleContent, { "data-slot": "trace-collapsible-content", className: "h-(--collapsible-panel-height) min-w-0 overflow-hidden transition-[height] duration-200 data-ending-style:h-0 data-starting-style:h-0", ...props, children: jsx("div", { "data-slot": "trace-collapsible-content-inner", className: cn("mt-2 mb-1 ml-[calc(--spacing(2)-1px)] min-w-0 border-l pr-1 pl-4 leading-relaxed wrap-break-word text-muted-foreground", className), children: children }) }));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { Trace, TraceCollapsible, TraceCollapsibleContent, TraceCollapsibleTrigger, TraceDescription, TraceIcon, TraceLine, TraceTitle };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { loadHighlighter } from '../lib/highlighter.js';
|
|
3
|
+
|
|
4
|
+
function useHighlightedCode(value, language) {
|
|
5
|
+
// A trailing newline would render as an extra empty line.
|
|
6
|
+
const code = value.endsWith("\n") ? value.slice(0, -1) : value;
|
|
7
|
+
const [loaded, setLoaded] = React.useState(null);
|
|
8
|
+
const highlighter = loaded?.language === language ? loaded.highlighter : null;
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
let cancelled = false;
|
|
11
|
+
loadHighlighter(language)
|
|
12
|
+
.then((next) => {
|
|
13
|
+
if (!cancelled) {
|
|
14
|
+
setLoaded({ language, highlighter: next });
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
.catch(() => { });
|
|
18
|
+
return () => {
|
|
19
|
+
cancelled = true;
|
|
20
|
+
};
|
|
21
|
+
}, [language]);
|
|
22
|
+
// The complete lines, highlighted. While the code streams, only the new
|
|
23
|
+
// lines are tokenized, starting from the grammar state of the last one.
|
|
24
|
+
const [done, setDone] = React.useState({ highlighter: null, text: "", lines: [] });
|
|
25
|
+
let next = done;
|
|
26
|
+
if (next.highlighter !== highlighter || !code.startsWith(next.text)) {
|
|
27
|
+
next = { highlighter, text: "", lines: [] };
|
|
28
|
+
}
|
|
29
|
+
const end = code.lastIndexOf("\n") + 1;
|
|
30
|
+
if (highlighter && end > next.text.length) {
|
|
31
|
+
const result = highlighter.highlight(code.slice(next.text.length, end - 1), next.state);
|
|
32
|
+
next = {
|
|
33
|
+
highlighter,
|
|
34
|
+
text: code.slice(0, end),
|
|
35
|
+
lines: [...next.lines, ...result.lines],
|
|
36
|
+
state: result.state,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (next !== done) {
|
|
40
|
+
setDone(next);
|
|
41
|
+
}
|
|
42
|
+
// The last line is still being written, so it is tokenized on every render.
|
|
43
|
+
const tail = code.slice(end);
|
|
44
|
+
return React.useMemo(() => {
|
|
45
|
+
if (!highlighter) {
|
|
46
|
+
return code.split("\n");
|
|
47
|
+
}
|
|
48
|
+
return [...next.lines, highlighter.highlight(tail, next.state).lines[0]];
|
|
49
|
+
}, [code, highlighter, next, tail]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { useHighlightedCode };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useSyncExternalStore } from 'react';
|
|
3
|
+
|
|
4
|
+
// 水合完成与否由 React 的服务端快照机制区分,无需 effect 中同步设置状态。
|
|
5
|
+
const subscribe = () => () => { };
|
|
6
|
+
const getSnapshot = () => true;
|
|
7
|
+
const getServerSnapshot = () => false;
|
|
8
|
+
/** 服务端及客户端水合首帧返回 false,之后返回 true;纯客户端渲染直接返回 true。 */
|
|
9
|
+
function useHydrated() {
|
|
10
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { useHydrated };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
const MOBILE_BREAKPOINT = 768;
|
|
4
|
+
function subscribe(onChange) {
|
|
5
|
+
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
|
|
6
|
+
mql.addEventListener("change", onChange);
|
|
7
|
+
return () => mql.removeEventListener("change", onChange);
|
|
8
|
+
}
|
|
9
|
+
const getSnapshot = () => window.innerWidth < MOBILE_BREAKPOINT;
|
|
10
|
+
const getServerSnapshot = () => false;
|
|
11
|
+
function useIsMobile() {
|
|
12
|
+
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { useIsMobile };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from './components/accordion.js';
|
|
2
|
+
export { Alert, AlertAction, AlertDescription, AlertTitle } from './components/alert.js';
|
|
3
|
+
export { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogMedia, AlertDialogOverlay, AlertDialogPortal, AlertDialogTitle, AlertDialogTrigger } from './components/alert-dialog.js';
|
|
4
|
+
export { AspectRatio } from './components/aspect-ratio.js';
|
|
5
|
+
export { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentGroup, AttachmentMedia, AttachmentTitle, AttachmentTrigger } from './components/attachment.js';
|
|
6
|
+
export { Avatar, AvatarBadge, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage } from './components/avatar.js';
|
|
7
|
+
export { Badge, badgeVariants } from './components/badge.js';
|
|
8
|
+
export { Breadcrumb, BreadcrumbEllipsis, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator } from './components/breadcrumb.js';
|
|
9
|
+
export { Bubble, BubbleContent, BubbleGroup, BubbleReactions } from './components/bubble.js';
|
|
10
|
+
export { Button, buttonVariants } from './components/button.js';
|
|
11
|
+
export { ButtonGroup, ButtonGroupSeparator, ButtonGroupText, buttonGroupVariants } from './components/button-group.js';
|
|
12
|
+
export { Calendar, CalendarDayButton } from './components/calendar.js';
|
|
13
|
+
export { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from './components/card.js';
|
|
14
|
+
export { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious, useCarousel } from './components/carousel.js';
|
|
15
|
+
export { ChartContainer, ChartLegend, ChartLegendContent, ChartStyle, ChartTooltip, ChartTooltipContent } from './components/chart.js';
|
|
16
|
+
export { Checkbox } from './components/checkbox.js';
|
|
17
|
+
export { CodeBlock, CodeBlockActions, CodeBlockContent, CodeBlockCopyButton, CodeBlockFooter, CodeBlockHeader, CodeBlockLine, CodeBlockTitle } from './components/code-block.js';
|
|
18
|
+
export { Collapsible, CollapsibleContent, CollapsibleTrigger } from './components/collapsible.js';
|
|
19
|
+
export { Combobox, ComboboxChip, ComboboxChips, ComboboxChipsInput, ComboboxCollection, ComboboxContent, ComboboxEmpty, ComboboxGroup, ComboboxInput, ComboboxItem, ComboboxLabel, ComboboxList, ComboboxSeparator, ComboboxTrigger, ComboboxValue, useComboboxAnchor } from './components/combobox.js';
|
|
20
|
+
export { Command, CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator, CommandShortcut } from './components/command.js';
|
|
21
|
+
export { ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuGroup, ContextMenuItem, ContextMenuLabel, ContextMenuPortal, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuShortcut, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger } from './components/context-menu.js';
|
|
22
|
+
export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger } from './components/dialog.js';
|
|
23
|
+
export { DirectionProvider, useDirection } from '@base-ui/react/direction-provider';
|
|
24
|
+
export { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerOverlay, DrawerPortal, DrawerSwipeHandle, DrawerTitle, DrawerTrigger } from './components/drawer.js';
|
|
25
|
+
export { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuPortal, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger } from './components/dropdown-menu.js';
|
|
26
|
+
export { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from './components/empty.js';
|
|
27
|
+
export { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldLegend, FieldSeparator, FieldSet, FieldTitle } from './components/field.js';
|
|
28
|
+
export { HoverCard, HoverCardContent, HoverCardTrigger } from './components/hover-card.js';
|
|
29
|
+
export { Input } from './components/input.js';
|
|
30
|
+
export { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, InputGroupText, InputGroupTextarea } from './components/input-group.js';
|
|
31
|
+
export { InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot } from './components/input-otp.js';
|
|
32
|
+
export { Item, ItemActions, ItemContent, ItemDescription, ItemFooter, ItemGroup, ItemHeader, ItemMedia, ItemSeparator, ItemTitle } from './components/item.js';
|
|
33
|
+
export { Kbd, KbdGroup } from './components/kbd.js';
|
|
34
|
+
export { Label } from './components/label.js';
|
|
35
|
+
export { Marker, MarkerContent, MarkerIcon, markerVariants } from './components/marker.js';
|
|
36
|
+
export { Menubar, MenubarCheckboxItem, MenubarContent, MenubarGroup, MenubarItem, MenubarLabel, MenubarMenu, MenubarPortal, MenubarRadioGroup, MenubarRadioItem, MenubarSeparator, MenubarShortcut, MenubarSub, MenubarSubContent, MenubarSubTrigger, MenubarTrigger } from './components/menubar.js';
|
|
37
|
+
export { Message, MessageAvatar, MessageContent, MessageFooter, MessageGroup, MessageHeader } from './components/message.js';
|
|
38
|
+
export { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport } from './components/message-scroller.js';
|
|
39
|
+
export { NativeSelect, NativeSelectOptGroup, NativeSelectOption } from './components/native-select.js';
|
|
40
|
+
export { NavigationMenu, NavigationMenuContent, NavigationMenuIndicator, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuPositioner, NavigationMenuTrigger, navigationMenuTriggerStyle } from './components/navigation-menu.js';
|
|
41
|
+
export { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious } from './components/pagination.js';
|
|
42
|
+
export { Popover, PopoverContent, PopoverDescription, PopoverHeader, PopoverTitle, PopoverTrigger } from './components/popover.js';
|
|
43
|
+
export { Progress, ProgressIndicator, ProgressLabel, ProgressTrack, ProgressValue } from './components/progress.js';
|
|
44
|
+
export { Questionnaire, QuestionnaireActions, QuestionnaireChoice, QuestionnaireChoiceDescription, QuestionnaireChoices, QuestionnaireDescription, QuestionnaireError, QuestionnaireInput, QuestionnaireItem, QuestionnaireNext, QuestionnairePrevious, QuestionnaireProgress, QuestionnaireSkip, QuestionnaireSubmit, QuestionnaireTitle } from './components/questionnaire.js';
|
|
45
|
+
export { RadioGroup, RadioGroupItem } from './components/radio-group.js';
|
|
46
|
+
export { ResizableHandle, ResizablePanel, ResizablePanelGroup } from './components/resizable.js';
|
|
47
|
+
export { ScrollArea, ScrollBar } from './components/scroll-area.js';
|
|
48
|
+
export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue } from './components/select.js';
|
|
49
|
+
export { Separator } from './components/separator.js';
|
|
50
|
+
export { Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, SheetTrigger } from './components/sheet.js';
|
|
51
|
+
export { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupAction, SidebarGroupContent, SidebarGroupLabel, SidebarHeader, SidebarInput, SidebarInset, SidebarMenu, SidebarMenuAction, SidebarMenuBadge, SidebarMenuButton, SidebarMenuItem, SidebarMenuSkeleton, SidebarMenuSub, SidebarMenuSubButton, SidebarMenuSubItem, SidebarProvider, SidebarRail, SidebarSeparator, SidebarTrigger, useSidebar } from './components/sidebar.js';
|
|
52
|
+
export { Skeleton } from './components/skeleton.js';
|
|
53
|
+
export { Slider } from './components/slider.js';
|
|
54
|
+
export { Spinner } from './components/spinner.js';
|
|
55
|
+
export { Switch } from './components/switch.js';
|
|
56
|
+
export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from './components/table.js';
|
|
57
|
+
export { Tabs, TabsContent, TabsList, TabsTrigger, tabsListVariants } from './components/tabs.js';
|
|
58
|
+
export { Textarea } from './components/textarea.js';
|
|
59
|
+
export { ThemeToggle } from './components/theme-toggle.js';
|
|
60
|
+
export { Toast, ToastAction, ToastClose, ToastContent, ToastDescription, ToastPortal, ToastProvider, ToastTitle, ToastViewport, Toaster, createToastManager, toast, useToastManager } from './components/toast.js';
|
|
61
|
+
export { Toggle, toggleVariants } from './components/toggle.js';
|
|
62
|
+
export { ToggleGroup, ToggleGroupItem } from './components/toggle-group.js';
|
|
63
|
+
export { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from './components/tooltip.js';
|
|
64
|
+
export { Trace, TraceCollapsible, TraceCollapsibleContent, TraceCollapsibleTrigger, TraceDescription, TraceIcon, TraceLine, TraceTitle } from './components/trace.js';
|
|
65
|
+
export { PreferenceProvider, usePreference } from './providers/preference-provider.js';
|
|
66
|
+
export { ThemeProvider, useTheme } from './providers/theme-provider.js';
|
|
67
|
+
export { useHighlightedCode } from './hooks/use-highlighted-code.js';
|
|
68
|
+
export { useHydrated } from './hooks/use-hydrated.js';
|
|
69
|
+
export { useIsMobile } from './hooks/use-mobile.js';
|
|
70
|
+
export { highlightCode, loadHighlighter } from './lib/highlighter.js';
|
|
71
|
+
export { BASE_COLORS, COLOR_THEMES, DEFAULT_PREFERENCE, RADIUS_OPTIONS } from './lib/preference.js';
|
|
72
|
+
export { cn } from 'cn';
|
|
73
|
+
export { useMessageScroller, useMessageScrollerScrollable, useMessageScrollerVisibility } from '@shadcn/react/message-scroller';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { createHighlighterCore } from 'shiki/core';
|
|
3
|
+
import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
|
|
4
|
+
|
|
5
|
+
// Add the languages you need. Each one loads the first time it is used.
|
|
6
|
+
const languages = {
|
|
7
|
+
bash: () => import('shiki/langs/shellscript.mjs'),
|
|
8
|
+
css: () => import('shiki/langs/css.mjs'),
|
|
9
|
+
diff: () => import('shiki/langs/diff.mjs'),
|
|
10
|
+
html: () => import('shiki/langs/html.mjs'),
|
|
11
|
+
javascript: () => import('shiki/langs/javascript.mjs'),
|
|
12
|
+
json: () => import('shiki/langs/json.mjs'),
|
|
13
|
+
jsx: () => import('shiki/langs/jsx.mjs'),
|
|
14
|
+
markdown: () => import('shiki/langs/markdown.mjs'),
|
|
15
|
+
python: () => import('shiki/langs/python.mjs'),
|
|
16
|
+
sql: () => import('shiki/langs/sql.mjs'),
|
|
17
|
+
tsx: () => import('shiki/langs/tsx.mjs'),
|
|
18
|
+
typescript: () => import('shiki/langs/typescript.mjs'),
|
|
19
|
+
yaml: () => import('shiki/langs/yaml.mjs'),
|
|
20
|
+
};
|
|
21
|
+
const aliases = {
|
|
22
|
+
js: "javascript",
|
|
23
|
+
md: "markdown",
|
|
24
|
+
py: "python",
|
|
25
|
+
sh: "bash",
|
|
26
|
+
shell: "bash",
|
|
27
|
+
shellscript: "bash",
|
|
28
|
+
ts: "typescript",
|
|
29
|
+
yml: "yaml",
|
|
30
|
+
zsh: "bash",
|
|
31
|
+
};
|
|
32
|
+
let highlighter;
|
|
33
|
+
function getHighlighter() {
|
|
34
|
+
highlighter ??= createHighlighterCore({
|
|
35
|
+
themes: [
|
|
36
|
+
import('shiki/themes/github-light.mjs'),
|
|
37
|
+
import('shiki/themes/github-dark.mjs'),
|
|
38
|
+
],
|
|
39
|
+
langs: [],
|
|
40
|
+
engine: createJavaScriptRegexEngine({ forgiving: true }),
|
|
41
|
+
});
|
|
42
|
+
return highlighter;
|
|
43
|
+
}
|
|
44
|
+
function getLanguage(language) {
|
|
45
|
+
const name = language.toLowerCase();
|
|
46
|
+
if (name === "ansi") {
|
|
47
|
+
return name;
|
|
48
|
+
}
|
|
49
|
+
if (name in languages) {
|
|
50
|
+
return name;
|
|
51
|
+
}
|
|
52
|
+
return aliases[name] ?? null;
|
|
53
|
+
}
|
|
54
|
+
// Loads a language and returns a highlighter for it, or null for a language
|
|
55
|
+
// that is not listed.
|
|
56
|
+
async function loadHighlighter(language) {
|
|
57
|
+
const lang = getLanguage(language);
|
|
58
|
+
if (!lang) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const shiki = await getHighlighter();
|
|
62
|
+
if (lang !== "ansi" && !shiki.getLoadedLanguages().includes(lang)) {
|
|
63
|
+
await shiki.loadLanguage(languages[lang]);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
// Returns one React node per line, and the grammar state after the last
|
|
67
|
+
// line. Pass that state back to continue where the code left off.
|
|
68
|
+
highlight(code, state) {
|
|
69
|
+
const tokenize = (text) => shiki.codeToTokens(text, {
|
|
70
|
+
lang,
|
|
71
|
+
themes: { light: "github-light", dark: "github-dark" },
|
|
72
|
+
defaultColor: false,
|
|
73
|
+
grammarState: state,
|
|
74
|
+
}).tokens;
|
|
75
|
+
// ANSI has no grammar state, so each line is tokenized on its own and
|
|
76
|
+
// gets the same colors whether it arrives in one piece or streams.
|
|
77
|
+
const tokens = lang === "ansi"
|
|
78
|
+
? code.split("\n").flatMap((line) => tokenize(line))
|
|
79
|
+
: tokenize(code);
|
|
80
|
+
return {
|
|
81
|
+
lines: tokens.map((line) => line.map((token, index) => (jsx("span", { style: token.htmlStyle, className: "text-(--shiki-light) dark:text-(--shiki-dark)", children: token.content }, index)))),
|
|
82
|
+
state: shiki.getLastGrammarState(tokens),
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
// Returns one React node per line, or null for a language that is not listed.
|
|
88
|
+
async function highlightCode(code, language) {
|
|
89
|
+
const highlighter = await loadHighlighter(language);
|
|
90
|
+
return highlighter ? highlighter.highlight(code).lines : null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { highlightCode, loadHighlighter };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// color 用于选项色块预览:基础色取浅色模式的 muted-foreground,主题色取 primary。
|
|
2
|
+
const BASE_COLORS = [
|
|
3
|
+
{ value: 'neutral', label: 'Neutral', color: 'oklch(0.556 0 0)' },
|
|
4
|
+
{ value: 'mist', label: 'Mist', color: 'oklch(0.56 0.021 213.5)' },
|
|
5
|
+
{ value: 'zinc', label: 'Zinc', color: 'oklch(0.552 0.016 285.938)' },
|
|
6
|
+
];
|
|
7
|
+
// value 对应 CSS 的 data-theme 属性,label 用于展示,color 是固定的预览色。
|
|
8
|
+
const COLOR_THEMES = [
|
|
9
|
+
{ value: 'neutral', label: 'Neutral', color: 'oklch(0.205 0 0)' },
|
|
10
|
+
{ value: 'blue', label: 'Blue', color: 'oklch(0.488 0.243 264.376)' },
|
|
11
|
+
{ value: 'green', label: 'Green', color: 'oklch(0.527 0.154 150.069)' },
|
|
12
|
+
{ value: 'orange', label: 'Orange', color: 'oklch(0.553 0.195 38.402)' },
|
|
13
|
+
{ value: 'pink', label: 'Pink', color: 'oklch(0.525 0.223 3.958)' },
|
|
14
|
+
{ value: 'purple', label: 'Purple', color: 'oklch(0.496 0.265 301.924)' },
|
|
15
|
+
{ value: 'red', label: 'Red', color: 'oklch(0.505 0.213 27.518)' },
|
|
16
|
+
{ value: 'rose', label: 'Rose', color: 'oklch(0.514 0.222 16.935)' },
|
|
17
|
+
{ value: 'sky', label: 'Sky', color: 'oklch(0.5 0.134 242.749)' },
|
|
18
|
+
{ value: 'yellow', label: 'Yellow', color: 'oklch(0.852 0.199 91.936)' },
|
|
19
|
+
];
|
|
20
|
+
// 圆角预设的 value 单位为 rem,可直接传给 setRadius,也可用于 borderRadius 预览。
|
|
21
|
+
const RADIUS_OPTIONS = [
|
|
22
|
+
{ value: 0, label: '0' },
|
|
23
|
+
{ value: 0.25, label: '0.25' },
|
|
24
|
+
{ value: 0.5, label: '0.5' },
|
|
25
|
+
{ value: 0.625, label: 'Default' },
|
|
26
|
+
{ value: 0.75, label: '0.75' },
|
|
27
|
+
{ value: 1, label: '1' },
|
|
28
|
+
];
|
|
29
|
+
const DEFAULT_PREFERENCE = {
|
|
30
|
+
baseColor: 'neutral',
|
|
31
|
+
theme: 'neutral',
|
|
32
|
+
radius: 0.625,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { BASE_COLORS, COLOR_THEMES, DEFAULT_PREFERENCE, RADIUS_OPTIONS };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { createContext, useMemo, useState, useRef, useCallback, useLayoutEffect, useEffect, useContext } from 'react';
|
|
4
|
+
import { useHydrated } from '../hooks/use-hydrated.js';
|
|
5
|
+
import { COLOR_THEMES, BASE_COLORS, DEFAULT_PREFERENCE } from '../lib/preference.js';
|
|
6
|
+
|
|
7
|
+
const PreferenceContext = createContext(undefined);
|
|
8
|
+
// 校验选项标识,并将 unknown 收窄为对应的字符串联合类型。
|
|
9
|
+
function isOption(options, value) {
|
|
10
|
+
return options.some((option) => option.value === value);
|
|
11
|
+
}
|
|
12
|
+
// 存储内容不可信:逐项校验,单个字段无效时不影响其他有效偏好。
|
|
13
|
+
function normalizePreference(value, fallback) {
|
|
14
|
+
const candidate = typeof value === 'object' && value !== null ? value : {};
|
|
15
|
+
return {
|
|
16
|
+
baseColor: isOption(BASE_COLORS, candidate.baseColor) ? candidate.baseColor : fallback.baseColor,
|
|
17
|
+
theme: isOption(COLOR_THEMES, candidate.theme) ? candidate.theme : fallback.theme,
|
|
18
|
+
// 预设用于展示,不限制自定义圆角;只接受有限的非负数。
|
|
19
|
+
radius: typeof candidate.radius === 'number' && Number.isFinite(candidate.radius) && candidate.radius >= 0
|
|
20
|
+
? candidate.radius
|
|
21
|
+
: fallback.radius,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
// 解析并校验存储字符串;没有记录或 JSON 损坏时回退到默认值。
|
|
25
|
+
function parsePreference(value, fallback) {
|
|
26
|
+
try {
|
|
27
|
+
return normalizePreference(value === null ? null : JSON.parse(value), fallback);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return fallback;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// 将存储访问与 JSON 解析分开处理,兼容浏览器禁止存储的情况。
|
|
34
|
+
function readPreference(storageKey, fallback) {
|
|
35
|
+
try {
|
|
36
|
+
return parsePreference(localStorage.getItem(storageKey), fallback);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// 存储不可用时,偏好仍可在当前页面内使用。
|
|
40
|
+
return fallback;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// 比较字段值,避免相同偏好触发重复渲染。
|
|
44
|
+
function isSamePreference(current, next) {
|
|
45
|
+
return current.baseColor === next.baseColor && current.theme === next.theme && current.radius === next.radius;
|
|
46
|
+
}
|
|
47
|
+
// 管理基础色、主题配色和圆角;深浅模式继续由 ThemeProvider 管理。
|
|
48
|
+
function PreferenceProvider({ children, defaultPreference, storageKey = 'bad-ui:preference', }) {
|
|
49
|
+
// 先补齐并校验调用方传入的默认值,再作为存储读取和重置时的回退值。
|
|
50
|
+
const { baseColor, theme, radius } = normalizePreference(defaultPreference, DEFAULT_PREFERENCE);
|
|
51
|
+
const defaults = useMemo(() => ({ baseColor, theme, radius }), [baseColor, theme, radius]);
|
|
52
|
+
const hydrated = useHydrated();
|
|
53
|
+
const [storedPreference, setPreference] = useState(() => readPreference(storageKey, defaults));
|
|
54
|
+
// 保存最新偏好,保证同一批次连续修改不同字段时保留先前修改。
|
|
55
|
+
const preferenceRef = useRef(storedPreference);
|
|
56
|
+
// 服务端及客户端水合首帧使用相同默认值,水合后才向子组件公开本地偏好。
|
|
57
|
+
const preference = hydrated ? storedPreference : defaults;
|
|
58
|
+
// 主动修改时才写入存储;跨标签页同步仅更新内存,避免删除后又写回默认值。
|
|
59
|
+
const updatePreference = useCallback((patch) => {
|
|
60
|
+
const current = preferenceRef.current;
|
|
61
|
+
const next = normalizePreference({ ...current, ...patch }, current);
|
|
62
|
+
if (isSamePreference(current, next))
|
|
63
|
+
return;
|
|
64
|
+
preferenceRef.current = next;
|
|
65
|
+
setPreference(next);
|
|
66
|
+
try {
|
|
67
|
+
localStorage.setItem(storageKey, JSON.stringify(next));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// 写入失败不阻断页面偏好更新。
|
|
71
|
+
}
|
|
72
|
+
}, [storageKey]);
|
|
73
|
+
const setBaseColor = useCallback((nextBaseColor) => {
|
|
74
|
+
updatePreference({ baseColor: nextBaseColor });
|
|
75
|
+
}, [updatePreference]);
|
|
76
|
+
// 此处 theme 是配色方案(如 blue),并非 light / dark / system。
|
|
77
|
+
const setTheme = useCallback((nextTheme) => {
|
|
78
|
+
updatePreference({ theme: nextTheme });
|
|
79
|
+
}, [updatePreference]);
|
|
80
|
+
// 圆角以 rem 保存,转换为 CSS 长度的工作由下方 effect 统一完成。
|
|
81
|
+
const setRadius = useCallback((nextRadius) => {
|
|
82
|
+
updatePreference({ radius: nextRadius });
|
|
83
|
+
}, [updatePreference]);
|
|
84
|
+
// 配色通过独立属性组合;圆角以内联变量覆盖主题默认值,不改动深浅模式类名。
|
|
85
|
+
useLayoutEffect(() => {
|
|
86
|
+
if (!hydrated)
|
|
87
|
+
return;
|
|
88
|
+
const root = document.documentElement;
|
|
89
|
+
// 在浏览器绘制前更新属性,分别匹配 base-color 和 themes 中的选择器。
|
|
90
|
+
root.dataset.baseColor = preference.baseColor;
|
|
91
|
+
root.dataset.theme = preference.theme;
|
|
92
|
+
root.style.setProperty('--radius', `${preference.radius}rem`);
|
|
93
|
+
}, [preference, hydrated]);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
// 接收其他标签页的变更;删除或清空存储时恢复默认偏好。
|
|
96
|
+
function handleStorageChange(event) {
|
|
97
|
+
// 只处理 localStorage 中当前键的变化;clear() 的事件 key 为 null。
|
|
98
|
+
if (event.storageArea !== localStorage || (event.key !== null && event.key !== storageKey)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const next = parsePreference(event.newValue, defaults);
|
|
102
|
+
if (isSamePreference(preferenceRef.current, next))
|
|
103
|
+
return;
|
|
104
|
+
preferenceRef.current = next;
|
|
105
|
+
setPreference(next);
|
|
106
|
+
}
|
|
107
|
+
window.addEventListener('storage', handleStorageChange);
|
|
108
|
+
// 卸载或依赖变化时注销监听,避免重复订阅。
|
|
109
|
+
return () => window.removeEventListener('storage', handleStorageChange);
|
|
110
|
+
}, [defaults, storageKey]);
|
|
111
|
+
// 保持 Context 值的引用稳定,避免父组件重渲染引起无关的订阅更新。
|
|
112
|
+
const value = useMemo(() => ({ ...preference, setBaseColor, setTheme, setRadius }), [preference, setBaseColor, setTheme, setRadius]);
|
|
113
|
+
return jsx(PreferenceContext.Provider, { value: value, children: children });
|
|
114
|
+
}
|
|
115
|
+
// 统一读取偏好上下文,在缺少 Provider 时给出明确的使用错误。
|
|
116
|
+
function usePreference() {
|
|
117
|
+
const context = useContext(PreferenceContext);
|
|
118
|
+
if (context === undefined) {
|
|
119
|
+
throw new Error('usePreference must be used within a PreferenceProvider');
|
|
120
|
+
}
|
|
121
|
+
return context;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { PreferenceProvider, usePreference };
|