"use client"; import { TabsList } from "@suphark/ui/components/ui/tabs"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react"; import { type ComponentProps, type RefObject, useCallback, useEffect, useRef, useState, } from "react"; /** องค์ประกอบที่ถือว่า "กำลังเลือกอยู่" ในแถบแท็บ/นำทาง — Tabs (aria-selected) และลิงก์หน้าปัจจุบัน (aria-current) */ const ACTIVE = '[aria-selected="true"],[aria-current="page"],[aria-current="true"]'; /** เลื่อนแถบให้แท็บที่เลือกอยู่กลางแถว (แถบไม่เกินความกว้าง = ไม่ทำอะไร) */ function centerActive(el: HTMLElement, smooth: boolean) { if (el.scrollWidth <= el.clientWidth) return; const active = el.querySelector(ACTIVE); if (!active) return; const a = active.getBoundingClientRect(); const r = el.getBoundingClientRect(); const left = el.scrollLeft + (a.left - r.left) - (r.width - a.width) / 2; const reduce = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches; el.scrollTo({ left: Math.max(0, left), behavior: smooth && !reduce ? "smooth" : "auto" }); } /** * แถบที่เลื่อนแนวนอนได้ (แท็บ/เมนูย่อย): บอกว่า "ยังมีต่อ" ฝั่งไหน (`edges`) และเลื่อนแท็บที่เลือกเข้ากลางแถวเอง * — ตอน mount (ทันที) และทุกครั้งที่ตัวที่เลือกเปลี่ยน (นุ่มนวล, เคารพ prefers-reduced-motion) * ใช้คู่ `` ในกล่องที่ `relative` */ export function useScrollFade(): { ref: RefObject; edges: { start: boolean; end: boolean }; onScroll: () => void; } { const ref = useRef(null); const [edges, setEdges] = useState({ start: false, end: false }); const update = useCallback(() => { const el = ref.current; if (!el) return; const max = el.scrollWidth - el.clientWidth; // 4px กันเศษจากการปัดทศนิยม setEdges({ start: el.scrollLeft > 4, end: el.scrollLeft < max - 4 }); }, []); useEffect(() => { const el = ref.current; if (!el) return; centerActive(el, false); update(); const resize = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update); resize?.observe(el); const mutation = typeof MutationObserver === "undefined" ? null : new MutationObserver(() => centerActive(el, true)); mutation?.observe(el, { attributes: true, attributeFilter: ["aria-selected", "aria-current"], subtree: true, }); return () => { resize?.disconnect(); mutation?.disconnect(); }; }, [update]); return { ref, edges, onScroll: update }; } /** เงาจางที่ขอบซ้าย/ขวาของแถบเลื่อน (+ ลูกศรจาง) — วางใน container `relative` เหนือแถบ */ export function ScrollFades({ edges, className, }: { edges: { start: boolean; end: boolean }; className?: string; }) { const base = "pointer-events-none absolute inset-y-0 flex w-8 items-center text-muted-foreground [&_svg]:size-4"; return ( <> {edges.start && ( )} {edges.end && ( )} ); } /** * `TabsList` ที่เลื่อนแนวนอนได้เมื่อแท็บเกินความกว้าง: มีเงา+ลูกศรบอกว่ายังมีต่อ และเลื่อนแท็บที่เลือกเข้ากลางแถวเอง * ใช้แทน `TabsList` ใน `Tabs` ดิบ (ไฟล์ shadcn แก้ไม่ได้) — `SimpleTabs` ใช้ตัวนี้อยู่แล้ว * * ……… */ export function ScrollableTabsList({ className, wrapperClassName, ...props }: ComponentProps & { wrapperClassName?: string }) { const { ref, edges, onScroll } = useScrollFade(); return (
); }