Files
kolaytercih/src/components/ui/accordion.tsx
bilalgursen 41d256bcf5
All checks were successful
Deploy / deploy (push) Successful in 11m30s
revert(tasarim): squircle kaldırıldı, köşeler eski ölçülerine döndü (→ 2.5.12)
Bilal: "tarayıcı desteği olmadığı için mobilde çok kötü oluyor". Safari ve Firefox
corner-shape çizmiyor; squircle'ı taklit etmek için büyütülen köşeler (kart 32,
geniş yüzey 56 px, logo %44) telefonda şişkin, logo neredeyse daire görünüyordu.
- globals.css: @supports corner-shape bloğu, .kt-squircle ve --radius-5xl silindi;
  --radius-2xl/3xl/4xl 18/22/26 px'e döndü.
- md:/lg:rounded-5xl ve md:rounded-t-5xl 19 dosyadan kalktı; gösterge kareleri
  rounded-3xl, keşfet kartı rounded-2xl, program çekmecesi KOSE 24.
- Logo KOSE 25 → 13; morf anahtar karelerinden cornerShape kalktı; Piksel gövdesi
  (piksel-blob logoKaresi) düz daire yayı, k = 13/28.
- Akordeon karosu 18 px, kt-squircle sınıfı kalktı.
- İç içe köşe kuralı (rounded-ic-*, --kt-kose) korundu. DESIGN §4.4, §4.34, §7, §11.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 08:30:27 +03:00

282 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client"
// Akordeon (DESIGN.md §4.45). Davranış Skiper UI 103 "Bouncy accordion"dan
// (skiper-ui.com/v1/skiper103, Pro bileşen: kaynağı alınmadı, yalnız önizlemedeki
// hareket 16 ms DOM kaydıyla ölçüldü, 27 Eki 2026; §11.3'e göre çevrildi):
// - Boy `YAY.acilir` ile açılır ve kapanır (sekmeli: %11 aşıp oturur).
// - Kapanırken boy dinlenme değerinin altına çöküp geri gelir (eksi kısım alt
// boşluk olur; alttaki öğeler bir an yukarı çekilip yerine döner).
// - İçerik boyla aynı anda 0,3 s'de bulanıktan netleşir, kapanırken bulanıklaşıp
// söner (`ACILIR_ICERIK`); yazı sekmez, yalnız kap seker.
// - Ok ikonu değişmez, 180° döner.
// - Tekli akordeonda başka öğeye basınca kapanan ve açılan aynı anda hareket eder.
// - `varyant="karo"` (Bilal, 27 Eki 2026): her öğe ayrı karo; açılan 10 px
// ayrılıp dört köşesi yuvarlanır, komşuların yalnız ona bakan köşesi
// yuvarlanır, kapalı grup tek blok gibi durur (ilkinin üstü, sonuncunun altı
// yuvarlak). Kenar boşluğu ve köşeler boyla aynı yayla sürülür. Kapalı
// komşular 1 px üst üste biner ki kenarlıklar çift çizgi olmasın.
// `varyant="duz"` (varsayılan): tek yüzey, ayraç çizgileri; sürüm notları.
// Radix erişilebilirliği (klavye, aria) her iki varyantta korunur.
import * as React from "react"
import { Accordion as AccordionPrimitive } from "radix-ui"
import {
animate,
motion,
useMotionValue,
useReducedMotion,
useTransform,
type AnimationPlaybackControls,
} from "motion/react"
import { cn } from "@/lib/utils"
import { ACILIR_BULANIKLIK_PX, ACILIR_ICERIK, YAY } from "@/lib/hareket"
import { ChevronDownIcon } from "lucide-react"
// Radix açık/kapalı durumunu bileşen ağacına vermez; boyu `motion` ile
// animasyonlayabilmek için kök değerini burada tutup öğeye soruyoruz.
const AcikMiBaglami = React.createContext<(deger: string) => boolean>(
() => false
)
const OgeDegeriBaglami = React.createContext<string>("")
// Karo varyantı: öğe sırası (komşu açık mı sorusu için) ve varyant.
const KaroBaglami = React.createContext<{ karo: boolean; sira: string[] }>({
karo: false,
sira: [],
})
/** Karo köşesi: satır ölçeği `rounded-2xl` = 18 px (DESIGN.md §2, `--radius-2xl`). */
const KARO_KOSE_PX = 18
/** Açılan karonun komşularından ayrılma payı (Skiper 103 ölçümü: margin-block 10 px). */
const KARO_ARALIK_PX = 10
type KokProps = React.ComponentProps<typeof AccordionPrimitive.Root> & {
varyant?: "duz" | "karo"
}
function Accordion({ className, varyant = "duz", ...props }: KokProps) {
const karo = varyant === "karo"
const sira = karo
? React.Children.toArray(props.children).flatMap((c) =>
React.isValidElement<{ value?: unknown }>(c) &&
typeof c.props.value === "string"
? [c.props.value]
: []
)
: []
const coklu = props.type === "multiple"
const [icDeger, setIcDeger] = React.useState<string | string[]>(() =>
coklu
? ((props as Extract<KokProps, { type: "multiple" }>).defaultValue ?? [])
: ((props as Extract<KokProps, { type: "single" }>).defaultValue ?? "")
)
const disDeger = props.value as string | string[] | undefined
const deger = disDeger ?? icDeger
const acikMi = React.useCallback(
(v: string) => (Array.isArray(deger) ? deger.includes(v) : deger === v),
[deger]
)
const onValueChange = props.onValueChange as
| ((v: string | string[]) => void)
| undefined
const { defaultValue: _varsayilan, ...kalan } = props as KokProps & {
defaultValue?: string | string[]
}
void _varsayilan
const kokProps = {
...kalan,
value: deger,
onValueChange: (v: string | string[]) => {
setIcDeger(v)
onValueChange?.(v)
},
} as KokProps
return (
<AcikMiBaglami.Provider value={acikMi}>
<KaroBaglami.Provider value={{ karo, sira }}>
<AccordionPrimitive.Root
data-slot="accordion"
className={cn("flex w-full flex-col", className)}
{...kokProps}
/>
</KaroBaglami.Provider>
</AcikMiBaglami.Provider>
)
}
function AccordionItem({
className,
children,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Item>) {
const { karo, sira } = React.useContext(KaroBaglami)
const acikMi = React.useContext(AcikMiBaglami)
const azalt = useReducedMotion()
if (!karo) {
return (
<OgeDegeriBaglami.Provider value={props.value}>
<AccordionPrimitive.Item
data-slot="accordion-item"
className={cn("not-last:border-b", className)}
{...props}
>
{children}
</AccordionPrimitive.Item>
</OgeDegeriBaglami.Provider>
)
}
const i = sira.indexOf(props.value)
const son = sira.length - 1
const acik = acikMi(props.value)
const ustAcik = i > 0 && acikMi(sira[i - 1])
const altAcik = i < son && acikMi(sira[i + 1])
const ust = acik || i === 0 || ustAcik ? KARO_KOSE_PX : 0
const alt = acik || i === son || altAcik ? KARO_KOSE_PX : 0
// Köşe ve boşluk satır içi stil (motion); kapalı komşular 1 px biner.
return (
<OgeDegeriBaglami.Provider value={props.value}>
<AccordionPrimitive.Item asChild data-slot="accordion-item" {...props}>
<motion.div
className={className}
initial={false}
animate={{
marginTop: acik ? KARO_ARALIK_PX : i === 0 || ustAcik ? 0 : -1,
marginBottom: acik ? KARO_ARALIK_PX : 0,
borderTopLeftRadius: ust,
borderTopRightRadius: ust,
borderBottomLeftRadius: alt,
borderBottomRightRadius: alt,
}}
transition={azalt ? { duration: 0 } : YAY.acilir}
>
{children}
</motion.div>
</AccordionPrimitive.Item>
</OgeDegeriBaglami.Provider>
)
}
function AccordionTrigger({
className,
children,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Trigger>) {
return (
<AccordionPrimitive.Header className="flex">
<AccordionPrimitive.Trigger
data-slot="accordion-trigger"
className={cn(
"group/accordion-trigger relative flex flex-1 items-start justify-between rounded-lg border border-transparent py-2.5 text-left text-sm font-medium transition-all outline-none hover:underline focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:after:border-ring disabled:pointer-events-none disabled:opacity-50 **:data-[slot=accordion-trigger-icon]:ml-auto **:data-[slot=accordion-trigger-icon]:size-4 **:data-[slot=accordion-trigger-icon]:text-muted-foreground",
className
)}
{...props}
>
{children}
<ChevronDownIcon
data-slot="accordion-trigger-icon"
className="pointer-events-none shrink-0 transition-transform duration-200 group-aria-expanded/accordion-trigger:rotate-180 motion-reduce:transition-none"
/>
</AccordionPrimitive.Trigger>
</AccordionPrimitive.Header>
)
}
function AccordionContent({
className,
children,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Content>) {
const acikMi = React.useContext(AcikMiBaglami)
const acik = acikMi(React.useContext(OgeDegeriBaglami))
const azalt = useReducedMotion()
const icRef = React.useRef<HTMLDivElement>(null)
const kontrol = React.useRef<AnimationPlaybackControls | null>(null)
// Boy tek bir sayıdan sürülür; eksiye inince yükseklik 0'da kalır, fazlası
// eksi alt boşluk olur: kapanan öğe referanstaki gibi dinlenme boyunun
// altına çöküp geri gelir (tarayıcı eksi yüksekliği reddeder ve yay son
// geçerli değerde takılırdı; 27 Eki 2026 ölçümü).
// Sonsuz = açık ve oturmuş: boy "auto"ya bırakılır ki iç içe akordeon ya da
// pencere genişliği değişince içerik kendi boyunu bulsun. React durumu yok;
// her şey MotionValue'da, yeniden render gerekmez.
const boy = useMotionValue(acik ? Number.POSITIVE_INFINITY : 0)
const yukseklik = useTransform(boy, (v) =>
v === Number.POSITIVE_INFINITY ? "auto" : Math.max(0, v)
)
const altBosluk = useTransform(boy, (v) => Math.min(0, v))
const ilk = React.useRef(true)
React.useEffect(() => {
if (ilk.current) {
ilk.current = false
return
}
kontrol.current?.stop()
const hedef = icRef.current?.offsetHeight ?? 0
if (acik) {
if (azalt) {
boy.jump(Number.POSITIVE_INFINITY)
return
}
kontrol.current = animate(boy, hedef, {
...YAY.acilir,
onComplete: () => boy.jump(Number.POSITIVE_INFINITY),
})
} else {
// Oturmuş açık öğe ölçülen boydan iner; yarıda yakalanan öğe olduğu
// yerden döner.
if (boy.get() === Number.POSITIVE_INFINITY) boy.jump(hedef)
if (azalt) {
boy.jump(0)
return
}
kontrol.current = animate(boy, 0, YAY.acilir)
}
// boy sabit bir MotionValue; yalnız açık/kapalı ve hareketi azalt tetikler.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [acik, azalt])
// forceMount: kapalı içerik de ilk HTML'de kalır — JS çalıştırmayan
// tarayıcılar (arama/AI botları) cevapları okuyabilsin. Radix kapalıyken
// içeriği hiç render etmez; forceMount ile de yükseklik ölçümü kapalıyken 0
// çıktığı için accordion-up/down keyframe'leri çalışmaz. Boy bu yüzden
// yukarıdaki yayla sürülür; `initial={false}` sunucu HTML'inde kapalı
// içeriği 0 boy ve 0 opaklıkla basar. visibility geçişi kapalı içeriği yay
// oturduktan sonra odak/ekran okuyucudan çıkarır (açılırken anında geri
// gelir).
return (
<AccordionPrimitive.Content
forceMount
data-slot="accordion-content"
className="group/accordion-content text-sm"
{...props}
>
<motion.div
className="overflow-hidden transition-[visibility] duration-700 group-data-[state=closed]/accordion-content:invisible motion-reduce:transition-none"
style={{ height: yukseklik, marginBottom: altBosluk }}
initial={false}
animate={
acik
? { opacity: 1, filter: "blur(0px)" }
: {
opacity: 0,
filter: azalt ? "blur(0px)" : `blur(${ACILIR_BULANIKLIK_PX}px)`,
}
}
transition={azalt ? { duration: 0 } : ACILIR_ICERIK}
>
<div
ref={icRef}
className={cn(
"pt-0 pb-2.5 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4",
className
)}
>
{children}
</div>
</motion.div>
</AccordionPrimitive.Content>
)
}
export { Accordion, AccordionItem, AccordionTrigger, AccordionContent }