@istmx/ui 0.1.3 → 0.1.6

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.
@@ -0,0 +1,438 @@
1
+ 'use client'
2
+
3
+ import { useRef, useState } from 'react'
4
+ import type { CSSProperties, RefObject } from 'react'
5
+ import { IconArrowUpRight } from '@tabler/icons-react'
6
+ import {
7
+ motion,
8
+ useMotionValueEvent,
9
+ useReducedMotion,
10
+ useScroll,
11
+ useTransform,
12
+ } from 'motion/react'
13
+ import type { MotionValue } from 'motion/react'
14
+ import { cn } from '@/lib/utils'
15
+
16
+ export type ScrollStoryCard = {
17
+ id: string
18
+ eyebrow?: string
19
+ title: string
20
+ description: string
21
+ image: string
22
+ imageAlt: string
23
+ color: string
24
+ overlayColor?: string
25
+ overlayOpacity?: number
26
+ buttonLabel?: string
27
+ buttonHref?: string
28
+ buttonColor?: string
29
+ }
30
+
31
+ export type ScrollStoryCardsProps = {
32
+ cards: ScrollStoryCard[]
33
+ eyebrow?: string
34
+ heading?: string
35
+ description?: string
36
+ className?: string
37
+ curveAmount?: number
38
+ cardWidth?: number
39
+ cardHeight?: number
40
+ overlayOpacity?: number
41
+ scrollDistance?: number
42
+ showCardLabels?: boolean
43
+ onActiveChange?: (card: ScrollStoryCard) => void
44
+ scrollContainerRef?: RefObject<HTMLElement | null>
45
+ }
46
+
47
+ function clamp(value: number, min: number, max: number) {
48
+ return Math.min(max, Math.max(min, value))
49
+ }
50
+
51
+ function smoothstep(value: number) {
52
+ const t = clamp(value, 0, 1)
53
+ return t * t * (3 - 2 * t)
54
+ }
55
+
56
+ function hexToRgb(color: string) {
57
+ const value = color.trim().replace('#', '')
58
+ const normalized =
59
+ value.length === 3
60
+ ? value
61
+ .split('')
62
+ .map((part) => part + part)
63
+ .join('')
64
+ : value
65
+
66
+ if (!/^[\da-f]{6}$/i.test(normalized)) return { r: 24, g: 24, b: 27 }
67
+
68
+ return {
69
+ r: Number.parseInt(normalized.slice(0, 2), 16),
70
+ g: Number.parseInt(normalized.slice(2, 4), 16),
71
+ b: Number.parseInt(normalized.slice(4, 6), 16),
72
+ }
73
+ }
74
+
75
+ function mixColor(from: string, to: string, amount: number) {
76
+ const start = hexToRgb(from)
77
+ const end = hexToRgb(to)
78
+ const mix = (a: number, b: number) => Math.round(a + (b - a) * amount)
79
+
80
+ return `rgb(${mix(start.r, end.r)}, ${mix(start.g, end.g)}, ${mix(start.b, end.b)})`
81
+ }
82
+
83
+ function storyPosition(progress: number, count: number) {
84
+ return progress * count
85
+ }
86
+
87
+ function cardRelativePosition(progress: number, index: number, count: number) {
88
+ return index - (storyPosition(progress, count) - 1)
89
+ }
90
+
91
+ function cardWeight(progress: number, index: number, count: number) {
92
+ const distance = Math.abs(cardRelativePosition(progress, index, count))
93
+ return smoothstep(1 - distance)
94
+ }
95
+
96
+ function colorAtProgress(cards: ScrollStoryCard[], progress: number) {
97
+ if (cards.length === 1) return cards[0].color
98
+
99
+ const position = clamp(
100
+ storyPosition(progress, cards.length) - 1,
101
+ 0,
102
+ cards.length - 1,
103
+ )
104
+ const index = Math.min(Math.floor(position), cards.length - 2)
105
+
106
+ return mixColor(
107
+ cards[index].color,
108
+ cards[index + 1].color,
109
+ smoothstep(position - index),
110
+ )
111
+ }
112
+
113
+ function FeaturedCard({
114
+ card,
115
+ index,
116
+ count,
117
+ progress,
118
+ reduceMotion,
119
+ curveAmount,
120
+ overlayOpacity,
121
+ active,
122
+ }: {
123
+ card: ScrollStoryCard
124
+ index: number
125
+ count: number
126
+ progress: MotionValue<number>
127
+ reduceMotion: boolean
128
+ curveAmount: number
129
+ overlayOpacity: number
130
+ active: boolean
131
+ }) {
132
+ const x = useTransform(progress, (value) => {
133
+ if (reduceMotion) return 0
134
+ const relative = cardRelativePosition(value, index, count)
135
+ return smoothstep(clamp(relative, 0, 1)) * curveAmount
136
+ })
137
+ const y = useTransform(progress, (value) => {
138
+ const relative = cardRelativePosition(value, index, count)
139
+ return `${relative * 56}vh`
140
+ })
141
+ const scale = useTransform(progress, (value) =>
142
+ reduceMotion ? 1 : 0.94 + cardWeight(value, index, count) * 0.06,
143
+ )
144
+ const rotate = useTransform(progress, (value) => {
145
+ if (reduceMotion) return 0
146
+ return clamp(cardRelativePosition(value, index, count) * 4, -7, 7)
147
+ })
148
+ const zIndex = useTransform(
149
+ progress,
150
+ (value) => Math.round(cardWeight(value, index, count) * 100) + index,
151
+ )
152
+ const imageY = useTransform(progress, (value) =>
153
+ reduceMotion ? 0 : (1 - cardWeight(value, index, count)) * -5,
154
+ )
155
+ const detailsY = useTransform(progress, (value) =>
156
+ reduceMotion ? 0 : (1 - cardWeight(value, index, count)) * 6,
157
+ )
158
+ const tint = hexToRgb(card.overlayColor ?? card.color)
159
+ const tintOpacity = clamp(card.overlayOpacity ?? overlayOpacity, 0, 1)
160
+
161
+ return (
162
+ <motion.article
163
+ aria-hidden={!active}
164
+ inert={!active}
165
+ className="group/image absolute top-[61%] left-1/2 grid h-[clamp(15rem,52svh,21rem)] min-h-0 w-[min(88vw,560px)] -translate-x-1/2 -translate-y-1/2 grid-cols-[minmax(0,0.8fr)_minmax(0,1fr)] overflow-hidden rounded-xl border border-zinc-950/[0.08] bg-[#fffef9] text-zinc-950 shadow-[0_28px_80px_-48px_rgba(48,38,28,0.32),0_2px_5px_rgba(48,38,28,0.08)] ring-1 ring-white/65 sm:top-[63%] sm:h-[17rem] sm:w-[min(80vw,620px)] sm:grid-cols-[0.9fr_1.1fr]"
166
+ style={{
167
+ x,
168
+ y,
169
+ scale,
170
+ rotate,
171
+ zIndex,
172
+ }}
173
+ >
174
+ <div className="relative m-1.5 min-h-0 overflow-hidden rounded-sm sm:m-3">
175
+ <motion.img
176
+ src={card.image}
177
+ alt={card.imageAlt}
178
+ loading={index === 0 ? 'eager' : 'lazy'}
179
+ decoding="async"
180
+ draggable={false}
181
+ className="absolute inset-0 size-full object-cover transition-transform duration-700 ease-out group-hover/image:scale-[1.035] motion-reduce:transition-none"
182
+ style={{ y: imageY }}
183
+ />
184
+ <span
185
+ aria-hidden="true"
186
+ className="absolute inset-0"
187
+ style={{
188
+ backgroundColor: `rgba(${tint.r}, ${tint.g}, ${tint.b}, ${tintOpacity})`,
189
+ }}
190
+ />
191
+ <span
192
+ aria-hidden="true"
193
+ className="pointer-events-none absolute inset-0 bg-gradient-to-tr from-black/10 via-transparent to-white/10"
194
+ />
195
+ </div>
196
+ <motion.div
197
+ className="flex min-h-0 flex-col items-start p-2.5 sm:p-4 md:p-5"
198
+ style={{ y: detailsY }}
199
+ >
200
+ <div className="w-full">
201
+ <div className="flex items-center justify-between gap-3">
202
+ <p className="font-mono text-[9px] tracking-[0.14em] text-zinc-500 uppercase sm:text-[10px]">
203
+ {card.eyebrow ?? 'Selected story'}
204
+ </p>
205
+ <span className="font-mono text-[9px] tracking-[0.12em] text-zinc-400 tabular-nums sm:text-[10px]">
206
+ {String(index + 1).padStart(2, '0')} /{' '}
207
+ {String(count).padStart(2, '0')}
208
+ </span>
209
+ </div>
210
+ <p className="font-display mt-1.5 text-sm leading-tight font-semibold tracking-tight sm:mt-2 sm:text-xl">
211
+ {card.title}
212
+ </p>
213
+ <p className="mt-1.5 max-w-lg text-[11px] leading-[1.4] text-zinc-600 sm:mt-3 sm:text-[13px] sm:leading-5">
214
+ {card.description}
215
+ </p>
216
+ </div>
217
+ {card.buttonLabel && card.buttonHref ? (
218
+ <a
219
+ href={card.buttonHref}
220
+ style={{
221
+ backgroundColor:
222
+ card.buttonColor ?? card.overlayColor ?? card.color,
223
+ }}
224
+ className="group/link mt-2 ml-auto inline-flex min-h-8 max-w-full shrink-0 items-center gap-2 rounded-sm border border-black/10 px-2.5 text-[9px] font-medium whitespace-nowrap text-zinc-950 shadow-[inset_0_1px_0_rgba(255,255,255,0.7),0_2px_8px_rgba(0,0,0,0.08)] transition-[transform,filter,box-shadow] duration-200 ease-out hover:-translate-y-0.5 hover:shadow-[inset_0_1px_0_rgba(255,255,255,0.75),0_5px_12px_rgba(0,0,0,0.1)] hover:brightness-95 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-zinc-900 active:translate-y-0 motion-reduce:transition-none sm:mt-auto sm:min-h-9 sm:gap-2.5 sm:px-3 sm:text-[11px]"
225
+ >
226
+ {card.buttonLabel}
227
+ <IconArrowUpRight
228
+ aria-hidden="true"
229
+ size={14}
230
+ stroke={1.8}
231
+ className="transition-transform duration-200 group-hover/link:translate-x-0.5 group-hover/link:-translate-y-0.5 motion-reduce:transition-none"
232
+ />
233
+ </a>
234
+ ) : null}
235
+ </motion.div>
236
+ </motion.article>
237
+ )
238
+ }
239
+
240
+ function WaitingCard({
241
+ card,
242
+ index,
243
+ count,
244
+ cardWidth,
245
+ cardHeight,
246
+ overlayOpacity,
247
+ showCardLabels,
248
+ }: {
249
+ card: ScrollStoryCard
250
+ index: number
251
+ count: number
252
+ cardWidth: number
253
+ cardHeight: number
254
+ overlayOpacity: number
255
+ showCardLabels: boolean
256
+ }) {
257
+ const tint = hexToRgb(card.overlayColor ?? card.color)
258
+ return (
259
+ <div
260
+ aria-hidden="true"
261
+ className="relative aspect-square shrink-0 overflow-hidden rounded-sm border border-white/65 bg-black/30 shadow-[0_8px_24px_-18px_rgba(0,0,0,0.6)]"
262
+ style={{
263
+ width: `clamp(58px, 14vw, ${Math.min(cardWidth, cardHeight)}px)`,
264
+ transform: `translateY(${68 - index * 2.5}%) rotate(${(index - (count - 1) / 2) * 1.2}deg)`,
265
+ zIndex: index + 1,
266
+ }}
267
+ >
268
+ <img
269
+ src={card.image}
270
+ alt=""
271
+ loading="lazy"
272
+ decoding="async"
273
+ draggable={false}
274
+ className="size-full object-cover"
275
+ />
276
+ <span
277
+ className="absolute inset-0"
278
+ style={{
279
+ backgroundColor: `rgba(${tint.r}, ${tint.g}, ${tint.b}, ${clamp(card.overlayOpacity ?? overlayOpacity, 0, 1)})`,
280
+ }}
281
+ />
282
+ {showCardLabels ? (
283
+ <span className="absolute inset-x-0 top-0 truncate bg-black/25 px-2 py-1 text-left text-[9px] font-medium text-white">
284
+ {card.title}
285
+ </span>
286
+ ) : null}
287
+ </div>
288
+ )
289
+ }
290
+
291
+ export default function ScrollStoryCards({
292
+ cards,
293
+ eyebrow = 'Selected work',
294
+ heading = 'A collection worth exploring.',
295
+ description,
296
+ className,
297
+ curveAmount = 64,
298
+ cardWidth = 250,
299
+ cardHeight = 156,
300
+ overlayOpacity = 0.38,
301
+ scrollDistance = 400,
302
+ showCardLabels = true,
303
+ onActiveChange,
304
+ scrollContainerRef,
305
+ }: ScrollStoryCardsProps) {
306
+ const sectionRef = useRef<HTMLElement>(null)
307
+ const activeIndexRef = useRef(-1)
308
+ const [activeIndex, setActiveIndex] = useState(-1)
309
+ const reduceMotion = useReducedMotion()
310
+ const storyCards = cards.filter((card) => card.image && card.color)
311
+ const { scrollYProgress } = useScroll({
312
+ container: scrollContainerRef,
313
+ target: sectionRef,
314
+ offset: ['start start', 'end end'],
315
+ })
316
+
317
+ useMotionValueEvent(scrollYProgress, 'change', (progress) => {
318
+ if (storyCards.length === 0) return
319
+ const sequence = storyPosition(progress, storyCards.length) - 1
320
+ const nextIndex =
321
+ sequence < -0.5
322
+ ? -1
323
+ : clamp(Math.round(sequence), 0, storyCards.length - 1)
324
+
325
+ if (activeIndexRef.current !== nextIndex) {
326
+ activeIndexRef.current = nextIndex
327
+ setActiveIndex(nextIndex)
328
+ if (nextIndex >= 0) onActiveChange?.(storyCards[nextIndex])
329
+ }
330
+ })
331
+
332
+ const backgroundColor = useTransform(scrollYProgress, (progress) =>
333
+ storyCards.length ? colorAtProgress(storyCards, progress) : '#18181b',
334
+ )
335
+ const dockOpacity = useTransform(scrollYProgress, [0, 0.012], [1, 0])
336
+ const safeDistance = clamp(scrollDistance, 240, 1200)
337
+ const safeCardWidth = clamp(cardWidth, 140, 420)
338
+ const safeCardHeight = clamp(cardHeight, 96, 280)
339
+ const deckStyle = {
340
+ '--story-deck-height': `${safeCardHeight}px`,
341
+ } as CSSProperties
342
+
343
+ if (storyCards.length === 0) return null
344
+
345
+ return (
346
+ <section
347
+ ref={sectionRef}
348
+ aria-label="Scroll story cards"
349
+ className={cn('relative', className)}
350
+ style={{ minHeight: `${safeDistance}vh` }}
351
+ >
352
+ <div className="sticky top-0 h-svh min-h-0 overflow-hidden">
353
+ <motion.div
354
+ aria-hidden="true"
355
+ className="absolute inset-0 z-0"
356
+ style={{ backgroundColor }}
357
+ />
358
+ <div
359
+ aria-hidden="true"
360
+ className="pointer-events-none absolute inset-0 z-0 bg-[radial-gradient(ellipse_at_50%_58%,rgba(255,255,255,0.56),transparent_65%)]"
361
+ />
362
+ <div className="relative z-20 mx-auto h-full w-full max-w-7xl px-4 pt-[clamp(4.5rem,8svh,7.5rem)] pb-20 text-zinc-950 sm:px-10 sm:pt-[clamp(5.5rem,10svh,7.5rem)] lg:px-16">
363
+ <div className="flex items-center justify-between gap-4 border-b border-zinc-950/10 pb-3">
364
+ <p className="font-mono text-[9px] tracking-[0.16em] text-zinc-600 uppercase sm:text-[10px]">
365
+ {eyebrow}
366
+ </p>
367
+ <p className="font-mono text-[9px] tracking-[0.12em] text-zinc-500 uppercase sm:text-[10px]">
368
+ Scroll to explore
369
+ </p>
370
+ </div>
371
+ <div className="mt-[clamp(2.5rem,7svh,5.5rem)] text-center">
372
+ <h2 className="font-display mx-auto max-w-5xl text-[clamp(2rem,8vw,3.5rem)] leading-[0.98] font-semibold tracking-[-0.055em] text-[#a95f49] sm:text-[clamp(3rem,6vw,4.5rem)] lg:text-[clamp(3.25rem,4.2vw,4.5rem)]">
373
+ {heading}
374
+ </h2>
375
+ {description ? (
376
+ <p className="mx-auto mt-4 max-w-2xl text-[clamp(0.8rem,2.5vw,1rem)] leading-[1.5] text-zinc-600 sm:mt-5 sm:text-base sm:leading-7 lg:text-lg">
377
+ {description}
378
+ </p>
379
+ ) : null}
380
+ </div>
381
+ </div>
382
+
383
+ <div
384
+ role="group"
385
+ aria-label="Featured story cards"
386
+ className="absolute inset-0 z-40 overflow-hidden"
387
+ >
388
+ {storyCards.map((card, index) => (
389
+ <FeaturedCard
390
+ key={card.id}
391
+ card={card}
392
+ index={index}
393
+ count={storyCards.length}
394
+ progress={scrollYProgress}
395
+ reduceMotion={Boolean(reduceMotion)}
396
+ curveAmount={curveAmount}
397
+ overlayOpacity={overlayOpacity}
398
+ active={activeIndex === index}
399
+ />
400
+ ))}
401
+ </div>
402
+
403
+ <motion.div
404
+ aria-hidden="true"
405
+ className="pointer-events-none absolute inset-x-0 bottom-0 z-50 flex h-[var(--story-deck-height)] items-start justify-center gap-2 overflow-hidden px-2 sm:gap-3 sm:px-6"
406
+ style={{ ...deckStyle, opacity: dockOpacity }}
407
+ >
408
+ {storyCards.map((card, index) => (
409
+ <WaitingCard
410
+ key={`${card.id}-stack`}
411
+ card={card}
412
+ index={index}
413
+ count={storyCards.length}
414
+ cardWidth={safeCardWidth}
415
+ cardHeight={safeCardHeight}
416
+ overlayOpacity={overlayOpacity}
417
+ showCardLabels={showCardLabels}
418
+ />
419
+ ))}
420
+ </motion.div>
421
+ </div>
422
+ <ul className="sr-only" aria-label="Story card details">
423
+ {storyCards.map((card, index) =>
424
+ index === activeIndex ? null : (
425
+ <li key={`${card.id}-accessible`}>
426
+ <span>{card.imageAlt}</span>
427
+ <span>{card.title}</span>
428
+ <span>{card.description}</span>
429
+ {card.buttonLabel && card.buttonHref ? (
430
+ <a href={card.buttonHref}>{card.buttonLabel}</a>
431
+ ) : null}
432
+ </li>
433
+ ),
434
+ )}
435
+ </ul>
436
+ </section>
437
+ )
438
+ }