Debian Codegraph @

frontend/src/charts/ChartLegend.svelte

raw
1 <script lang="ts">
2 import type { Writable } from "svelte/store";
3
4 import { currenciesScale } from "./helpers";
5
6 interface Props {
7 /** The chart legend to show. */
8 legend: readonly string[];
9 /** Whether to use currency colors. */
10 color: boolean;
11 /** A list of elements that are toggled. */
12 toggled?: Writable<string[]>;
13 /** Alternatively, a single active element, all others are toggled. */
14 active?: Writable<string | null>;
15 }
16
17 let { legend, color, toggled, active }: Props = $props();
18 </script>
19
20 <div>
21 {#each legend as item}
22 <button
23 type="button"
24 onclick={() => {
25 if (active) {
26 active.set(item);
27 } else if (toggled) {
28 toggled.update((v) =>
29 v.includes(item) ? v.filter((i) => i !== item) : [...v, item],
30 );
31 }
32 }}
33 class:inactive={active ? item !== $active : $toggled?.includes(item)}
34 >
35 <i style="background-color: {color ? $currenciesScale(item) : '#bbb'}"
36 ></i>
37 <span>{item}</span>
38 </button>
39 {/each}
40 </div>
41
42 <style>
43 button {
44 display: contents;
45 color: inherit;
46 }
47
48 .inactive span {
49 text-decoration: line-through;
50 }
51
52 i {
53 display: inline-block;
54 width: 10px;
55 height: 10px;
56 margin-left: 5px;
57 border-radius: 10px;
58 }
59
60 .inactive i {
61 filter: grayscale();
62 }
63
64 @media print {
65 .inactive {
66 display: none;
67 }
68 }
69 </style>