Expand gallery
A row of project panels: the one you point at opens wide and the others fold into slim strips with their names on their side. Good for a selected work strip on a home page.
A row of project panels: the one you point at opens wide and the others fold into slim strips with their names on their side. Good for a selected work strip on a home page.
/**
* MIT License
*
* Copyright (c) 2024 Eduardo Calvo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Rewritten for Tastebud in plain React and CSS, without Tailwind or framer-motion.
*/
"use client";
import { type CSSProperties, type KeyboardEvent, useRef, useState, useSyncExternalStore } from "react";
import styles from "./expand-gallery.module.css";
export type ExpandGalleryOrientation = "horizontal" | "vertical";
export interface ExpandGalleryItem {
id: string;
title: string;
/** Fill colour of the panel. */
color: string;
/** Text colour on the panel. Defaults to near-black; use white on a dark fill. */
ink?: string;
description?: string;
}
export interface ExpandGalleryProps {
items: ExpandGalleryItem[];
orientation?: ExpandGalleryOrientation;
/** Controlled open panel. Pair it with `onActiveIndexChange`. */
activeIndex?: number;
onActiveIndexChange?: (index: number) => void;
/** How much wider the open panel grows than the closed ones. */
expandedFlex?: number;
collapsedFlex?: number;
/** Keep the titles on closed panels, turned on their side. */
showLabelsWhenCollapsed?: boolean;
/** Smaller type and padding, for tight spaces. */
compact?: boolean;
className?: string;
style?: CSSProperties;
}
const HOVER = "(hover: hover) and (pointer: fine)";
const subscribeHover = (onChange: () => void) => {
const query = window.matchMedia(HOVER);
query.addEventListener("change", onChange);
return () => query.removeEventListener("change", onChange);
};
/** True on a device with a mouse or trackpad, where pointing at a panel opens it. */
function useHoverDevice() {
return useSyncExternalStore(
subscribeHover,
() => window.matchMedia(HOVER).matches,
() => false,
);
}
/**
* A row of flat colour panels. The one under the pointer (or tapped, or focused) opens wide and
* the rest fold into slim strips with their titles turned on their side.
*/
export function ExpandGallery({
items,
orientation = "horizontal",
activeIndex: activeIndexProp,
onActiveIndexChange,
expandedFlex = 4,
collapsedFlex = 1,
showLabelsWhenCollapsed = true,
compact = false,
className = "",
style,
}: ExpandGalleryProps) {
const [internalActive, setInternalActive] = useState(0);
const isControlled = activeIndexProp !== undefined;
const activeIndex = isControlled ? activeIndexProp : internalActive;
const isHoverDevice = useHoverDevice();
const panelRefs = useRef<(HTMLButtonElement | null)[]>([]);
const isHorizontal = orientation === "horizontal";
const setActive = (index: number) => {
if (!isControlled) setInternalActive(index);
onActiveIndexChange?.(index);
};
const handleKeyDown = (event: KeyboardEvent<HTMLButtonElement>, index: number) => {
const nextKey = isHorizontal ? "ArrowRight" : "ArrowDown";
const prevKey = isHorizontal ? "ArrowLeft" : "ArrowUp";
if (event.key === nextKey) {
event.preventDefault();
const next = Math.min(index + 1, items.length - 1);
setActive(next);
panelRefs.current[next]?.focus();
} else if (event.key === prevKey) {
event.preventDefault();
const prev = Math.max(index - 1, 0);
setActive(prev);
panelRefs.current[prev]?.focus();
}
};
return (
<div
aria-label="Project panels"
role="group"
className={`${styles.gallery} ${isHorizontal ? styles.horizontal : styles.vertical} ${compact ? styles.compact : ""} ${className}`}
style={style}
>
{items.map((item, index) => {
const isActive = index === activeIndex;
const showLabel = isActive || showLabelsWhenCollapsed;
const turned = isHorizontal && !isActive;
return (
<button
key={item.id}
ref={(el) => {
panelRefs.current[index] = el;
}}
type="button"
aria-label={item.title}
aria-pressed={isActive}
style={{ flexGrow: isActive ? expandedFlex : collapsedFlex, background: item.color, color: item.ink ?? "#1e1e1e" }}
onClick={() => setActive(index)}
onFocus={() => setActive(index)}
onKeyDown={(event) => handleKeyDown(event, index)}
onMouseEnter={() => {
if (isHoverDevice) setActive(index);
}}
className={styles.panel}
>
<span aria-hidden="true" className={styles.number}>
{String(index + 1).padStart(2, "0")}
</span>
<div className={`${styles.label} ${turned ? styles.turnedLabel : ""}`} style={{ opacity: showLabel ? 1 : 0 }}>
{turned ? (
<p className={styles.sideTitle}>{item.title}</p>
) : (
<div>
<p className={styles.title}>{item.title}</p>
{item.description ? <p className={styles.description}>{item.description}</p> : null}
</div>
)}
</div>
</button>
);
})}
</div>
);
}
/* Expand gallery: a row (or column) of flat colour panels; the open one grows, the rest fold to strips. */
.gallery {
display: flex;
width: 100%;
gap: 8px;
}
.horizontal {
flex-direction: row;
height: 360px;
}
.vertical {
flex-direction: column;
height: 520px;
}
.compact {
gap: 6px;
}
.panel {
position: relative;
flex: 1 1 0;
min-width: 0;
min-height: 0;
margin: 0;
padding: 0;
overflow: hidden;
border: 0;
border-radius: 20px;
/* Each panel's own colour is set inline; this is the fallback. */
background: #f4f4f2;
box-shadow: inset 0 0 0 1px rgba(30, 30, 30, 0.06);
color: #1e1e1e;
font: inherit;
text-align: left;
cursor: pointer;
outline: none;
transition: flex-grow 380ms cubic-bezier(0.22, 1, 0.36, 1);
}
.compact .panel {
border-radius: 14px;
}
.panel:focus-visible {
box-shadow:
0 0 0 2px #ffffff,
0 0 0 4px #1abcfe;
}
/* A small number at the top of each panel. */
.number {
position: absolute;
top: 16px;
left: 16px;
font-family: var(--font-mono);
font-size: 11px;
letter-spacing: 0.06em;
line-height: 1;
opacity: 0.7;
}
.compact .number {
top: 8px;
left: 8px;
font-size: 8px;
}
.label {
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 16px;
transition: opacity 300ms ease;
}
.compact .label {
padding: 8px;
}
.turnedLabel {
display: flex;
align-items: flex-end;
justify-content: center;
height: 100%;
}
.sideTitle {
margin: 0;
white-space: nowrap;
writing-mode: vertical-rl;
transform: rotate(180deg);
font-size: 14px;
font-weight: 600;
letter-spacing: -0.01em;
}
.compact .sideTitle {
font-size: 10px;
}
.title {
margin: 0;
font-family: var(--font);
font-weight: 600;
font-size: 24px;
letter-spacing: -0.02em;
line-height: 1.1;
}
.compact .title {
font-size: 13px;
}
.description {
margin: 4px 0 0;
font-size: 14px;
line-height: 1.4;
opacity: 0.78;
}
.compact .description {
font-size: 10px;
line-height: 1.35;
}
@media (prefers-reduced-motion: reduce) {
.panel,
.label {
transition: none;
}
}