@istmx/ui 0.1.12 → 0.1.13
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 -21
- package/README.md +533 -469
- package/bin/istmx.mjs +481 -318
- package/package.json +27 -27
- package/registry/accordion.tsx +186 -0
- package/registry/ai-chat-block/ai-chat-block.tsx +456 -460
- package/registry/ai-chat-block/ai-chat-input.tsx +694 -694
- package/registry/ai-chat-block/attachment.tsx +466 -466
- package/registry/ai-chat-block/chat-code-block.tsx +50 -50
- package/registry/ai-chat-block/chat-composer.tsx +51 -51
- package/registry/ai-chat-block/chat-logo.tsx +18 -18
- package/registry/ai-chat-block/chat-message.tsx +161 -161
- package/registry/ai-chat-block/chat-sidebar.tsx +273 -273
- package/registry/ai-chat-block/chat-user-avatar.tsx +9 -9
- package/registry/ai-chat-block/code-highlighter.tsx +342 -357
- package/registry/ai-chat-block/default-attachments.tsx +42 -42
- package/registry/ai-chat-block/index.ts +30 -30
- package/registry/ai-chat-block/message-content.tsx +287 -287
- package/registry/ai-chat-block/message-list.tsx +93 -93
- package/registry/ai-chat-block/send-button.tsx +102 -102
- package/registry/ai-chat-block/types.ts +70 -71
- package/registry/ai-chat-block/utils.ts +6 -6
- package/registry/ai-chat-block/voice-button.tsx +66 -66
- package/registry/alert-dialog.tsx +229 -0
- package/registry/alert.tsx +188 -0
- package/registry/animated-text.tsx +228 -225
- package/registry/aspect-ratio.tsx +32 -0
- package/registry/avatar-group.tsx +66 -0
- package/registry/avatar.tsx +121 -0
- package/registry/badge.tsx +52 -0
- package/registry/bento-grid.tsx +438 -433
- package/registry/button.tsx +156 -237
- package/registry/carousel.tsx +184 -0
- package/registry/dock-navigation.tsx +200 -198
- package/registry/drift-canvas.tsx +383 -0
- package/registry/drift-wall.tsx +361 -0
- package/registry/flex-carousel.tsx +1430 -0
- package/registry/guided-gallery.tsx +1506 -0
- package/registry/image-accordion.tsx +306 -302
- package/registry/image-trail.tsx +126 -121
- package/registry/infinite-image-canvas.tsx +268 -263
- package/registry/input-otp.tsx +332 -0
- package/registry/list-card.tsx +130 -0
- package/registry/mobile-menu-dock.tsx +216 -211
- package/registry/morph-slider.tsx +756 -0
- package/registry/pixel-cat.tsx +972 -1020
- package/registry/scroll-canvas-cards.tsx +643 -640
- package/registry/scroll-parallax-hero.tsx +566 -565
- package/registry/scroll-story-cards.tsx +443 -438
- package/registry/slider.tsx +163 -0
- package/registry/spotlight-grid.tsx +226 -0
- package/registry/streaming-text.tsx +269 -278
- package/registry/text-reveal.tsx +311 -307
- package/registry/user-avatar.tsx +99 -0
- package/registry/utils.ts +6 -6
|
@@ -1,640 +1,643 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
4
|
-
import type { RefObject } from 'react'
|
|
5
|
-
import { gsap } from 'gsap'
|
|
6
|
-
import { ScrollTrigger } from 'gsap/ScrollTrigger'
|
|
7
|
-
import { useGSAP } from '@gsap/react'
|
|
8
|
-
import { motion, useInView, useReducedMotion } from 'motion/react'
|
|
9
|
-
import { cn } from '@/lib/utils'
|
|
10
|
-
|
|
11
|
-
if (typeof window !== 'undefined') {
|
|
12
|
-
gsap.registerPlugin(ScrollTrigger, useGSAP)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type ScrollCanvasSlide = {
|
|
16
|
-
image: string
|
|
17
|
-
imageAlt?: string
|
|
18
|
-
leftEyebrow?: string
|
|
19
|
-
leftHeading?: string
|
|
20
|
-
leftBody?: string
|
|
21
|
-
rightEyebrow?: string
|
|
22
|
-
rightHeading?: string
|
|
23
|
-
rightBody?: string
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type ScrollCanvasCardsProps = {
|
|
27
|
-
slides: ScrollCanvasSlide[]
|
|
28
|
-
className?: string
|
|
29
|
-
backgroundColor?: string
|
|
30
|
-
cardRadius?: number
|
|
31
|
-
cardMaxWidth?: number
|
|
32
|
-
cardHeight?: number
|
|
33
|
-
scrollLength?: number
|
|
34
|
-
cardBorderColor?: string
|
|
35
|
-
cardShadow?: string
|
|
36
|
-
headingClassName?: string
|
|
37
|
-
bodyClassName?: string
|
|
38
|
-
eyebrowClassName?: string
|
|
39
|
-
snap?: boolean
|
|
40
|
-
scrollContainerRef?: RefObject<HTMLElement | null>
|
|
41
|
-
onSlideChange?: (index: number) => void
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const DEFAULT_BG = 'var(--background)'
|
|
45
|
-
const DEFAULT_CARD_RADIUS = 16
|
|
46
|
-
const DEFAULT_CARD_MAX_WIDTH = 320
|
|
47
|
-
const DEFAULT_CARD_HEIGHT = 220
|
|
48
|
-
const DEFAULT_SCROLL_LENGTH = 5
|
|
49
|
-
const DEFAULT_BORDER_COLOR = 'var(--border)'
|
|
50
|
-
const DEFAULT_SHADOW =
|
|
51
|
-
'0 12px 40px color-mix(in srgb, var(--foreground) 10%, transparent), 0 2px 10px color-mix(in srgb, var(--foreground) 6%, transparent)'
|
|
52
|
-
|
|
53
|
-
const HOLD = 0.06
|
|
54
|
-
const SPAN = 1 - HOLD * 2
|
|
55
|
-
const EASE_OUT = [0.22, 1, 0.36, 1] as const
|
|
56
|
-
|
|
57
|
-
type PanelContent = {
|
|
58
|
-
eyebrow?: string
|
|
59
|
-
heading?: string
|
|
60
|
-
body?: string
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const easeInOut = (t: number) =>
|
|
64
|
-
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2
|
|
65
|
-
|
|
66
|
-
function paintCover(
|
|
67
|
-
ctx: CanvasRenderingContext2D,
|
|
68
|
-
img: HTMLImageElement,
|
|
69
|
-
w: number,
|
|
70
|
-
h: number,
|
|
71
|
-
zoom: number,
|
|
72
|
-
shiftY: number,
|
|
73
|
-
alpha: number,
|
|
74
|
-
) {
|
|
75
|
-
const imgRatio = img.naturalWidth / img.naturalHeight
|
|
76
|
-
const canvasRatio = w / h
|
|
77
|
-
let dw = imgRatio > canvasRatio ? h * imgRatio : w
|
|
78
|
-
let dh = imgRatio > canvasRatio ? h : w / imgRatio
|
|
79
|
-
dw *= zoom
|
|
80
|
-
dh *= zoom
|
|
81
|
-
ctx.save()
|
|
82
|
-
ctx.globalAlpha = alpha
|
|
83
|
-
ctx.drawImage(img, (w - dw) / 2, (h - dh) / 2 + shiftY, dw, dh)
|
|
84
|
-
ctx.restore()
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function pose(slot: number, shift: number, reduce: boolean) {
|
|
88
|
-
if (slot === 0) {
|
|
89
|
-
return { opacity: 1, x: 0, y: 0, filter: 'blur(0px)' }
|
|
90
|
-
}
|
|
91
|
-
return {
|
|
92
|
-
opacity: 0,
|
|
93
|
-
x: reduce || slot < 0 ? 0 : shift,
|
|
94
|
-
y: reduce ? 0 : slot > 0 ? 30 : -22,
|
|
95
|
-
filter: reduce ? 'blur(0px)' : 'blur(8px)',
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function SidePanel({
|
|
100
|
-
items,
|
|
101
|
-
active,
|
|
102
|
-
revealed,
|
|
103
|
-
side,
|
|
104
|
-
reduce,
|
|
105
|
-
eyebrowClassName,
|
|
106
|
-
headingClassName,
|
|
107
|
-
bodyClassName,
|
|
108
|
-
}: {
|
|
109
|
-
items: PanelContent[]
|
|
110
|
-
active: number
|
|
111
|
-
revealed: boolean
|
|
112
|
-
side: 'left' | 'right'
|
|
113
|
-
reduce: boolean
|
|
114
|
-
eyebrowClassName?: string
|
|
115
|
-
headingClassName?: string
|
|
116
|
-
bodyClassName?: string
|
|
117
|
-
}) {
|
|
118
|
-
const shift = side === 'left' ? -12 : 12
|
|
119
|
-
const align =
|
|
120
|
-
side === 'left' ? 'text-center md:text-right' : 'text-center md:text-left'
|
|
121
|
-
|
|
122
|
-
const timing = (isActive: boolean, order: number) => ({
|
|
123
|
-
duration: reduce ? 0.15 : isActive ? 0.75 : 0.4,
|
|
124
|
-
ease: EASE_OUT,
|
|
125
|
-
delay: reduce || !isActive ? 0 : 0.08 + order * 0.06,
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
return (
|
|
129
|
-
<div className="grid w-full max-w-xs md:max-w-[240px] lg:max-w-sm">
|
|
130
|
-
{items.map((item, i) => {
|
|
131
|
-
const offset = i - active
|
|
132
|
-
const slot = !revealed && offset === 0 ? 1 : offset
|
|
133
|
-
const isActive = slot === 0
|
|
134
|
-
const state = pose(slot, shift, reduce)
|
|
135
|
-
|
|
136
|
-
return (
|
|
137
|
-
<div
|
|
138
|
-
key={i}
|
|
139
|
-
aria-hidden={!isActive}
|
|
140
|
-
className={cn(
|
|
141
|
-
'col-start-1 row-start-1 flex flex-col gap-1.5 sm:gap-2.5',
|
|
142
|
-
align,
|
|
143
|
-
!isActive && 'pointer-events-none select-none',
|
|
144
|
-
)}
|
|
145
|
-
>
|
|
146
|
-
{item.eyebrow && (
|
|
147
|
-
<motion.p
|
|
148
|
-
initial={false}
|
|
149
|
-
animate={state}
|
|
150
|
-
transition={timing(isActive, 0)}
|
|
151
|
-
className={cn(
|
|
152
|
-
'text-muted font-mono text-[9px] font-medium tracking-[0.18em] uppercase sm:text-[10px] md:text-xs',
|
|
153
|
-
eyebrowClassName,
|
|
154
|
-
)}
|
|
155
|
-
>
|
|
156
|
-
{item.eyebrow}
|
|
157
|
-
</motion.p>
|
|
158
|
-
)}
|
|
159
|
-
{item.heading && (
|
|
160
|
-
<motion.h3
|
|
161
|
-
initial={false}
|
|
162
|
-
animate={state}
|
|
163
|
-
transition={timing(isActive, 1)}
|
|
164
|
-
className={cn(
|
|
165
|
-
'text-foreground font-display text-base font-semibold tracking-tight leading-snug sm:text-lg md:text-xl',
|
|
166
|
-
headingClassName,
|
|
167
|
-
)}
|
|
168
|
-
>
|
|
169
|
-
{item.heading}
|
|
170
|
-
</motion.h3>
|
|
171
|
-
)}
|
|
172
|
-
{item.body && (
|
|
173
|
-
<motion.p
|
|
174
|
-
initial={false}
|
|
175
|
-
animate={state}
|
|
176
|
-
transition={timing(isActive, 2)}
|
|
177
|
-
className={cn(
|
|
178
|
-
'text-muted max-w-prose text-xs leading-relaxed sm:text-sm md:text-base',
|
|
179
|
-
bodyClassName,
|
|
180
|
-
)}
|
|
181
|
-
>
|
|
182
|
-
{item.body}
|
|
183
|
-
</motion.p>
|
|
184
|
-
)}
|
|
185
|
-
</div>
|
|
186
|
-
)
|
|
187
|
-
})}
|
|
188
|
-
</div>
|
|
189
|
-
)
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function ProgressDots({
|
|
193
|
-
total,
|
|
194
|
-
active,
|
|
195
|
-
reduce,
|
|
196
|
-
onSelect,
|
|
197
|
-
}: {
|
|
198
|
-
total: number
|
|
199
|
-
active: number
|
|
200
|
-
reduce: boolean
|
|
201
|
-
onSelect: (index: number) => void
|
|
202
|
-
}) {
|
|
203
|
-
return (
|
|
204
|
-
<div className="mt-4 flex items-center justify-center gap-1 sm:mt-6">
|
|
205
|
-
{Array.from({ length: total }).map((_, i) => (
|
|
206
|
-
<button
|
|
207
|
-
key={i}
|
|
208
|
-
type="button"
|
|
209
|
-
aria-label={`Go to slide ${i + 1}`}
|
|
210
|
-
aria-current={i === active}
|
|
211
|
-
onClick={() => onSelect(i)}
|
|
212
|
-
className="flex h-5 cursor-pointer items-center rounded-full px-1 outline-none focus-visible:ring-2 focus-visible:ring-foreground/40"
|
|
213
|
-
>
|
|
214
|
-
<motion.span
|
|
215
|
-
initial={false}
|
|
216
|
-
animate={{
|
|
217
|
-
width: i === active ? 20 : 6,
|
|
218
|
-
opacity: i === active ? 1 : 0.35,
|
|
219
|
-
}}
|
|
220
|
-
transition={
|
|
221
|
-
reduce
|
|
222
|
-
? { duration: 0.15 }
|
|
223
|
-
: { type: 'spring', stiffness: 400, damping: 28 }
|
|
224
|
-
}
|
|
225
|
-
className={cn(
|
|
226
|
-
'block h-1.5 rounded-full transition-colors',
|
|
227
|
-
i === active ? 'bg-foreground' : 'bg-muted',
|
|
228
|
-
)}
|
|
229
|
-
/>
|
|
230
|
-
</button>
|
|
231
|
-
))}
|
|
232
|
-
</div>
|
|
233
|
-
)
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function useScrollCanvas({
|
|
237
|
-
canvasRef,
|
|
238
|
-
images,
|
|
239
|
-
positionRef,
|
|
240
|
-
reduce,
|
|
241
|
-
onIndexChange,
|
|
242
|
-
onFrame,
|
|
243
|
-
}: {
|
|
244
|
-
canvasRef: React.RefObject<HTMLCanvasElement | null>
|
|
245
|
-
images: string[]
|
|
246
|
-
positionRef: { current: number }
|
|
247
|
-
reduce: boolean
|
|
248
|
-
onIndexChange: (index: number) => void
|
|
249
|
-
onFrame?: (position: number) => void
|
|
250
|
-
}) {
|
|
251
|
-
const loaded = useRef<(HTMLImageElement | null)[]>([])
|
|
252
|
-
const current = useRef(0)
|
|
253
|
-
const lastIndex = useRef(0)
|
|
254
|
-
const dirty = useRef(true)
|
|
255
|
-
const size = useRef({ w: 0, h: 0, dpr: 1 })
|
|
256
|
-
const onIndexRef = useRef(onIndexChange)
|
|
257
|
-
const onFrameRef = useRef(onFrame)
|
|
258
|
-
|
|
259
|
-
useEffect(() => {
|
|
260
|
-
onIndexRef.current = onIndexChange
|
|
261
|
-
onFrameRef.current = onFrame
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
useEffect(() => {
|
|
265
|
-
let cancelled = false
|
|
266
|
-
loaded.current = images.map(() => null)
|
|
267
|
-
images.forEach((src, i) => {
|
|
268
|
-
const img = new Image()
|
|
269
|
-
img.decoding = 'async'
|
|
270
|
-
img.onload = () => {
|
|
271
|
-
if (cancelled) return
|
|
272
|
-
loaded.current[i] = img
|
|
273
|
-
dirty.current = true
|
|
274
|
-
}
|
|
275
|
-
img.src = src
|
|
276
|
-
})
|
|
277
|
-
return () => {
|
|
278
|
-
cancelled = true
|
|
279
|
-
}
|
|
280
|
-
}, [images])
|
|
281
|
-
|
|
282
|
-
useEffect(() => {
|
|
283
|
-
const canvas = canvasRef.current
|
|
284
|
-
if (!canvas) return
|
|
285
|
-
const ctx = canvas.getContext('2d')
|
|
286
|
-
if (!ctx) return
|
|
287
|
-
|
|
288
|
-
const resize = () => {
|
|
289
|
-
const w = canvas.clientWidth
|
|
290
|
-
const h = canvas.clientHeight
|
|
291
|
-
const dpr = Math.min(window.devicePixelRatio || 1, 2)
|
|
292
|
-
canvas.width = Math.round(w * dpr)
|
|
293
|
-
canvas.height = Math.round(h * dpr)
|
|
294
|
-
ctx.imageSmoothingQuality = 'high'
|
|
295
|
-
size.current = { w, h, dpr }
|
|
296
|
-
dirty.current = true
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const draw = (pos: number) => {
|
|
300
|
-
const { w, h, dpr } = size.current
|
|
301
|
-
const frames = loaded.current
|
|
302
|
-
const last = frames.length - 1
|
|
303
|
-
if (!w || !h || last < 0) return
|
|
304
|
-
|
|
305
|
-
const clamped = Math.min(Math.max(pos, 0), last)
|
|
306
|
-
const base = Math.min(Math.floor(clamped), last)
|
|
307
|
-
const t = clamped - base
|
|
308
|
-
const e = easeInOut(t)
|
|
309
|
-
const from = frames[base]
|
|
310
|
-
const to = base < last ? frames[base + 1] : null
|
|
311
|
-
|
|
312
|
-
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
|
|
313
|
-
ctx.clearRect(0, 0, w, h)
|
|
314
|
-
if (from) paintCover(ctx, from, w, h, 1 + e * 0.07, -e * 12, 1)
|
|
315
|
-
if (to && t > 0) paintCover(ctx, to, w, h, 1.07 - e * 0.07, (1 - e) * 12, e)
|
|
316
|
-
onFrameRef.current?.(clamped)
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const tick = (_time: number, deltaTime: number) => {
|
|
320
|
-
const target = reduce
|
|
321
|
-
? Math.round(positionRef.current)
|
|
322
|
-
: positionRef.current
|
|
323
|
-
const delta = target - current.current
|
|
324
|
-
|
|
325
|
-
if (Math.abs(delta) > 0.0004) {
|
|
326
|
-
const k = reduce ? 1 : 1 - Math.exp(-Math.min(deltaTime, 100) / 150)
|
|
327
|
-
current.current += delta * k
|
|
328
|
-
dirty.current = true
|
|
329
|
-
} else if (delta !== 0) {
|
|
330
|
-
current.current = target
|
|
331
|
-
dirty.current = true
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
const index = Math.round(current.current)
|
|
335
|
-
if (index !== lastIndex.current) {
|
|
336
|
-
lastIndex.current = index
|
|
337
|
-
onIndexRef.current(index)
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
if (dirty.current) {
|
|
341
|
-
dirty.current = false
|
|
342
|
-
draw(current.current)
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
resize()
|
|
347
|
-
const observer = new ResizeObserver(resize)
|
|
348
|
-
observer.observe(canvas)
|
|
349
|
-
gsap.ticker.add(tick)
|
|
350
|
-
|
|
351
|
-
return () => {
|
|
352
|
-
observer.disconnect()
|
|
353
|
-
gsap.ticker.remove(tick)
|
|
354
|
-
}
|
|
355
|
-
}, [canvasRef, positionRef, reduce])
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
export function ScrollCanvasCards({
|
|
359
|
-
slides,
|
|
360
|
-
className,
|
|
361
|
-
backgroundColor = DEFAULT_BG,
|
|
362
|
-
cardRadius = DEFAULT_CARD_RADIUS,
|
|
363
|
-
cardMaxWidth = DEFAULT_CARD_MAX_WIDTH,
|
|
364
|
-
cardHeight = DEFAULT_CARD_HEIGHT,
|
|
365
|
-
scrollLength = DEFAULT_SCROLL_LENGTH,
|
|
366
|
-
cardBorderColor = DEFAULT_BORDER_COLOR,
|
|
367
|
-
cardShadow = DEFAULT_SHADOW,
|
|
368
|
-
headingClassName,
|
|
369
|
-
bodyClassName,
|
|
370
|
-
eyebrowClassName,
|
|
371
|
-
snap = true,
|
|
372
|
-
scrollContainerRef,
|
|
373
|
-
onSlideChange,
|
|
374
|
-
}
|
|
375
|
-
const containerRef = useRef<HTMLElement>(null)
|
|
376
|
-
|
|
377
|
-
const
|
|
378
|
-
const
|
|
379
|
-
const
|
|
380
|
-
const
|
|
381
|
-
const
|
|
382
|
-
const
|
|
383
|
-
const
|
|
384
|
-
const
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
{
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'
|
|
4
|
+
import type { RefObject } from 'react'
|
|
5
|
+
import { gsap } from 'gsap'
|
|
6
|
+
import { ScrollTrigger } from 'gsap/ScrollTrigger'
|
|
7
|
+
import { useGSAP } from '@gsap/react'
|
|
8
|
+
import { motion, useInView, useReducedMotion } from 'motion/react'
|
|
9
|
+
import { cn } from '@/lib/utils'
|
|
10
|
+
|
|
11
|
+
if (typeof window !== 'undefined') {
|
|
12
|
+
gsap.registerPlugin(ScrollTrigger, useGSAP)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type ScrollCanvasSlide = {
|
|
16
|
+
image: string
|
|
17
|
+
imageAlt?: string
|
|
18
|
+
leftEyebrow?: string
|
|
19
|
+
leftHeading?: string
|
|
20
|
+
leftBody?: string
|
|
21
|
+
rightEyebrow?: string
|
|
22
|
+
rightHeading?: string
|
|
23
|
+
rightBody?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ScrollCanvasCardsProps = {
|
|
27
|
+
slides: ScrollCanvasSlide[]
|
|
28
|
+
className?: string
|
|
29
|
+
backgroundColor?: string
|
|
30
|
+
cardRadius?: number
|
|
31
|
+
cardMaxWidth?: number
|
|
32
|
+
cardHeight?: number
|
|
33
|
+
scrollLength?: number
|
|
34
|
+
cardBorderColor?: string
|
|
35
|
+
cardShadow?: string
|
|
36
|
+
headingClassName?: string
|
|
37
|
+
bodyClassName?: string
|
|
38
|
+
eyebrowClassName?: string
|
|
39
|
+
snap?: boolean
|
|
40
|
+
scrollContainerRef?: RefObject<HTMLElement | null>
|
|
41
|
+
onSlideChange?: (index: number) => void
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const DEFAULT_BG = 'var(--background)'
|
|
45
|
+
const DEFAULT_CARD_RADIUS = 16
|
|
46
|
+
const DEFAULT_CARD_MAX_WIDTH = 320
|
|
47
|
+
const DEFAULT_CARD_HEIGHT = 220
|
|
48
|
+
const DEFAULT_SCROLL_LENGTH = 5
|
|
49
|
+
const DEFAULT_BORDER_COLOR = 'var(--border)'
|
|
50
|
+
const DEFAULT_SHADOW =
|
|
51
|
+
'0 12px 40px color-mix(in srgb, var(--foreground) 10%, transparent), 0 2px 10px color-mix(in srgb, var(--foreground) 6%, transparent)'
|
|
52
|
+
|
|
53
|
+
const HOLD = 0.06
|
|
54
|
+
const SPAN = 1 - HOLD * 2
|
|
55
|
+
const EASE_OUT = [0.22, 1, 0.36, 1] as const
|
|
56
|
+
|
|
57
|
+
type PanelContent = {
|
|
58
|
+
eyebrow?: string
|
|
59
|
+
heading?: string
|
|
60
|
+
body?: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const easeInOut = (t: number) =>
|
|
64
|
+
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2
|
|
65
|
+
|
|
66
|
+
function paintCover(
|
|
67
|
+
ctx: CanvasRenderingContext2D,
|
|
68
|
+
img: HTMLImageElement,
|
|
69
|
+
w: number,
|
|
70
|
+
h: number,
|
|
71
|
+
zoom: number,
|
|
72
|
+
shiftY: number,
|
|
73
|
+
alpha: number,
|
|
74
|
+
) {
|
|
75
|
+
const imgRatio = img.naturalWidth / img.naturalHeight
|
|
76
|
+
const canvasRatio = w / h
|
|
77
|
+
let dw = imgRatio > canvasRatio ? h * imgRatio : w
|
|
78
|
+
let dh = imgRatio > canvasRatio ? h : w / imgRatio
|
|
79
|
+
dw *= zoom
|
|
80
|
+
dh *= zoom
|
|
81
|
+
ctx.save()
|
|
82
|
+
ctx.globalAlpha = alpha
|
|
83
|
+
ctx.drawImage(img, (w - dw) / 2, (h - dh) / 2 + shiftY, dw, dh)
|
|
84
|
+
ctx.restore()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function pose(slot: number, shift: number, reduce: boolean) {
|
|
88
|
+
if (slot === 0) {
|
|
89
|
+
return { opacity: 1, x: 0, y: 0, filter: 'blur(0px)' }
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
opacity: 0,
|
|
93
|
+
x: reduce || slot < 0 ? 0 : shift,
|
|
94
|
+
y: reduce ? 0 : slot > 0 ? 30 : -22,
|
|
95
|
+
filter: reduce ? 'blur(0px)' : 'blur(8px)',
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function SidePanel({
|
|
100
|
+
items,
|
|
101
|
+
active,
|
|
102
|
+
revealed,
|
|
103
|
+
side,
|
|
104
|
+
reduce,
|
|
105
|
+
eyebrowClassName,
|
|
106
|
+
headingClassName,
|
|
107
|
+
bodyClassName,
|
|
108
|
+
}: {
|
|
109
|
+
items: PanelContent[]
|
|
110
|
+
active: number
|
|
111
|
+
revealed: boolean
|
|
112
|
+
side: 'left' | 'right'
|
|
113
|
+
reduce: boolean
|
|
114
|
+
eyebrowClassName?: string
|
|
115
|
+
headingClassName?: string
|
|
116
|
+
bodyClassName?: string
|
|
117
|
+
}) {
|
|
118
|
+
const shift = side === 'left' ? -12 : 12
|
|
119
|
+
const align =
|
|
120
|
+
side === 'left' ? 'text-center md:text-right' : 'text-center md:text-left'
|
|
121
|
+
|
|
122
|
+
const timing = (isActive: boolean, order: number) => ({
|
|
123
|
+
duration: reduce ? 0.15 : isActive ? 0.75 : 0.4,
|
|
124
|
+
ease: EASE_OUT,
|
|
125
|
+
delay: reduce || !isActive ? 0 : 0.08 + order * 0.06,
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<div className="grid w-full max-w-xs md:max-w-[240px] lg:max-w-sm">
|
|
130
|
+
{items.map((item, i) => {
|
|
131
|
+
const offset = i - active
|
|
132
|
+
const slot = !revealed && offset === 0 ? 1 : offset
|
|
133
|
+
const isActive = slot === 0
|
|
134
|
+
const state = pose(slot, shift, reduce)
|
|
135
|
+
|
|
136
|
+
return (
|
|
137
|
+
<div
|
|
138
|
+
key={i}
|
|
139
|
+
aria-hidden={!isActive}
|
|
140
|
+
className={cn(
|
|
141
|
+
'col-start-1 row-start-1 flex flex-col gap-1.5 sm:gap-2.5',
|
|
142
|
+
align,
|
|
143
|
+
!isActive && 'pointer-events-none select-none',
|
|
144
|
+
)}
|
|
145
|
+
>
|
|
146
|
+
{item.eyebrow && (
|
|
147
|
+
<motion.p
|
|
148
|
+
initial={false}
|
|
149
|
+
animate={state}
|
|
150
|
+
transition={timing(isActive, 0)}
|
|
151
|
+
className={cn(
|
|
152
|
+
'text-muted font-mono text-[9px] font-medium tracking-[0.18em] uppercase sm:text-[10px] md:text-xs',
|
|
153
|
+
eyebrowClassName,
|
|
154
|
+
)}
|
|
155
|
+
>
|
|
156
|
+
{item.eyebrow}
|
|
157
|
+
</motion.p>
|
|
158
|
+
)}
|
|
159
|
+
{item.heading && (
|
|
160
|
+
<motion.h3
|
|
161
|
+
initial={false}
|
|
162
|
+
animate={state}
|
|
163
|
+
transition={timing(isActive, 1)}
|
|
164
|
+
className={cn(
|
|
165
|
+
'text-foreground font-display text-base font-semibold tracking-tight leading-snug sm:text-lg md:text-xl',
|
|
166
|
+
headingClassName,
|
|
167
|
+
)}
|
|
168
|
+
>
|
|
169
|
+
{item.heading}
|
|
170
|
+
</motion.h3>
|
|
171
|
+
)}
|
|
172
|
+
{item.body && (
|
|
173
|
+
<motion.p
|
|
174
|
+
initial={false}
|
|
175
|
+
animate={state}
|
|
176
|
+
transition={timing(isActive, 2)}
|
|
177
|
+
className={cn(
|
|
178
|
+
'text-muted max-w-prose text-xs leading-relaxed sm:text-sm md:text-base',
|
|
179
|
+
bodyClassName,
|
|
180
|
+
)}
|
|
181
|
+
>
|
|
182
|
+
{item.body}
|
|
183
|
+
</motion.p>
|
|
184
|
+
)}
|
|
185
|
+
</div>
|
|
186
|
+
)
|
|
187
|
+
})}
|
|
188
|
+
</div>
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function ProgressDots({
|
|
193
|
+
total,
|
|
194
|
+
active,
|
|
195
|
+
reduce,
|
|
196
|
+
onSelect,
|
|
197
|
+
}: {
|
|
198
|
+
total: number
|
|
199
|
+
active: number
|
|
200
|
+
reduce: boolean
|
|
201
|
+
onSelect: (index: number) => void
|
|
202
|
+
}) {
|
|
203
|
+
return (
|
|
204
|
+
<div className="mt-4 flex items-center justify-center gap-1 sm:mt-6">
|
|
205
|
+
{Array.from({ length: total }).map((_, i) => (
|
|
206
|
+
<button
|
|
207
|
+
key={i}
|
|
208
|
+
type="button"
|
|
209
|
+
aria-label={`Go to slide ${i + 1}`}
|
|
210
|
+
aria-current={i === active}
|
|
211
|
+
onClick={() => onSelect(i)}
|
|
212
|
+
className="flex h-5 cursor-pointer items-center rounded-full px-1 outline-none focus-visible:ring-2 focus-visible:ring-foreground/40"
|
|
213
|
+
>
|
|
214
|
+
<motion.span
|
|
215
|
+
initial={false}
|
|
216
|
+
animate={{
|
|
217
|
+
width: i === active ? 20 : 6,
|
|
218
|
+
opacity: i === active ? 1 : 0.35,
|
|
219
|
+
}}
|
|
220
|
+
transition={
|
|
221
|
+
reduce
|
|
222
|
+
? { duration: 0.15 }
|
|
223
|
+
: { type: 'spring', stiffness: 400, damping: 28 }
|
|
224
|
+
}
|
|
225
|
+
className={cn(
|
|
226
|
+
'block h-1.5 rounded-full transition-colors',
|
|
227
|
+
i === active ? 'bg-foreground' : 'bg-muted',
|
|
228
|
+
)}
|
|
229
|
+
/>
|
|
230
|
+
</button>
|
|
231
|
+
))}
|
|
232
|
+
</div>
|
|
233
|
+
)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function useScrollCanvas({
|
|
237
|
+
canvasRef,
|
|
238
|
+
images,
|
|
239
|
+
positionRef,
|
|
240
|
+
reduce,
|
|
241
|
+
onIndexChange,
|
|
242
|
+
onFrame,
|
|
243
|
+
}: {
|
|
244
|
+
canvasRef: React.RefObject<HTMLCanvasElement | null>
|
|
245
|
+
images: string[]
|
|
246
|
+
positionRef: { current: number }
|
|
247
|
+
reduce: boolean
|
|
248
|
+
onIndexChange: (index: number) => void
|
|
249
|
+
onFrame?: (position: number) => void
|
|
250
|
+
}) {
|
|
251
|
+
const loaded = useRef<(HTMLImageElement | null)[]>([])
|
|
252
|
+
const current = useRef(0)
|
|
253
|
+
const lastIndex = useRef(0)
|
|
254
|
+
const dirty = useRef(true)
|
|
255
|
+
const size = useRef({ w: 0, h: 0, dpr: 1 })
|
|
256
|
+
const onIndexRef = useRef(onIndexChange)
|
|
257
|
+
const onFrameRef = useRef(onFrame)
|
|
258
|
+
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
onIndexRef.current = onIndexChange
|
|
261
|
+
onFrameRef.current = onFrame
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
useEffect(() => {
|
|
265
|
+
let cancelled = false
|
|
266
|
+
loaded.current = images.map(() => null)
|
|
267
|
+
images.forEach((src, i) => {
|
|
268
|
+
const img = new Image()
|
|
269
|
+
img.decoding = 'async'
|
|
270
|
+
img.onload = () => {
|
|
271
|
+
if (cancelled) return
|
|
272
|
+
loaded.current[i] = img
|
|
273
|
+
dirty.current = true
|
|
274
|
+
}
|
|
275
|
+
img.src = src
|
|
276
|
+
})
|
|
277
|
+
return () => {
|
|
278
|
+
cancelled = true
|
|
279
|
+
}
|
|
280
|
+
}, [images])
|
|
281
|
+
|
|
282
|
+
useEffect(() => {
|
|
283
|
+
const canvas = canvasRef.current
|
|
284
|
+
if (!canvas) return
|
|
285
|
+
const ctx = canvas.getContext('2d')
|
|
286
|
+
if (!ctx) return
|
|
287
|
+
|
|
288
|
+
const resize = () => {
|
|
289
|
+
const w = canvas.clientWidth
|
|
290
|
+
const h = canvas.clientHeight
|
|
291
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2)
|
|
292
|
+
canvas.width = Math.round(w * dpr)
|
|
293
|
+
canvas.height = Math.round(h * dpr)
|
|
294
|
+
ctx.imageSmoothingQuality = 'high'
|
|
295
|
+
size.current = { w, h, dpr }
|
|
296
|
+
dirty.current = true
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const draw = (pos: number) => {
|
|
300
|
+
const { w, h, dpr } = size.current
|
|
301
|
+
const frames = loaded.current
|
|
302
|
+
const last = frames.length - 1
|
|
303
|
+
if (!w || !h || last < 0) return
|
|
304
|
+
|
|
305
|
+
const clamped = Math.min(Math.max(pos, 0), last)
|
|
306
|
+
const base = Math.min(Math.floor(clamped), last)
|
|
307
|
+
const t = clamped - base
|
|
308
|
+
const e = easeInOut(t)
|
|
309
|
+
const from = frames[base]
|
|
310
|
+
const to = base < last ? frames[base + 1] : null
|
|
311
|
+
|
|
312
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
|
|
313
|
+
ctx.clearRect(0, 0, w, h)
|
|
314
|
+
if (from) paintCover(ctx, from, w, h, 1 + e * 0.07, -e * 12, 1)
|
|
315
|
+
if (to && t > 0) paintCover(ctx, to, w, h, 1.07 - e * 0.07, (1 - e) * 12, e)
|
|
316
|
+
onFrameRef.current?.(clamped)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const tick = (_time: number, deltaTime: number) => {
|
|
320
|
+
const target = reduce
|
|
321
|
+
? Math.round(positionRef.current)
|
|
322
|
+
: positionRef.current
|
|
323
|
+
const delta = target - current.current
|
|
324
|
+
|
|
325
|
+
if (Math.abs(delta) > 0.0004) {
|
|
326
|
+
const k = reduce ? 1 : 1 - Math.exp(-Math.min(deltaTime, 100) / 150)
|
|
327
|
+
current.current += delta * k
|
|
328
|
+
dirty.current = true
|
|
329
|
+
} else if (delta !== 0) {
|
|
330
|
+
current.current = target
|
|
331
|
+
dirty.current = true
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const index = Math.round(current.current)
|
|
335
|
+
if (index !== lastIndex.current) {
|
|
336
|
+
lastIndex.current = index
|
|
337
|
+
onIndexRef.current(index)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (dirty.current) {
|
|
341
|
+
dirty.current = false
|
|
342
|
+
draw(current.current)
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
resize()
|
|
347
|
+
const observer = new ResizeObserver(resize)
|
|
348
|
+
observer.observe(canvas)
|
|
349
|
+
gsap.ticker.add(tick)
|
|
350
|
+
|
|
351
|
+
return () => {
|
|
352
|
+
observer.disconnect()
|
|
353
|
+
gsap.ticker.remove(tick)
|
|
354
|
+
}
|
|
355
|
+
}, [canvasRef, positionRef, reduce])
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export const ScrollCanvasCards = forwardRef<HTMLElement, ScrollCanvasCardsProps>(function ScrollCanvasCards({
|
|
359
|
+
slides,
|
|
360
|
+
className,
|
|
361
|
+
backgroundColor = DEFAULT_BG,
|
|
362
|
+
cardRadius = DEFAULT_CARD_RADIUS,
|
|
363
|
+
cardMaxWidth = DEFAULT_CARD_MAX_WIDTH,
|
|
364
|
+
cardHeight = DEFAULT_CARD_HEIGHT,
|
|
365
|
+
scrollLength = DEFAULT_SCROLL_LENGTH,
|
|
366
|
+
cardBorderColor = DEFAULT_BORDER_COLOR,
|
|
367
|
+
cardShadow = DEFAULT_SHADOW,
|
|
368
|
+
headingClassName,
|
|
369
|
+
bodyClassName,
|
|
370
|
+
eyebrowClassName,
|
|
371
|
+
snap = true,
|
|
372
|
+
scrollContainerRef,
|
|
373
|
+
onSlideChange,
|
|
374
|
+
}, ref) {
|
|
375
|
+
const containerRef = useRef<HTMLElement>(null)
|
|
376
|
+
useImperativeHandle(ref, () => containerRef.current as HTMLElement)
|
|
377
|
+
const canvasRef = useRef<HTMLCanvasElement>(null)
|
|
378
|
+
const cardRef = useRef<HTMLDivElement>(null)
|
|
379
|
+
const triggerRef = useRef<ScrollTrigger | null>(null)
|
|
380
|
+
const positionRef = useRef(0)
|
|
381
|
+
const [activeIndex, setActiveIndex] = useState(0)
|
|
382
|
+
const reduce = useReducedMotion() ?? false
|
|
383
|
+
const inView = useInView(containerRef, { once: true, amount: 0.3 })
|
|
384
|
+
const revealed = reduce || inView
|
|
385
|
+
const count = slides.length
|
|
386
|
+
const currentSlide = slides[activeIndex] ?? slides[0]
|
|
387
|
+
|
|
388
|
+
const imageKey = JSON.stringify(slides.map((s) => s.image))
|
|
389
|
+
const imageUrls = useMemo(() => JSON.parse(imageKey) as string[], [imageKey])
|
|
390
|
+
|
|
391
|
+
const leftItems = useMemo<PanelContent[]>(
|
|
392
|
+
() =>
|
|
393
|
+
slides.map((s) => ({
|
|
394
|
+
eyebrow: s.leftEyebrow,
|
|
395
|
+
heading: s.leftHeading,
|
|
396
|
+
body: s.leftBody,
|
|
397
|
+
})),
|
|
398
|
+
[slides],
|
|
399
|
+
)
|
|
400
|
+
const rightItems = useMemo<PanelContent[]>(
|
|
401
|
+
() =>
|
|
402
|
+
slides.map((s) => ({
|
|
403
|
+
eyebrow: s.rightEyebrow,
|
|
404
|
+
heading: s.rightHeading,
|
|
405
|
+
body: s.rightBody,
|
|
406
|
+
})),
|
|
407
|
+
[slides],
|
|
408
|
+
)
|
|
409
|
+
const hasLeft = leftItems.some((i) => i.eyebrow || i.heading || i.body)
|
|
410
|
+
const hasRight = rightItems.some((i) => i.eyebrow || i.heading || i.body)
|
|
411
|
+
|
|
412
|
+
const handleIndex = useCallback(
|
|
413
|
+
(index: number) => {
|
|
414
|
+
setActiveIndex(index)
|
|
415
|
+
onSlideChange?.(index)
|
|
416
|
+
},
|
|
417
|
+
[onSlideChange],
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
const handleFrame = useCallback(
|
|
421
|
+
(position: number) => {
|
|
422
|
+
const card = cardRef.current
|
|
423
|
+
if (!card || reduce) return
|
|
424
|
+
const wave = Math.sin((position - Math.floor(position)) * Math.PI)
|
|
425
|
+
card.style.transform = `translateY(${(-wave * 5).toFixed(2)}px) scale(${(1 - wave * 0.025).toFixed(4)})`
|
|
426
|
+
},
|
|
427
|
+
[reduce],
|
|
428
|
+
)
|
|
429
|
+
|
|
430
|
+
useScrollCanvas({
|
|
431
|
+
canvasRef,
|
|
432
|
+
images: imageUrls,
|
|
433
|
+
positionRef,
|
|
434
|
+
reduce,
|
|
435
|
+
onIndexChange: handleIndex,
|
|
436
|
+
onFrame: handleFrame,
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
useGSAP(
|
|
440
|
+
() => {
|
|
441
|
+
const el = containerRef.current
|
|
442
|
+
if (!el || count < 2) return
|
|
443
|
+
|
|
444
|
+
const scroller = scrollContainerRef?.current || undefined
|
|
445
|
+
const toPosition = (progress: number) =>
|
|
446
|
+
Math.min(Math.max((progress - HOLD) / SPAN, 0), 1) * (count - 1)
|
|
447
|
+
|
|
448
|
+
triggerRef.current = ScrollTrigger.create({
|
|
449
|
+
trigger: el,
|
|
450
|
+
scroller: scroller,
|
|
451
|
+
start: 'top top',
|
|
452
|
+
end: () => {
|
|
453
|
+
const vh = scroller ? scroller.clientHeight : window.innerHeight
|
|
454
|
+
return `+=${Math.round(vh * scrollLength)}`
|
|
455
|
+
},
|
|
456
|
+
pin: true,
|
|
457
|
+
pinSpacing: true,
|
|
458
|
+
anticipatePin: 1,
|
|
459
|
+
invalidateOnRefresh: true,
|
|
460
|
+
snap:
|
|
461
|
+
snap && !reduce
|
|
462
|
+
? {
|
|
463
|
+
snapTo: (value: number) => {
|
|
464
|
+
if (value <= HOLD || value >= 1 - HOLD) return value
|
|
465
|
+
const step = Math.round(((value - HOLD) / SPAN) * (count - 1))
|
|
466
|
+
return HOLD + (step / (count - 1)) * SPAN
|
|
467
|
+
},
|
|
468
|
+
duration: { min: 0.25, max: 0.7 },
|
|
469
|
+
delay: 0.12,
|
|
470
|
+
ease: 'power2.inOut',
|
|
471
|
+
inertia: false,
|
|
472
|
+
}
|
|
473
|
+
: undefined,
|
|
474
|
+
onRefresh: (self: any) => {
|
|
475
|
+
positionRef.current = toPosition(self.progress)
|
|
476
|
+
},
|
|
477
|
+
onUpdate: (self: any) => {
|
|
478
|
+
positionRef.current = toPosition(self.progress)
|
|
479
|
+
},
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
ScrollTrigger.refresh()
|
|
483
|
+
},
|
|
484
|
+
{
|
|
485
|
+
scope: containerRef,
|
|
486
|
+
dependencies: [count, scrollLength, reduce, snap, scrollContainerRef],
|
|
487
|
+
},
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
const scrollToSlide = useCallback(
|
|
491
|
+
(index: number) => {
|
|
492
|
+
const trigger = triggerRef.current
|
|
493
|
+
if (!trigger) return
|
|
494
|
+
const progress = HOLD + SPAN * (index / Math.max(count - 1, 1))
|
|
495
|
+
const targetTop = trigger.start + (trigger.end - trigger.start) * progress
|
|
496
|
+
const scroller = scrollContainerRef?.current
|
|
497
|
+
if (scroller) {
|
|
498
|
+
scroller.scrollTo({
|
|
499
|
+
top: targetTop,
|
|
500
|
+
behavior: reduce ? 'auto' : 'smooth',
|
|
501
|
+
})
|
|
502
|
+
} else {
|
|
503
|
+
window.scrollTo({
|
|
504
|
+
top: targetTop,
|
|
505
|
+
behavior: reduce ? 'auto' : 'smooth',
|
|
506
|
+
})
|
|
507
|
+
}
|
|
508
|
+
},
|
|
509
|
+
[count, reduce, scrollContainerRef],
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
const cardVariants = {
|
|
513
|
+
out: {
|
|
514
|
+
opacity: reduce ? 1 : 0,
|
|
515
|
+
scale: reduce ? 1 : 0.9,
|
|
516
|
+
y: reduce ? 0 : 24,
|
|
517
|
+
},
|
|
518
|
+
in: {
|
|
519
|
+
opacity: 1,
|
|
520
|
+
scale: 1,
|
|
521
|
+
y: 0,
|
|
522
|
+
transition: reduce
|
|
523
|
+
? { duration: 0.15 }
|
|
524
|
+
: { type: 'spring' as const, stiffness: 170, damping: 24, mass: 0.9 },
|
|
525
|
+
},
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
return (
|
|
529
|
+
<section
|
|
530
|
+
ref={containerRef}
|
|
531
|
+
className={cn('relative w-full', className)}
|
|
532
|
+
style={{ backgroundColor, minHeight: '100svh' }}
|
|
533
|
+
>
|
|
534
|
+
<div className="sr-only" role="status" aria-live="polite">
|
|
535
|
+
Slide {activeIndex + 1} of {count}
|
|
536
|
+
{currentSlide?.imageAlt ? `: ${currentSlide.imageAlt}` : ''}
|
|
537
|
+
</div>
|
|
538
|
+
|
|
539
|
+
<div className="mx-auto flex min-h-svh w-full max-w-6xl items-center px-4 py-8 sm:px-6 md:px-8 md:py-16">
|
|
540
|
+
<div className="grid w-full grid-cols-1 items-center justify-items-center gap-5 sm:gap-6 md:grid-cols-[minmax(0,1fr)_auto_minmax(0,1fr)] md:gap-6 lg:gap-10">
|
|
541
|
+
<div
|
|
542
|
+
className={cn(
|
|
543
|
+
'order-2 w-full justify-center md:order-1 md:flex md:justify-end',
|
|
544
|
+
hasLeft ? 'flex' : 'hidden',
|
|
545
|
+
)}
|
|
546
|
+
>
|
|
547
|
+
{hasLeft && (
|
|
548
|
+
<SidePanel
|
|
549
|
+
items={leftItems}
|
|
550
|
+
active={activeIndex}
|
|
551
|
+
revealed={revealed}
|
|
552
|
+
side="left"
|
|
553
|
+
reduce={reduce}
|
|
554
|
+
eyebrowClassName={eyebrowClassName}
|
|
555
|
+
headingClassName={headingClassName}
|
|
556
|
+
bodyClassName={bodyClassName}
|
|
557
|
+
/>
|
|
558
|
+
)}
|
|
559
|
+
</div>
|
|
560
|
+
|
|
561
|
+
<motion.div
|
|
562
|
+
variants={cardVariants}
|
|
563
|
+
initial="out"
|
|
564
|
+
animate={revealed ? 'in' : 'out'}
|
|
565
|
+
className="order-1 flex w-full flex-col items-center justify-center md:order-2 md:w-auto"
|
|
566
|
+
>
|
|
567
|
+
<div
|
|
568
|
+
ref={cardRef}
|
|
569
|
+
className="relative will-change-transform"
|
|
570
|
+
style={{
|
|
571
|
+
width: '100%',
|
|
572
|
+
maxWidth: `min(${cardMaxWidth}px, calc(100vw - 2.5rem))`,
|
|
573
|
+
aspectRatio: `${cardMaxWidth} / ${cardHeight}`,
|
|
574
|
+
maxHeight: cardHeight,
|
|
575
|
+
}}
|
|
576
|
+
>
|
|
577
|
+
<div
|
|
578
|
+
className="pointer-events-none absolute -inset-[1px]"
|
|
579
|
+
style={{
|
|
580
|
+
border: `1px solid ${cardBorderColor}`,
|
|
581
|
+
borderRadius: cardRadius + 1,
|
|
582
|
+
}}
|
|
583
|
+
/>
|
|
584
|
+
<div
|
|
585
|
+
className="bg-surface relative h-full w-full overflow-hidden"
|
|
586
|
+
style={{
|
|
587
|
+
borderRadius: cardRadius,
|
|
588
|
+
boxShadow: cardShadow,
|
|
589
|
+
}}
|
|
590
|
+
>
|
|
591
|
+
<canvas
|
|
592
|
+
ref={canvasRef}
|
|
593
|
+
role="img"
|
|
594
|
+
aria-label={currentSlide?.imageAlt}
|
|
595
|
+
className="absolute inset-0 h-full w-full"
|
|
596
|
+
/>
|
|
597
|
+
<div
|
|
598
|
+
className="pointer-events-none absolute inset-0"
|
|
599
|
+
style={{
|
|
600
|
+
borderRadius: cardRadius,
|
|
601
|
+
boxShadow:
|
|
602
|
+
'inset 0 0 50px color-mix(in srgb, var(--foreground) 8%, transparent)',
|
|
603
|
+
}}
|
|
604
|
+
/>
|
|
605
|
+
</div>
|
|
606
|
+
</div>
|
|
607
|
+
<ProgressDots
|
|
608
|
+
total={count}
|
|
609
|
+
active={activeIndex}
|
|
610
|
+
reduce={reduce}
|
|
611
|
+
onSelect={scrollToSlide}
|
|
612
|
+
/>
|
|
613
|
+
</motion.div>
|
|
614
|
+
|
|
615
|
+
<div
|
|
616
|
+
className={cn(
|
|
617
|
+
'order-3 flex w-full justify-center border-border/40 pt-2 md:order-3 md:border-0 md:pt-0 md:justify-start',
|
|
618
|
+
hasRight ? 'flex' : 'hidden',
|
|
619
|
+
hasLeft && 'border-t md:border-t-0',
|
|
620
|
+
)}
|
|
621
|
+
>
|
|
622
|
+
{hasRight && (
|
|
623
|
+
<SidePanel
|
|
624
|
+
items={rightItems}
|
|
625
|
+
active={activeIndex}
|
|
626
|
+
revealed={revealed}
|
|
627
|
+
side="right"
|
|
628
|
+
reduce={reduce}
|
|
629
|
+
eyebrowClassName={eyebrowClassName}
|
|
630
|
+
headingClassName={headingClassName}
|
|
631
|
+
bodyClassName={bodyClassName}
|
|
632
|
+
/>
|
|
633
|
+
)}
|
|
634
|
+
</div>
|
|
635
|
+
</div>
|
|
636
|
+
</div>
|
|
637
|
+
</section>
|
|
638
|
+
)
|
|
639
|
+
})
|
|
640
|
+
|
|
641
|
+
ScrollCanvasCards.displayName = 'ScrollCanvasCards'
|
|
642
|
+
|
|
643
|
+
export default ScrollCanvasCards
|