Align admin, community, search, library, learning and course management with the mockup. Add saved courses, download history, public community feed, instructor directory, backend search, course views, admin content tools and community moderation. Harden API and Nginx for production.
56 lines
2.0 KiB
TypeScript
56 lines
2.0 KiB
TypeScript
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { Link } from 'react-router-dom';
|
|
import { CaretLeftIcon, CaretRightIcon } from '@phosphor-icons/react';
|
|
|
|
interface ShelfProps {
|
|
title: string;
|
|
to?: string;
|
|
kind?: 'wide' | 'tall';
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
/** A horizontal row with scroll-snap; the title is a link and the arrows appear on hover. */
|
|
export const Shelf: React.FC<ShelfProps> = ({ title, to, kind, children }) => {
|
|
const railRef = useRef<HTMLDivElement>(null);
|
|
const [edges, setEdges] = useState({ start: true, end: false, fits: true });
|
|
|
|
const update = useCallback(() => {
|
|
const rail = railRef.current;
|
|
if (!rail) return;
|
|
setEdges({
|
|
start: rail.scrollLeft < 4,
|
|
end: rail.scrollLeft + rail.clientWidth >= rail.scrollWidth - 4,
|
|
fits: rail.scrollWidth <= rail.clientWidth + 4,
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
update();
|
|
const rail = railRef.current;
|
|
if (!rail) return;
|
|
const observer = new ResizeObserver(update);
|
|
observer.observe(rail);
|
|
return () => observer.disconnect();
|
|
}, [update, children]);
|
|
|
|
const scroll = (direction: -1 | 1) => {
|
|
const rail = railRef.current;
|
|
if (rail) rail.scrollBy({ left: direction * rail.clientWidth * 0.9 });
|
|
};
|
|
|
|
return (
|
|
<section className="shelf">
|
|
<div className="rowhead">
|
|
{to ? <Link className="shead" to={to}><h2>{title}</h2><CaretRightIcon className="ph" weight="bold" /></Link> : <h2>{title}</h2>}
|
|
{!edges.fits && (
|
|
<div className="nav2">
|
|
<button type="button" className="iconbtn" onClick={() => scroll(-1)} disabled={edges.start} aria-label="Anterior"><CaretLeftIcon className="ph" weight="bold" /></button>
|
|
<button type="button" className="iconbtn" onClick={() => scroll(1)} disabled={edges.end} aria-label="Próximo"><CaretRightIcon className="ph" weight="bold" /></button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div ref={railRef} className={`rail${kind ? ` ${kind}` : ''}`} onScroll={update}>{children}</div>
|
|
</section>
|
|
);
|
|
};
|