feat(mdx): add ImageCarousel component for post galleries #9
No files matched your search
@@ -0,0 +1,188 @@
|
||||
'use client'
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import Image from './Image'
|
||||
|
||||
export type CarouselImage = {
|
||||
src: string
|
||||
alt?: string
|
||||
caption?: string
|
||||
}
|
||||
|
||||
interface ImageCarouselProps {
|
||||
images: CarouselImage[]
|
||||
/** Tailwind aspect class for the slide box. */
|
||||
aspect?: string
|
||||
/** `cover` crops to fill, `contain` letterboxes. */
|
||||
fit?: 'cover' | 'contain'
|
||||
showArrows?: boolean
|
||||
showDots?: boolean
|
||||
showCounter?: boolean
|
||||
}
|
||||
|
||||
const Arrow = ({ dir }: { dir: 'left' | 'right' }) => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={2}
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
className="h-5 w-5"
|
||||
aria-hidden="true"
|
||||
>
|
||||
{dir === 'left' ? <path d="M15 18l-6-6 6-6" /> : <path d="M9 18l6-6-6-6" />}
|
||||
</svg>
|
||||
)
|
||||
|
||||
export default function ImageCarousel({
|
||||
images,
|
||||
aspect = 'aspect-[16/9]',
|
||||
fit = 'cover',
|
||||
showArrows = true,
|
||||
showDots = true,
|
||||
showCounter = true,
|
||||
}: ImageCarouselProps) {
|
||||
const trackRef = useRef<HTMLDivElement>(null)
|
||||
const [index, setIndex] = useState(0)
|
||||
|
||||
const count = images?.length ?? 0
|
||||
const multiple = count > 1
|
||||
|
||||
const goTo = useCallback(
|
||||
(i: number) => {
|
||||
const track = trackRef.current
|
||||
if (!track) return
|
||||
const clamped = Math.max(0, Math.min(count - 1, i))
|
||||
track.scrollTo({ left: clamped * track.clientWidth, behavior: 'smooth' })
|
||||
},
|
||||
[count]
|
||||
)
|
||||
|
||||
// Keep the dot/counter state in sync with native swipe + trackpad scrolling.
|
||||
useEffect(() => {
|
||||
const track = trackRef.current
|
||||
if (!track) return
|
||||
let raf = 0
|
||||
const onScroll = () => {
|
||||
cancelAnimationFrame(raf)
|
||||
raf = requestAnimationFrame(() => {
|
||||
const width = track.clientWidth || 1
|
||||
setIndex(Math.round(track.scrollLeft / width))
|
||||
})
|
||||
}
|
||||
track.addEventListener('scroll', onScroll, { passive: true })
|
||||
return () => {
|
||||
track.removeEventListener('scroll', onScroll)
|
||||
cancelAnimationFrame(raf)
|
||||
}
|
||||
}, [])
|
||||
|
||||
if (count === 0) return null
|
||||
|
||||
const onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
||||
if (!multiple) return
|
||||
if (event.key === 'ArrowLeft') {
|
||||
event.preventDefault()
|
||||
goTo(index - 1)
|
||||
} else if (event.key === 'ArrowRight') {
|
||||
event.preventDefault()
|
||||
goTo(index + 1)
|
||||
}
|
||||
}
|
||||
|
||||
const activeCaption = images[index]?.caption
|
||||
|
||||
return (
|
||||
<figure className="not-prose my-8">
|
||||
{/* WAI-ARIA carousel pattern: the region is focusable so arrow keys work. */}
|
||||
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */}
|
||||
<div
|
||||
role="group"
|
||||
aria-roledescription="carousel"
|
||||
aria-label="Image carousel"
|
||||
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
|
||||
tabIndex={multiple ? 0 : -1}
|
||||
onKeyDown={onKeyDown}
|
||||
className="group relative overflow-hidden rounded-lg border border-gray-200 bg-gray-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 dark:border-gray-700 dark:bg-gray-800"
|
||||
>
|
||||
<div
|
||||
ref={trackRef}
|
||||
className="flex snap-x snap-mandatory overflow-x-auto scroll-smooth [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
|
||||
>
|
||||
{images.map((image, i) => (
|
||||
<div
|
||||
key={`${image.src}-${i}`}
|
||||
className={`relative w-full shrink-0 snap-center ${aspect}`}
|
||||
aria-hidden={i !== index}
|
||||
>
|
||||
<Image
|
||||
src={image.src}
|
||||
alt={image.alt ?? ''}
|
||||
fill
|
||||
sizes="(max-width: 768px) 100vw, 768px"
|
||||
className={fit === 'cover' ? 'object-cover' : 'object-contain'}
|
||||
priority={i === 0}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{multiple && showArrows && (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => goTo(index - 1)}
|
||||
disabled={index === 0}
|
||||
aria-label="Previous image"
|
||||
className="absolute left-2 top-1/2 -translate-y-1/2 rounded-full bg-black/50 p-2 text-white opacity-70 transition hover:bg-black/70 hover:opacity-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-white disabled:cursor-not-allowed disabled:opacity-0"
|
||||
>
|
||||
<Arrow dir="left" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => goTo(index + 1)}
|
||||
disabled={index === count - 1}
|
||||
aria-label="Next image"
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 rounded-full bg-black/50 p-2 text-white opacity-70 transition hover:bg-black/70 hover:opacity-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-white disabled:cursor-not-allowed disabled:opacity-0"
|
||||
>
|
||||
<Arrow dir="right" />
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{multiple && showCounter && (
|
||||
<div className="absolute right-2 top-2 rounded-full bg-black/50 px-2 py-0.5 text-xs font-medium text-white">
|
||||
{index + 1} / {count}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{multiple && showDots && (
|
||||
<div className="mt-3 flex items-center justify-center gap-2">
|
||||
{images.map((image, i) => (
|
||||
<button
|
||||
key={`dot-${image.src}-${i}`}
|
||||
type="button"
|
||||
onClick={() => goTo(i)}
|
||||
aria-label={`Go to image ${i + 1}`}
|
||||
aria-current={i === index}
|
||||
className={`h-2 rounded-full transition-all focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 ${
|
||||
i === index
|
||||
? 'w-6 bg-primary-500'
|
||||
: 'w-2 bg-gray-300 hover:bg-gray-400 dark:bg-gray-600 dark:hover:bg-gray-500'
|
||||
}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeCaption && (
|
||||
<figcaption className="mt-2 text-center text-sm text-gray-500 dark:text-gray-400">
|
||||
{activeCaption}
|
||||
</figcaption>
|
||||
)}
|
||||
</figure>
|
||||
)
|
||||
}
|
||||
@@ -3,11 +3,13 @@ import Pre from 'pliny/ui/Pre'
|
||||
import BlogNewsletterForm from 'pliny/ui/BlogNewsletterForm'
|
||||
import type { MDXComponents } from 'mdx/types'
|
||||
import Image from './Image'
|
||||
import ImageCarousel from './ImageCarousel'
|
||||
import CustomLink from './Link'
|
||||
import TableWrapper from './TableWrapper'
|
||||
|
||||
export const components: MDXComponents = {
|
||||
Image,
|
||||
ImageCarousel,
|
||||
TOCInline,
|
||||
a: CustomLink,
|
||||
pre: Pre,
|
||||
|
||||
Reference in new issue
Block a user