@dinachi/cli 0.9.0 → 0.11.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.
- package/CHANGELOG.md +101 -0
- package/README.md +34 -5
- package/dist/index.js +294 -63
- package/package.json +8 -6
- package/templates/accordion/accordion.tsx +4 -5
- package/templates/alert-dialog/alert-dialog.tsx +9 -2
- package/templates/animated-icon/animated-icon.tsx +145 -0
- package/templates/animated-icon/index.ts +1 -0
- package/templates/animated-list/animated-list.tsx +161 -0
- package/templates/animated-list/index.ts +2 -0
- package/templates/animated-tabs/animated-tabs.tsx +244 -0
- package/templates/animated-tabs/index.ts +13 -0
- package/templates/autocomplete/autocomplete.tsx +6 -4
- package/templates/button/button.tsx +19 -2
- package/templates/carousel/carousel.tsx +492 -0
- package/templates/carousel/index.ts +1 -0
- package/templates/collapsible/collapsible.tsx +3 -7
- package/templates/combobox/combobox.tsx +6 -4
- package/templates/compare-slider/compare-slider.tsx +383 -0
- package/templates/compare-slider/index.ts +2 -0
- package/templates/context-menu/context-menu.tsx +10 -6
- package/templates/dialog/dialog.tsx +9 -2
- package/templates/drawer/drawer.tsx +24 -2
- package/templates/expandable-card/expandable-card.tsx +509 -0
- package/templates/expandable-card/index.ts +1 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +580 -0
- package/templates/hold-to-confirm/index.ts +2 -0
- package/templates/load-transition/index.ts +1 -0
- package/templates/load-transition/load-transition.tsx +163 -0
- package/templates/marquee/index.ts +2 -0
- package/templates/marquee/marquee.tsx +256 -0
- package/templates/menu/menu.tsx +10 -6
- package/templates/menubar/menubar.tsx +10 -6
- package/templates/meter/meter.tsx +1 -1
- package/templates/navigation-menu/navigation-menu.tsx +9 -6
- package/templates/number-ticker/index.ts +2 -0
- package/templates/number-ticker/number-ticker.tsx +470 -0
- package/templates/otp-field/otp-field.tsx +3 -2
- package/templates/popover/popover.tsx +7 -6
- package/templates/preview-card/preview-card.tsx +2 -1
- package/templates/progress/progress.tsx +1 -1
- package/templates/progress-ring/index.ts +1 -0
- package/templates/progress-ring/progress-ring.tsx +268 -0
- package/templates/scroll-progress/index.ts +2 -0
- package/templates/scroll-progress/scroll-progress.tsx +147 -0
- package/templates/scroll-reveal/index.ts +2 -0
- package/templates/scroll-reveal/scroll-reveal.tsx +198 -0
- package/templates/select/select.tsx +18 -4
- package/templates/slider/slider.tsx +3 -2
- package/templates/sortable/index.ts +1 -0
- package/templates/sortable/sortable.tsx +349 -0
- package/templates/stagger-list/index.ts +2 -0
- package/templates/stagger-list/stagger-list.tsx +204 -0
- package/templates/streaming-text/index.ts +2 -0
- package/templates/streaming-text/streaming-text.tsx +220 -0
- package/templates/swipeable-row/index.ts +2 -0
- package/templates/swipeable-row/swipeable-row.tsx +368 -0
- package/templates/switch/switch.tsx +4 -2
- package/templates/tabs/tabs.tsx +10 -2
- package/templates/text-morph/index.ts +2 -0
- package/templates/text-morph/text-morph.tsx +265 -0
- package/templates/text-shimmer/index.ts +2 -0
- package/templates/text-shimmer/text-shimmer.tsx +151 -0
- package/templates/textarea/textarea.tsx +1 -2
- package/templates/toast/toast.tsx +24 -19
- package/templates/toggle/toggle.tsx +1 -1
- package/templates/tooltip/tooltip.tsx +10 -4
- package/templates/tsconfig.json +1 -0
- package/templates/utils/variants.ts +0 -6
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import {
|
|
5
|
+
AnimatePresence,
|
|
6
|
+
motion,
|
|
7
|
+
MotionConfigContext,
|
|
8
|
+
useReducedMotion,
|
|
9
|
+
type Target,
|
|
10
|
+
} from "motion/react"
|
|
11
|
+
import { cn } from "@/lib/utils"
|
|
12
|
+
|
|
13
|
+
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
14
|
+
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
15
|
+
* reaches this component. */
|
|
16
|
+
function useReducedMotionConfig() {
|
|
17
|
+
const config = React.useContext(MotionConfigContext)
|
|
18
|
+
const prefersReduced = useReducedMotion()
|
|
19
|
+
if (config.reducedMotion === "always") return true
|
|
20
|
+
if (config.reducedMotion === "never") return false
|
|
21
|
+
return prefersReduced ?? false
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The budget for a control someone presses dozens of times a day. Short enough that it never
|
|
26
|
+
* sits between the press and the answer: no bounce, no delay.
|
|
27
|
+
*/
|
|
28
|
+
const SNAP = { duration: 0.15, ease: [0.23, 1, 0.32, 1] } as const
|
|
29
|
+
|
|
30
|
+
export type AnimatedIconMode = "scale" | "rotate" | "flip" | "fade"
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* How the outgoing icon leaves and the incoming one arrives.
|
|
34
|
+
*
|
|
35
|
+
* All four are transforms and opacity, so any pair of icons works — including two that share
|
|
36
|
+
* no geometry at all. `exit` deliberately mirrors `initial` rather than repeating it: the
|
|
37
|
+
* icon that leaves goes the way the next one is coming from, so the pair reads as one
|
|
38
|
+
* movement rather than as two.
|
|
39
|
+
*/
|
|
40
|
+
const MODES: Record<AnimatedIconMode, { initial: Target; animate: Target; exit: Target }> = {
|
|
41
|
+
// Nothing appears from nothing: 0.7 rather than 0, so the icon is always a shape.
|
|
42
|
+
scale: {
|
|
43
|
+
initial: { opacity: 0, scale: 0.7 },
|
|
44
|
+
animate: { opacity: 1, scale: 1 },
|
|
45
|
+
exit: { opacity: 0, scale: 0.7 },
|
|
46
|
+
},
|
|
47
|
+
rotate: {
|
|
48
|
+
initial: { opacity: 0, scale: 0.7, rotate: -90 },
|
|
49
|
+
animate: { opacity: 1, scale: 1, rotate: 0 },
|
|
50
|
+
exit: { opacity: 0, scale: 0.7, rotate: 90 },
|
|
51
|
+
},
|
|
52
|
+
flip: {
|
|
53
|
+
initial: { opacity: 0, rotateX: -90 },
|
|
54
|
+
animate: { opacity: 1, rotateX: 0 },
|
|
55
|
+
exit: { opacity: 0, rotateX: 90 },
|
|
56
|
+
},
|
|
57
|
+
fade: {
|
|
58
|
+
initial: { opacity: 0 },
|
|
59
|
+
animate: { opacity: 1 },
|
|
60
|
+
exit: { opacity: 0 },
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface AnimatedIconProps
|
|
65
|
+
extends Omit<React.ComponentProps<"span">, "children"> {
|
|
66
|
+
/** Which of the two is showing. `false` shows `from`, `true` shows `to`. */
|
|
67
|
+
active: boolean
|
|
68
|
+
/** The resting icon. Any element: a lucide icon, an inline `<svg>`, an emoji. */
|
|
69
|
+
from: React.ReactNode
|
|
70
|
+
/** The icon for the active state. */
|
|
71
|
+
to: React.ReactNode
|
|
72
|
+
mode?: AnimatedIconMode
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Two icons in one place, trading with a transition instead of a swap.
|
|
77
|
+
*
|
|
78
|
+
* The value is not that the icon is animated. It is that the two states read as the *same
|
|
79
|
+
* object* changing, so the control is one toggle rather than two buttons taking turns. Swap
|
|
80
|
+
* the element outright and the eye registers a replacement; move it and the eye registers a
|
|
81
|
+
* state.
|
|
82
|
+
*
|
|
83
|
+
* This is a shell, not a set of icons. Give it any two — play and pause, volume and mute,
|
|
84
|
+
* copy and check, a lucide pair, your own SVG — and it handles the crossfade, the stacking,
|
|
85
|
+
* the sizing and the reduced-motion path. It cannot morph one path into another, because
|
|
86
|
+
* that requires two paths written with the same commands in the same order and no two icons
|
|
87
|
+
* from a library ever are. What it does instead is a transform pair, which works for every
|
|
88
|
+
* combination rather than for the handful that happen to be compatible.
|
|
89
|
+
*
|
|
90
|
+
* Both icons live in one grid cell, so nothing reflows mid-transition, and the cell owns the
|
|
91
|
+
* size: `className="size-8"` and both of them are 8. Sizing each icon separately is the
|
|
92
|
+
* thing that goes wrong here — an icon library ships an intrinsic 24×24 that a smaller box
|
|
93
|
+
* squashes on one axis, and two icons sized apart are two icons that jump.
|
|
94
|
+
*
|
|
95
|
+
* It carries `aria-hidden`, because it is decoration inside a control and the control is what
|
|
96
|
+
* gets named. If the icon is the only thing in a button, name the button.
|
|
97
|
+
*/
|
|
98
|
+
const AnimatedIcon = React.forwardRef<HTMLSpanElement, AnimatedIconProps>(
|
|
99
|
+
({ active, from, to, mode = "scale", className, style, ...props }, ref) => {
|
|
100
|
+
const reducedMotion = useReducedMotionConfig()
|
|
101
|
+
const variants = MODES[mode]
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<span
|
|
105
|
+
ref={ref}
|
|
106
|
+
aria-hidden
|
|
107
|
+
// A grid rather than a stack of absolutely positioned children: both icons occupy
|
|
108
|
+
// one cell, so nothing reflows mid-transition and neither one has to be measured.
|
|
109
|
+
//
|
|
110
|
+
// The box owns the size, and the icons fill it. An icon library ships its own
|
|
111
|
+
// intrinsic 24×24, which a smaller cell would squash on one axis and a larger one
|
|
112
|
+
// would leave floating in the middle — so the size is stated once, here, and both
|
|
113
|
+
// icons are made to agree with it.
|
|
114
|
+
className={cn(
|
|
115
|
+
"relative inline-grid size-5 shrink-0 place-items-center [&_svg]:size-full",
|
|
116
|
+
className
|
|
117
|
+
)}
|
|
118
|
+
// `flip` turns around an axis in the plane of the screen, which is a rotation you
|
|
119
|
+
// cannot see without a vanishing point. The others do not need one.
|
|
120
|
+
style={mode === "flip" ? { perspective: 400, ...style } : style}
|
|
121
|
+
{...props}
|
|
122
|
+
>
|
|
123
|
+
{/* `initial={false}` so the icon is simply present on first paint. An entrance on
|
|
124
|
+
mount would animate every icon on the page during hydration. */}
|
|
125
|
+
<AnimatePresence initial={false} mode="sync">
|
|
126
|
+
<motion.span
|
|
127
|
+
key={active ? "to" : "from"}
|
|
128
|
+
className="col-start-1 row-start-1 inline-flex size-full"
|
|
129
|
+
initial={variants.initial}
|
|
130
|
+
animate={variants.animate}
|
|
131
|
+
exit={variants.exit}
|
|
132
|
+
// Reduced motion keeps the change and drops the movement. At zero duration the
|
|
133
|
+
// two states cut, which is the correct answer for a control this frequent.
|
|
134
|
+
transition={reducedMotion ? { duration: 0 } : SNAP}
|
|
135
|
+
>
|
|
136
|
+
{active ? to : from}
|
|
137
|
+
</motion.span>
|
|
138
|
+
</AnimatePresence>
|
|
139
|
+
</span>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
AnimatedIcon.displayName = "AnimatedIcon"
|
|
144
|
+
|
|
145
|
+
export { AnimatedIcon }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './animated-icon'
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import {
|
|
5
|
+
AnimatePresence,
|
|
6
|
+
motion,
|
|
7
|
+
MotionConfigContext,
|
|
8
|
+
useReducedMotion,
|
|
9
|
+
} from "motion/react"
|
|
10
|
+
import { cn } from "@/lib/utils"
|
|
11
|
+
|
|
12
|
+
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
13
|
+
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
14
|
+
* reaches this component. */
|
|
15
|
+
function useReducedMotionConfig() {
|
|
16
|
+
const config = React.useContext(MotionConfigContext)
|
|
17
|
+
const prefersReduced = useReducedMotion()
|
|
18
|
+
if (config.reducedMotion === "always") return true
|
|
19
|
+
if (config.reducedMotion === "never") return false
|
|
20
|
+
return prefersReduced ?? false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]
|
|
24
|
+
|
|
25
|
+
const DEFAULT_DURATION = 0.3
|
|
26
|
+
|
|
27
|
+
interface AnimatedListContextValue {
|
|
28
|
+
from: "top" | "bottom"
|
|
29
|
+
duration: number
|
|
30
|
+
reducedMotion: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const AnimatedListContext = React.createContext<AnimatedListContextValue | null>(null)
|
|
34
|
+
|
|
35
|
+
export interface AnimatedListProps extends React.ComponentProps<"ul"> {
|
|
36
|
+
/** Which edge new rows arrive from. The direction of travel is a claim about where a
|
|
37
|
+
* row came from, so a feed that grows downward should not have its rows rising. */
|
|
38
|
+
from?: "top" | "bottom"
|
|
39
|
+
/**
|
|
40
|
+
* The key of the row the reader closed themselves, if any.
|
|
41
|
+
*
|
|
42
|
+
* A row the reader dismissed and a row the server retracted are the same removal to
|
|
43
|
+
* React and two different events to the reader, so they leave differently: the first
|
|
44
|
+
* slides out towards the control that was just pressed, the second collapses in place.
|
|
45
|
+
*
|
|
46
|
+
* Set it in the same update that removes the row. By the time the row is leaving it is
|
|
47
|
+
* already gone from the list, so nothing captured at render can still say why — this
|
|
48
|
+
* value rides `AnimatePresence`'s `custom`, which is the one channel motion re-reads
|
|
49
|
+
* when it resolves an exit. Leave it unset and every removal collapses in place.
|
|
50
|
+
*/
|
|
51
|
+
dismissed?: string | null
|
|
52
|
+
/** Seconds for each row's own animation. The reflow spring is scaled from it. */
|
|
53
|
+
duration?: number
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A list whose contents change under the reader.
|
|
58
|
+
*
|
|
59
|
+
* Entering and leaving are the easy half. The case that decides whether a live list feels
|
|
60
|
+
* built or assembled is the third one: the row that neither entered nor left but had to
|
|
61
|
+
* move because something above it did. That movement is FLIP — motion measures both
|
|
62
|
+
* positions across the commit and animates the difference — so a row that shifts up reads
|
|
63
|
+
* as *displaced* rather than as having been repainted somewhere else.
|
|
64
|
+
*
|
|
65
|
+
* `mode="popLayout"` is what makes entrances, exits and displacement cooperate. An exiting
|
|
66
|
+
* row is pulled out of layout flow immediately and animated on top of it, so the rows
|
|
67
|
+
* below start closing the gap on the same frame the row starts leaving instead of waiting
|
|
68
|
+
* for it to finish.
|
|
69
|
+
*
|
|
70
|
+
* Reduced motion drops the travel but not the reflow. Rows still have to end up in the
|
|
71
|
+
* right place; they just get there in one frame.
|
|
72
|
+
*/
|
|
73
|
+
const AnimatedList = React.forwardRef<HTMLUListElement, AnimatedListProps>(
|
|
74
|
+
(
|
|
75
|
+
{ from = "top", dismissed = null, duration, className, children, ...props },
|
|
76
|
+
ref
|
|
77
|
+
) => {
|
|
78
|
+
const reducedMotion = useReducedMotionConfig()
|
|
79
|
+
const seconds = duration ?? DEFAULT_DURATION
|
|
80
|
+
|
|
81
|
+
const context = React.useMemo(
|
|
82
|
+
() => ({ from, duration: seconds, reducedMotion }),
|
|
83
|
+
[from, seconds, reducedMotion]
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<AnimatedListContext.Provider value={context}>
|
|
88
|
+
<ul ref={ref} className={cn("list-none", className)} {...props}>
|
|
89
|
+
<AnimatePresence mode="popLayout" initial={false} custom={dismissed}>
|
|
90
|
+
{children}
|
|
91
|
+
</AnimatePresence>
|
|
92
|
+
</ul>
|
|
93
|
+
</AnimatedListContext.Provider>
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
AnimatedList.displayName = "AnimatedList"
|
|
98
|
+
|
|
99
|
+
export interface AnimatedListItemProps
|
|
100
|
+
extends React.ComponentProps<typeof motion.li> {
|
|
101
|
+
/** The same string given to React's `key`, which a component cannot read back. Only
|
|
102
|
+
* needed if the list is using `dismissed`. */
|
|
103
|
+
itemKey?: string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const AnimatedListItem = React.forwardRef<HTMLLIElement, AnimatedListItemProps>(
|
|
107
|
+
({ itemKey, className, children, ...props }, ref) => {
|
|
108
|
+
const context = React.useContext(AnimatedListContext)
|
|
109
|
+
|
|
110
|
+
if (!context) {
|
|
111
|
+
throw new Error("AnimatedListItem must be used within an AnimatedList")
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const { from, duration, reducedMotion } = context
|
|
115
|
+
|
|
116
|
+
// `x`, `y` and `scale` rather than the full transform strings the rest of the tier
|
|
117
|
+
// prefers. Those keep motion off the main thread, but `layout` builds the element's
|
|
118
|
+
// transform out of these very values plus its own projection delta — hand it a
|
|
119
|
+
// `transform` string and the two write over each other.
|
|
120
|
+
const variants = {
|
|
121
|
+
exit: (closedByReader: string | null) => {
|
|
122
|
+
if (reducedMotion) return { opacity: 0, x: 0, scale: 1 }
|
|
123
|
+
return closedByReader != null && closedByReader === itemKey
|
|
124
|
+
? { opacity: 0, x: 24, scale: 0.97 }
|
|
125
|
+
: { opacity: 0, x: 0, scale: 0.94 }
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<motion.li
|
|
131
|
+
ref={ref}
|
|
132
|
+
layout
|
|
133
|
+
className={cn(className)}
|
|
134
|
+
// Every key `animate` sets appears in every branch. Dropping one under reduced
|
|
135
|
+
// motion does not disable it — it animates it from `undefined`.
|
|
136
|
+
initial={
|
|
137
|
+
reducedMotion
|
|
138
|
+
? { opacity: 0, x: 0, y: 0, scale: 1 }
|
|
139
|
+
: { opacity: 0, x: 0, y: from === "top" ? -12 : 12, scale: 0.97 }
|
|
140
|
+
}
|
|
141
|
+
animate={{ opacity: 1, x: 0, y: 0, scale: 1 }}
|
|
142
|
+
variants={variants}
|
|
143
|
+
exit="exit"
|
|
144
|
+
transition={{
|
|
145
|
+
layout: reducedMotion
|
|
146
|
+
? { duration: 0 }
|
|
147
|
+
: { type: "spring", bounce: 0.15, duration: duration * 1.4 },
|
|
148
|
+
// Opacity stays on a fixed clock so a row is never left half-visible waiting
|
|
149
|
+
// for a spring to settle.
|
|
150
|
+
default: { duration: reducedMotion ? 0.2 : duration, ease: EASE_OUT },
|
|
151
|
+
}}
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
{children}
|
|
155
|
+
</motion.li>
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
AnimatedListItem.displayName = "AnimatedListItem"
|
|
160
|
+
|
|
161
|
+
export { AnimatedList, AnimatedListItem }
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Tabs as BaseTabs } from "@base-ui/react/tabs"
|
|
5
|
+
import {
|
|
6
|
+
motion,
|
|
7
|
+
MotionConfigContext,
|
|
8
|
+
useReducedMotion,
|
|
9
|
+
type Transition,
|
|
10
|
+
} from "motion/react"
|
|
11
|
+
import { cn } from "@/lib/utils"
|
|
12
|
+
|
|
13
|
+
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
14
|
+
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
15
|
+
* reaches this component. */
|
|
16
|
+
function useReducedMotionConfig() {
|
|
17
|
+
const config = React.useContext(MotionConfigContext)
|
|
18
|
+
const prefersReduced = useReducedMotion()
|
|
19
|
+
if (config.reducedMotion === "always") return true
|
|
20
|
+
if (config.reducedMotion === "never") return false
|
|
21
|
+
return prefersReduced
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type BaseRootProps = React.ComponentProps<typeof BaseTabs.Root>
|
|
25
|
+
|
|
26
|
+
/** Critically damped: a click carries no momentum, so an overshoot on arrival is
|
|
27
|
+
* motion the gesture never asked for. 0.3s is the short end of the response range
|
|
28
|
+
* for a reposition, which is where a control this frequently used belongs. */
|
|
29
|
+
const DEFAULT_TRANSITION: Transition = {
|
|
30
|
+
type: "spring",
|
|
31
|
+
bounce: 0,
|
|
32
|
+
duration: 0.3,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const INSTANT_TRANSITION: Transition = { duration: 0 }
|
|
36
|
+
|
|
37
|
+
/** Enter and Space on a focused tab reach Base UI as a synthesized click — no pointer
|
|
38
|
+
* type, no click count — while arrow keys under `activateOnFocus` arrive as a real key
|
|
39
|
+
* event. Both are keyboard, and keyboard-driven tab changes cut rather than travel:
|
|
40
|
+
* they repeat far more often than pointer changes and animating them puts the pill
|
|
41
|
+
* between the keypress and the answer. */
|
|
42
|
+
function isKeyboardActivation(event: Event | undefined) {
|
|
43
|
+
if (!event) return false
|
|
44
|
+
if (event.type.startsWith("key")) return true
|
|
45
|
+
if (event.type !== "click") return false
|
|
46
|
+
const { pointerType } = event as PointerEvent
|
|
47
|
+
return pointerType === "" || (event as MouseEvent).detail === 0
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface AnimatedTabsContextValue {
|
|
51
|
+
value: unknown
|
|
52
|
+
layoutId: string
|
|
53
|
+
transition: Transition
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const AnimatedTabsContext = React.createContext<AnimatedTabsContextValue | null>(
|
|
57
|
+
null
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
function useAnimatedTabsContext(component: string) {
|
|
61
|
+
const context = React.useContext(AnimatedTabsContext)
|
|
62
|
+
if (!context) {
|
|
63
|
+
throw new Error(`${component} must be used within an AnimatedTabs`)
|
|
64
|
+
}
|
|
65
|
+
return context
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface AnimatedTabsProps extends BaseRootProps {
|
|
69
|
+
/** Overrides the indicator spring. */
|
|
70
|
+
transition?: Transition
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Tabs whose active pill travels between triggers instead of cutting.
|
|
75
|
+
*
|
|
76
|
+
* Built on Base UI's Tabs for roving focus, arrow-key navigation and panel wiring.
|
|
77
|
+
* The indicator is a single `layoutId` element, so motion measures both positions
|
|
78
|
+
* and interpolates — no width or offset maths, and it stays correct when the tab
|
|
79
|
+
* labels are a different width on every render.
|
|
80
|
+
*
|
|
81
|
+
* The travel is for the pointer only. Keyboard-driven changes, and anyone who has
|
|
82
|
+
* asked for reduced motion, get the pill in its new position immediately.
|
|
83
|
+
*/
|
|
84
|
+
const AnimatedTabs = React.forwardRef<
|
|
85
|
+
React.ComponentRef<typeof BaseTabs.Root>,
|
|
86
|
+
AnimatedTabsProps
|
|
87
|
+
>(
|
|
88
|
+
(
|
|
89
|
+
{
|
|
90
|
+
value: valueProp,
|
|
91
|
+
defaultValue,
|
|
92
|
+
onValueChange,
|
|
93
|
+
transition = DEFAULT_TRANSITION,
|
|
94
|
+
className,
|
|
95
|
+
children,
|
|
96
|
+
...props
|
|
97
|
+
},
|
|
98
|
+
ref
|
|
99
|
+
) => {
|
|
100
|
+
const layoutId = React.useId()
|
|
101
|
+
const [internalValue, setInternalValue] = React.useState(defaultValue)
|
|
102
|
+
const [instant, setInstant] = React.useState(false)
|
|
103
|
+
const isControlled = valueProp !== undefined
|
|
104
|
+
const value = isControlled ? valueProp : internalValue
|
|
105
|
+
const reducedMotion = useReducedMotionConfig()
|
|
106
|
+
|
|
107
|
+
const handleValueChange = React.useCallback<
|
|
108
|
+
NonNullable<BaseRootProps["onValueChange"]>
|
|
109
|
+
>(
|
|
110
|
+
(next, eventDetails) => {
|
|
111
|
+
// Batched with the value change, so the render that moves the pill is
|
|
112
|
+
// already the one that knows whether it should move at all.
|
|
113
|
+
setInstant(isKeyboardActivation(eventDetails?.event))
|
|
114
|
+
if (!isControlled) setInternalValue(next)
|
|
115
|
+
onValueChange?.(next, eventDetails)
|
|
116
|
+
},
|
|
117
|
+
[isControlled, onValueChange]
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
const contextValue = React.useMemo<AnimatedTabsContextValue>(
|
|
121
|
+
() => ({
|
|
122
|
+
value,
|
|
123
|
+
layoutId,
|
|
124
|
+
transition: reducedMotion || instant ? INSTANT_TRANSITION : transition,
|
|
125
|
+
}),
|
|
126
|
+
[value, layoutId, transition, reducedMotion, instant]
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<AnimatedTabsContext.Provider value={contextValue}>
|
|
131
|
+
<BaseTabs.Root
|
|
132
|
+
ref={ref}
|
|
133
|
+
value={value}
|
|
134
|
+
defaultValue={defaultValue}
|
|
135
|
+
onValueChange={handleValueChange}
|
|
136
|
+
className={cn("w-full", className)}
|
|
137
|
+
{...props}
|
|
138
|
+
>
|
|
139
|
+
{children}
|
|
140
|
+
</BaseTabs.Root>
|
|
141
|
+
</AnimatedTabsContext.Provider>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
)
|
|
145
|
+
AnimatedTabs.displayName = "AnimatedTabs"
|
|
146
|
+
|
|
147
|
+
export type AnimatedTabsListProps = React.ComponentProps<typeof BaseTabs.List>
|
|
148
|
+
|
|
149
|
+
const AnimatedTabsList = React.forwardRef<
|
|
150
|
+
React.ComponentRef<typeof BaseTabs.List>,
|
|
151
|
+
AnimatedTabsListProps
|
|
152
|
+
>(({ className, ...props }, ref) => (
|
|
153
|
+
<BaseTabs.List
|
|
154
|
+
ref={ref}
|
|
155
|
+
className={cn(
|
|
156
|
+
"relative inline-flex items-center gap-0.5 rounded-full bg-muted p-1",
|
|
157
|
+
className
|
|
158
|
+
)}
|
|
159
|
+
{...props}
|
|
160
|
+
/>
|
|
161
|
+
))
|
|
162
|
+
AnimatedTabsList.displayName = "AnimatedTabsList"
|
|
163
|
+
|
|
164
|
+
export interface AnimatedTabsTriggerProps
|
|
165
|
+
extends React.ComponentProps<typeof BaseTabs.Tab> {
|
|
166
|
+
/** Class applied to the travelling indicator. */
|
|
167
|
+
indicatorClassName?: string
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const AnimatedTabsTrigger = React.forwardRef<
|
|
171
|
+
React.ComponentRef<typeof BaseTabs.Tab>,
|
|
172
|
+
AnimatedTabsTriggerProps
|
|
173
|
+
>(({ className, indicatorClassName, children, value, ...props }, ref) => {
|
|
174
|
+
const context = useAnimatedTabsContext("AnimatedTabsTrigger")
|
|
175
|
+
const isActive = context.value === value
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<BaseTabs.Tab
|
|
179
|
+
ref={ref}
|
|
180
|
+
value={value}
|
|
181
|
+
className={cn(
|
|
182
|
+
"relative isolate inline-flex select-none items-center justify-center whitespace-nowrap",
|
|
183
|
+
"rounded-full px-4 py-1.5 text-sm font-medium text-muted-foreground",
|
|
184
|
+
// Paced to land with the pill rather than ahead of it — a label that darkens
|
|
185
|
+
// while the pill is still in transit reads as two things moving, not one.
|
|
186
|
+
"transition-colors duration-200 ease-out",
|
|
187
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
188
|
+
"data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
|
189
|
+
"data-[active]:text-foreground",
|
|
190
|
+
// Only the tabs you could move to. Hovering the selected one has nothing to
|
|
191
|
+
// offer, and a bare `hover:` would also outrank the colour it was given.
|
|
192
|
+
"[&:not([data-active])]:hover:text-foreground",
|
|
193
|
+
className
|
|
194
|
+
)}
|
|
195
|
+
{...props}
|
|
196
|
+
>
|
|
197
|
+
{isActive && (
|
|
198
|
+
<motion.span
|
|
199
|
+
aria-hidden
|
|
200
|
+
layoutId={context.layoutId}
|
|
201
|
+
transition={context.transition}
|
|
202
|
+
className={cn(
|
|
203
|
+
"absolute inset-0 -z-10 rounded-full bg-background shadow-sm",
|
|
204
|
+
indicatorClassName
|
|
205
|
+
)}
|
|
206
|
+
/>
|
|
207
|
+
)}
|
|
208
|
+
{children}
|
|
209
|
+
</BaseTabs.Tab>
|
|
210
|
+
)
|
|
211
|
+
})
|
|
212
|
+
AnimatedTabsTrigger.displayName = "AnimatedTabsTrigger"
|
|
213
|
+
|
|
214
|
+
export type AnimatedTabsContentProps = React.ComponentProps<
|
|
215
|
+
typeof BaseTabs.Panel
|
|
216
|
+
>
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The panel cuts. The pill already carries the state change, and a second animation
|
|
220
|
+
* behind it only delays the content the user switched tabs to read.
|
|
221
|
+
*/
|
|
222
|
+
const AnimatedTabsContent = React.forwardRef<
|
|
223
|
+
React.ComponentRef<typeof BaseTabs.Panel>,
|
|
224
|
+
AnimatedTabsContentProps
|
|
225
|
+
>(({ className, children, ...props }, ref) => (
|
|
226
|
+
<BaseTabs.Panel
|
|
227
|
+
ref={ref}
|
|
228
|
+
className={cn(
|
|
229
|
+
"mt-3 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
230
|
+
className
|
|
231
|
+
)}
|
|
232
|
+
{...props}
|
|
233
|
+
>
|
|
234
|
+
{children}
|
|
235
|
+
</BaseTabs.Panel>
|
|
236
|
+
))
|
|
237
|
+
AnimatedTabsContent.displayName = "AnimatedTabsContent"
|
|
238
|
+
|
|
239
|
+
export {
|
|
240
|
+
AnimatedTabs,
|
|
241
|
+
AnimatedTabsList,
|
|
242
|
+
AnimatedTabsTrigger,
|
|
243
|
+
AnimatedTabsContent,
|
|
244
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {
|
|
2
|
+
AnimatedTabs,
|
|
3
|
+
AnimatedTabsList,
|
|
4
|
+
AnimatedTabsTrigger,
|
|
5
|
+
AnimatedTabsContent,
|
|
6
|
+
} from './animated-tabs'
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
AnimatedTabsProps,
|
|
10
|
+
AnimatedTabsListProps,
|
|
11
|
+
AnimatedTabsTriggerProps,
|
|
12
|
+
AnimatedTabsContentProps,
|
|
13
|
+
} from './animated-tabs'
|
|
@@ -81,10 +81,12 @@ const AutocompleteContent = React.forwardRef<
|
|
|
81
81
|
<AutocompletePrimitive.Popup
|
|
82
82
|
ref={ref}
|
|
83
83
|
className={cn(
|
|
84
|
-
"relative z-50 min-w-(--anchor-width) overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md",
|
|
85
|
-
"origin-(--transform-origin) outline-none",
|
|
86
|
-
"
|
|
87
|
-
"data-
|
|
84
|
+
"relative z-50 min-w-[var(--anchor-width)] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md",
|
|
85
|
+
"origin-[var(--transform-origin)] outline-none",
|
|
86
|
+
"transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
|
|
87
|
+
"data-[starting-style]:scale-95 data-[starting-style]:opacity-0",
|
|
88
|
+
"data-[ending-style]:scale-95 data-[ending-style]:opacity-0",
|
|
89
|
+
"motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
|
|
88
90
|
className
|
|
89
91
|
)}
|
|
90
92
|
{...props}
|
|
@@ -4,7 +4,23 @@ import { cva, type VariantProps } from "class-variance-authority"
|
|
|
4
4
|
import { cn } from "@/lib/utils"
|
|
5
5
|
|
|
6
6
|
const buttonVariants = cva(
|
|
7
|
-
|
|
7
|
+
[
|
|
8
|
+
"inline-flex items-center cursor-pointer justify-center whitespace-nowrap rounded-md text-sm font-medium",
|
|
9
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
|
|
10
|
+
// `transform` and `scale` are both named because Tailwind 3 compiles scale-* into
|
|
11
|
+
// `transform` while Tailwind 4 compiles it into `scale`, and a copied component has
|
|
12
|
+
// no say in which major its project runs.
|
|
13
|
+
"transition-[color,background-color,border-color,transform,scale] duration-[var(--motion-duration-fast,150ms)]",
|
|
14
|
+
// The button gives way under the press and comes back when released. 0.97 is small
|
|
15
|
+
// enough to read as the surface yielding rather than the button resizing.
|
|
16
|
+
"active:scale-[0.97]",
|
|
17
|
+
// Faster down than up: the press is the system answering the user, the release is
|
|
18
|
+
// the button settling. Equal timings in both directions read as mushy.
|
|
19
|
+
"active:duration-100",
|
|
20
|
+
// The colour change on hover and press already carries the feedback, so dropping
|
|
21
|
+
// the movement costs nothing.
|
|
22
|
+
"motion-reduce:active:scale-100",
|
|
23
|
+
],
|
|
8
24
|
{
|
|
9
25
|
variants: {
|
|
10
26
|
variant: {
|
|
@@ -16,7 +32,8 @@ const buttonVariants = cva(
|
|
|
16
32
|
secondary:
|
|
17
33
|
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
18
34
|
ghost: "hover:bg-accent hover:text-accent-foreground",
|
|
19
|
-
|
|
35
|
+
// A run of text is not a surface, so there is nothing to press into.
|
|
36
|
+
link: "text-primary underline-offset-4 hover:underline active:scale-100",
|
|
20
37
|
},
|
|
21
38
|
size: {
|
|
22
39
|
default: "h-10 px-4 py-2",
|