hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { Show, type Component } from "solid-js"; |
| 2 | import { cn } from "@/lib/cn"; |
| 3 | |
| 4 | export type ChartComparatorBarProps = { |
| 5 | label: string; |
| 6 | /** Where the fill ends, as a 0..1 fraction of the track. */ |
| 7 | fraction: number; |
| 8 | /** The value as the caller already formatted it — a percent, a mark, a count. */ |
| 9 | valueText: string; |
| 10 | /** |
| 11 | * The baseline this value is read against (a cohort median, a class average) |
| 12 | * as a fraction of the same track. Null when the service withheld it, and |
| 13 | * then the bar is drawn without a tick rather than against a guessed one. |
| 14 | */ |
| 15 | comparatorFraction?: number | null; |
| 16 | /** What the baseline is, e.g. "sınıf ortalaması". */ |
| 17 | comparatorLabel?: string; |
| 18 | comparatorText?: string | null; |
| 19 | /** The signed distance between value and baseline, formatted by the caller. */ |
| 20 | gapText?: string | null; |
| 21 | class?: string; |
| 22 | }; |
| 23 | |
| 24 | const clamp = (value: number) => Math.max(0, Math.min(1, Number.isFinite(value) ? value : 0)); |
| 25 | |
| 26 | /** |
| 27 | * One value read against one baseline, in the width of a table row: the fill is |
| 28 | * the value, the tick across it is the baseline. |
| 29 | * |
| 30 | * Both numbers stay printed next to the bar rather than being encoded in it — |
| 31 | * the bar is the quick read, the text is the actual claim, so the comparison |
| 32 | * survives for a reader who cannot separate the two colours (WCAG 1.4.1). |
| 33 | */ |
| 34 | export const ChartComparatorBar: Component<ChartComparatorBarProps> = (props) => { |
| 35 | const fill = () => clamp(props.fraction); |
| 36 | const tick = () => (props.comparatorFraction == null ? null : clamp(props.comparatorFraction)); |
| 37 | const ariaLabel = () => |
| 38 | [ |
| 39 | `${props.label}: ${props.valueText}`, |
| 40 | props.comparatorText ? `${props.comparatorLabel ?? ""} ${props.comparatorText}`.trim() : null, |
| 41 | props.gapText, |
| 42 | ] |
| 43 | .filter(Boolean) |
| 44 | .join(", "); |
| 45 | |
| 46 | return ( |
| 47 | <div class={cn("space-y-1", props.class)} role="img" aria-label={ariaLabel()}> |
| 48 | <div class="flex items-baseline justify-between gap-3"> |
| 49 | <span class="min-w-0 truncate text-xs text-muted-foreground">{props.label}</span> |
| 50 | <span class="shrink-0 text-sm font-semibold tabular-nums text-text-strong">{props.valueText}</span> |
| 51 | </div> |
| 52 | <div class="relative h-2 overflow-hidden rounded-full bg-surface-overlay"> |
| 53 | <div class="h-full rounded-full bg-primary" style={{ width: `${fill() * 100}%` }} /> |
| 54 | <Show when={tick() != null}> |
| 55 | {/* Pulled half its own width back so the tick sits on the baseline, |
| 56 | not just past it; the last pixel stays inside the track. */} |
| 57 | <span |
| 58 | class="absolute inset-y-0 w-0.5 -translate-x-1/2 rounded-full bg-text-strong" |
| 59 | style={{ left: `${tick()! * 100}%` }} |
| 60 | /> |
| 61 | </Show> |
| 62 | </div> |
| 63 | <Show when={props.comparatorText || props.gapText}> |
| 64 | <p class="flex flex-wrap items-baseline gap-x-2 text-[11px] leading-5 text-muted-foreground"> |
| 65 | <Show when={props.comparatorText}> |
| 66 | {(text) => ( |
| 67 | <span> |
| 68 | <Show when={props.comparatorLabel}> |
| 69 | <span>{props.comparatorLabel}: </span> |
| 70 | </Show> |
| 71 | <span class="font-medium tabular-nums">{text()}</span> |
| 72 | </span> |
| 73 | )} |
| 74 | </Show> |
| 75 | <Show when={props.gapText}> |
| 76 | {(gap) => <span class="font-medium tabular-nums text-text-strong">{gap()}</span>} |
| 77 | </Show> |
| 78 | </p> |
| 79 | </Show> |
| 80 | </div> |
| 81 | ); |
| 82 | }; |