basalt-ui 2.13.2 → 2.14.0
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/CHANGELOG.md +34 -0
- package/MIGRATING.md +40 -12
- package/dist/agent/chat/thread-feed-row.d.ts.map +1 -1
- package/dist/agent/chat/thread-feed-row.js +2 -2
- package/dist/agent/chat/thread-message.d.ts.map +1 -1
- package/dist/agent/chat/thread-message.js +2 -2
- package/dist/agent/chat/thread-outcome-card.js +2 -2
- package/dist/charts/DualPanel.d.ts.map +1 -1
- package/dist/charts/DualPanel.js +1 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +3 -2
- package/dist/charts/cursor.d.ts +10 -6
- package/dist/charts/cursor.d.ts.map +1 -1
- package/dist/charts/cursor.js +20 -13
- package/dist/charts/legend.d.ts +5 -4
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +3 -3
- package/dist/charts/panel.d.ts +2 -0
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +1 -0
- package/dist/commands/notifications/center.d.ts.map +1 -1
- package/dist/commands/notifications/center.js +2 -11
- package/dist/controls/control-name.d.ts +27 -0
- package/dist/controls/control-name.d.ts.map +1 -0
- package/dist/controls/control-name.js +11 -0
- package/dist/controls/enum-filter.d.ts +3 -6
- package/dist/controls/enum-filter.d.ts.map +1 -1
- package/dist/controls/enum-filter.js +8 -4
- package/dist/controls/filter-base.d.ts +30 -0
- package/dist/controls/filter-base.d.ts.map +1 -0
- package/dist/controls/filter-base.js +0 -0
- package/dist/controls/filter-pill.d.ts +3 -1
- package/dist/controls/filter-pill.d.ts.map +1 -1
- package/dist/controls/filter-pill.js +2 -0
- package/dist/controls/filter-sheet.d.ts +0 -32
- package/dist/controls/filter-sheet.d.ts.map +1 -1
- package/dist/controls/filter-sheet.js +1 -28
- package/dist/controls/glyphs.d.ts +12 -0
- package/dist/controls/glyphs.d.ts.map +1 -1
- package/dist/controls/glyphs.js +23 -2
- package/dist/controls/index.d.ts +2 -1
- package/dist/controls/index.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.d.ts +8 -3
- package/dist/controls/multi-select-filter.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.js +27 -10
- package/dist/controls/number-filter.d.ts +3 -12
- package/dist/controls/number-filter.d.ts.map +1 -1
- package/dist/controls/number-filter.js +17 -6
- package/dist/controls/panel-row.d.ts +5 -8
- package/dist/controls/panel-row.d.ts.map +1 -1
- package/dist/controls/panel-row.js +3 -0
- package/dist/controls/range-filter.d.ts +14 -14
- package/dist/controls/range-filter.d.ts.map +1 -1
- package/dist/controls/range-filter.js +18 -41
- package/dist/controls/search-filter.d.ts +5 -2
- package/dist/controls/search-filter.d.ts.map +1 -1
- package/dist/controls/search-filter.js +74 -40
- package/dist/controls/select-filter.d.ts +2 -15
- package/dist/controls/select-filter.d.ts.map +1 -1
- package/dist/controls/sync-button.d.ts +0 -6
- package/dist/controls/sync-button.d.ts.map +1 -1
- package/dist/controls/sync-button.js +6 -36
- package/dist/controls/toggle-filter.d.ts +2 -2
- package/dist/controls/toggle-filter.d.ts.map +1 -1
- package/dist/controls/toggle-filter.js +3 -2
- package/dist/controls/view-tabs.d.ts +3 -4
- package/dist/controls/view-tabs.d.ts.map +1 -1
- package/dist/controls/view-tabs.js +24 -5
- package/dist/format/date.d.ts +43 -10
- package/dist/format/date.d.ts.map +1 -1
- package/dist/format/date.js +109 -18
- package/dist/format/index.d.ts +12 -11
- package/dist/format/index.d.ts.map +1 -1
- package/dist/format/index.js +11 -2
- package/dist/format/measure.d.ts +9 -9
- package/dist/format/measure.d.ts.map +1 -1
- package/dist/format/measure.js +19 -8
- package/dist/format/number.d.ts +35 -27
- package/dist/format/number.d.ts.map +1 -1
- package/dist/format/number.js +41 -20
- package/dist/format/shared.d.ts +38 -7
- package/dist/format/shared.d.ts.map +1 -1
- package/dist/format/shared.js +61 -13
- package/dist/provider/create-router.d.ts +9 -4
- package/dist/provider/create-router.d.ts.map +1 -1
- package/dist/provider/create-router.js +1 -19
- package/dist/provider/error-fallback.module.css +4 -15
- package/dist/provider/error-fallback.module.css.d.ts +0 -1
- package/dist/provider/fallback.module.css +37 -0
- package/dist/provider/fallback.module.css.d.ts +11 -0
- package/dist/provider/index.d.ts +13 -1
- package/dist/provider/index.d.ts.map +1 -1
- package/dist/provider/index.js +10 -1
- package/dist/provider/route-defaults.d.ts +10 -0
- package/dist/provider/route-defaults.d.ts.map +1 -0
- package/dist/provider/route-defaults.js +25 -0
- package/dist/provider/route-defaults.module.css +39 -0
- package/dist/provider/route-defaults.module.css.d.ts +10 -0
- package/llms.txt +35 -14
- package/package.json +1 -1
- package/src/agent/chat/thread-feed-row.tsx +2 -2
- package/src/agent/chat/thread-message.tsx +2 -2
- package/src/agent/chat/thread-outcome-card.tsx +2 -2
- package/src/charts/DualPanel.tsx +1 -0
- package/src/charts/compile.ts +3 -2
- package/src/charts/cursor.ts +44 -27
- package/src/charts/legend.tsx +9 -6
- package/src/charts/panel.tsx +3 -0
- package/src/commands/notifications/center.tsx +2 -14
- package/src/controls/control-name.ts +33 -0
- package/src/controls/enum-filter.tsx +29 -27
- package/src/controls/filter-base.ts +31 -0
- package/src/controls/filter-pill.tsx +4 -0
- package/src/controls/filter-sheet.tsx +0 -56
- package/src/controls/glyphs.tsx +45 -2
- package/src/controls/index.ts +2 -1
- package/src/controls/multi-select-filter.tsx +67 -43
- package/src/controls/number-filter.tsx +47 -47
- package/src/controls/panel-row.tsx +7 -8
- package/src/controls/range-filter.tsx +54 -78
- package/src/controls/search-filter.tsx +107 -54
- package/src/controls/select-filter.tsx +2 -18
- package/src/controls/sync-button.tsx +7 -40
- package/src/controls/toggle-filter.tsx +9 -7
- package/src/controls/view-tabs.tsx +41 -21
- package/src/format/date.ts +189 -36
- package/src/format/index.ts +12 -11
- package/src/format/measure.ts +29 -18
- package/src/format/number.ts +76 -37
- package/src/format/shared.ts +116 -25
- package/src/provider/create-router.tsx +2 -36
- package/src/provider/error-fallback.module.css +4 -15
- package/src/provider/error-fallback.module.css.d.ts +0 -1
- package/src/provider/fallback.module.css +37 -0
- package/src/provider/fallback.module.css.d.ts +11 -0
- package/src/provider/index.tsx +25 -1
- package/src/provider/route-defaults.module.css +39 -0
- package/src/provider/route-defaults.module.css.d.ts +10 -0
- package/src/provider/route-defaults.tsx +39 -0
- package/dist/agent/chat/relative-time.d.ts +0 -27
- package/dist/agent/chat/relative-time.d.ts.map +0 -1
- package/dist/agent/chat/relative-time.js +0 -27
- package/src/agent/chat/relative-time.ts +0 -55
|
@@ -34,33 +34,44 @@ import { assertRequiredProps } from '../common/validate'
|
|
|
34
34
|
import type { EnumField, FieldHandle } from '../state'
|
|
35
35
|
import classes from './controls.module.css'
|
|
36
36
|
import { useFilterSurface } from './filter-context'
|
|
37
|
-
import { useControlName } from './
|
|
37
|
+
import { useControlName } from './control-name'
|
|
38
|
+
import type { FilterBase, FilterOption } from './filter-base'
|
|
38
39
|
import { PanelChoice, PanelRow, useTrackFits } from './panel-row'
|
|
39
40
|
import type { UncontrolledProps } from './uncontrolled'
|
|
40
41
|
|
|
42
|
+
/** The `data` both Mantine controls take — `disabled` always a boolean, never absent. */
|
|
43
|
+
function toSegmentData(
|
|
44
|
+
options: readonly FilterOption[],
|
|
45
|
+
): { value: string; label: string; disabled: boolean }[] {
|
|
46
|
+
return options.map((option) => ({
|
|
47
|
+
value: option.value,
|
|
48
|
+
label: option.label,
|
|
49
|
+
disabled: option.disabled === true,
|
|
50
|
+
}))
|
|
51
|
+
}
|
|
52
|
+
|
|
41
53
|
/** Past three options the phone form is a `Select`, not a track. */
|
|
42
54
|
const PHONE_TRACK_MAX = 3
|
|
43
55
|
|
|
44
|
-
export type ViewTabsOption<T extends string> = {
|
|
45
|
-
readonly value: T
|
|
46
|
-
readonly label: string
|
|
56
|
+
export type ViewTabsOption<T extends string> = FilterOption<T> & {
|
|
47
57
|
/** Render this option on ONE viewport only. Omitted → both. */
|
|
48
58
|
readonly only?: 'sm-up' | 'sm-down'
|
|
49
59
|
}
|
|
50
60
|
|
|
51
|
-
export type ViewTabsProps<T extends string> = UncontrolledProps &
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
export type ViewTabsProps<T extends string> = UncontrolledProps &
|
|
62
|
+
Pick<FilterBase, 'disabled'> & {
|
|
63
|
+
readonly field: FieldHandle<EnumField<T>>
|
|
64
|
+
/** Defaults to `field.options` (every declared value, labelled by `store.labels()`). */
|
|
65
|
+
readonly options?: readonly ViewTabsOption<T>[]
|
|
66
|
+
/**
|
|
67
|
+
* The switch's accessible name. Both forms render without a visible Mantine `label` (a home has
|
|
68
|
+
* no room for one), so without this the desktop track is an unnamed `radiogroup` and the phone
|
|
69
|
+
* form an unnamed `combobox`.
|
|
70
|
+
*
|
|
71
|
+
* @default 'View'
|
|
72
|
+
*/
|
|
73
|
+
readonly label?: string
|
|
74
|
+
}
|
|
64
75
|
|
|
65
76
|
export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
66
77
|
// F-ERR-1 — without this a missing `field` surfaces as `undefined is not an object
|
|
@@ -68,7 +79,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
|
68
79
|
assertRequiredProps('ViewTabs', props, ['field'], {
|
|
69
80
|
field: 'bind it to a store field (`store.field.<name>`), never a value/onChange pair.',
|
|
70
81
|
})
|
|
71
|
-
const { field, options, label = 'View', className, style } = props
|
|
82
|
+
const { field, options, label = 'View', disabled, className, style } = props
|
|
72
83
|
const [value, setValue] = field.use()
|
|
73
84
|
const surface = useFilterSurface()
|
|
74
85
|
const inPanel = surface === 'panel'
|
|
@@ -84,7 +95,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
|
84
95
|
setValue(next as T)
|
|
85
96
|
}
|
|
86
97
|
|
|
87
|
-
const phoneData = phone
|
|
98
|
+
const phoneData = toSegmentData(phone)
|
|
88
99
|
const phoneCountFits = phone.length <= PHONE_TRACK_MAX
|
|
89
100
|
// Called UNCONDITIONALLY, even on the panel branch that never renders the phone form — a hook
|
|
90
101
|
// may not sit behind the `if (inPanel) return …` below (`react-hooks/rules-of-hooks`), and
|
|
@@ -106,7 +117,13 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
|
106
117
|
{...(className !== undefined && { className })}
|
|
107
118
|
{...(style !== undefined && { style })}
|
|
108
119
|
>
|
|
109
|
-
<PanelChoice
|
|
120
|
+
<PanelChoice
|
|
121
|
+
nameProps={nameProps}
|
|
122
|
+
value={value}
|
|
123
|
+
options={all}
|
|
124
|
+
onChange={commit}
|
|
125
|
+
{...(disabled === true && { disabled: true })}
|
|
126
|
+
/>
|
|
110
127
|
</PanelRow>
|
|
111
128
|
)
|
|
112
129
|
}
|
|
@@ -120,8 +137,9 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
|
120
137
|
aria-label={label}
|
|
121
138
|
visibleFrom="sm"
|
|
122
139
|
size="ctl"
|
|
140
|
+
{...(disabled === true && { disabled: true })}
|
|
123
141
|
value={value}
|
|
124
|
-
data={desktop
|
|
142
|
+
data={toSegmentData(desktop)}
|
|
125
143
|
onChange={commit}
|
|
126
144
|
{...(className !== undefined && { className })}
|
|
127
145
|
{...(style !== undefined && { style })}
|
|
@@ -133,6 +151,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
|
133
151
|
hiddenFrom="sm"
|
|
134
152
|
size="ctl"
|
|
135
153
|
fullWidth
|
|
154
|
+
{...(disabled === true && { disabled: true })}
|
|
136
155
|
value={value}
|
|
137
156
|
data={phoneData}
|
|
138
157
|
onChange={commit}
|
|
@@ -151,6 +170,7 @@ export function ViewTabs<T extends string>(props: ViewTabsProps<T>): ReactNode {
|
|
|
151
170
|
{...(style !== undefined && { style })}
|
|
152
171
|
value={value}
|
|
153
172
|
allowDeselect={false}
|
|
173
|
+
{...(disabled === true && { disabled: true })}
|
|
154
174
|
data={phoneData}
|
|
155
175
|
onChange={(next) => {
|
|
156
176
|
if (next !== null) commit(next)
|
package/src/format/date.ts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Date/time/duration formatting (`
|
|
2
|
+
* Date/time/duration formatting (`date`/`time`/`dateTime`/`weekday`/`relativeTime`/`duration`/`durationClock`),
|
|
3
3
|
* seeded from argo consumers (`features/{walking-pad,reading,astro-window,garmin-health}/{formatters,format,
|
|
4
4
|
* formulas}.ts` — read those files' own JSDoc for the exact call sites this generalizes).
|
|
5
5
|
*/
|
|
6
|
-
import { dateFormat, NON_FINITE, toDate } from './shared'
|
|
6
|
+
import { dateFormat, NON_FINITE, numberFormat, toDate } from './shared'
|
|
7
7
|
import type { LocaleOption } from './shared'
|
|
8
8
|
|
|
9
|
+
type DateInput = Date | number | string | null | undefined
|
|
10
|
+
|
|
11
|
+
const CALENDAR_DATE = /^(\d{4})-(\d{2})-(\d{2})$/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A plain `YYYY-MM-DD` is a calendar date, not an instant. Returns `undefined` when `value` is not
|
|
15
|
+
* one, `null` when it is shaped like one but names no real day (`2026-02-31`), else midnight of
|
|
16
|
+
* that day — in UTC or in the local zone. `setFullYear` (not the `Date` constructor) so years
|
|
17
|
+
* 0–99 are not remapped to 19xx; the round-trip check rejects the overflow `Date` would roll over.
|
|
18
|
+
*/
|
|
19
|
+
function calendarDate(value: string, utc: boolean): Date | null | undefined {
|
|
20
|
+
const match = CALENDAR_DATE.exec(value)
|
|
21
|
+
if (match === null) return undefined
|
|
22
|
+
const year = Number(match[1])
|
|
23
|
+
const month = Number(match[2]) - 1
|
|
24
|
+
const day = Number(match[3])
|
|
25
|
+
const result = new Date(0)
|
|
26
|
+
if (utc) {
|
|
27
|
+
result.setUTCFullYear(year, month, day)
|
|
28
|
+
result.setUTCHours(0, 0, 0, 0)
|
|
29
|
+
const ok =
|
|
30
|
+
result.getUTCFullYear() === year &&
|
|
31
|
+
result.getUTCMonth() === month &&
|
|
32
|
+
result.getUTCDate() === day
|
|
33
|
+
return ok ? result : null
|
|
34
|
+
}
|
|
35
|
+
result.setFullYear(year, month, day)
|
|
36
|
+
result.setHours(0, 0, 0, 0)
|
|
37
|
+
const ok =
|
|
38
|
+
result.getFullYear() === year && result.getMonth() === month && result.getDate() === day
|
|
39
|
+
return ok ? result : null
|
|
40
|
+
}
|
|
41
|
+
|
|
9
42
|
export type WeekdayOptions = LocaleOption & {
|
|
10
43
|
/** `Intl.DateTimeFormat`'s own weekday widths. Default `'short'` (`"Mon"`). */
|
|
11
44
|
format?: 'short' | 'long' | 'narrow'
|
|
@@ -13,26 +46,30 @@ export type WeekdayOptions = LocaleOption & {
|
|
|
13
46
|
|
|
14
47
|
/** Weekday name — seeded from `astro-window/formulas.ts#fmtWeekday`. A plain `YYYY-MM-DD` string
|
|
15
48
|
* is parsed as a LOCAL calendar date (never UTC), matching that seed's own rationale: the weekday
|
|
16
|
-
* must not shift a day depending on the reader's timezone. A non-finite/unparseable input
|
|
17
|
-
* {@link NON_FINITE}. */
|
|
49
|
+
* must not shift a day depending on the reader's timezone. A non-finite/unparseable input (or a
|
|
50
|
+
* day that does not exist, `2026-02-31`) prints {@link NON_FINITE}. */
|
|
18
51
|
export function weekday(value: Date | number | string, options: WeekdayOptions = {}): string {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const [y, m, d] = value.split('-').map(Number)
|
|
23
|
-
return new Date(y ?? 1970, (m ?? 1) - 1, d ?? 1)
|
|
24
|
-
})()
|
|
25
|
-
: toDate(value)
|
|
26
|
-
if (date === null) return NON_FINITE
|
|
52
|
+
const parsed = typeof value === 'string' ? calendarDate(value, false) : undefined
|
|
53
|
+
const resolved = parsed === undefined ? toDate(value) : parsed
|
|
54
|
+
if (resolved === null) return NON_FINITE
|
|
27
55
|
const { locale, format = 'short' } = options
|
|
28
|
-
return dateFormat(locale, { weekday: format }).format(
|
|
56
|
+
return dateFormat(locale, { weekday: format }).format(resolved)
|
|
29
57
|
}
|
|
30
58
|
|
|
31
59
|
export type RelativeTimeOptions = LocaleOption & {
|
|
60
|
+
/** `Intl.RelativeTimeFormat` width. Default `'long'` (`"3 hours ago"`); `'short'` is `"3 hr. ago"`. */
|
|
61
|
+
style?: 'long' | 'short' | 'narrow'
|
|
62
|
+
/** `'auto'` (default) uses idioms (`"yesterday"`, `"now"`); `'always'` keeps the number. */
|
|
63
|
+
numeric?: 'auto' | 'always'
|
|
32
64
|
/** The reference "now" instant, epoch ms. Default `Date.now()` — mainly for tests. */
|
|
33
65
|
now?: number
|
|
34
66
|
}
|
|
35
67
|
|
|
68
|
+
const MINUTE_MS = 60_000
|
|
69
|
+
|
|
70
|
+
/** Largest first. The unit is picked by magnitude; the count is rounded on the absolute value
|
|
71
|
+
* (so +/- are symmetric) and the unit is promoted only once the rounded count fills the next
|
|
72
|
+
* larger unit — 45 minutes stays `45 minutes`, 59.6 minutes becomes `1 hour`. */
|
|
36
73
|
const RELATIVE_TIME_UNITS: readonly {
|
|
37
74
|
readonly unit: Intl.RelativeTimeFormatUnit
|
|
38
75
|
readonly ms: number
|
|
@@ -42,42 +79,108 @@ const RELATIVE_TIME_UNITS: readonly {
|
|
|
42
79
|
{ unit: 'week', ms: 604_800_000 },
|
|
43
80
|
{ unit: 'day', ms: 86_400_000 },
|
|
44
81
|
{ unit: 'hour', ms: 3_600_000 },
|
|
45
|
-
{ unit: 'minute', ms:
|
|
82
|
+
{ unit: 'minute', ms: MINUTE_MS },
|
|
46
83
|
]
|
|
47
84
|
|
|
85
|
+
/** `format(-0, …)` reads "0 seconds ago"; `format(0, …)` reads "in 0 seconds". */
|
|
86
|
+
const NEGATIVE_ZERO = -0
|
|
87
|
+
|
|
48
88
|
const relativeTimeFormatterCache = new Map<string, Intl.RelativeTimeFormat>()
|
|
49
89
|
|
|
50
|
-
function relativeTimeFormat(
|
|
51
|
-
|
|
90
|
+
function relativeTimeFormat(
|
|
91
|
+
locale: string | undefined,
|
|
92
|
+
style: 'long' | 'short' | 'narrow',
|
|
93
|
+
numeric: 'auto' | 'always',
|
|
94
|
+
): Intl.RelativeTimeFormat {
|
|
95
|
+
const key = `${locale ?? ''}|${style}|${numeric}`
|
|
52
96
|
const cached = relativeTimeFormatterCache.get(key)
|
|
53
97
|
if (cached !== undefined) return cached
|
|
54
|
-
const created = new Intl.RelativeTimeFormat(locale, { numeric
|
|
98
|
+
const created = new Intl.RelativeTimeFormat(locale, { style, numeric })
|
|
55
99
|
relativeTimeFormatterCache.set(key, created)
|
|
56
100
|
return created
|
|
57
101
|
}
|
|
58
102
|
|
|
59
103
|
/**
|
|
60
|
-
*
|
|
61
|
-
* `
|
|
62
|
-
*
|
|
63
|
-
* `
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* render-path code where one bad timestamp must not blank a whole list.
|
|
104
|
+
* THE relative-time formatter on `Intl.RelativeTimeFormat` — `"3 hours ago"`, `"in 2 days"`,
|
|
105
|
+
* `"now"`. Under a minute is `format(0, 'second')`, so "now" is the locale's own word. The count is
|
|
106
|
+
* rounded before the unit is chosen, so 59.98 minutes reads `"1 hour ago"`, never
|
|
107
|
+
* `"60 minutes ago"`. Accepts epoch ms, `Date` or an ISO string; `null`/`undefined`/unparseable
|
|
108
|
+
* prints {@link NON_FINITE} (`Intl` throws a `RangeError` on non-finite input, and this is
|
|
109
|
+
* render-path code where one bad timestamp must not blank a list).
|
|
67
110
|
*/
|
|
68
|
-
export function relativeTime(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
const
|
|
73
|
-
const timestamp =
|
|
74
|
-
value instanceof Date ? value.getTime() : typeof value === 'number' ? value : Date.parse(value)
|
|
75
|
-
if (!Number.isFinite(timestamp)) return NON_FINITE
|
|
111
|
+
export function relativeTime(value: DateInput, options: RelativeTimeOptions = {}): string {
|
|
112
|
+
const { locale, style = 'long', numeric = 'auto', now = Date.now() } = options
|
|
113
|
+
const timestamp = value === null || value === undefined ? undefined : toDate(value)?.getTime()
|
|
114
|
+
if (timestamp === undefined) return NON_FINITE
|
|
115
|
+
const formatter = relativeTimeFormat(locale, style, numeric)
|
|
76
116
|
const diffMs = timestamp - now
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
117
|
+
const magnitude = Math.abs(diffMs)
|
|
118
|
+
const sign = diffMs < 0 ? -1 : 1
|
|
119
|
+
const index = RELATIVE_TIME_UNITS.findIndex(({ ms }) => magnitude >= ms)
|
|
120
|
+
const picked = RELATIVE_TIME_UNITS[index]
|
|
121
|
+
if (picked !== undefined) {
|
|
122
|
+
const larger = RELATIVE_TIME_UNITS[index - 1]
|
|
123
|
+
const unit =
|
|
124
|
+
larger !== undefined && Math.round(magnitude / picked.ms) * picked.ms >= larger.ms
|
|
125
|
+
? larger
|
|
126
|
+
: picked
|
|
127
|
+
return formatter.format(sign * Math.round(magnitude / unit.ms), unit.unit)
|
|
128
|
+
}
|
|
129
|
+
return formatter.format(diffMs < 0 ? NEGATIVE_ZERO : 0, 'second')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type DateOptions = LocaleOption & {
|
|
133
|
+
/** `Intl.DateTimeFormat` `dateStyle`/`timeStyle` width. Default `'medium'`. */
|
|
134
|
+
format?: 'short' | 'medium' | 'long'
|
|
135
|
+
/** IANA zone. Default `undefined` — passed straight through to `Intl`. Ignored for a plain
|
|
136
|
+
* `YYYY-MM-DD` string, which is a calendar date and prints the same day in every zone. */
|
|
137
|
+
timeZone?: string
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Resolves `value` to a `Date` plus the zone to format it in. A plain `YYYY-MM-DD` is pinned to
|
|
141
|
+
* UTC midnight and formatted in UTC; anything unrepresentable is `null`. */
|
|
142
|
+
function resolveDateInput(
|
|
143
|
+
value: DateInput,
|
|
144
|
+
timeZone: string | undefined,
|
|
145
|
+
): { date: Date; timeZone: string | undefined } | null {
|
|
146
|
+
if (value === null || value === undefined) return null
|
|
147
|
+
const calendar = typeof value === 'string' ? calendarDate(value, true) : undefined
|
|
148
|
+
if (calendar !== undefined) return calendar === null ? null : { date: calendar, timeZone: 'UTC' }
|
|
149
|
+
const parsed = toDate(value)
|
|
150
|
+
return parsed === null ? null : { date: parsed, timeZone }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Calendar date — `"Sep 2, 2026"`. `null`/`undefined`/unparseable prints {@link NON_FINITE}. */
|
|
154
|
+
export function date(value: DateInput, options: DateOptions = {}): string {
|
|
155
|
+
const resolved = resolveDateInput(value, options.timeZone)
|
|
156
|
+
if (resolved === null) return NON_FINITE
|
|
157
|
+
const { locale, format = 'medium' } = options
|
|
158
|
+
return dateFormat(locale, { dateStyle: format, timeZone: resolved.timeZone }).format(
|
|
159
|
+
resolved.date,
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Time of day — `"3:04:05 PM"` (`format: 'short'`: `"3:04 PM"`). A plain `YYYY-MM-DD` string reads as midnight UTC. */
|
|
164
|
+
export function time(value: DateInput, options: DateOptions = {}): string {
|
|
165
|
+
const resolved = resolveDateInput(value, options.timeZone)
|
|
166
|
+
if (resolved === null) return NON_FINITE
|
|
167
|
+
const { locale, format = 'medium' } = options
|
|
168
|
+
return dateFormat(locale, { timeStyle: format, timeZone: resolved.timeZone }).format(
|
|
169
|
+
resolved.date,
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Date and time — `"Sep 2, 2026, 3:04:05 PM"` (`format: 'short'`: `"9/2/26, 3:04 PM"`; the date/time
|
|
174
|
+
* separator is the runtime's). */
|
|
175
|
+
export function dateTime(value: DateInput, options: DateOptions = {}): string {
|
|
176
|
+
const resolved = resolveDateInput(value, options.timeZone)
|
|
177
|
+
if (resolved === null) return NON_FINITE
|
|
178
|
+
const { locale, format = 'medium' } = options
|
|
179
|
+
return dateFormat(locale, {
|
|
180
|
+
dateStyle: format,
|
|
181
|
+
timeStyle: format,
|
|
182
|
+
timeZone: resolved.timeZone,
|
|
183
|
+
}).format(resolved.date)
|
|
81
184
|
}
|
|
82
185
|
|
|
83
186
|
/** Clock-style duration — `"1:02:03"`, `"04:12"`. Seeded from `walking-pad/
|
|
@@ -92,3 +195,53 @@ export function durationClock(seconds: number): string {
|
|
|
92
195
|
const ss = String(s).padStart(2, '0')
|
|
93
196
|
return h > 0 ? `${h}:${mm}:${ss}` : `${mm}:${ss}`
|
|
94
197
|
}
|
|
198
|
+
|
|
199
|
+
export type DurationOptions = LocaleOption & {
|
|
200
|
+
/** `'clock'` (default) `"1:02:03"`; `'compact'` `"1h 02m 03s"`; `'long'` `"1 hour 2 minutes 3 seconds"`. */
|
|
201
|
+
style?: 'clock' | 'compact' | 'long'
|
|
202
|
+
/** Largest unit `'compact'`/`'long'` break down to. Default `'hour'` (90 000 s is `"25h"`). */
|
|
203
|
+
largest?: 'day' | 'hour' | 'minute'
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const DURATION_UNITS = [
|
|
207
|
+
{ unit: 'day', short: 'd', seconds: 86_400 },
|
|
208
|
+
{ unit: 'hour', short: 'h', seconds: 3_600 },
|
|
209
|
+
{ unit: 'minute', short: 'm', seconds: 60 },
|
|
210
|
+
{ unit: 'second', short: 's', seconds: 1 },
|
|
211
|
+
] as const
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* THE duration formatter. `'clock'` is {@link durationClock}; `'compact'` and `'long'` trim leading
|
|
215
|
+
* and trailing zero units but keep a zero between two non-zero ones (`"1h 00m 03s"`); zero is
|
|
216
|
+
* `"0s"` / `"0 seconds"`. `'long'` labels through `Intl.NumberFormat` unit style (locale-aware) —
|
|
217
|
+
* `Intl.DurationFormat` is not in the pinned TS lib. `null`/`undefined`/non-finite prints
|
|
218
|
+
* {@link NON_FINITE}; negatives clamp to zero.
|
|
219
|
+
*/
|
|
220
|
+
export function duration(
|
|
221
|
+
seconds: number | null | undefined,
|
|
222
|
+
options: DurationOptions = {},
|
|
223
|
+
): string {
|
|
224
|
+
if (seconds === null || seconds === undefined || !Number.isFinite(seconds)) return NON_FINITE
|
|
225
|
+
const { style = 'clock', largest = 'hour', locale } = options
|
|
226
|
+
if (style === 'clock') return durationClock(seconds)
|
|
227
|
+
const total = Math.max(0, Math.round(seconds))
|
|
228
|
+
const shown = DURATION_UNITS.slice(DURATION_UNITS.findIndex(({ unit }) => unit === largest))
|
|
229
|
+
const parts = shown.map(({ unit, short, seconds: size }, i) => {
|
|
230
|
+
const count = Math.floor(total / size)
|
|
231
|
+
const upper = shown[i - 1]?.seconds
|
|
232
|
+
return { unit, short, value: upper === undefined ? count : count % (upper / size) }
|
|
233
|
+
})
|
|
234
|
+
const firstNonZero = parts.findIndex(({ value }) => value > 0)
|
|
235
|
+
const start = firstNonZero === -1 ? parts.length - 1 : firstNonZero
|
|
236
|
+
const end = firstNonZero === -1 ? start : parts.findLastIndex(({ value }) => value > 0)
|
|
237
|
+
return parts
|
|
238
|
+
.slice(start, end + 1)
|
|
239
|
+
.map(({ unit, short, value }, i) => {
|
|
240
|
+
if (style === 'long') {
|
|
241
|
+
return numberFormat(locale, { style: 'unit', unit, unitDisplay: 'long' }).format(value)
|
|
242
|
+
}
|
|
243
|
+
const digits = i === 0 ? String(value) : String(value).padStart(2, '0')
|
|
244
|
+
return `${digits}${short}`
|
|
245
|
+
})
|
|
246
|
+
.join(' ')
|
|
247
|
+
}
|
package/src/format/index.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `basalt-ui/format` — Mantine-free, React-free `Intl`-backed formatting
|
|
3
|
-
*
|
|
4
|
-
* (`km`/`kcal`)
|
|
5
|
-
* lines across four duration spellings) and the 1.x chart layer's own formatters (2.0's charts
|
|
6
|
-
* import the internals directly).
|
|
2
|
+
* `basalt-ui/format` — Mantine-free, React-free `Intl`-backed formatting: numbers, percentages,
|
|
3
|
+
* money, bytes and signed deltas, dates and durations, and the two measurement units argo needed
|
|
4
|
+
* (`km`/`kcal`).
|
|
7
5
|
*
|
|
8
|
-
* Every formatter takes `(value, opts?)
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Every formatter takes `(value, opts?)`. `locale` defaults to what `defineFormat` (or
|
|
7
|
+
* the provider's locale and time zone props) set, else the runtime's locale. That default is
|
|
8
|
+
* ONE value per process; pass `locale` per call to override it. Number formatters accept
|
|
9
|
+
* `null | undefined`, and every non-finite/unparseable input prints the same {@link NON_FINITE}
|
|
10
|
+
* em dash rather than `"NaN"` or throwing.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* import { money, percent, durationClock, weekday, relativeTime } from 'basalt-ui/format'
|
|
14
14
|
*
|
|
15
|
-
* money(1234, { currency: 'USD' }) // "$1,234"
|
|
15
|
+
* money(1234, { currency: 'USD' }) // "$1,234.00"
|
|
16
16
|
* percent(0.42) // "42%"
|
|
17
17
|
* durationClock(3_723) // "1:02:03"
|
|
18
18
|
* weekday('2026-09-02') // "Wed"
|
|
19
19
|
* relativeTime(Date.now() - 3_600_000) // "1 hour ago"
|
|
20
20
|
*/
|
|
21
|
-
export { integer, money, percent } from './number'
|
|
22
|
-
export { durationClock, relativeTime, weekday } from './date'
|
|
21
|
+
export { bytes, compact, delta, integer, money, percent } from './number'
|
|
22
|
+
export { date, dateTime, duration, durationClock, relativeTime, time, weekday } from './date'
|
|
23
23
|
export { kcal, km } from './measure'
|
|
24
|
+
export { defineFormat } from './shared'
|
package/src/format/measure.ts
CHANGED
|
@@ -1,31 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Distance/energy formatting
|
|
2
|
+
* Distance/energy formatting. Both accept `null | undefined` and print {@link NON_FINITE} for it.
|
|
3
3
|
*/
|
|
4
|
-
import { NON_FINITE } from './shared'
|
|
4
|
+
import { isFiniteNumber, NON_FINITE, numberFormat, scaleThousands } from './shared'
|
|
5
|
+
import type { LocaleOption, Numeric } from './shared'
|
|
5
6
|
|
|
6
|
-
export type KmOptions = {
|
|
7
|
+
export type KmOptions = LocaleOption & {
|
|
7
8
|
/** Fraction digits. Default 2 (`"5.30 km"`). */
|
|
8
9
|
digits?: number
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
/** Meters as kilometers — `"5.30 km"
|
|
12
|
-
export function km(meters:
|
|
13
|
-
if (!
|
|
14
|
-
const { digits = 2 } = options
|
|
15
|
-
return
|
|
12
|
+
/** Meters as kilometers — `"5.30 km"` (`"5,30 km"` in `de`). */
|
|
13
|
+
export function km(meters: Numeric, options: KmOptions = {}): string {
|
|
14
|
+
if (!isFiniteNumber(meters)) return NON_FINITE
|
|
15
|
+
const { digits = 2, locale } = options
|
|
16
|
+
return numberFormat(locale, {
|
|
17
|
+
signDisplay: 'negative',
|
|
18
|
+
style: 'unit',
|
|
19
|
+
unit: 'kilometer',
|
|
20
|
+
unitDisplay: 'short',
|
|
21
|
+
minimumFractionDigits: digits,
|
|
22
|
+
maximumFractionDigits: digits,
|
|
23
|
+
}).format(meters / 1000)
|
|
16
24
|
}
|
|
17
25
|
|
|
18
|
-
export type KcalOptions = {
|
|
19
|
-
/**
|
|
20
|
-
* whole number, matching the seed (`formatKcal`). */
|
|
26
|
+
export type KcalOptions = LocaleOption & {
|
|
27
|
+
/** Max fraction digits once scaled to thousands. Default 2. Below 1000 always whole. */
|
|
21
28
|
digits?: number
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
/** Kilocalories
|
|
25
|
-
*
|
|
26
|
-
export function kcal(value:
|
|
27
|
-
if (!
|
|
28
|
-
const { digits = 2 } = options
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
/** Kilocalories — `"842 kcal"`, and `"1.24k kcal"` from 1000 up. The unit string is always
|
|
32
|
+
* `kcal`. */
|
|
33
|
+
export function kcal(value: Numeric, options: KcalOptions = {}): string {
|
|
34
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
35
|
+
const { digits = 2, locale } = options
|
|
36
|
+
const { scaled, unit } = scaleThousands(value, digits, 1)
|
|
37
|
+
const text = numberFormat(locale, {
|
|
38
|
+
signDisplay: 'negative',
|
|
39
|
+
maximumFractionDigits: unit === 0 ? 0 : digits,
|
|
40
|
+
}).format(scaled)
|
|
41
|
+
return `${text}${unit === 0 ? '' : 'k'} kcal`
|
|
31
42
|
}
|