@allbluecn/ui 0.2.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 (49) hide show
  1. package/package.json +85 -0
  2. package/src/ai-command-button.tsx +36 -0
  3. package/src/ai-stream-text.tsx +37 -0
  4. package/src/alert-dialog.tsx +216 -0
  5. package/src/animated-theme-toggler.tsx +272 -0
  6. package/src/auth-client.ts +108 -0
  7. package/src/auth-layout.tsx +66 -0
  8. package/src/avatar.tsx +129 -0
  9. package/src/badge.tsx +66 -0
  10. package/src/button.tsx +84 -0
  11. package/src/card.tsx +120 -0
  12. package/src/checkbox.tsx +50 -0
  13. package/src/context-menu.tsx +280 -0
  14. package/src/dialog.tsx +183 -0
  15. package/src/dropdown-menu.tsx +286 -0
  16. package/src/empty.tsx +121 -0
  17. package/src/grid-pattern.tsx +87 -0
  18. package/src/icon-packs/index.ts +27 -0
  19. package/src/icon-packs/knowledge.ts +55 -0
  20. package/src/icon-packs/prd.ts +63 -0
  21. package/src/icon-packs/project.ts +59 -0
  22. package/src/icon-picker.tsx +68 -0
  23. package/src/index.ts +23 -0
  24. package/src/input.tsx +36 -0
  25. package/src/item.tsx +213 -0
  26. package/src/kbd.tsx +33 -0
  27. package/src/label.tsx +39 -0
  28. package/src/layout-panel-top.tsx +160 -0
  29. package/src/name-confirm-dialog.tsx +153 -0
  30. package/src/nav-float-button.tsx +301 -0
  31. package/src/notion-avatar.tsx +126 -0
  32. package/src/placeholder-page.tsx +42 -0
  33. package/src/popover.tsx +106 -0
  34. package/src/reference-inline.tsx +53 -0
  35. package/src/reference-search.tsx +52 -0
  36. package/src/resizable.tsx +67 -0
  37. package/src/select.tsx +207 -0
  38. package/src/separator.tsx +45 -0
  39. package/src/sheet.tsx +164 -0
  40. package/src/sidebar.tsx +708 -0
  41. package/src/skeleton.tsx +30 -0
  42. package/src/sonner.tsx +37 -0
  43. package/src/switch.tsx +48 -0
  44. package/src/textarea.tsx +35 -0
  45. package/src/timezone-provider.tsx +55 -0
  46. package/src/tooltip.tsx +72 -0
  47. package/src/use-mobile.ts +36 -0
  48. package/src/user-avatar.tsx +79 -0
  49. package/src/utils.ts +6 -0
package/package.json ADDED
@@ -0,0 +1,85 @@
1
+ {
2
+ "name": "@allbluecn/ui",
3
+ "version": "0.2.0",
4
+ "license": "AGPL-3.0-or-later",
5
+ "type": "module",
6
+ "main": "./src/index.ts",
7
+ "types": "./src/index.ts",
8
+ "exports": {
9
+ ".": "./src/index.ts",
10
+ "./sidebar": "./src/sidebar.tsx",
11
+ "./tooltip": "./src/tooltip.tsx",
12
+ "./dialog": "./src/dialog.tsx",
13
+ "./input": "./src/input.tsx",
14
+ "./button": "./src/button.tsx",
15
+ "./utils": "./src/utils.ts",
16
+ "./auth-client": "./src/auth-client.ts",
17
+ "./sonner": "./src/sonner.tsx",
18
+ "./select": "./src/select.tsx",
19
+ "./badge": "./src/badge.tsx",
20
+ "./alert-dialog": "./src/alert-dialog.tsx",
21
+ "./textarea": "./src/textarea.tsx",
22
+ "./skeleton": "./src/skeleton.tsx",
23
+ "./dropdown-menu": "./src/dropdown-menu.tsx",
24
+ "./switch": "./src/switch.tsx",
25
+ "./label": "./src/label.tsx",
26
+ "./empty": "./src/empty.tsx",
27
+ "./checkbox": "./src/checkbox.tsx",
28
+ "./resizable": "./src/resizable.tsx",
29
+ "./popover": "./src/popover.tsx",
30
+ "./context-menu": "./src/context-menu.tsx",
31
+ "./card": "./src/card.tsx",
32
+ "./placeholder-page": "./src/placeholder-page.tsx",
33
+ "./name-confirm-dialog": "./src/name-confirm-dialog.tsx",
34
+ "./nav-float-button": "./src/nav-float-button.tsx",
35
+ "./icon-picker": "./src/icon-picker.tsx",
36
+ "./icon-packs": "./src/icon-packs/index.ts",
37
+ "./icon-packs/prd": "./src/icon-packs/prd.ts",
38
+ "./icon-packs/knowledge": "./src/icon-packs/knowledge.ts",
39
+ "./icon-packs/project": "./src/icon-packs/project.ts",
40
+ "./grid-pattern": "./src/grid-pattern.tsx",
41
+ "./layout-panel-top": "./src/layout-panel-top.tsx",
42
+ "./timezone-provider": "./src/timezone-provider.tsx",
43
+ "./auth-layout": "./src/auth-layout.tsx",
44
+ "./animated-theme-toggler": "./src/animated-theme-toggler.tsx",
45
+ "./separator": "./src/separator.tsx",
46
+ "./notion-avatar": "./src/notion-avatar.tsx",
47
+ "./avatar": "./src/avatar.tsx",
48
+ "./item": "./src/item.tsx",
49
+ "./kbd": "./src/kbd.tsx"
50
+ },
51
+ "dependencies": {
52
+ "@allbluecn/shared": "workspace:*",
53
+ "@tanstack/react-query": "^5.101.2",
54
+ "@tanstack/react-hotkeys": "^0.10.0",
55
+ "class-variance-authority": "^0.7.1",
56
+ "clsx": "^2.1.1",
57
+ "lucide-react": "^1.24.0",
58
+ "motion": "^12.42.2",
59
+ "radix-ui": "^1.4.3",
60
+ "react": "^19",
61
+ "react-i18next": "^15.7.4",
62
+ "react-resizable-panels": "^4.11.1",
63
+ "sonner": "^2.0.7",
64
+ "next-themes": "^0.4.6",
65
+ "tailwind-merge": "^3.5.0"
66
+ },
67
+ "devDependencies": {
68
+ "@types/react": "^19",
69
+ "@types/react-dom": "^19",
70
+ "typescript": "^7"
71
+ },
72
+ "repository": {
73
+ "type": "git",
74
+ "url": "git+https://github.com/urmynami/AllBlue.git"
75
+ },
76
+ "homepage": "https://github.com/urmynami/AllBlue#readme",
77
+ "bugs": {
78
+ "url": "https://github.com/urmynami/AllBlue/issues"
79
+ },
80
+ "files": [
81
+ "src",
82
+ "!src/**/*.test.*",
83
+ "!src/**/__tests__/**"
84
+ ]
85
+ }
@@ -0,0 +1,36 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ import type { AICommandDef } from "@allbluecn/shared";
19
+
20
+ interface Props {
21
+ command: AICommandDef;
22
+ onClick: () => void;
23
+ loading?: boolean;
24
+ }
25
+
26
+ export function AICommandButton({ command, onClick, loading }: Props) {
27
+ return (
28
+ <button
29
+ className="inline-flex items-center gap-1 px-2 py-1 text-xs rounded bg-primary/10 text-primary hover:bg-primary/20"
30
+ onClick={onClick}
31
+ disabled={loading}
32
+ >
33
+ {loading ? "..." : command.label}
34
+ </button>
35
+ );
36
+ }
@@ -0,0 +1,37 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ "use client"
19
+
20
+ interface Props {
21
+ text: string
22
+ streaming?: boolean
23
+ className?: string
24
+ }
25
+
26
+ export function AIStreamText({ text, streaming = false, className }: Props) {
27
+ if (!text && !streaming) return null
28
+
29
+ return (
30
+ <div className={"prose prose-sm max-w-none" + (className ? " " + className : "")}>
31
+ {text || "\u00A0"}
32
+ {streaming && (
33
+ <span className="inline-block w-2 h-4 ml-0.5 bg-primary animate-pulse rounded-sm align-middle" />
34
+ )}
35
+ </div>
36
+ )
37
+ }
@@ -0,0 +1,216 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ "use client"
19
+
20
+ import * as React from "react"
21
+ import { AlertDialog as AlertDialogPrimitive } from "radix-ui"
22
+
23
+ import { cn } from "./utils"
24
+ import { Button } from "./button"
25
+
26
+ function AlertDialog({
27
+ ...props
28
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Root>) {
29
+ return <AlertDialogPrimitive.Root data-slot="alert-dialog" {...props} />
30
+ }
31
+
32
+ function AlertDialogTrigger({
33
+ ...props
34
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Trigger>) {
35
+ return (
36
+ <AlertDialogPrimitive.Trigger data-slot="alert-dialog-trigger" {...props} />
37
+ )
38
+ }
39
+
40
+ function AlertDialogPortal({
41
+ ...props
42
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Portal>) {
43
+ return (
44
+ <AlertDialogPrimitive.Portal data-slot="alert-dialog-portal" {...props} />
45
+ )
46
+ }
47
+
48
+ function AlertDialogOverlay({
49
+ className,
50
+ ...props
51
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Overlay>) {
52
+ return (
53
+ <AlertDialogPrimitive.Overlay
54
+ data-slot="alert-dialog-overlay"
55
+ className={cn(
56
+ "fixed inset-0 z-50 bg-black/10 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
57
+ className
58
+ )}
59
+ {...props}
60
+ />
61
+ )
62
+ }
63
+
64
+ function AlertDialogContent({
65
+ className,
66
+ size = "default",
67
+ ...props
68
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Content> & {
69
+ size?: "default" | "sm"
70
+ }) {
71
+ return (
72
+ <AlertDialogPortal>
73
+ <AlertDialogOverlay />
74
+ <AlertDialogPrimitive.Content
75
+ data-slot="alert-dialog-content"
76
+ data-size={size}
77
+ className={cn(
78
+ "group/alert-dialog-content fixed top-1/2 left-1/2 z-50 grid w-full -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none data-[size=default]:max-w-xs data-[size=sm]:max-w-xs data-[size=default]:sm:max-w-sm 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",
79
+ className
80
+ )}
81
+ {...props}
82
+ />
83
+ </AlertDialogPortal>
84
+ )
85
+ }
86
+
87
+ function AlertDialogHeader({
88
+ className,
89
+ ...props
90
+ }: React.ComponentProps<"div">) {
91
+ return (
92
+ <div
93
+ data-slot="alert-dialog-header"
94
+ className={cn(
95
+ "grid grid-rows-[auto_1fr] place-items-center gap-1.5 text-center has-data-[slot=alert-dialog-media]:grid-rows-[auto_auto_1fr] has-data-[slot=alert-dialog-media]:gap-x-4 sm:group-data-[size=default]/alert-dialog-content:place-items-start sm:group-data-[size=default]/alert-dialog-content:text-left sm:group-data-[size=default]/alert-dialog-content:has-data-[slot=alert-dialog-media]:grid-rows-[auto_1fr]",
96
+ className
97
+ )}
98
+ {...props}
99
+ />
100
+ )
101
+ }
102
+
103
+ function AlertDialogFooter({
104
+ className,
105
+ ...props
106
+ }: React.ComponentProps<"div">) {
107
+ return (
108
+ <div
109
+ data-slot="alert-dialog-footer"
110
+ className={cn(
111
+ "-mx-4 -mb-4 flex flex-col-reverse gap-2 rounded-b-xl border-t bg-muted/50 p-4 group-data-[size=sm]/alert-dialog-content:grid group-data-[size=sm]/alert-dialog-content:grid-cols-2 sm:flex-row sm:justify-end",
112
+ className
113
+ )}
114
+ {...props}
115
+ />
116
+ )
117
+ }
118
+
119
+ function AlertDialogMedia({
120
+ className,
121
+ ...props
122
+ }: React.ComponentProps<"div">) {
123
+ return (
124
+ <div
125
+ data-slot="alert-dialog-media"
126
+ className={cn(
127
+ "mb-2 inline-flex size-10 items-center justify-center rounded-md bg-muted sm:group-data-[size=default]/alert-dialog-content:row-span-2 *:[svg:not([class*='size-'])]:size-6",
128
+ className
129
+ )}
130
+ {...props}
131
+ />
132
+ )
133
+ }
134
+
135
+ function AlertDialogTitle({
136
+ className,
137
+ ...props
138
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Title>) {
139
+ return (
140
+ <AlertDialogPrimitive.Title
141
+ data-slot="alert-dialog-title"
142
+ className={cn(
143
+ "text-base font-medium sm:group-data-[size=default]/alert-dialog-content:group-has-data-[slot=alert-dialog-media]/alert-dialog-content:col-start-2",
144
+ className
145
+ )}
146
+ {...props}
147
+ />
148
+ )
149
+ }
150
+
151
+ function AlertDialogDescription({
152
+ className,
153
+ ...props
154
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Description>) {
155
+ return (
156
+ <AlertDialogPrimitive.Description
157
+ data-slot="alert-dialog-description"
158
+ className={cn(
159
+ "text-sm text-balance text-muted-foreground md:text-pretty *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground",
160
+ className
161
+ )}
162
+ {...props}
163
+ />
164
+ )
165
+ }
166
+
167
+ function AlertDialogAction({
168
+ className,
169
+ variant = "default",
170
+ size = "default",
171
+ ...props
172
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Action> &
173
+ Pick<React.ComponentProps<typeof Button>, "variant" | "size">) {
174
+ return (
175
+ <Button variant={variant} size={size} asChild>
176
+ <AlertDialogPrimitive.Action
177
+ data-slot="alert-dialog-action"
178
+ className={cn(className)}
179
+ {...props}
180
+ />
181
+ </Button>
182
+ )
183
+ }
184
+
185
+ function AlertDialogCancel({
186
+ className,
187
+ variant = "outline",
188
+ size = "default",
189
+ ...props
190
+ }: React.ComponentProps<typeof AlertDialogPrimitive.Cancel> &
191
+ Pick<React.ComponentProps<typeof Button>, "variant" | "size">) {
192
+ return (
193
+ <Button variant={variant} size={size} asChild>
194
+ <AlertDialogPrimitive.Cancel
195
+ data-slot="alert-dialog-cancel"
196
+ className={cn(className)}
197
+ {...props}
198
+ />
199
+ </Button>
200
+ )
201
+ }
202
+
203
+ export {
204
+ AlertDialog,
205
+ AlertDialogAction,
206
+ AlertDialogCancel,
207
+ AlertDialogContent,
208
+ AlertDialogDescription,
209
+ AlertDialogFooter,
210
+ AlertDialogHeader,
211
+ AlertDialogMedia,
212
+ AlertDialogOverlay,
213
+ AlertDialogPortal,
214
+ AlertDialogTitle,
215
+ AlertDialogTrigger,
216
+ }
@@ -0,0 +1,272 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+
19
+ import { useCallback, useEffect, useRef, useState } from "react"
20
+ import { Moon, Sun } from "lucide-react"
21
+ import { flushSync } from "react-dom"
22
+ import { useTheme } from "next-themes"
23
+
24
+ import { cn } from "./utils"
25
+
26
+ export type TransitionVariant =
27
+ | "circle"
28
+ | "square"
29
+ | "triangle"
30
+ | "diamond"
31
+ | "hexagon"
32
+ | "rectangle"
33
+ | "star"
34
+
35
+ interface AnimatedThemeTogglerProps extends React.ComponentPropsWithoutRef<"button"> {
36
+ duration?: number
37
+ variant?: TransitionVariant
38
+ /** When true, the transition expands from the viewport center instead of the button center. */
39
+ fromCenter?: boolean
40
+ }
41
+
42
+ function polygonCollapsed(cx: number, cy: number, vertexCount: number): string {
43
+ const pairs = Array.from(
44
+ { length: vertexCount },
45
+ () => `${cx}px ${cy}px`
46
+ ).join(", ")
47
+ return `polygon(${pairs})`
48
+ }
49
+
50
+ function getThemeTransitionClipPaths(
51
+ variant: TransitionVariant,
52
+ cx: number,
53
+ cy: number,
54
+ maxRadius: number,
55
+ viewportWidth: number,
56
+ viewportHeight: number
57
+ ): [string, string] {
58
+ switch (variant) {
59
+ case "circle":
60
+ return [
61
+ `circle(0px at ${cx}px ${cy}px)`,
62
+ `circle(${maxRadius}px at ${cx}px ${cy}px)`,
63
+ ]
64
+ case "square": {
65
+ const halfW = Math.max(cx, viewportWidth - cx)
66
+ const halfH = Math.max(cy, viewportHeight - cy)
67
+ const halfSide = Math.max(halfW, halfH) * 1.05
68
+ const end = [
69
+ `${cx - halfSide}px ${cy - halfSide}px`,
70
+ `${cx + halfSide}px ${cy - halfSide}px`,
71
+ `${cx + halfSide}px ${cy + halfSide}px`,
72
+ `${cx - halfSide}px ${cy + halfSide}px`,
73
+ ].join(", ")
74
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
75
+ }
76
+ case "triangle": {
77
+ const scale = maxRadius * 2.2
78
+ const dx = (Math.sqrt(3) / 2) * scale
79
+ const verts = [
80
+ `${cx}px ${cy - scale}px`,
81
+ `${cx + dx}px ${cy + 0.5 * scale}px`,
82
+ `${cx - dx}px ${cy + 0.5 * scale}px`,
83
+ ].join(", ")
84
+ return [polygonCollapsed(cx, cy, 3), `polygon(${verts})`]
85
+ }
86
+ case "diamond": {
87
+ // Slightly larger than the view-transition circle radius so axis-aligned coverage matches the circle reveal.
88
+ const R = maxRadius * Math.SQRT2
89
+ const end = [
90
+ `${cx}px ${cy - R}px`,
91
+ `${cx + R}px ${cy}px`,
92
+ `${cx}px ${cy + R}px`,
93
+ `${cx - R}px ${cy}px`,
94
+ ].join(", ")
95
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
96
+ }
97
+ case "hexagon": {
98
+ const R = maxRadius * Math.SQRT2
99
+ const verts: string[] = []
100
+ for (let i = 0; i < 6; i++) {
101
+ const a = -Math.PI / 2 + (i * Math.PI) / 3
102
+ verts.push(`${cx + R * Math.cos(a)}px ${cy + R * Math.sin(a)}px`)
103
+ }
104
+ return [polygonCollapsed(cx, cy, 6), `polygon(${verts.join(", ")})`]
105
+ }
106
+ case "rectangle": {
107
+ const halfW = Math.max(cx, viewportWidth - cx)
108
+ const halfH = Math.max(cy, viewportHeight - cy)
109
+ const end = [
110
+ `${cx - halfW}px ${cy - halfH}px`,
111
+ `${cx + halfW}px ${cy - halfH}px`,
112
+ `${cx + halfW}px ${cy + halfH}px`,
113
+ `${cx - halfW}px ${cy + halfH}px`,
114
+ ].join(", ")
115
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
116
+ }
117
+ case "star": {
118
+ // Small overscan so the last frames never leave a 1px seam before the transition group ends.
119
+ const R = maxRadius * Math.SQRT2 * 1.03
120
+ const innerRatio = 0.42
121
+ const starPolygon = (radius: number) => {
122
+ const verts: string[] = []
123
+ for (let i = 0; i < 5; i++) {
124
+ const outerA = -Math.PI / 2 + (i * 2 * Math.PI) / 5
125
+ verts.push(
126
+ `${cx + radius * Math.cos(outerA)}px ${cy + radius * Math.sin(outerA)}px`
127
+ )
128
+ const innerA = outerA + Math.PI / 5
129
+ verts.push(
130
+ `${cx + radius * innerRatio * Math.cos(innerA)}px ${cy + radius * innerRatio * Math.sin(innerA)}px`
131
+ )
132
+ }
133
+ return `polygon(${verts.join(", ")})`
134
+ }
135
+ const startR = Math.max(2, R * 0.025)
136
+ return [starPolygon(startR), starPolygon(R)]
137
+ }
138
+ default:
139
+ return [
140
+ `circle(0px at ${cx}px ${cy}px)`,
141
+ `circle(${maxRadius}px at ${cx}px ${cy}px)`,
142
+ ]
143
+ }
144
+ }
145
+
146
+ export const AnimatedThemeToggler = ({
147
+ className,
148
+ duration = 400,
149
+ variant,
150
+ fromCenter = false,
151
+ ...props
152
+ }: AnimatedThemeTogglerProps) => {
153
+ const shape = variant ?? "circle"
154
+ const { theme, setTheme } = useTheme()
155
+ const [isDark, setIsDark] = useState(false)
156
+ const buttonRef = useRef<HTMLButtonElement>(null)
157
+
158
+ useEffect(() => {
159
+ setIsDark(theme === "dark")
160
+ }, [theme])
161
+
162
+ const toggleTheme = useCallback(() => {
163
+ const button = buttonRef.current
164
+ if (!button) return
165
+
166
+ const viewportWidth = window.visualViewport?.width ?? window.innerWidth
167
+ const viewportHeight = window.visualViewport?.height ?? window.innerHeight
168
+
169
+ let x: number
170
+ let y: number
171
+ if (fromCenter) {
172
+ x = viewportWidth / 2
173
+ y = viewportHeight / 2
174
+ } else {
175
+ const { top, left, width, height } = button.getBoundingClientRect()
176
+ x = left + width / 2
177
+ y = top + height / 2
178
+ }
179
+
180
+ const maxRadius = Math.hypot(
181
+ Math.max(x, viewportWidth - x),
182
+ Math.max(y, viewportHeight - y)
183
+ )
184
+
185
+ const newTheme = !isDark
186
+
187
+ const applyTheme = () => {
188
+ setIsDark(newTheme)
189
+ document.documentElement.classList.toggle("dark", newTheme)
190
+ }
191
+
192
+ const syncNextThemes = () => {
193
+ setTheme(newTheme ? "dark" : "light")
194
+ }
195
+
196
+ if (typeof document.startViewTransition !== "function") {
197
+ applyTheme()
198
+ syncNextThemes()
199
+ return
200
+ }
201
+
202
+ const clipPath = getThemeTransitionClipPaths(
203
+ shape,
204
+ x,
205
+ y,
206
+ maxRadius,
207
+ viewportWidth,
208
+ viewportHeight
209
+ )
210
+
211
+ const root = document.documentElement
212
+ root.dataset.magicuiThemeVt = "active"
213
+ root.style.setProperty(
214
+ "--magicui-theme-toggle-vt-duration",
215
+ `${duration}ms`
216
+ )
217
+ // Pin the collapsed clip-path via CSS so Firefox does not paint the new
218
+ // theme unclipped between snapshot and the ready.then() JS animation.
219
+ root.style.setProperty("--magicui-theme-vt-clip-from", clipPath[0])
220
+ const cleanup = () => {
221
+ delete root.dataset.magicuiThemeVt
222
+ root.style.removeProperty("--magicui-theme-toggle-vt-duration")
223
+ root.style.removeProperty("--magicui-theme-vt-clip-from")
224
+ }
225
+
226
+ const transition = document.startViewTransition(() => {
227
+ flushSync(applyTheme)
228
+ })
229
+
230
+ const onFinish = () => {
231
+ cleanup()
232
+ syncNextThemes()
233
+ }
234
+
235
+ if (typeof transition?.finished?.then === "function") {
236
+ transition.finished.then(onFinish)
237
+ } else {
238
+ onFinish()
239
+ }
240
+
241
+ const ready = transition?.ready
242
+ if (ready && typeof ready.then === "function") {
243
+ ready.then(() => {
244
+ document.documentElement.animate(
245
+ {
246
+ clipPath,
247
+ },
248
+ {
249
+ duration,
250
+ // Star: linear avoids easing overshoot that fights polygon interpolation at t→1; VT group duration is synced above.
251
+ easing: shape === "star" ? "linear" : "ease-in-out",
252
+ fill: "forwards",
253
+ pseudoElement: "::view-transition-new(root)",
254
+ }
255
+ )
256
+ })
257
+ }
258
+ }, [shape, fromCenter, duration, isDark, setTheme])
259
+
260
+ return (
261
+ <button
262
+ type="button"
263
+ ref={buttonRef}
264
+ onClick={toggleTheme}
265
+ className={cn(className)}
266
+ {...props}
267
+ >
268
+ {isDark ? <Sun className={cn("size-5", className)} /> : <Moon className={cn("size-5", className)} />}
269
+ <span className="sr-only">Toggle theme</span>
270
+ </button>
271
+ )
272
+ }