velvetkit 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +109 -0
- data/README.md +5 -1
- data/lib/generators/velvetkit/add/add_generator.rb +58 -0
- data/lib/velvetkit/version.rb +4 -1
- data/lib/velvetkit.rb +7 -0
- data/registry/app/assets/tailwind/velvetkit.css +90 -155
- data/registry/app/components/ui/chart.html.erb +47 -0
- data/registry/app/components/ui/chart.rb +177 -0
- data/registry/app/components/ui/command/group.rb +27 -0
- data/registry/app/components/ui/command/item.rb +64 -0
- data/registry/app/components/ui/command.html.erb +41 -0
- data/registry/app/components/ui/command.rb +122 -0
- data/registry/app/components/ui/date_picker.html.erb +58 -64
- data/registry/app/components/ui/date_picker.rb +199 -75
- data/registry/app/components/ui/file_field.html.erb +14 -4
- data/registry/app/components/ui/file_field.rb +179 -28
- data/registry/app/components/ui/gallery/image.rb +20 -0
- data/registry/app/components/ui/gallery.html.erb +52 -0
- data/registry/app/components/ui/gallery.rb +164 -0
- data/registry/app/components/ui/kbd.html.erb +27 -0
- data/registry/app/components/ui/kbd.rb +134 -0
- data/registry/app/components/ui/money_input.html.erb +8 -0
- data/registry/app/components/ui/money_input.rb +153 -0
- data/registry/app/components/ui/number_input.html.erb +11 -0
- data/registry/app/components/ui/number_input.rb +103 -0
- data/registry/app/components/ui/progress/segment.rb +25 -0
- data/registry/app/components/ui/progress.html.erb +25 -0
- data/registry/app/components/ui/progress.rb +182 -0
- data/registry/app/components/ui/scan_input.html.erb +6 -0
- data/registry/app/components/ui/scan_input.rb +94 -0
- data/registry/app/components/ui/select.html.erb +24 -5
- data/registry/app/components/ui/select.rb +44 -5
- data/registry/app/components/ui/slider.html.erb +23 -0
- data/registry/app/components/ui/slider.rb +254 -0
- data/registry/app/components/ui/timeline/item.html.erb +21 -0
- data/registry/app/components/ui/timeline/item.rb +145 -0
- data/registry/app/components/ui/timeline.html.erb +1 -0
- data/registry/app/components/ui/timeline.rb +42 -0
- data/registry/app/javascript/chart_js.ts +37 -0
- data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
- data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
- data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
- data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
- data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
- data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
- data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
- data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
- data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
- data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
- data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
- data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
- data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
- data/registry/app/javascript/vanilla_calendar.ts +15 -0
- data/registry/components.yml +25 -3
- data/registry/config/locales/velvetkit.en.yml +79 -0
- data/registry/icons/lucide/download.svg +17 -0
- data/registry/icons/lucide/file-plus.svg +18 -0
- data/registry/icons/lucide/file.svg +16 -0
- data/registry/icons/lucide/folder.svg +15 -0
- data/registry/icons/lucide/keyboard.svg +23 -0
- data/registry/icons/lucide/minus.svg +15 -0
- data/registry/icons/lucide/package.svg +18 -0
- data/registry/icons/lucide/pause.svg +16 -0
- data/registry/icons/lucide/percent.svg +17 -0
- data/registry/icons/lucide/printer.svg +17 -0
- data/registry/icons/lucide/receipt.svg +17 -0
- data/registry/icons/lucide/scan-barcode.svg +21 -0
- data/registry/icons/lucide/sun-moon.svg +19 -0
- data/registry/icons/lucide/truck.svg +19 -0
- data/registry/icons/lucide/user-plus.svg +18 -0
- data/registry/icons/lucide/volume-2.svg +17 -0
- data/registry/icons/lucide/volume-x.svg +17 -0
- data/registry/icons/lucide/zoom-in.svg +18 -0
- data/registry/icons/lucide/zoom-out.svg +17 -0
- data/registry/icons/phosphor/barcode.svg +2 -0
- data/registry/icons/phosphor/circle-half.svg +2 -0
- data/registry/icons/phosphor/download-simple.svg +2 -0
- data/registry/icons/phosphor/file-plus.svg +2 -0
- data/registry/icons/phosphor/file.svg +2 -0
- data/registry/icons/phosphor/folder.svg +2 -0
- data/registry/icons/phosphor/keyboard.svg +2 -0
- data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
- data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
- data/registry/icons/phosphor/minus.svg +2 -0
- data/registry/icons/phosphor/package.svg +2 -0
- data/registry/icons/phosphor/pause.svg +2 -0
- data/registry/icons/phosphor/percent.svg +2 -0
- data/registry/icons/phosphor/printer.svg +2 -0
- data/registry/icons/phosphor/receipt.svg +2 -0
- data/registry/icons/phosphor/speaker-high.svg +2 -0
- data/registry/icons/phosphor/speaker-x.svg +2 -0
- data/registry/icons/phosphor/truck.svg +2 -0
- data/registry/icons/phosphor/user-plus.svg +2 -0
- data/registry/icons/tabler/barcode.svg +23 -0
- data/registry/icons/tabler/download.svg +18 -0
- data/registry/icons/tabler/file-plus.svg +19 -0
- data/registry/icons/tabler/file.svg +17 -0
- data/registry/icons/tabler/folder.svg +16 -0
- data/registry/icons/tabler/keyboard.svg +23 -0
- data/registry/icons/tabler/minus.svg +16 -0
- data/registry/icons/tabler/package.svg +20 -0
- data/registry/icons/tabler/percentage.svg +18 -0
- data/registry/icons/tabler/player-pause.svg +17 -0
- data/registry/icons/tabler/printer.svg +18 -0
- data/registry/icons/tabler/receipt.svg +16 -0
- data/registry/icons/tabler/sun-moon.svg +21 -0
- data/registry/icons/tabler/truck.svg +18 -0
- data/registry/icons/tabler/user-plus.svg +19 -0
- data/registry/icons/tabler/volume-3.svg +17 -0
- data/registry/icons/tabler/volume.svg +18 -0
- data/registry/icons/tabler/zoom-in.svg +19 -0
- data/registry/icons/tabler/zoom-out.svg +18 -0
- data/registry/icons.yml +19 -0
- data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
- data/registry/test/components/previews/ui/chart_preview.rb +46 -0
- data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
- data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
- data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
- data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
- data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
- data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
- data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
- data/registry/test/components/previews/ui/command_preview.rb +25 -0
- data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
- data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
- data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
- data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
- data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
- data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
- data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
- data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
- data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
- data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
- data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
- data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
- data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
- data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
- data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
- data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
- data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
- data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
- data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
- data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
- data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
- data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
- data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
- data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
- data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
- data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
- data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
- data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
- data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
- data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
- data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
- data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
- data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
- data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
- data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
- data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
- data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
- data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
- data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
- data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
- data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
- data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
- data/registry/test/components/previews/ui/progress_preview.rb +46 -0
- data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
- data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
- data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
- data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
- data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
- data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
- data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
- data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
- data/registry/test/components/previews/ui/select_preview.rb +5 -0
- data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
- data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
- data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
- data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
- data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
- data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
- data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
- data/registry/test/components/previews/ui/slider_preview.rb +45 -0
- data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
- data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
- data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
- data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
- data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
- data/registry/test/components/ui/chart_test.rb +187 -0
- data/registry/test/components/ui/command/group_test.rb +19 -0
- data/registry/test/components/ui/command/item_test.rb +47 -0
- data/registry/test/components/ui/command_test.rb +110 -0
- data/registry/test/components/ui/date_picker_test.rb +129 -111
- data/registry/test/components/ui/file_field_test.rb +105 -9
- data/registry/test/components/ui/gallery_test.rb +192 -0
- data/registry/test/components/ui/kbd_test.rb +150 -0
- data/registry/test/components/ui/money_input_test.rb +149 -0
- data/registry/test/components/ui/number_input_test.rb +155 -0
- data/registry/test/components/ui/progress_test.rb +188 -0
- data/registry/test/components/ui/scan_input_test.rb +110 -0
- data/registry/test/components/ui/select_test.rb +62 -0
- data/registry/test/components/ui/slider_test.rb +190 -0
- data/registry/test/components/ui/timeline_test.rb +175 -0
- metadata +188 -2
- data/registry/app/javascript/cally.ts +0 -8
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
import type { Chart as ChartType, ChartConfiguration, ChartDataset, Plugin, TooltipModel } from 'chart.js';
|
|
3
|
+
|
|
4
|
+
// Connects to data-controller="ui--chart"
|
|
5
|
+
//
|
|
6
|
+
// Chart.js arrives from its own bundle (app/javascript/chart_js.ts) when the
|
|
7
|
+
// first chart on a page connects, so other pages never download it. Everything
|
|
8
|
+
// it draws takes its colours from the theme's CSS variables, read again when
|
|
9
|
+
// the theme changes, and the values in the tooltip are the ones Ruby wrote out,
|
|
10
|
+
// so they match the table.
|
|
11
|
+
//
|
|
12
|
+
// A chart is drawn when it scrolls into view, so its entrance is seen: bars
|
|
13
|
+
// grow from the baseline one after another, a line draws itself from left to
|
|
14
|
+
// right, and a pie sweeps round. Nothing moves for anyone who asks for reduced
|
|
15
|
+
// motion.
|
|
16
|
+
type Series = { name: string; color: number; data: (number | null)[]; text: string[]; colors?: number[]; shares?: string[] };
|
|
17
|
+
type AxisFormat = { style: 'number' | 'currency' | 'percent'; locale: string; unit: string; pattern: string };
|
|
18
|
+
type Config = {
|
|
19
|
+
type: 'line' | 'bar' | 'pie' | 'donut';
|
|
20
|
+
stacked: boolean;
|
|
21
|
+
labels: string[];
|
|
22
|
+
series: Series[];
|
|
23
|
+
axis: AxisFormat;
|
|
24
|
+
total?: string;
|
|
25
|
+
total_label?: string;
|
|
26
|
+
};
|
|
27
|
+
type Theme = { series: string[]; hovered: string[]; surface: string; grid: string; muted: string; text: string; font: string };
|
|
28
|
+
|
|
29
|
+
const BAR_THICKNESS = 24;
|
|
30
|
+
const BAR_RADIUS = 4;
|
|
31
|
+
const SURFACE_GAP = 2;
|
|
32
|
+
const LINE_WIDTH = 2;
|
|
33
|
+
const POINT_RADIUS = 5;
|
|
34
|
+
const HIT_RADIUS = 12;
|
|
35
|
+
const END_LABEL_GAP = 8;
|
|
36
|
+
const END_LABEL_HEIGHT = 16;
|
|
37
|
+
const TICK_SIZE = 12;
|
|
38
|
+
const COMPACT_FROM = 10_000;
|
|
39
|
+
const BAR_GROW_MS = 600;
|
|
40
|
+
const SWEEP_MS = 800;
|
|
41
|
+
const DONUT_HOLE = '62%';
|
|
42
|
+
const SLICE_LIFT = 6;
|
|
43
|
+
const BAR_STAGGER_MS = 360;
|
|
44
|
+
const LINE_DRAW_MS = 900;
|
|
45
|
+
const VISIBLE_AT = 0.2;
|
|
46
|
+
const HOVER_MS = 150;
|
|
47
|
+
// How far the crosshair closes on its point each frame, so it slides there.
|
|
48
|
+
const CROSSHAIR_EASE = 0.3;
|
|
49
|
+
|
|
50
|
+
let chartModule: Promise<{ Chart: typeof ChartType }> | null = null;
|
|
51
|
+
|
|
52
|
+
export default class extends Controller<HTMLElement> {
|
|
53
|
+
static targets = ['canvas', 'tooltip'];
|
|
54
|
+
static values = { config: Object, moduleUrl: String };
|
|
55
|
+
|
|
56
|
+
declare readonly canvasTarget: HTMLCanvasElement;
|
|
57
|
+
declare readonly tooltipTarget: HTMLElement;
|
|
58
|
+
declare readonly configValue: Config;
|
|
59
|
+
declare readonly moduleUrlValue: string;
|
|
60
|
+
|
|
61
|
+
private chart?: ChartType;
|
|
62
|
+
private theme?: Theme;
|
|
63
|
+
private themeWatcher?: MutationObserver;
|
|
64
|
+
private schemeQuery?: MediaQueryList;
|
|
65
|
+
private focusIndex = -1;
|
|
66
|
+
private visibility?: IntersectionObserver;
|
|
67
|
+
private revealed = false;
|
|
68
|
+
private reveal = 1;
|
|
69
|
+
private revealFrame?: number;
|
|
70
|
+
private crosshairX: number | null = null;
|
|
71
|
+
private crosshairFrame?: number;
|
|
72
|
+
|
|
73
|
+
// Chart.js starts downloading at once; the chart waits to be on screen.
|
|
74
|
+
connect() {
|
|
75
|
+
chartModule ??= import(this.moduleUrlValue);
|
|
76
|
+
if (!('IntersectionObserver' in window)) {
|
|
77
|
+
this.draw();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.visibility = new IntersectionObserver((entries) => {
|
|
82
|
+
if (!entries.some((entry) => entry.isIntersecting)) return;
|
|
83
|
+
|
|
84
|
+
this.visibility?.disconnect();
|
|
85
|
+
this.draw();
|
|
86
|
+
}, { threshold: VISIBLE_AT });
|
|
87
|
+
this.visibility.observe(this.element);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private async draw() {
|
|
91
|
+
const { Chart } = await chartModule!;
|
|
92
|
+
if (!this.element.isConnected || this.chart) return;
|
|
93
|
+
|
|
94
|
+
this.theme = this.readTheme();
|
|
95
|
+
this.chart = new Chart(this.canvasTarget, this.chartConfig());
|
|
96
|
+
if (this.configValue.type === 'line') this.drawLineIn();
|
|
97
|
+
this.themeWatcher = new MutationObserver(this.retheme);
|
|
98
|
+
this.themeWatcher.observe(document.documentElement, { attributes: true, attributeFilter: ['class', 'data-theme'] });
|
|
99
|
+
this.schemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
100
|
+
this.schemeQuery.addEventListener('change', this.retheme);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
disconnect() {
|
|
104
|
+
this.visibility?.disconnect();
|
|
105
|
+
if (this.revealFrame) cancelAnimationFrame(this.revealFrame);
|
|
106
|
+
if (this.crosshairFrame) cancelAnimationFrame(this.crosshairFrame);
|
|
107
|
+
this.themeWatcher?.disconnect();
|
|
108
|
+
this.schemeQuery?.removeEventListener('change', this.retheme);
|
|
109
|
+
this.chart?.destroy();
|
|
110
|
+
this.chart = undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── Keyboard ─────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
// The arrow keys step through the points, showing the same tooltip as hover.
|
|
116
|
+
keydown(event: KeyboardEvent) {
|
|
117
|
+
const last = this.configValue.labels.length - 1;
|
|
118
|
+
const index = { ArrowRight: this.focusIndex + 1, ArrowLeft: this.focusIndex - 1, Home: 0, End: last }[event.key];
|
|
119
|
+
if (index === undefined || !this.chart) {
|
|
120
|
+
if (event.key === 'Escape') this.blur();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
this.focusIndex = Math.max(0, Math.min(last, index));
|
|
126
|
+
const active = this.configValue.series
|
|
127
|
+
.map((_, datasetIndex) => ({ datasetIndex, index: this.focusIndex }))
|
|
128
|
+
.filter(({ datasetIndex }) => this.configValue.series[datasetIndex].data[this.focusIndex] !== null);
|
|
129
|
+
const point = this.chart.getDatasetMeta(active[0]?.datasetIndex ?? 0).data[this.focusIndex];
|
|
130
|
+
this.chart.setActiveElements(active);
|
|
131
|
+
this.chart.tooltip?.setActiveElements(active, { x: point?.x ?? 0, y: point?.y ?? 0 });
|
|
132
|
+
this.chart.update('none');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
blur() {
|
|
136
|
+
this.focusIndex = -1;
|
|
137
|
+
if (!this.chart) return;
|
|
138
|
+
|
|
139
|
+
this.chart.setActiveElements([]);
|
|
140
|
+
this.chart.tooltip?.setActiveElements([], { x: 0, y: 0 });
|
|
141
|
+
this.chart.update('none');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ── Building the chart ───────────────────────────────────────────────────
|
|
145
|
+
|
|
146
|
+
private chartConfig(): ChartConfiguration {
|
|
147
|
+
const { type, stacked, labels } = this.configValue;
|
|
148
|
+
const theme = this.theme!;
|
|
149
|
+
const shared = {
|
|
150
|
+
responsive: true,
|
|
151
|
+
maintainAspectRatio: false,
|
|
152
|
+
animation: this.entrance(),
|
|
153
|
+
// Hovered points grow, and hovered bars and slices lighten, over a moment.
|
|
154
|
+
transitions: this.reducedMotion ? {} : { active: { animation: { duration: HOVER_MS } } },
|
|
155
|
+
font: { family: theme.font },
|
|
156
|
+
plugins: {
|
|
157
|
+
legend: { display: false },
|
|
158
|
+
tooltip: { enabled: false, external: (context: { tooltip: TooltipModel<'line' | 'bar'> }) => this.drawTooltip(context.tooltip) },
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
if (this.circular) {
|
|
163
|
+
return {
|
|
164
|
+
type: type === 'donut' ? 'doughnut' : 'pie',
|
|
165
|
+
data: { labels, datasets: this.datasets() },
|
|
166
|
+
options: {
|
|
167
|
+
...shared,
|
|
168
|
+
cutout: type === 'donut' ? DONUT_HOLE : 0,
|
|
169
|
+
layout: { padding: SLICE_LIFT + 2 },
|
|
170
|
+
interaction: { mode: 'nearest', intersect: true },
|
|
171
|
+
},
|
|
172
|
+
plugins: type === 'donut' ? [this.donutTotal()] : [],
|
|
173
|
+
} as unknown as ChartConfiguration;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
type,
|
|
178
|
+
data: { labels, datasets: this.datasets() },
|
|
179
|
+
options: {
|
|
180
|
+
...shared,
|
|
181
|
+
interaction: { mode: 'index', intersect: false },
|
|
182
|
+
layout: { padding: { top: 8, right: type === 'line' ? this.endLabelRoom() : 0 } },
|
|
183
|
+
scales: {
|
|
184
|
+
x: {
|
|
185
|
+
stacked,
|
|
186
|
+
grid: { display: false },
|
|
187
|
+
border: { color: theme.grid },
|
|
188
|
+
ticks: { color: theme.muted, font: { family: theme.font, size: TICK_SIZE }, maxRotation: 0, autoSkipPadding: 12 },
|
|
189
|
+
},
|
|
190
|
+
y: {
|
|
191
|
+
stacked,
|
|
192
|
+
beginAtZero: true,
|
|
193
|
+
grid: { color: theme.grid, lineWidth: 1, drawTicks: false },
|
|
194
|
+
border: { display: false },
|
|
195
|
+
ticks: {
|
|
196
|
+
color: theme.muted,
|
|
197
|
+
padding: 8,
|
|
198
|
+
maxTicksLimit: 6,
|
|
199
|
+
font: { family: theme.font, size: TICK_SIZE },
|
|
200
|
+
callback: (value: number | string) => this.axisText(Number(value)),
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
plugins: type === 'line' ? [this.lineReveal(), this.crosshair(), this.endLabels()] : [],
|
|
206
|
+
} as unknown as ChartConfiguration;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private get circular(): boolean {
|
|
210
|
+
return this.configValue.type === 'pie' || this.configValue.type === 'donut';
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private datasets(): ChartDataset[] {
|
|
214
|
+
const theme = this.theme!;
|
|
215
|
+
if (this.circular) {
|
|
216
|
+
// A slice per label, each its own colour, parted by a gap in the surface
|
|
217
|
+
// colour. A hovered slice lifts out a little.
|
|
218
|
+
const series = this.configValue.series[0];
|
|
219
|
+
const colors = series.colors ?? [];
|
|
220
|
+
return [{
|
|
221
|
+
label: series.name,
|
|
222
|
+
data: series.data,
|
|
223
|
+
backgroundColor: colors.map((color) => theme.series[color - 1]),
|
|
224
|
+
hoverBackgroundColor: colors.map((color) => theme.hovered[color - 1]),
|
|
225
|
+
borderColor: theme.surface,
|
|
226
|
+
hoverBorderColor: theme.surface,
|
|
227
|
+
borderWidth: SURFACE_GAP,
|
|
228
|
+
hoverOffset: SLICE_LIFT,
|
|
229
|
+
} as unknown as ChartDataset];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return this.configValue.series.map((series, index) => {
|
|
233
|
+
const color = theme.series[series.color - 1];
|
|
234
|
+
if (this.configValue.type === 'line') {
|
|
235
|
+
return {
|
|
236
|
+
label: series.name,
|
|
237
|
+
data: series.data,
|
|
238
|
+
borderColor: color,
|
|
239
|
+
backgroundColor: color,
|
|
240
|
+
borderWidth: LINE_WIDTH,
|
|
241
|
+
borderCapStyle: 'round',
|
|
242
|
+
borderJoinStyle: 'round',
|
|
243
|
+
tension: 0,
|
|
244
|
+
pointRadius: 0,
|
|
245
|
+
pointHoverRadius: POINT_RADIUS,
|
|
246
|
+
pointHoverBorderWidth: SURFACE_GAP,
|
|
247
|
+
pointHoverBorderColor: theme.surface,
|
|
248
|
+
pointHoverBackgroundColor: color,
|
|
249
|
+
pointHitRadius: HIT_RADIUS,
|
|
250
|
+
} as ChartDataset<'line'>;
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
label: series.name,
|
|
254
|
+
data: series.data,
|
|
255
|
+
backgroundColor: color,
|
|
256
|
+
hoverBackgroundColor: theme.hovered[series.color - 1],
|
|
257
|
+
maxBarThickness: BAR_THICKNESS,
|
|
258
|
+
categoryPercentage: 0.7,
|
|
259
|
+
barPercentage: 0.9,
|
|
260
|
+
borderSkipped: 'start',
|
|
261
|
+
borderRadius: (context: { dataIndex: number }) => (this.isOuterBar(index, context.dataIndex) ? BAR_RADIUS : 0),
|
|
262
|
+
// A surface-coloured edge on each stacked segment is the gap between them.
|
|
263
|
+
borderWidth: this.configValue.stacked ? { top: SURFACE_GAP } : 0,
|
|
264
|
+
borderColor: theme.surface,
|
|
265
|
+
} as unknown as ChartDataset<'bar'>;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Only the end of a stack is rounded; the segments under it stay square.
|
|
270
|
+
private isOuterBar(datasetIndex: number, index: number): boolean {
|
|
271
|
+
if (!this.configValue.stacked) return true;
|
|
272
|
+
|
|
273
|
+
const value = this.configValue.series[datasetIndex].data[index] ?? 0;
|
|
274
|
+
const outer = this.configValue.series
|
|
275
|
+
.map((series, i) => ({ i, value: series.data[index] ?? 0 }))
|
|
276
|
+
.filter((entry) => entry.value !== 0 && Math.sign(entry.value) === Math.sign(value))
|
|
277
|
+
.pop();
|
|
278
|
+
return outer?.i === datasetIndex;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ── Entrance ─────────────────────────────────────────────────────────────
|
|
282
|
+
|
|
283
|
+
// Bars grow from the baseline, left to right, the whole run taking about a
|
|
284
|
+
// second however many there are. A line draws itself instead (drawLineIn), so
|
|
285
|
+
// its data doesn't animate, though hovering still does.
|
|
286
|
+
private entrance() {
|
|
287
|
+
if (this.reducedMotion) return false;
|
|
288
|
+
if (this.configValue.type === 'line') return { duration: 0 };
|
|
289
|
+
if (this.circular) return { duration: SWEEP_MS, easing: 'easeOutQuart' as const, animateRotate: true, animateScale: false };
|
|
290
|
+
|
|
291
|
+
const step = BAR_STAGGER_MS / Math.max(1, this.configValue.labels.length - 1);
|
|
292
|
+
return {
|
|
293
|
+
duration: BAR_GROW_MS,
|
|
294
|
+
easing: 'easeOutQuart' as const,
|
|
295
|
+
onComplete: () => {
|
|
296
|
+
this.revealed = true;
|
|
297
|
+
},
|
|
298
|
+
delay: (context: { type: string; mode: string; dataIndex: number }) =>
|
|
299
|
+
context.type === 'data' && context.mode === 'default' && !this.revealed ? context.dataIndex * step : 0,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Uncovers the lines from left to right, easing out.
|
|
304
|
+
private drawLineIn() {
|
|
305
|
+
if (this.reducedMotion) return;
|
|
306
|
+
|
|
307
|
+
const start = performance.now();
|
|
308
|
+
this.reveal = 0;
|
|
309
|
+
const frame = (now: number) => {
|
|
310
|
+
const t = Math.min(1, (now - start) / LINE_DRAW_MS);
|
|
311
|
+
this.reveal = 1 - (1 - t) ** 3;
|
|
312
|
+
this.chart?.draw();
|
|
313
|
+
this.revealFrame = t < 1 ? requestAnimationFrame(frame) : undefined;
|
|
314
|
+
};
|
|
315
|
+
this.revealFrame = requestAnimationFrame(frame);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ── Plugins ──────────────────────────────────────────────────────────────
|
|
319
|
+
|
|
320
|
+
// Clips the lines to the part drawn so far. Registered first, so the clip is
|
|
321
|
+
// lifted before the crosshair and end labels draw.
|
|
322
|
+
private lineReveal(): Plugin {
|
|
323
|
+
return {
|
|
324
|
+
id: 'uiLineReveal',
|
|
325
|
+
beforeDatasetsDraw: (chart) => {
|
|
326
|
+
if (this.reveal >= 1) return;
|
|
327
|
+
|
|
328
|
+
const { ctx, chartArea } = chart;
|
|
329
|
+
ctx.save();
|
|
330
|
+
ctx.beginPath();
|
|
331
|
+
ctx.rect(0, 0, chartArea.left + (chartArea.right - chartArea.left) * this.reveal, chart.height);
|
|
332
|
+
ctx.clip();
|
|
333
|
+
},
|
|
334
|
+
afterDatasetsDraw: (chart) => {
|
|
335
|
+
if (this.reveal < 1) chart.ctx.restore();
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// The total, in the middle of a donut, sized to fit its hole.
|
|
341
|
+
private donutTotal(): Plugin {
|
|
342
|
+
return {
|
|
343
|
+
id: 'uiDonutTotal',
|
|
344
|
+
afterDatasetsDraw: (chart) => {
|
|
345
|
+
const arc = chart.getDatasetMeta(0).data[0] as unknown as { x: number; y: number; innerRadius: number } | undefined;
|
|
346
|
+
if (!arc || !this.configValue.total) return;
|
|
347
|
+
|
|
348
|
+
const { ctx } = chart;
|
|
349
|
+
const size = Math.max(12, Math.min(24, arc.innerRadius * 0.32));
|
|
350
|
+
ctx.save();
|
|
351
|
+
ctx.textAlign = 'center';
|
|
352
|
+
ctx.textBaseline = 'middle';
|
|
353
|
+
ctx.fillStyle = this.theme!.text;
|
|
354
|
+
ctx.font = `600 ${size}px ${this.theme!.font}`;
|
|
355
|
+
ctx.fillText(this.configValue.total, arc.x, arc.y - size * 0.35);
|
|
356
|
+
ctx.fillStyle = this.theme!.muted;
|
|
357
|
+
ctx.font = `400 ${TICK_SIZE}px ${this.theme!.font}`;
|
|
358
|
+
ctx.fillText(this.configValue.total_label ?? '', arc.x, arc.y + size * 0.6);
|
|
359
|
+
ctx.restore();
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// A hairline down the chart at the point being read. Moving to the next
|
|
365
|
+
// point, it slides across instead of jumping.
|
|
366
|
+
private crosshair(): Plugin {
|
|
367
|
+
return {
|
|
368
|
+
id: 'uiCrosshair',
|
|
369
|
+
afterDatasetsDraw: (chart) => {
|
|
370
|
+
const active = chart.getActiveElements()[0];
|
|
371
|
+
if (!active) {
|
|
372
|
+
this.crosshairX = null;
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const target = active.element.x;
|
|
377
|
+
const x = this.crosshairX === null || this.reducedMotion
|
|
378
|
+
? target
|
|
379
|
+
: this.crosshairX + (target - this.crosshairX) * CROSSHAIR_EASE;
|
|
380
|
+
this.crosshairX = Math.abs(target - x) < 0.5 ? target : x;
|
|
381
|
+
if (this.crosshairX !== target && !this.crosshairFrame) {
|
|
382
|
+
this.crosshairFrame = requestAnimationFrame(() => {
|
|
383
|
+
this.crosshairFrame = undefined;
|
|
384
|
+
chart.draw();
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const { ctx, chartArea } = chart;
|
|
389
|
+
ctx.save();
|
|
390
|
+
ctx.strokeStyle = this.theme!.muted;
|
|
391
|
+
ctx.globalAlpha = 0.5;
|
|
392
|
+
ctx.lineWidth = 1;
|
|
393
|
+
ctx.beginPath();
|
|
394
|
+
ctx.moveTo(Math.round(this.crosshairX) + 0.5, chartArea.top);
|
|
395
|
+
ctx.lineTo(Math.round(this.crosshairX) + 0.5, chartArea.bottom);
|
|
396
|
+
ctx.stroke();
|
|
397
|
+
ctx.restore();
|
|
398
|
+
},
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Each line's last value, beside its end. Labels that would overlap one
|
|
403
|
+
// already placed are left out rather than nudged away from their line; the
|
|
404
|
+
// legend, tooltip and table still have them.
|
|
405
|
+
private endLabels(): Plugin {
|
|
406
|
+
return {
|
|
407
|
+
id: 'uiEndLabels',
|
|
408
|
+
afterDatasetsDraw: (chart) => {
|
|
409
|
+
const { ctx } = chart;
|
|
410
|
+
const placed: number[] = [];
|
|
411
|
+
ctx.save();
|
|
412
|
+
// They appear as the line reaches them, over its last fifth.
|
|
413
|
+
ctx.globalAlpha = Math.max(0, (this.reveal - 0.8) / 0.2);
|
|
414
|
+
ctx.font = `500 ${TICK_SIZE}px ${this.theme!.font}`;
|
|
415
|
+
ctx.fillStyle = this.theme!.text;
|
|
416
|
+
ctx.textBaseline = 'middle';
|
|
417
|
+
this.configValue.series.forEach((series, datasetIndex) => {
|
|
418
|
+
const last = this.lastIndex(series);
|
|
419
|
+
if (last < 0) return;
|
|
420
|
+
|
|
421
|
+
const point = chart.getDatasetMeta(datasetIndex).data[last];
|
|
422
|
+
if (!point || placed.some((y) => Math.abs(y - point.y) < END_LABEL_HEIGHT)) return;
|
|
423
|
+
|
|
424
|
+
placed.push(point.y);
|
|
425
|
+
ctx.fillText(series.text[last], point.x + END_LABEL_GAP, point.y);
|
|
426
|
+
});
|
|
427
|
+
ctx.restore();
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private lastIndex(series: Series): number {
|
|
433
|
+
for (let i = series.data.length - 1; i >= 0; i--) if (series.data[i] !== null) return i;
|
|
434
|
+
return -1;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Room on the right for the widest end label.
|
|
438
|
+
private endLabelRoom(): number {
|
|
439
|
+
const context = document.createElement('canvas').getContext('2d');
|
|
440
|
+
if (!context) return 0;
|
|
441
|
+
|
|
442
|
+
context.font = `500 ${TICK_SIZE}px ${this.theme!.font}`;
|
|
443
|
+
const widths = this.configValue.series.map((series) => {
|
|
444
|
+
const last = this.lastIndex(series);
|
|
445
|
+
return last < 0 ? 0 : context.measureText(series.text[last]).width;
|
|
446
|
+
});
|
|
447
|
+
return Math.ceil(Math.max(0, ...widths) + END_LABEL_GAP * 2);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// ── Tooltip ──────────────────────────────────────────────────────────────
|
|
451
|
+
|
|
452
|
+
// The value leads and the series name follows, keyed by a short stroke of
|
|
453
|
+
// its colour. Built with textContent: names come from the app's data.
|
|
454
|
+
private drawTooltip(tooltip: TooltipModel<'line' | 'bar'>) {
|
|
455
|
+
const box = this.tooltipTarget;
|
|
456
|
+
if (tooltip.opacity === 0 || tooltip.dataPoints.length === 0) {
|
|
457
|
+
box.removeAttribute('data-shown');
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const index = tooltip.dataPoints[0].dataIndex;
|
|
462
|
+
const title = document.createElement('p');
|
|
463
|
+
title.className = 'mb-1 text-muted-foreground';
|
|
464
|
+
title.textContent = this.configValue.labels[index];
|
|
465
|
+
const rows = tooltip.dataPoints.map((point) => {
|
|
466
|
+
const series = this.configValue.series[point.datasetIndex];
|
|
467
|
+
const row = document.createElement('p');
|
|
468
|
+
row.className = 'flex items-center gap-2 whitespace-nowrap';
|
|
469
|
+
const key = document.createElement('span');
|
|
470
|
+
key.className = 'h-0.5 w-3 shrink-0 rounded-full';
|
|
471
|
+
const color = series.colors ? series.colors[point.dataIndex] : series.color;
|
|
472
|
+
key.style.background = this.theme!.series[color - 1];
|
|
473
|
+
const value = document.createElement('span');
|
|
474
|
+
value.className = 'font-semibold tabular-nums text-foreground';
|
|
475
|
+
value.textContent = series.text[point.dataIndex];
|
|
476
|
+
const name = document.createElement('span');
|
|
477
|
+
name.className = 'text-muted-foreground';
|
|
478
|
+
// A slice's share says more than the series' name, which is the same for every slice.
|
|
479
|
+
name.textContent = series.shares ? series.shares[point.dataIndex] : series.name;
|
|
480
|
+
row.append(key, value, name);
|
|
481
|
+
return row;
|
|
482
|
+
});
|
|
483
|
+
box.replaceChildren(title, ...rows);
|
|
484
|
+
const appearing = !box.hasAttribute('data-shown');
|
|
485
|
+
|
|
486
|
+
// Beside the point, on whichever side has room.
|
|
487
|
+
const gap = 12;
|
|
488
|
+
const frame = this.canvasTarget.parentElement!.getBoundingClientRect();
|
|
489
|
+
const left = tooltip.caretX + gap + box.offsetWidth > frame.width ? tooltip.caretX - gap - box.offsetWidth : tooltip.caretX + gap;
|
|
490
|
+
const top = Math.min(Math.max(0, tooltip.caretY - box.offsetHeight / 2), frame.height - box.offsetHeight);
|
|
491
|
+
const place = `translate(${Math.max(0, left)}px, ${Math.max(0, top)}px)`;
|
|
492
|
+
// Appearing, it fades in where it is; only moving between points glides.
|
|
493
|
+
if (appearing) {
|
|
494
|
+
box.style.transition = 'none';
|
|
495
|
+
box.style.transform = place;
|
|
496
|
+
void box.offsetWidth;
|
|
497
|
+
box.style.transition = '';
|
|
498
|
+
box.setAttribute('data-shown', '');
|
|
499
|
+
} else {
|
|
500
|
+
box.style.transform = place;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// ── Numbers ──────────────────────────────────────────────────────────────
|
|
505
|
+
|
|
506
|
+
private axisText(value: number): string {
|
|
507
|
+
const { style, locale, unit, pattern } = this.configValue.axis;
|
|
508
|
+
const number = new Intl.NumberFormat(locale, Math.abs(value) >= COMPACT_FROM ? { notation: 'compact', maximumFractionDigits: 1 } : {})
|
|
509
|
+
.format(Math.abs(value));
|
|
510
|
+
const sign = value < 0 ? '-' : '';
|
|
511
|
+
if (style === 'percent') return `${sign}${number}%`;
|
|
512
|
+
if (style === 'currency') return sign + pattern.replace('%u', unit).replace('%n', number);
|
|
513
|
+
return sign + number;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// ── Theme ────────────────────────────────────────────────────────────────
|
|
517
|
+
|
|
518
|
+
private retheme = () => {
|
|
519
|
+
if (!this.chart) return;
|
|
520
|
+
|
|
521
|
+
this.theme = this.readTheme();
|
|
522
|
+
const theme = this.theme;
|
|
523
|
+
this.chart.data.datasets = this.datasets();
|
|
524
|
+
const scales = (this.chart.options.scales ?? {}) as Record<string, { grid?: { color?: string }; border?: { color?: string }; ticks?: { color?: string } }>;
|
|
525
|
+
for (const scale of Object.values(scales)) {
|
|
526
|
+
if (scale.grid) scale.grid.color = theme.grid;
|
|
527
|
+
if (scale.border) scale.border.color = theme.grid;
|
|
528
|
+
if (scale.ticks) scale.ticks.color = theme.muted;
|
|
529
|
+
}
|
|
530
|
+
this.chart.update('none');
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
// Every colour as the canvas needs it, from the CSS variables in effect here.
|
|
534
|
+
private readTheme(): Theme {
|
|
535
|
+
const style = getComputedStyle(this.element);
|
|
536
|
+
const surface = this.surfaceColor();
|
|
537
|
+
// A see-through token (dark mode's border is white at 10%) is laid over the
|
|
538
|
+
// surface, since the canvas is given solid colours.
|
|
539
|
+
const token = (name: string) => {
|
|
540
|
+
const color = this.toRgb(style.getPropertyValue(name).trim());
|
|
541
|
+
return color[3] < 255 ? this.mix(surface, color, color[3] / 255) : color;
|
|
542
|
+
};
|
|
543
|
+
const series = [1, 2, 3, 4, 5].map((n) => token(`--chart-${n}`));
|
|
544
|
+
return {
|
|
545
|
+
series: series.map((color) => this.rgbString(color)),
|
|
546
|
+
hovered: series.map((color) => this.rgbString(this.mix(color, surface, 0.2))),
|
|
547
|
+
surface: this.rgbString(surface),
|
|
548
|
+
grid: this.rgbString(token('--border')),
|
|
549
|
+
muted: this.rgbString(token('--muted-foreground')),
|
|
550
|
+
text: this.rgbString(token('--foreground')),
|
|
551
|
+
font: style.fontFamily,
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// What the chart is drawn on: the nearest background that isn't see-through.
|
|
556
|
+
private surfaceColor(): number[] {
|
|
557
|
+
for (let element: Element | null = this.element; element; element = element.parentElement) {
|
|
558
|
+
const color = this.toRgb(getComputedStyle(element).backgroundColor);
|
|
559
|
+
if (color[3] > 0) return color;
|
|
560
|
+
}
|
|
561
|
+
return this.toRgb(getComputedStyle(document.body).getPropertyValue('--background').trim() || '#fff');
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Any CSS colour (oklch, hex, color-mix) to RGBA, by painting one pixel.
|
|
565
|
+
private toRgb(color: string): number[] {
|
|
566
|
+
const context = document.createElement('canvas').getContext('2d', { willReadFrequently: true });
|
|
567
|
+
if (!context || !color) return [0, 0, 0, 255];
|
|
568
|
+
|
|
569
|
+
context.fillStyle = color;
|
|
570
|
+
context.fillRect(0, 0, 1, 1);
|
|
571
|
+
return Array.from(context.getImageData(0, 0, 1, 1).data);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
private mix(color: number[], other: number[], amount: number): number[] {
|
|
575
|
+
return color.map((channel, i) => (i === 3 ? 255 : Math.round(channel * (1 - amount) + other[i] * amount)));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
private rgbString([r, g, b]: number[]): string {
|
|
579
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
private get reducedMotion(): boolean {
|
|
583
|
+
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
584
|
+
}
|
|
585
|
+
}
|