Tabs: paginowany karuzelowy układ - całe zakładki na stronach, płynny slide, strzałki wewnątrz z fade
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { ReactNode } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { ArrowLeft, ChevronLeft, ChevronRight, X } from 'lucide-react';
|
||||
import { buttonVariants } from './button';
|
||||
@@ -204,8 +204,10 @@ export function EmptyState({
|
||||
);
|
||||
}
|
||||
|
||||
/* Tabs — przy przepełnieniu: ukryty scrollbar + strzałki przewijające o kolejną
|
||||
grupę zakładek; aktywna zakładka dosuwana do widoku. */
|
||||
/* Tabs — paginowane: zakładki dzielone na strony mieszczące się w całości
|
||||
(brak uciętych słów), płynny slide między stronami, strzałki wewnątrz
|
||||
kontenera (fade, miejsce zarezerwowane — bez skoków layoutu).
|
||||
Strona z aktywną zakładką wybierana automatycznie. */
|
||||
export function Tabs({
|
||||
tabs,
|
||||
active,
|
||||
@@ -215,88 +217,138 @@ export function Tabs({
|
||||
active: string;
|
||||
onChange: (id: string) => void;
|
||||
}) {
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const [canPrev, setCanPrev] = useState(false);
|
||||
const [canNext, setCanNext] = useState(false);
|
||||
const wrapRef = useRef<HTMLDivElement>(null);
|
||||
const measureRef = useRef<HTMLDivElement>(null);
|
||||
const [pages, setPages] = useState<number[][]>(() => [tabs.map((_, i) => i)]);
|
||||
const [page, setPage] = useState(0);
|
||||
|
||||
const updateArrows = () => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
const over = el.scrollWidth > el.clientWidth + 2;
|
||||
setCanPrev(over && el.scrollLeft > 2);
|
||||
setCanNext(over && el.scrollLeft + el.clientWidth < el.scrollWidth - 2);
|
||||
const recompute = () => {
|
||||
const wrap = wrapRef.current;
|
||||
const meas = measureRef.current;
|
||||
if (!wrap || !meas || meas.children.length === 0) return;
|
||||
const GAP = 4;
|
||||
const PAD = 8; // p-1 kontenera
|
||||
const ARROW = 28 + GAP; // strzałka + odstęp po jednej stronie
|
||||
const widths = Array.from(meas.children).map((c) => (c as HTMLElement).offsetWidth);
|
||||
const build = (avail: number) => {
|
||||
const out: number[][] = [];
|
||||
let cur: number[] = [];
|
||||
let w = 0;
|
||||
widths.forEach((tw, i) => {
|
||||
const add = tw + (cur.length ? GAP : 0);
|
||||
if (cur.length && w + add > avail) {
|
||||
out.push(cur);
|
||||
cur = [];
|
||||
w = 0;
|
||||
}
|
||||
cur.push(i);
|
||||
w += tw + (cur.length > 1 ? GAP : 0);
|
||||
});
|
||||
if (cur.length) out.push(cur);
|
||||
return out;
|
||||
};
|
||||
let p = build(wrap.clientWidth - PAD);
|
||||
if (p.length > 1) p = build(wrap.clientWidth - PAD - 2 * ARROW);
|
||||
setPages(p);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
updateArrows();
|
||||
window.addEventListener('resize', updateArrows);
|
||||
return () => window.removeEventListener('resize', updateArrows);
|
||||
}, [tabs.length]);
|
||||
useLayoutEffect(() => {
|
||||
recompute();
|
||||
}, [tabs]);
|
||||
|
||||
// aktywna zakładka zawsze w widoku (dosuwa też zapamiętany scroll)
|
||||
useEffect(() => {
|
||||
const el = scrollRef.current;
|
||||
const btn = el?.querySelector<HTMLElement>(`[data-tab-id="${CSS.escape(active)}"]`);
|
||||
if (!el || !btn) return;
|
||||
const br = btn.getBoundingClientRect();
|
||||
const er = el.getBoundingClientRect();
|
||||
if (br.left < er.left) el.scrollBy({ left: br.left - er.left - 8, behavior: 'smooth' });
|
||||
else if (br.right > er.right) el.scrollBy({ left: br.right - er.right + 8, behavior: 'smooth' });
|
||||
updateArrows();
|
||||
}, [active]);
|
||||
const onResize = () => recompute();
|
||||
window.addEventListener('resize', onResize);
|
||||
return () => window.removeEventListener('resize', onResize);
|
||||
}, [tabs]);
|
||||
|
||||
const scrollByGroup = (dir: 1 | -1) => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
el.scrollBy({ left: dir * el.clientWidth * 0.9, behavior: 'smooth' });
|
||||
// strona zawierająca aktywną zakładkę (np. przywróconą z pamięci)
|
||||
useEffect(() => {
|
||||
const idx = tabs.findIndex((t) => t.id === active);
|
||||
const p = pages.findIndex((pg) => pg.includes(idx));
|
||||
if (p >= 0) setPage(p);
|
||||
}, [active, pages, tabs]);
|
||||
|
||||
const multi = pages.length > 1;
|
||||
const safePage = Math.min(page, pages.length - 1);
|
||||
|
||||
const tabButton = (i: number) => {
|
||||
const t = tabs[i];
|
||||
return (
|
||||
<button
|
||||
key={t.id}
|
||||
type="button"
|
||||
data-tab-id={t.id}
|
||||
data-active={active === t.id || undefined}
|
||||
onClick={() => onChange(t.id)}
|
||||
className={`px-3.5 py-2 rounded-lg text-[0.9rem] font-semibold whitespace-nowrap cursor-pointer transition-all duration-150 ${
|
||||
active === t.id ? 'bg-card text-ink border border-line shadow-card' : 'border border-transparent text-dim hover:text-ink'
|
||||
}`}
|
||||
>
|
||||
{t.label}
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={wrapRef}
|
||||
data-slot="tabs"
|
||||
className="flex items-center gap-1 p-1 bg-input rounded-[10px] border border-line"
|
||||
className="relative flex items-center gap-1 p-1 bg-input rounded-[10px] border border-line"
|
||||
>
|
||||
{/* strzałki wewnątrz kontenera, tylko gdy trzeba — zajmują miejsce tylko wtedy */}
|
||||
{canPrev && (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Poprzednie zakładki"
|
||||
onClick={() => scrollByGroup(-1)}
|
||||
className="grid h-7 w-7 shrink-0 cursor-pointer place-items-center rounded-full border border-line bg-card text-dim shadow-card transition-colors hover:text-ink"
|
||||
>
|
||||
<ChevronLeft className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
{/* niewidoczny wiersz pomiarowy — te same klasy co docelowe przyciski */}
|
||||
<div
|
||||
ref={scrollRef}
|
||||
onScroll={updateArrows}
|
||||
className="flex min-w-0 flex-1 gap-1 overflow-x-auto [scroll-snap-type:x_mandatory] [scroll-padding-inline:4px] [scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden"
|
||||
ref={measureRef}
|
||||
aria-hidden="true"
|
||||
className="pointer-events-none invisible absolute flex gap-1"
|
||||
>
|
||||
{tabs.map((t) => (
|
||||
<button
|
||||
<span
|
||||
key={t.id}
|
||||
type="button"
|
||||
data-tab-id={t.id}
|
||||
data-active={active === t.id || undefined}
|
||||
onClick={() => onChange(t.id)}
|
||||
className={`px-3.5 py-2 rounded-lg text-[0.9rem] font-semibold whitespace-nowrap cursor-pointer transition-all duration-150 [scroll-snap-align:start] ${
|
||||
active === t.id ? 'bg-card text-ink border border-line shadow-card' : 'border border-transparent text-dim hover:text-ink'
|
||||
}`}
|
||||
className="px-3.5 py-2 rounded-lg text-[0.9rem] font-semibold whitespace-nowrap border border-transparent"
|
||||
>
|
||||
{t.label}
|
||||
</button>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
{canNext && (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Następne zakładki"
|
||||
onClick={() => scrollByGroup(1)}
|
||||
className="grid h-7 w-7 shrink-0 cursor-pointer place-items-center rounded-full border border-line bg-card text-dim shadow-card transition-colors hover:text-ink"
|
||||
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Poprzednie zakładki"
|
||||
onClick={() => setPage((p) => Math.max(0, p - 1))}
|
||||
className={`grid h-7 w-7 shrink-0 cursor-pointer place-items-center rounded-full border border-line bg-card text-dim shadow-card transition-all duration-150 hover:text-ink ${
|
||||
multi && safePage > 0 ? 'opacity-100' : 'pointer-events-none opacity-0'
|
||||
}`}
|
||||
>
|
||||
<ChevronLeft className="h-4 w-4" />
|
||||
</button>
|
||||
|
||||
<div className="min-w-0 flex-1 overflow-hidden">
|
||||
<div
|
||||
className="flex transition-transform duration-200 ease-out"
|
||||
style={{
|
||||
width: `${pages.length * 100}%`,
|
||||
transform: `translateX(-${safePage * (100 / pages.length)}%)`,
|
||||
}}
|
||||
>
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
{pages.map((pg, pi) => (
|
||||
<div key={pi} className="flex gap-1" style={{ width: `${100 / pages.length}%` }}>
|
||||
{pg.map(tabButton)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Następne zakładki"
|
||||
onClick={() => setPage((p) => Math.min(pages.length - 1, p + 1))}
|
||||
className={`grid h-7 w-7 shrink-0 cursor-pointer place-items-center rounded-full border border-line bg-card text-dim shadow-card transition-all duration-150 hover:text-ink ${
|
||||
multi && safePage < pages.length - 1 ? 'opacity-100' : 'pointer-events-none opacity-0'
|
||||
}`}
|
||||
>
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user