Datasets:
File size: 3,199 Bytes
41235a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import { useState } from 'react'
const noop = (event) => event.preventDefault()
function ViewGlyph({ kind }) {
if (kind === 'two') {
return (
<svg viewBox="0 0 14 14" width="14" height="14" aria-hidden="true">
<rect x="0" y="0" width="6" height="14" />
<rect x="8" y="0" width="6" height="14" />
</svg>
)
}
if (kind === 'four') {
return (
<svg viewBox="0 0 14 14" width="14" height="14" aria-hidden="true">
{[0, 8].map((x) =>
[0, 8].map((y) => <rect key={`${x}-${y}`} x={x} y={y} width="6" height="6" />),
)}
</svg>
)
}
return (
<svg viewBox="0 0 14 14" width="14" height="14" aria-hidden="true">
{[0, 5, 10].map((x) =>
[0, 5, 10].map((y) => <rect key={`${x}-${y}`} x={x} y={y} width="4" height="4" />),
)}
</svg>
)
}
const VIEWS = [
{ cols: 2, kind: 'two', label: '2 columns' },
{ cols: 3, kind: 'four', label: '3 columns' },
{ cols: 4, kind: 'nine', label: '4 columns' },
]
function Cell({ cell, projectName, index }) {
const style = { aspectRatio: `928.5 / ${cell.h}` }
if (cell.off) style.marginTop = `${cell.off / 2}px`
return (
<a
className="cell"
href="#"
onClick={noop}
style={style}
aria-label={`${projectName} — visual ${index + 1}`}
>
{cell.type === 'img' ? (
<img src={cell.src} alt={`${projectName} case visual ${index + 1}`} />
) : (
<span className="hatch" aria-hidden="true" />
)}
</a>
)
}
export default function ProjectSection({ project }) {
const [cols, setCols] = useState(4)
const [pressedCols, setPressedCols] = useState(4)
const cells = project.rows.flat()
return (
<section className="project" id={project.id}>
<div className="project-head">
<h2 className="project-title">
<a className="project-name" href="#" onClick={noop}>
{project.name}
</a>
<span className="project-meta">
, {project.tags.join(', ')}, {project.year}
</span>
</h2>
<button className="plus" type="button" aria-label={`Expand ${project.name}`}>
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
<path d="M8 1v14M1 8h14" stroke="currentColor" strokeWidth="1.2" fill="none" />
</svg>
</button>
<div className="views" role="group" aria-label={`${project.name} grid density`}>
{VIEWS.map((view) => (
<button
key={view.cols}
type="button"
className={`view-btn${pressedCols === view.cols ? ' active' : ''}`}
aria-pressed={pressedCols === view.cols}
aria-label={view.label}
onClick={() => {
setCols(view.cols)
setPressedCols(cols)
}}
>
<ViewGlyph kind={view.kind} />
</button>
))}
</div>
</div>
<div className="grid" style={{ '--cols': cols }}>
{cells.map((cell, i) => (
<Cell key={i} cell={cell} projectName={project.name} index={i} />
))}
</div>
</section>
)
}
|