Tabs: kierunkowy autoscroll do klikanej zakladki (lewa->lewa krawedz, prawa->prawa), snap wyciszony na czas animacji
This commit is contained in:
@@ -235,15 +235,25 @@ export function Tabs({
|
||||
return () => window.removeEventListener('resize', onResize);
|
||||
}, [tabs.length]);
|
||||
|
||||
// aktywna zakładka zawsze w widoku
|
||||
// automatyczny scroll kierunkowy: zakładka schowana bliżej lewej dosuwa się do
|
||||
// lewej krawędzi, bliżej prawej — do prawej; snap wyciszony na czas animacji,
|
||||
// żeby nie cofał wyrównania
|
||||
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 - 4, behavior: 'smooth' });
|
||||
else if (br.right > er.right) el.scrollBy({ left: br.right - er.right + 4, behavior: 'smooth' });
|
||||
let target: number | null = null;
|
||||
if (br.left < er.left) target = el.scrollLeft + (br.left - er.left) - 4;
|
||||
else if (br.right > er.right) target = el.scrollLeft + (br.right - er.right) + 4;
|
||||
if (target == null) return;
|
||||
el.style.scrollSnapType = 'none';
|
||||
el.scrollTo({ left: Math.max(0, target), behavior: 'smooth' });
|
||||
const t = window.setTimeout(() => {
|
||||
el.style.scrollSnapType = '';
|
||||
}, 400);
|
||||
return () => window.clearTimeout(t);
|
||||
}, [active]);
|
||||
|
||||
const scrollByGroup = (dir: 1 | -1) => {
|
||||
|
||||
Reference in New Issue
Block a user