@nextlyhq/ui 0.0.2-alpha.65 → 0.0.2-alpha.67
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.
- package/dist/format.cjs +51 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +71 -0
- package/dist/format.d.ts +71 -0
- package/dist/format.mjs +48 -0
- package/dist/format.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/styles.scoped.css +1 -1
- package/dist/theme.css +22 -5
- package/package.json +16 -6
package/dist/format.cjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/lib/format/number.ts
|
|
4
|
+
var COMPACT_FROM = 1e3;
|
|
5
|
+
var groupedCache = /* @__PURE__ */ new Map();
|
|
6
|
+
var compactCache = /* @__PURE__ */ new Map();
|
|
7
|
+
function grouped(locale) {
|
|
8
|
+
let f = groupedCache.get(locale);
|
|
9
|
+
if (!f) {
|
|
10
|
+
f = new Intl.NumberFormat(locale);
|
|
11
|
+
groupedCache.set(locale, f);
|
|
12
|
+
}
|
|
13
|
+
return f;
|
|
14
|
+
}
|
|
15
|
+
function compact(locale) {
|
|
16
|
+
let f = compactCache.get(locale);
|
|
17
|
+
if (!f) {
|
|
18
|
+
f = new Intl.NumberFormat(locale, {
|
|
19
|
+
notation: "compact",
|
|
20
|
+
maximumFractionDigits: 1
|
|
21
|
+
});
|
|
22
|
+
compactCache.set(locale, f);
|
|
23
|
+
}
|
|
24
|
+
return f;
|
|
25
|
+
}
|
|
26
|
+
function formatCount(value, options = {}) {
|
|
27
|
+
const locale = options.locale ?? "en-US";
|
|
28
|
+
const full = grouped(locale).format(value);
|
|
29
|
+
const shown = options.tier === "small" && Math.abs(value) >= COMPACT_FROM ? compact(locale).format(value).replace(/K$/, "k") : full;
|
|
30
|
+
return options.floor ? { text: `${shown}+`, label: `${full} or more` } : { text: shown, label: full };
|
|
31
|
+
}
|
|
32
|
+
function formatChange(delta, options = {}) {
|
|
33
|
+
const magnitude = formatCount(Math.abs(delta), options);
|
|
34
|
+
if (delta === 0) {
|
|
35
|
+
return { text: "\u2192 \xB10", label: "no change", direction: "none" };
|
|
36
|
+
}
|
|
37
|
+
return delta > 0 ? {
|
|
38
|
+
text: `\u2191 +${magnitude.text}`,
|
|
39
|
+
label: `up ${magnitude.label}`,
|
|
40
|
+
direction: "up"
|
|
41
|
+
} : {
|
|
42
|
+
text: `\u2193 \u2212${magnitude.text}`,
|
|
43
|
+
label: `down ${magnitude.label}`,
|
|
44
|
+
direction: "down"
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
exports.formatChange = formatChange;
|
|
49
|
+
exports.formatCount = formatCount;
|
|
50
|
+
//# sourceMappingURL=format.cjs.map
|
|
51
|
+
//# sourceMappingURL=format.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/format/number.ts"],"names":[],"mappings":";;;AA6CA,IAAM,YAAA,GAAe,GAAA;AAIrB,IAAM,YAAA,uBAAmB,GAAA,EAA+B;AACxD,IAAM,YAAA,uBAAmB,GAAA,EAA+B;AAExD,SAAS,QAAQ,MAAA,EAAmC;AAClD,EAAA,IAAI,CAAA,GAAI,YAAA,CAAa,GAAA,CAAI,MAAM,CAAA;AAC/B,EAAA,IAAI,CAAC,CAAA,EAAG;AACN,IAAA,CAAA,GAAI,IAAI,IAAA,CAAK,YAAA,CAAa,MAAM,CAAA;AAChC,IAAA,YAAA,CAAa,GAAA,CAAI,QAAQ,CAAC,CAAA;AAAA,EAC5B;AACA,EAAA,OAAO,CAAA;AACT;AAEA,SAAS,QAAQ,MAAA,EAAmC;AAClD,EAAA,IAAI,CAAA,GAAI,YAAA,CAAa,GAAA,CAAI,MAAM,CAAA;AAC/B,EAAA,IAA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A,CAAA;AAAA,IAC1B,KAAA,EAAO,CAAA,GAAA,EAAM,SAAA,CAAU,KAAK,CAAA,CAAA;AAAA,IAC5B,SAAA,EAAW;AAAA,GACb,GACA;AAAA,IACE,IAAA,EAAM,CAAA,aAAA,EAAM,SAAA,CAAU,IAAI,CAAA,CAAA;AAAA,IAC1B,KAAA,EAAO,CAAA,KAAA,EAAQ,SAAA,CAAU,KAAK,CAAA,CAAA;AAAA,IAC9B,SAAA,EAAW;AAAA,GACb;AACN","file":"format.cjs","sourcesContent":["/**\n * The one number formatter for dashboard cards.\n *\n * Every figure a card shows -- a count, a chart value, a tooltip, a table cell, a text\n * alternative -- goes through here, so the same number reads the same everywhere it appears.\n * Two figures for one value that disagree (\"1.2k\" in a chart, \"1,248\" in its table, \"1248\" in\n * its description) are what this exists to make impossible.\n *\n * It is pure arithmetic on numbers with `Intl.NumberFormat`: no React, no DOM, so a server can\n * format the same way the admin does.\n *\n * @module lib/format/number\n */\n\n/**\n * @experimental The size a card is drawn at, from its own width. Compact numbers appear only in the small\n * tier, where a full grouped value would not fit; everywhere else the exact value fits and is\n * shown.\n */\nexport type SizeTier = \"small\" | \"medium\" | \"large\";\n\n/** @experimental How a count is formatted: its locale, its card's tier, and whether it is a floor. */\nexport interface FormatCountOptions {\n /** The admin's locale. Defaults to `en-US`, the admin's own default. */\n locale?: string;\n /** The tier the figure is drawn in. Only `small` compacts. */\n tier?: SizeTier;\n /**\n * The value is a lower bound: a source that stops counting at a limit knows only \"at\n * least this many\". Shown as \"N+\" and named \"N or more\".\n */\n floor?: boolean;\n}\n\n/** @experimental What a figure looks like, and what assistive technology hears for it. */\nexport interface FormattedNumber {\n /** Drawn on screen: \"1,248\", \"1.2k\", \"100+\". */\n text: string;\n /**\n * The accessible name: always the full grouped value, so \"1.2k\" is heard as \"1,248\"; a\n * floor is heard as \"100 or more\", never as a bare \"100\".\n */\n label: string;\n}\n\nconst COMPACT_FROM = 1000;\n\n// Intl formatters are costly to build and a dashboard formats hundreds of figures, so one of\n// each kind is kept per locale.\nconst groupedCache = new Map<string, Intl.NumberFormat>();\nconst compactCache = new Map<string, Intl.NumberFormat>();\n\nfunction grouped(locale: string): Intl.NumberFormat {\n let f = groupedCache.get(locale);\n if (!f) {\n f = new Intl.NumberFormat(locale);\n groupedCache.set(locale, f);\n }\n return f;\n}\n\nfunction compact(locale: string): Intl.NumberFormat {\n let f = compactCache.get(locale);\n if (!f) {\n f = new Intl.NumberFormat(locale, {\n notation: \"compact\",\n maximumFractionDigits: 1,\n });\n compactCache.set(locale, f);\n }\n return f;\n}\n\n/**\n * @experimental Format a count, total or chart value.\n *\n * Compact form (\"1.2k\", \"3.4M\") only in the small tier and only from 1,000; the thousands\n * suffix is lowercased and the larger ones keep their capitals, so \"M\" (million) cannot be\n * read as \"m\" (metres, or milli). The accessible name is always the full grouped value.\n *\n * @example\n * formatCount(1248, { tier: \"small\" }) // { text: \"1.2k\", label: \"1,248\" }\n * formatCount(1248, { tier: \"medium\" }) // { text: \"1,248\", label: \"1,248\" }\n * formatCount(100, { floor: true }) // { text: \"100+\", label: \"100 or more\" }\n */\nexport function formatCount(\n value: number,\n options: FormatCountOptions = {}\n): FormattedNumber {\n const locale = options.locale ?? \"en-US\";\n const full = grouped(locale).format(value);\n const shown =\n options.tier === \"small\" && Math.abs(value) >= COMPACT_FROM\n ? compact(locale)\n .format(value)\n // \"1.2K\" -> \"1.2k\"; \"3.4M\" and \"1.2B\" keep their capitals.\n .replace(/K$/, \"k\")\n : full;\n return options.floor\n ? { text: `${shown}+`, label: `${full} or more` }\n : { text: shown, label: full };\n}\n\n/** @experimental Which way a change goes, for styling; never the only signal (the arrow and sign are). */\nexport type ChangeDirection = \"up\" | \"down\" | \"none\";\n\n/** @experimental A formatted change, with its direction. */\nexport interface FormattedChange extends FormattedNumber {\n direction: ChangeDirection;\n}\n\n/**\n * @experimental Format a change against an earlier period.\n *\n * The direction is carried by an arrow AND a sign, so it reads without colour (WCAG 1.4.1):\n * \"↑ +18\", \"↓ −18\", \"→ ±0\". The accessible name says it in words: \"up 18\", \"down 18\",\n * \"no change\". The minus is U+2212, which screen readers and typography both treat as a minus\n * rather than a hyphen.\n */\nexport function formatChange(\n delta: number,\n options: Omit<FormatCountOptions, \"floor\"> = {}\n): FormattedChange {\n const magnitude = formatCount(Math.abs(delta), options);\n if (delta === 0) {\n return { text: \"→ ±0\", label: \"no change\", direction: \"none\" };\n }\n return delta > 0\n ? {\n text: `↑ +${magnitude.text}`,\n label: `up ${magnitude.label}`,\n direction: \"up\",\n }\n : {\n text: `↓ −${magnitude.text}`,\n label: `down ${magnitude.label}`,\n direction: \"down\",\n };\n}\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one number formatter for dashboard cards.
|
|
3
|
+
*
|
|
4
|
+
* Every figure a card shows -- a count, a chart value, a tooltip, a table cell, a text
|
|
5
|
+
* alternative -- goes through here, so the same number reads the same everywhere it appears.
|
|
6
|
+
* Two figures for one value that disagree ("1.2k" in a chart, "1,248" in its table, "1248" in
|
|
7
|
+
* its description) are what this exists to make impossible.
|
|
8
|
+
*
|
|
9
|
+
* It is pure arithmetic on numbers with `Intl.NumberFormat`: no React, no DOM, so a server can
|
|
10
|
+
* format the same way the admin does.
|
|
11
|
+
*
|
|
12
|
+
* @module lib/format/number
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* @experimental The size a card is drawn at, from its own width. Compact numbers appear only in the small
|
|
16
|
+
* tier, where a full grouped value would not fit; everywhere else the exact value fits and is
|
|
17
|
+
* shown.
|
|
18
|
+
*/
|
|
19
|
+
type SizeTier = "small" | "medium" | "large";
|
|
20
|
+
/** @experimental How a count is formatted: its locale, its card's tier, and whether it is a floor. */
|
|
21
|
+
interface FormatCountOptions {
|
|
22
|
+
/** The admin's locale. Defaults to `en-US`, the admin's own default. */
|
|
23
|
+
locale?: string;
|
|
24
|
+
/** The tier the figure is drawn in. Only `small` compacts. */
|
|
25
|
+
tier?: SizeTier;
|
|
26
|
+
/**
|
|
27
|
+
* The value is a lower bound: a source that stops counting at a limit knows only "at
|
|
28
|
+
* least this many". Shown as "N+" and named "N or more".
|
|
29
|
+
*/
|
|
30
|
+
floor?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** @experimental What a figure looks like, and what assistive technology hears for it. */
|
|
33
|
+
interface FormattedNumber {
|
|
34
|
+
/** Drawn on screen: "1,248", "1.2k", "100+". */
|
|
35
|
+
text: string;
|
|
36
|
+
/**
|
|
37
|
+
* The accessible name: always the full grouped value, so "1.2k" is heard as "1,248"; a
|
|
38
|
+
* floor is heard as "100 or more", never as a bare "100".
|
|
39
|
+
*/
|
|
40
|
+
label: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @experimental Format a count, total or chart value.
|
|
44
|
+
*
|
|
45
|
+
* Compact form ("1.2k", "3.4M") only in the small tier and only from 1,000; the thousands
|
|
46
|
+
* suffix is lowercased and the larger ones keep their capitals, so "M" (million) cannot be
|
|
47
|
+
* read as "m" (metres, or milli). The accessible name is always the full grouped value.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* formatCount(1248, { tier: "small" }) // { text: "1.2k", label: "1,248" }
|
|
51
|
+
* formatCount(1248, { tier: "medium" }) // { text: "1,248", label: "1,248" }
|
|
52
|
+
* formatCount(100, { floor: true }) // { text: "100+", label: "100 or more" }
|
|
53
|
+
*/
|
|
54
|
+
declare function formatCount(value: number, options?: FormatCountOptions): FormattedNumber;
|
|
55
|
+
/** @experimental Which way a change goes, for styling; never the only signal (the arrow and sign are). */
|
|
56
|
+
type ChangeDirection = "up" | "down" | "none";
|
|
57
|
+
/** @experimental A formatted change, with its direction. */
|
|
58
|
+
interface FormattedChange extends FormattedNumber {
|
|
59
|
+
direction: ChangeDirection;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @experimental Format a change against an earlier period.
|
|
63
|
+
*
|
|
64
|
+
* The direction is carried by an arrow AND a sign, so it reads without colour (WCAG 1.4.1):
|
|
65
|
+
* "↑ +18", "↓ −18", "→ ±0". The accessible name says it in words: "up 18", "down 18",
|
|
66
|
+
* "no change". The minus is U+2212, which screen readers and typography both treat as a minus
|
|
67
|
+
* rather than a hyphen.
|
|
68
|
+
*/
|
|
69
|
+
declare function formatChange(delta: number, options?: Omit<FormatCountOptions, "floor">): FormattedChange;
|
|
70
|
+
|
|
71
|
+
export { type ChangeDirection, type FormatCountOptions, type FormattedChange, type FormattedNumber, type SizeTier, formatChange, formatCount };
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one number formatter for dashboard cards.
|
|
3
|
+
*
|
|
4
|
+
* Every figure a card shows -- a count, a chart value, a tooltip, a table cell, a text
|
|
5
|
+
* alternative -- goes through here, so the same number reads the same everywhere it appears.
|
|
6
|
+
* Two figures for one value that disagree ("1.2k" in a chart, "1,248" in its table, "1248" in
|
|
7
|
+
* its description) are what this exists to make impossible.
|
|
8
|
+
*
|
|
9
|
+
* It is pure arithmetic on numbers with `Intl.NumberFormat`: no React, no DOM, so a server can
|
|
10
|
+
* format the same way the admin does.
|
|
11
|
+
*
|
|
12
|
+
* @module lib/format/number
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* @experimental The size a card is drawn at, from its own width. Compact numbers appear only in the small
|
|
16
|
+
* tier, where a full grouped value would not fit; everywhere else the exact value fits and is
|
|
17
|
+
* shown.
|
|
18
|
+
*/
|
|
19
|
+
type SizeTier = "small" | "medium" | "large";
|
|
20
|
+
/** @experimental How a count is formatted: its locale, its card's tier, and whether it is a floor. */
|
|
21
|
+
interface FormatCountOptions {
|
|
22
|
+
/** The admin's locale. Defaults to `en-US`, the admin's own default. */
|
|
23
|
+
locale?: string;
|
|
24
|
+
/** The tier the figure is drawn in. Only `small` compacts. */
|
|
25
|
+
tier?: SizeTier;
|
|
26
|
+
/**
|
|
27
|
+
* The value is a lower bound: a source that stops counting at a limit knows only "at
|
|
28
|
+
* least this many". Shown as "N+" and named "N or more".
|
|
29
|
+
*/
|
|
30
|
+
floor?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** @experimental What a figure looks like, and what assistive technology hears for it. */
|
|
33
|
+
interface FormattedNumber {
|
|
34
|
+
/** Drawn on screen: "1,248", "1.2k", "100+". */
|
|
35
|
+
text: string;
|
|
36
|
+
/**
|
|
37
|
+
* The accessible name: always the full grouped value, so "1.2k" is heard as "1,248"; a
|
|
38
|
+
* floor is heard as "100 or more", never as a bare "100".
|
|
39
|
+
*/
|
|
40
|
+
label: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @experimental Format a count, total or chart value.
|
|
44
|
+
*
|
|
45
|
+
* Compact form ("1.2k", "3.4M") only in the small tier and only from 1,000; the thousands
|
|
46
|
+
* suffix is lowercased and the larger ones keep their capitals, so "M" (million) cannot be
|
|
47
|
+
* read as "m" (metres, or milli). The accessible name is always the full grouped value.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* formatCount(1248, { tier: "small" }) // { text: "1.2k", label: "1,248" }
|
|
51
|
+
* formatCount(1248, { tier: "medium" }) // { text: "1,248", label: "1,248" }
|
|
52
|
+
* formatCount(100, { floor: true }) // { text: "100+", label: "100 or more" }
|
|
53
|
+
*/
|
|
54
|
+
declare function formatCount(value: number, options?: FormatCountOptions): FormattedNumber;
|
|
55
|
+
/** @experimental Which way a change goes, for styling; never the only signal (the arrow and sign are). */
|
|
56
|
+
type ChangeDirection = "up" | "down" | "none";
|
|
57
|
+
/** @experimental A formatted change, with its direction. */
|
|
58
|
+
interface FormattedChange extends FormattedNumber {
|
|
59
|
+
direction: ChangeDirection;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @experimental Format a change against an earlier period.
|
|
63
|
+
*
|
|
64
|
+
* The direction is carried by an arrow AND a sign, so it reads without colour (WCAG 1.4.1):
|
|
65
|
+
* "↑ +18", "↓ −18", "→ ±0". The accessible name says it in words: "up 18", "down 18",
|
|
66
|
+
* "no change". The minus is U+2212, which screen readers and typography both treat as a minus
|
|
67
|
+
* rather than a hyphen.
|
|
68
|
+
*/
|
|
69
|
+
declare function formatChange(delta: number, options?: Omit<FormatCountOptions, "floor">): FormattedChange;
|
|
70
|
+
|
|
71
|
+
export { type ChangeDirection, type FormatCountOptions, type FormattedChange, type FormattedNumber, type SizeTier, formatChange, formatCount };
|
package/dist/format.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/lib/format/number.ts
|
|
2
|
+
var COMPACT_FROM = 1e3;
|
|
3
|
+
var groupedCache = /* @__PURE__ */ new Map();
|
|
4
|
+
var compactCache = /* @__PURE__ */ new Map();
|
|
5
|
+
function grouped(locale) {
|
|
6
|
+
let f = groupedCache.get(locale);
|
|
7
|
+
if (!f) {
|
|
8
|
+
f = new Intl.NumberFormat(locale);
|
|
9
|
+
groupedCache.set(locale, f);
|
|
10
|
+
}
|
|
11
|
+
return f;
|
|
12
|
+
}
|
|
13
|
+
function compact(locale) {
|
|
14
|
+
let f = compactCache.get(locale);
|
|
15
|
+
if (!f) {
|
|
16
|
+
f = new Intl.NumberFormat(locale, {
|
|
17
|
+
notation: "compact",
|
|
18
|
+
maximumFractionDigits: 1
|
|
19
|
+
});
|
|
20
|
+
compactCache.set(locale, f);
|
|
21
|
+
}
|
|
22
|
+
return f;
|
|
23
|
+
}
|
|
24
|
+
function formatCount(value, options = {}) {
|
|
25
|
+
const locale = options.locale ?? "en-US";
|
|
26
|
+
const full = grouped(locale).format(value);
|
|
27
|
+
const shown = options.tier === "small" && Math.abs(value) >= COMPACT_FROM ? compact(locale).format(value).replace(/K$/, "k") : full;
|
|
28
|
+
return options.floor ? { text: `${shown}+`, label: `${full} or more` } : { text: shown, label: full };
|
|
29
|
+
}
|
|
30
|
+
function formatChange(delta, options = {}) {
|
|
31
|
+
const magnitude = formatCount(Math.abs(delta), options);
|
|
32
|
+
if (delta === 0) {
|
|
33
|
+
return { text: "\u2192 \xB10", label: "no change", direction: "none" };
|
|
34
|
+
}
|
|
35
|
+
return delta > 0 ? {
|
|
36
|
+
text: `\u2191 +${magnitude.text}`,
|
|
37
|
+
label: `up ${magnitude.label}`,
|
|
38
|
+
direction: "up"
|
|
39
|
+
} : {
|
|
40
|
+
text: `\u2193 \u2212${magnitude.text}`,
|
|
41
|
+
label: `down ${magnitude.label}`,
|
|
42
|
+
direction: "down"
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { formatChange, formatCount };
|
|
47
|
+
//# sourceMappingURL=format.mjs.map
|
|
48
|
+
//# sourceMappingURL=format.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/format/number.ts"],"names":[],"mappings":";AA6CA,IAAM,YAAA,GAAe,GAAA;AAIrB,IAAM,YAAA,uBAAmB,GAAA,EAA+B;AACxD,IAAM,YAAA,uBAAmB,GAAA,EAA+B;AAExD,SAAS,QAAQ,MAAA,EAAmC;AAClD,EAAA,IAAI,CAAA,GAAI,YAAA,CAAa,GAAA,CAAI,MAAM,CAAA;AAC/B,EAAA,IAAI,CAAC,CAAA,EAAG;AACN,IAAA,CAAA,GAAI,IAAI,IAAA,CAAK,YAAA,CAAa,MAAM,CAAA;AAChC,IAAA,YAAA,CAAa,GAAA,CAAI,QAAQ,CAAC,CAAA;AAAA,EAC5B;AACA,EAAA,OAAO,CAAA;AACT;AAEA,SAAS,QAAQ,MAAA,EAAmC;AAClD,EAAA,IAAI,CAAA,GAAI,YAAA,CAAa,GAAA,CAAI,MAAM,CAAA;AAC/B,EAAA,IAAI,CAAC,CAAA,EAAG;AACN,IAAA,CAAA,GAAI,IAAI,IAAA,CAAK,YAAA,CAAa,MAAA,EAAQ;AAAA,MAChC,QAAA,EAAU,SAAA;AAAA,MACV,qBAAA,EAAuB;AAAA,KACxB,CAAA;AACD,IAAA,YAAA,CAAa,GAAA,CAAI,QAAQ,CAAC,CAAA;AAAA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CAAA,KAAA,EAAQ,SAAA,CAAU,KAAK,CAAA,CAAA;AAAA,IAC9B,SAAA,EAAW;AAAA,GACb;AACN","file":"format.mjs","sourcesContent":["/**\n * The one number formatter for dashboard cards.\n *\n * Every figure a card shows -- a count, a chart value, a tooltip, a table cell, a text\n * alternative -- goes through here, so the same number reads the same everywhere it appears.\n * Two figures for one value that disagree (\"1.2k\" in a chart, \"1,248\" in its table, \"1248\" in\n * its description) are what this exists to make impossible.\n *\n * It is pure arithmetic on numbers with `Intl.NumberFormat`: no React, no DOM, so a server can\n * format the same way the admin does.\n *\n * @module lib/format/number\n */\n\n/**\n * @experimental The size a card is drawn at, from its own width. Compact numbers appear only in the small\n * tier, where a full grouped value would not fit; everywhere else the exact value fits and is\n * shown.\n */\nexport type SizeTier = \"small\" | \"medium\" | \"large\";\n\n/** @experimental How a count is formatted: its locale, its card's tier, and whether it is a floor. */\nexport interface FormatCountOptions {\n /** The admin's locale. Defaults to `en-US`, the admin's own default. */\n locale?: string;\n /** The tier the figure is drawn in. Only `small` compacts. */\n tier?: SizeTier;\n /**\n * The value is a lower bound: a source that stops counting at a limit knows only \"at\n * least this many\". Shown as \"N+\" and named \"N or more\".\n */\n floor?: boolean;\n}\n\n/** @experimental What a figure looks like, and what assistive technology hears for it. */\nexport interface FormattedNumber {\n /** Drawn on screen: \"1,248\", \"1.2k\", \"100+\". */\n text: string;\n /**\n * The accessible name: always the full grouped value, so \"1.2k\" is heard as \"1,248\"; a\n * floor is heard as \"100 or more\", never as a bare \"100\".\n */\n label: string;\n}\n\nconst COMPACT_FROM = 1000;\n\n// Intl formatters are costly to build and a dashboard formats hundreds of figures, so one of\n// each kind is kept per locale.\nconst groupedCache = new Map<string, Intl.NumberFormat>();\nconst compactCache = new Map<string, Intl.NumberFormat>();\n\nfunction grouped(locale: string): Intl.NumberFormat {\n let f = groupedCache.get(locale);\n if (!f) {\n f = new Intl.NumberFormat(locale);\n groupedCache.set(locale, f);\n }\n return f;\n}\n\nfunction compact(locale: string): Intl.NumberFormat {\n let f = compactCache.get(locale);\n if (!f) {\n f = new Intl.NumberFormat(locale, {\n notation: \"compact\",\n maximumFractionDigits: 1,\n });\n compactCache.set(locale, f);\n }\n return f;\n}\n\n/**\n * @experimental Format a count, total or chart value.\n *\n * Compact form (\"1.2k\", \"3.4M\") only in the small tier and only from 1,000; the thousands\n * suffix is lowercased and the larger ones keep their capitals, so \"M\" (million) cannot be\n * read as \"m\" (metres, or milli). The accessible name is always the full grouped value.\n *\n * @example\n * formatCount(1248, { tier: \"small\" }) // { text: \"1.2k\", label: \"1,248\" }\n * formatCount(1248, { tier: \"medium\" }) // { text: \"1,248\", label: \"1,248\" }\n * formatCount(100, { floor: true }) // { text: \"100+\", label: \"100 or more\" }\n */\nexport function formatCount(\n value: number,\n options: FormatCountOptions = {}\n): FormattedNumber {\n const locale = options.locale ?? \"en-US\";\n const full = grouped(locale).format(value);\n const shown =\n options.tier === \"small\" && Math.abs(value) >= COMPACT_FROM\n ? compact(locale)\n .format(value)\n // \"1.2K\" -> \"1.2k\"; \"3.4M\" and \"1.2B\" keep their capitals.\n .replace(/K$/, \"k\")\n : full;\n return options.floor\n ? { text: `${shown}+`, label: `${full} or more` }\n : { text: shown, label: full };\n}\n\n/** @experimental Which way a change goes, for styling; never the only signal (the arrow and sign are). */\nexport type ChangeDirection = \"up\" | \"down\" | \"none\";\n\n/** @experimental A formatted change, with its direction. */\nexport interface FormattedChange extends FormattedNumber {\n direction: ChangeDirection;\n}\n\n/**\n * @experimental Format a change against an earlier period.\n *\n * The direction is carried by an arrow AND a sign, so it reads without colour (WCAG 1.4.1):\n * \"↑ +18\", \"↓ −18\", \"→ ±0\". The accessible name says it in words: \"up 18\", \"down 18\",\n * \"no change\". The minus is U+2212, which screen readers and typography both treat as a minus\n * rather than a hyphen.\n */\nexport function formatChange(\n delta: number,\n options: Omit<FormatCountOptions, \"floor\"> = {}\n): FormattedChange {\n const magnitude = formatCount(Math.abs(delta), options);\n if (delta === 0) {\n return { text: \"→ ±0\", label: \"no change\", direction: \"none\" };\n }\n return delta > 0\n ? {\n text: `↑ +${magnitude.text}`,\n label: `up ${magnitude.label}`,\n direction: \"up\",\n }\n : {\n text: `↓ −${magnitude.text}`,\n label: `down ${magnitude.label}`,\n direction: \"down\",\n };\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1746,7 +1746,7 @@ function SelectContent({
|
|
|
1746
1746
|
{
|
|
1747
1747
|
"data-slot": "select-content",
|
|
1748
1748
|
className: cn(
|
|
1749
|
-
"bg-popover text-popover-foreground border border-border shadow-none rounded-lg z-9999 max-h-(--radix-select-content-available-height) min-w-32 overflow-hidden data-[state=open]:animate-zoom-in-95 data-[state=closed]:animate-zoom-out-95 data-[side=bottom]:animate-slide-in-from-top-2 data-[side=left]:animate-slide-in-from-right-2 data-[side=right]:animate-slide-in-from-left-2 data-[side=top]:animate-slide-in-from-bottom-2",
|
|
1749
|
+
"bg-popover text-popover-foreground border border-border shadow-none rounded-lg z-9999 max-h-(--radix-select-content-available-height,20rem) min-w-32 overflow-hidden data-[state=open]:animate-zoom-in-95 data-[state=closed]:animate-zoom-out-95 data-[side=bottom]:animate-slide-in-from-top-2 data-[side=left]:animate-slide-in-from-right-2 data-[side=right]:animate-slide-in-from-left-2 data-[side=top]:animate-slide-in-from-bottom-2",
|
|
1750
1750
|
position === "popper" && "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
|
|
1751
1751
|
className
|
|
1752
1752
|
),
|