@onsvisual/svelte-components 0.1.58 → 0.1.60

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.
@@ -7,6 +7,11 @@ export function formatDate(str: any, locale?: string, opts?: {
7
7
  day: string;
8
8
  }): string;
9
9
  export function format(val: any, dp?: any): any;
10
+ export function formatter(dp?: any): {
11
+ (value: number): string;
12
+ (value: number | bigint): string;
13
+ };
10
14
  export function ascending(a: any, b: any): number;
11
15
  export function descending(a: any, b: any): number;
12
16
  export function sleep(ms?: number): Promise<any>;
17
+ export function contrastColor(color: any): "black" | "white";
@@ -12,6 +12,7 @@ export default class Titleblock extends SvelteComponentTyped<{
12
12
  censusLogo?: boolean;
13
13
  titleBadge?: string;
14
14
  titleBadgeAriaLabel?: string;
15
+ titleBadgeColor?: string;
15
16
  }, {
16
17
  [evt: string]: CustomEvent<any>;
17
18
  }, {
@@ -37,6 +38,7 @@ declare const __propDef: {
37
38
  censusLogo?: boolean;
38
39
  titleBadge?: string;
39
40
  titleBadgeAriaLabel?: string;
41
+ titleBadgeColor?: string;
40
42
  };
41
43
  events: {
42
44
  [evt: string]: CustomEvent<any>;
@@ -1,5 +1,5 @@
1
1
  <script>
2
- import { format, isNumeric, ascending, descending } from "../../js/utils.js";
2
+ import { formatter, isNumeric, ascending, descending } from "../../js/utils.js";
3
3
 
4
4
  /**
5
5
  * An optional title for the table
@@ -46,6 +46,7 @@
46
46
  let sort = columns.map((c) => "none");
47
47
 
48
48
  $: sortable = columns.map((d) => d.sortable).includes(true);
49
+ $: formatters = columns.map((d) => formatter(d.dp));
49
50
  </script>
50
51
 
51
52
  <div
@@ -121,16 +122,14 @@
121
122
  <tbody class="ons-table__body">
122
123
  {#each _data as row}
123
124
  <tr class="ons-table__row">
124
- {#each columns as col}
125
+ {#each columns as col, i}
125
126
  <td
126
127
  class="ons-table__cell"
127
128
  class:ons-table__cell--numeric="{col.numeric}"
128
129
  data-th="{col.label}"
129
130
  >{@html col.numeric && isNumeric(row[col.key])
130
- ? format(row[col.key], col.dp)
131
- : row[col.key]
132
- ? row[col.key]
133
- : "&ndash;"}</td
131
+ ? formatters[i](row[col.key])
132
+ : row[col.key] || "&ndash;"}</td
134
133
  >
135
134
  {/each}
136
135
  </tr>
@@ -1,5 +1,5 @@
1
1
  <script>
2
- import parse from "parse-color";
2
+ import { contrastColor } from "../../js/utils";
3
3
 
4
4
  /**
5
5
  * Any valid CSS colour
@@ -12,16 +12,7 @@
12
12
  */
13
13
  export let nowrap = true;
14
14
 
15
- function textColor(color) {
16
- const rgb = parse(color).rgb;
17
- if (rgb) {
18
- const brightness = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;
19
- return brightness > 125 ? "black" : "white";
20
- }
21
- return "black";
22
- }
23
-
24
- $: text = textColor(color);
15
+ $: text = contrastColor(color);
25
16
  </script>
26
17
 
27
18
  <mark
package/dist/js/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ import parse from "parse-color";
2
+
1
3
  const randomString = () => {
2
4
  return Math.random().toString(16).slice(2, 8);
3
5
  };
@@ -44,6 +46,15 @@ export const format = (val, dp = null) => {
44
46
  : val.toLocaleString("en-GB");
45
47
  };
46
48
 
49
+ export const formatter = (dp = null) => {
50
+ return Number.isInteger(dp)
51
+ ? new Intl.NumberFormat("en-GB", {
52
+ minimumFractionDigits: dp,
53
+ maximumFractionDigits: dp,
54
+ }).format
55
+ : new Intl.NumberFormat("en-GB").format;
56
+ };
57
+
47
58
  export const ascending = (a, b) =>
48
59
  a == null || b == null ? NaN : a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
49
60
 
@@ -51,3 +62,13 @@ export const descending = (a, b) =>
51
62
  a == null || b == null ? NaN : b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
52
63
 
53
64
  export const sleep = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms));
65
+
66
+ export const contrastColor = (color) => {
67
+ if (!color || typeof color !== "string") return "black";
68
+ const rgb = parse(color).rgb;
69
+ if (rgb) {
70
+ const brightness = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;
71
+ return brightness > 125 ? "black" : "white";
72
+ }
73
+ return "black";
74
+ };
@@ -1,4 +1,5 @@
1
1
  <script>
2
+ import { contrastColor } from "../../js/utils";
2
3
  import Container from "../../wrappers/Container/Container.svelte";
3
4
  import Meta from "./Meta.svelte";
4
5
 
@@ -52,6 +53,13 @@
52
53
  * @type {string}
53
54
  */
54
55
  export let titleBadgeAriaLabel = null;
56
+ /**
57
+ * Set a colour for the title badge
58
+ * @type {string}
59
+ */
60
+ export let titleBadgeColor = "#003C57";
61
+
62
+ $: titleBadgeTextColor = contrastColor(titleBadgeColor);
55
63
  </script>
56
64
 
57
65
  <Container
@@ -68,8 +76,11 @@
68
76
  <h1 class="ons-u-fs-xxxl ons-u-mt-s ons-u-mb-m ons-u-pb-no ons-u-pt-no">
69
77
  {#if titleBadge}
70
78
  <span style="margin-right: 6px">{@html title}</span>
71
- <span class="title-badge" aria-label="{titleBadgeAriaLabel ?? titleBadge}"
72
- >{titleBadge}</span
79
+ <span
80
+ class="title-badge"
81
+ aria-label="{titleBadgeAriaLabel ?? titleBadge}"
82
+ style:background="{titleBadgeColor}"
83
+ style:color="{titleBadgeTextColor}">{titleBadge}</span
73
84
  >
74
85
  {:else}
75
86
  {@html title}
@@ -131,7 +142,6 @@
131
142
  font-size: 40%;
132
143
  font-weight: bold;
133
144
  color: white;
134
- background-color: #003c57;
135
145
  padding: 2px 8px 4px 8px;
136
146
  border-radius: 4px;
137
147
  }</style>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "0.1.58",
3
+ "version": "0.1.60",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "homepage": "https://onsvisual.github.io/svelte-components",