Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/docs/demos/enlarge.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
sidebar_position: 12
---

import { SourceTabs } from "@site/src/components/source-tabs/source-tabs"
import { EnlargeGallery } from "@site/src/components/demos/enlarge-gallery/enlarge-gallery"
import { OpenInCodePen } from "@site/src/components/codepen/open-in-codepen"

import enlargeSource from "!!raw-loader!@site/src/components/demos/enlarge-gallery/enlarge-gallery.tsx"
import enlargeStyles from "!!raw-loader!@site/src/components/demos/enlarge-gallery/enlarge-gallery.module.scss"
import enlargeJs from "!!ts-to-js!@site/src/components/demos/enlarge-gallery/enlarge-gallery.tsx"
import enlargePen from "!!ts-to-js?codepen!@site/src/components/demos/enlarge-gallery/enlarge-gallery.tsx"

# Enlarge Gallery

Enlarge Gallery using custom state control and the useGallery hook.

<EnlargeGallery />

<OpenInCodePen title="Enlarge Gallery" source={enlargePen} />

## Source

<SourceTabs file="enlarge-gallery" ts={enlargeSource} js={enlargeJs} />

## Styles

The demos on this site are styled with [SCSS modules](https://github.com/css-modules/css-modules).

<SourceTabs file="enlarge-gallery" scss={enlargeStyles} />
206 changes: 206 additions & 0 deletions docs/src/components/demos/enlarge-gallery/enlarge-gallery.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
.root {
--thumb-scale: 0.11;
--thumb-gap: 14px;
--thumb-bottom: 28px;
--zoom-duration: 0.8s;
--zoom-ease: cubic-bezier(0.22, 1, 0.36, 1);

background: #0b0b0f;
color: #fff;
block-size: auto;
inline-size: 100%;
aspect-ratio: 16/9;
overflow: hidden;
position: relative;
margin-block-end: 16px;
border-radius: 10px;

// To increase the specificty
.item {
inset: 0;
margin: 0;
pointer-events: none;
position: absolute;
transform: none;
width: auto;
}

.gallery {
height: 100%;
}

.track {
height: 100%;
overflow: visible;
}

.item[data-active="true"] {
z-index: 1;
}

.item[data-active="false"] {
z-index: 10;
}
}

.slide {
border-radius: 0;
inset: 0;
overflow: hidden;
pointer-events: auto;
position: absolute;
transform: translate(0, 0) scale(1);
transition:
transform var(--zoom-duration) var(--zoom-ease),
border-radius var(--zoom-duration) var(--zoom-ease),
filter 0.25s ease;
will-change: transform;
}

.item[data-active="false"] .slide {
border-radius: 48px;
filter: brightness(0.7);
transform: translate(
calc(var(--offset) * (var(--thumb-scale) * 100vw + var(--thumb-gap))),
calc((50vh - var(--thumb-scale) * 50vh) - var(--thumb-bottom))
)
scale(var(--thumb-scale));
}

.item[data-active="false"] .slide:hover {
filter: brightness(1);
}

.image {
display: block;
height: 100%;
object-fit: cover;
width: 100%;
}

.thumbButton {
appearance: none;
background: none;
border: 0;
cursor: pointer;
inset: 0;
padding: 0;
position: absolute;
}

.rail {
align-items: flex-start;
background: linear-gradient(90deg, rgb(0 0 0 / 60%), transparent);
display: flex;
flex-direction: column;
gap: 2rem;
inset: 0 auto 0 0;
justify-content: center;
max-width: min(620px, 70%);
padding: 0 3.5rem 0 3rem;
pointer-events: none;
position: absolute;
z-index: 5;
}

.content {
animation: content-in 0.6s var(--zoom-ease) calc(var(--zoom-duration) * 0.4) backwards;
display: flex;
flex-direction: column;
gap: 1rem;
}

.kicker {
font-size: 0.8rem;
margin: 0;
opacity: 0.7;
}

.title {
font-size: clamp(2rem, 5vw, 3.5rem);
line-height: 1.05;
margin: 0;
}

.description {
color: rgb(255 255 255 / 85%);
font-size: clamp(0.9rem, 1.4vw, 1.05rem);
line-height: 1.6;
margin: 0;
}

.controls {
display: flex;
gap: 0.75rem;
pointer-events: auto;
}

.navBtn {
align-items: center;
appearance: none;
backdrop-filter: blur(8px);
background: rgb(255 255 255 / 10%);
border: 1px solid rgb(255 255 255 / 20%);
border-radius: 50%;
color: #fff;
cursor: pointer;
display: flex;
height: 3rem;
justify-content: center;
transition:
background 0.2s,
border-color 0.2s,
opacity 0.2s;
width: 3rem;
}

.navBtn:hover:not(:disabled) {
background: rgb(255 255 255 / 22%);
border-color: rgb(255 255 255 / 40%);
}

.navBtn:disabled {
cursor: default;
opacity: 0.35;
}

.visuallyHidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}

@media (width <= 640px) {
.root {
--thumb-scale: 0.16;
--thumb-gap: 10px;
--thumb-bottom: 20px;
}

.rail {
max-width: 90%;
padding: 0 1.5rem;
}
}

@media (prefers-reduced-motion: reduce) {
.slide {
transition: none;
}

.content {
animation: none;
}
}

@keyframes content-in {
from {
opacity: 0;
transform: translateY(24px);
}
}
153 changes: 153 additions & 0 deletions docs/src/components/demos/enlarge-gallery/enlarge-gallery.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { useCallback, useState, PropsWithChildren, CSSProperties } from "react"
import { Gallery, useGallery, GalleryMain, GalleryItem } from "@wethegit/react-gallery"
import "@wethegit/react-gallery/style.css"

import styles from "./enlarge-gallery.module.scss"

import { SLIDES, type Slide } from "../../../data/shared-data"

interface Item extends Slide {
number: number
}

const ITEMS: Item[] = SLIDES.map((slide, index) => ({ ...slide, number: index + 1 }))

export function EnlargeGallery() {
// The thumbnail strip as a queue of item ids, left to right; the active
// item is excluded. Every change removes the incoming item from the queue
// and pushes the outgoing item onto the end.
const [thumbOrder, setThumbOrder] = useState(() =>
ITEMS.slice(1).map((item) => item.number)
)

const handleChange = useCallback(
({ oldIndex, newIndex }: { oldIndex: number; newIndex: number }) => {
setThumbOrder((order) => [
...order.filter((number) => number !== ITEMS[newIndex].number),
ITEMS[oldIndex].number,
])
},
[]
)

return (
<div className={styles.root}>
<Gallery
items={ITEMS}
draggable={false}
className={styles.gallery}
onChange={handleChange}
>
<SlidesLayout thumbOrder={thumbOrder} />
<ActiveContent thumbOrder={thumbOrder} />
</Gallery>
</div>
)
}

function ActiveContent({ thumbOrder }: { thumbOrder: number[] }) {
const indexOfId = (id: number) => ITEMS.findIndex((item) => item.number === id)

const { activeIndex, goToIndex } = useGallery()
const item = SLIDES[activeIndex]

return (
<div className={styles.rail}>
<div className={styles.content} key={item.id}>
<p className={styles.kicker}>
{String(activeIndex + 1).padStart(2, "0")} /{" "}
{String(SLIDES.length).padStart(2, "0")}
</p>
<h2 className={styles.title}>{item.title}</h2>
<p className={styles.description}>{item.description}</p>
</div>

{/*
* Deck-rotation nav: next pulls up the leftmost thumbnail, previous the
* rightmost — which is the most recently viewed, since every change
* pushes the outgoing item to the end of the strip. The queue never
* empties, so neither direction ever disables.
*/}
<div className={styles.controls}>
<button
type="button"
className={styles.navBtn}
onClick={() => goToIndex(indexOfId(thumbOrder[thumbOrder.length - 1]))}
>
<SvgIcon>
<polyline points="15 18 9 12 15 6" />
</SvgIcon>
<span className={styles.visuallyHidden}>Previous</span>
</button>
<button
type="button"
className={styles.navBtn}
onClick={() => goToIndex(indexOfId(thumbOrder[0]))}
>
<SvgIcon>
<polyline points="9 18 15 12 9 6" />
</SvgIcon>
<span className={styles.visuallyHidden}>Next</span>
</button>
</div>
</div>
)
}

function SlidesLayout({ thumbOrder }: { thumbOrder: number[] }) {
const { goToIndex } = useGallery()

return (
<GalleryMain<Item>
className={styles.track}
renderGalleryItem={({ item, index, active }) => {
// Strip slot comes from the queue, centered around the middle of the
// screen. The active item isn't in the queue (indexOf -1), but its
// offset is unused while the full-screen transform applies.
const offset = thumbOrder.indexOf(item.number) - (thumbOrder.length - 1) / 2

return (
<GalleryItem
key={item.id}
index={index}
active={active}
className={styles.item}
data-active={String(active)}
>
<div className={styles.slide} style={{ "--offset": offset } as CSSProperties}>
<img src={item.image} alt={item.alt} className={styles.image} />
{!active && (
<button
type="button"
className={styles.thumbButton}
tabIndex={-1}
onClick={() => goToIndex(index)}
>
<span className={styles.visuallyHidden}>Show {item.title}</span>
</button>
)}
</div>
</GalleryItem>
)
}}
/>
)
}

function SvgIcon({ children }: PropsWithChildren) {
return (
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
{children}
</svg>
)
}
Loading