@istmx/ui 0.1.2 → 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,302 @@
1
+ 'use client'
2
+
3
+ import { useId, useState } from 'react'
4
+ import type { CSSProperties } from 'react'
5
+ import {
6
+ AnimatePresence,
7
+ LayoutGroup,
8
+ motion,
9
+ useReducedMotion,
10
+ } from 'motion/react'
11
+ import { cn } from '@/lib/utils'
12
+
13
+ export type ImageAccordionItem = {
14
+ id?: string
15
+ src: string
16
+ alt: string
17
+ title: string
18
+ description?: string
19
+ className?: string
20
+ imageClassName?: string
21
+ }
22
+
23
+ export type ImageAccordionGlow = {
24
+ color?: string
25
+ position?: string
26
+ size?: string
27
+ opacity?: number
28
+ blur?: number
29
+ }
30
+
31
+ export type ImageAccordionProps = {
32
+ images: ImageAccordionItem[]
33
+ className?: string
34
+ stackClassName?: string
35
+ cardClassName?: string
36
+ imageClassName?: string
37
+ previewClassName?: string
38
+ visibleCount?: number
39
+ glow?: false | ImageAccordionGlow
40
+ transition?: {
41
+ stiffness?: number
42
+ damping?: number
43
+ mass?: number
44
+ }
45
+ }
46
+
47
+ export function ImageAccordion({
48
+ images,
49
+ className,
50
+ stackClassName,
51
+ cardClassName,
52
+ imageClassName,
53
+ previewClassName,
54
+ visibleCount = 5,
55
+ glow = {},
56
+ transition,
57
+ }: ImageAccordionProps) {
58
+ const [stackImages, setStackImages] = useState(images)
59
+ const [activeImage, setActiveImage] = useState<ImageAccordionItem | null>(
60
+ null,
61
+ )
62
+ const reduceMotion = useReducedMotion()
63
+ const layoutId = useId()
64
+ const visibleLimit = Number.isFinite(visibleCount)
65
+ ? Math.max(1, Math.floor(visibleCount))
66
+ : 5
67
+ const visibleImages = stackImages.slice(0, visibleLimit)
68
+ const glowOptions = typeof glow === 'object' ? glow : {}
69
+ const spring = {
70
+ type: 'spring' as const,
71
+ stiffness: transition?.stiffness ?? 190,
72
+ damping: transition?.damping ?? 24,
73
+ mass: transition?.mass ?? 0.8,
74
+ }
75
+ const glowStyle = {
76
+ '--image-accordion-glow-color': glowOptions.color ?? 'var(--foreground)',
77
+ '--image-accordion-glow-position': glowOptions.position ?? 'center',
78
+ '--image-accordion-glow-size': glowOptions.size ?? '72%',
79
+ '--image-accordion-glow-opacity': Math.min(
80
+ 1,
81
+ Math.max(0, glowOptions.opacity ?? 0.24),
82
+ ),
83
+ '--image-accordion-glow-blur': `${Math.max(0, glowOptions.blur ?? 18)}px`,
84
+ } as CSSProperties
85
+ const getImageKey = (image: ImageAccordionItem) => image.id ?? image.src
86
+ const activePosition = activeImage
87
+ ? stackImages.findIndex(
88
+ (image) => getImageKey(image) === getImageKey(activeImage),
89
+ ) + 1
90
+ : 0
91
+
92
+ function selectImage(image: ImageAccordionItem) {
93
+ const imageKey = getImageKey(image)
94
+
95
+ if (activeImage && getImageKey(activeImage) === imageKey) {
96
+ setActiveImage(null)
97
+ return
98
+ }
99
+
100
+ setActiveImage(image)
101
+ setStackImages((current) => [
102
+ ...current.filter((item) => getImageKey(item) !== imageKey),
103
+ image,
104
+ ])
105
+ }
106
+
107
+ return (
108
+ <LayoutGroup id={layoutId}>
109
+ <section
110
+ aria-label="Image accordion"
111
+ className={cn(
112
+ 'grid gap-5 md:grid-cols-[minmax(240px,0.8fr)_minmax(0,1.5fr)] md:items-center md:gap-6',
113
+ className,
114
+ )}
115
+ >
116
+ <div className={stackClassName}>
117
+ <div
118
+ role="group"
119
+ aria-label="Choose an image"
120
+ className="relative mx-auto h-[19rem] w-full max-w-[22rem] sm:h-[22rem]"
121
+ >
122
+ {visibleImages.map((image, index) => {
123
+ const imageKey = getImageKey(image)
124
+ const isActive =
125
+ activeImage !== null && getImageKey(activeImage) === imageKey
126
+ const originalIndex = images.findIndex(
127
+ (item) => getImageKey(item) === imageKey,
128
+ )
129
+ const offset = visibleImages.length - index - 1
130
+
131
+ return (
132
+ <motion.button
133
+ key={imageKey}
134
+ type="button"
135
+ aria-label={
136
+ isActive
137
+ ? `Hide preview for ${image.title}`
138
+ : `Show image ${index + 1}: ${image.title}`
139
+ }
140
+ aria-pressed={isActive}
141
+ onClick={() => selectImage(image)}
142
+ initial={false}
143
+ animate={{
144
+ x: index * 13,
145
+ y: index * 14,
146
+ rotate: (index - offset / 2) * 1.4,
147
+ scale: 1 - offset * 0.018,
148
+ }}
149
+ whileHover={
150
+ reduceMotion
151
+ ? undefined
152
+ : { x: index * 13 - 5, y: index * 14 - 7, scale: 1.035 }
153
+ }
154
+ whileTap={reduceMotion ? undefined : { scale: 0.98 }}
155
+ transition={{
156
+ type: 'spring',
157
+ stiffness: 280,
158
+ damping: 25,
159
+ mass: 0.8,
160
+ }}
161
+ style={
162
+ {
163
+ ...glowStyle,
164
+ left: `${index * 7}%`,
165
+ top: `${index * 7}%`,
166
+ zIndex: visibleImages.length - index,
167
+ } as CSSProperties
168
+ }
169
+ className={cn(
170
+ 'group focus-visible:ring-foreground focus-visible:ring-offset-background absolute isolate aspect-[4/3] w-[70%] cursor-pointer overflow-visible rounded-xl text-left focus-visible:ring-2 focus-visible:ring-offset-4 focus-visible:outline-none',
171
+ !reduceMotion && 'transition-transform duration-300',
172
+ cardClassName,
173
+ image.className,
174
+ )}
175
+ >
176
+ {glow !== false ? (
177
+ <span
178
+ aria-hidden="true"
179
+ className={cn(
180
+ 'pointer-events-none absolute -inset-3 -z-10 rounded-[inherit] bg-[radial-gradient(ellipse_at_var(--image-accordion-glow-position),var(--image-accordion-glow-color)_0%,transparent_var(--image-accordion-glow-size))] blur-[var(--image-accordion-glow-blur)] transition-opacity duration-300',
181
+ isActive
182
+ ? 'opacity-[var(--image-accordion-glow-opacity)]'
183
+ : 'opacity-0 group-hover:opacity-[var(--image-accordion-glow-opacity)] group-focus-visible:opacity-[var(--image-accordion-glow-opacity)]',
184
+ )}
185
+ />
186
+ ) : null}
187
+
188
+ <span
189
+ className={cn(
190
+ 'border-border/70 bg-surface group-hover:border-foreground/55 relative block size-full overflow-hidden rounded-xl border shadow-[0_12px_32px_-18px_rgba(0,0,0,0.7)] transition-colors duration-300',
191
+ isActive && 'border-foreground/60',
192
+ )}
193
+ >
194
+ <motion.img
195
+ layoutId={
196
+ isActive
197
+ ? undefined
198
+ : `${layoutId}-image-${originalIndex}`
199
+ }
200
+ src={image.src}
201
+ alt={image.alt}
202
+ loading="lazy"
203
+ decoding="async"
204
+ referrerPolicy="no-referrer"
205
+ draggable={false}
206
+ transition={spring}
207
+ className={cn(
208
+ 'size-full object-cover',
209
+ !reduceMotion &&
210
+ 'transition-transform duration-500 ease-out group-hover:scale-105',
211
+ imageClassName,
212
+ image.imageClassName,
213
+ )}
214
+ />
215
+ <span className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/75 via-black/20 to-transparent px-3 pt-8 pb-2 text-xs font-medium text-white opacity-0 transition-opacity duration-300 group-hover:opacity-100 group-focus-visible:opacity-100">
216
+ {String(index + 1).padStart(2, '0')} / {image.title}
217
+ </span>
218
+ </span>
219
+ </motion.button>
220
+ )
221
+ })}
222
+ </div>
223
+
224
+ <div className="text-muted mt-3 flex items-center justify-between font-mono text-[10px] tracking-[0.14em] uppercase">
225
+ <span>Image stack</span>
226
+ <span>
227
+ {String(visibleImages.length).padStart(2, '0')} /{' '}
228
+ {String(stackImages.length).padStart(2, '0')} images
229
+ </span>
230
+ </div>
231
+ </div>
232
+
233
+ <div
234
+ className={cn(
235
+ 'border-border/70 bg-surface/30 relative flex aspect-[4/3] min-h-64 items-center justify-center overflow-hidden rounded-xl border md:min-h-0',
236
+ previewClassName,
237
+ )}
238
+ >
239
+ <AnimatePresence mode="wait" initial={false}>
240
+ {activeImage ? (
241
+ <motion.figure
242
+ key={getImageKey(activeImage)}
243
+ initial={reduceMotion ? { opacity: 0 } : { opacity: 0.8 }}
244
+ animate={{ opacity: 1 }}
245
+ exit={{ opacity: 0 }}
246
+ transition={{ duration: reduceMotion ? 0 : 0.22 }}
247
+ className="absolute inset-0 m-0"
248
+ >
249
+ <motion.img
250
+ layoutId={`${layoutId}-image-${images.findIndex((image) => getImageKey(image) === getImageKey(activeImage))}`}
251
+ src={activeImage.src}
252
+ alt={activeImage.alt}
253
+ decoding="async"
254
+ referrerPolicy="no-referrer"
255
+ draggable={false}
256
+ transition={spring}
257
+ className={cn(
258
+ 'size-full object-cover',
259
+ imageClassName,
260
+ activeImage.imageClassName,
261
+ )}
262
+ />
263
+ <figcaption className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/85 via-black/40 to-transparent px-5 pt-16 pb-5 text-white sm:px-7 sm:pb-7">
264
+ <p className="font-mono text-[10px] tracking-[0.18em] text-white/65 uppercase">
265
+ Image {String(activePosition).padStart(2, '0')} /{' '}
266
+ {String(stackImages.length).padStart(2, '0')}
267
+ </p>
268
+ <h2 className="mt-2 text-xl font-medium tracking-tight sm:text-2xl">
269
+ {activeImage.title}
270
+ </h2>
271
+ {activeImage.description ? (
272
+ <p className="mt-1 max-w-lg text-sm leading-6 text-white/75">
273
+ {activeImage.description}
274
+ </p>
275
+ ) : null}
276
+ </figcaption>
277
+ </motion.figure>
278
+ ) : (
279
+ <motion.div
280
+ key="empty-preview"
281
+ initial={reduceMotion ? false : { opacity: 0 }}
282
+ animate={{ opacity: 1 }}
283
+ exit={{ opacity: 0 }}
284
+ transition={{ duration: reduceMotion ? 0 : 0.2 }}
285
+ className="px-6 text-center"
286
+ >
287
+ <p className="text-muted font-mono text-[10px] tracking-[0.18em] uppercase">
288
+ Preview
289
+ </p>
290
+ <p className="text-muted mt-2 text-sm">
291
+ Select a corner to reveal an image.
292
+ </p>
293
+ </motion.div>
294
+ )}
295
+ </AnimatePresence>
296
+ </div>
297
+ </section>
298
+ </LayoutGroup>
299
+ )
300
+ }
301
+
302
+ export default ImageAccordion