@canonical/svelte-ds-app-launchpad 0.18.1 → 0.20.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/LICENSE +166 -0
- package/dist/components/DateTime/DateTime.ssr.test.d.ts +1 -0
- package/dist/components/DateTime/DateTime.ssr.test.js +94 -0
- package/dist/components/DateTime/DateTime.stories.svelte +34 -0
- package/dist/components/DateTime/DateTime.stories.svelte.d.ts +19 -0
- package/dist/components/DateTime/DateTime.svelte +27 -0
- package/dist/components/DateTime/DateTime.svelte.d.ts +12 -0
- package/dist/components/DateTime/DateTime.svelte.test.d.ts +1 -0
- package/dist/components/DateTime/DateTime.svelte.test.js +81 -0
- package/dist/components/DateTime/index.d.ts +2 -0
- package/dist/components/DateTime/index.js +2 -0
- package/dist/components/DateTime/types.d.ts +16 -0
- package/dist/components/DateTime/types.js +2 -0
- package/dist/components/DateTime/utils/formatters.d.ts +1 -0
- package/dist/components/DateTime/utils/formatters.js +5 -0
- package/dist/components/DescriptionList/DescriptionList.stories.svelte +2 -1
- package/dist/components/RelativeDateTime/RelativeDateTime.ssr.test.d.ts +1 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.ssr.test.js +130 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.stories.svelte +41 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.stories.svelte.d.ts +19 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte +86 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte.d.ts +13 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte.test.d.ts +1 -0
- package/dist/components/RelativeDateTime/RelativeDateTime.svelte.test.js +152 -0
- package/dist/components/RelativeDateTime/index.d.ts +2 -0
- package/dist/components/RelativeDateTime/index.js +2 -0
- package/dist/components/RelativeDateTime/types.d.ts +26 -0
- package/dist/components/RelativeDateTime/types.js +2 -0
- package/dist/components/RelativeDateTime/utils/constants.d.ts +1 -0
- package/dist/components/RelativeDateTime/utils/constants.js +9 -0
- package/dist/components/RelativeDateTime/utils/formatters.d.ts +2 -0
- package/dist/components/RelativeDateTime/utils/formatters.js +9 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.d.ts +15 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.js +21 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.test.d.ts +1 -0
- package/dist/components/RelativeDateTime/utils/getOptimalRelativeTimeFormatValue.test.js +65 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +2 -0
- package/package.json +9 -8
- package/dist/components/Breadcrumbs/__screenshots__/Breadcrumbs.svelte.test.ts/Breadcrumbs-component-accessibility-focus-management-allows-focusing-links-with-tab-key-1.png +0 -0
- package/dist/components/Breadcrumbs/__screenshots__/Breadcrumbs.svelte.test.ts/Breadcrumbs-component-accessibility-focus-management-allows-focusing-links-with-tab-key-when-segments-are-collapsed-1.png +0 -0
- package/dist/components/Checkbox/__screenshots__/Checkbox.svelte.test.ts/Checkbox-component-Checked-state-toggles-on-click-1.png +0 -0
- package/dist/components/Checkbox/__screenshots__/Checkbox.svelte.test.ts/Checkbox-component-Events-emits-change-event-on-click-1.png +0 -0
- package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-apply-custom-class-and-modifiers-1.png +0 -0
- package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-not-render-a-button-when-dismiss-is-provided-1.png +0 -0
- package/dist/components/Chip/__screenshots__/Chip.svelte.test.ts/Chip-component-Initial-Rendering-should-not-render-a-button-when-readonly-is-provided-1.png +0 -0
- package/dist/components/Radio/__screenshots__/Radio.svelte.test.ts/Radio-component-Checked-state-can-be-checked-by-on-click-1.png +0 -0
- package/dist/components/Radio/__screenshots__/Radio.svelte.test.ts/Radio-component-Events-emits-change-event-on-click-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Accessibility-can-be-toggled-with-space-key-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Events-emits-change-event-on-click-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Switch-state-can-be-disabled-1.png +0 -0
- package/dist/components/Switch/__screenshots__/Switch.svelte.test.ts/Switch-component-Switch-state-toggles-checked-state-on-click-1.png +0 -0
- package/dist/components/Textarea/utils/__screenshots__/calculateDynamicRows.svelte.test.ts/Textarea---calculateDynamicRows-Swapped-min-max-swaps-min-and-max-when-max-is-smaller-than-min-1.png +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
import { defineMeta } from "@storybook/addon-svelte-csf";
|
|
3
|
+
import RelativeDateTime from "./RelativeDateTime.svelte";
|
|
4
|
+
|
|
5
|
+
const { Story } = defineMeta({
|
|
6
|
+
title: "Components/RelativeDateTime",
|
|
7
|
+
tags: ["autodocs"],
|
|
8
|
+
component: RelativeDateTime,
|
|
9
|
+
});
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
const times = [
|
|
14
|
+
1000 * 60 * 5, // 5 minutes
|
|
15
|
+
1000 * 60 * 60 * 1.5, // 1.5 hour
|
|
16
|
+
1000 * 60 * 60 * 24 * 1.5, // 1.5 days
|
|
17
|
+
1000 * 60 * 60 * 24 * 7 * 1.5, // 1.5 week
|
|
18
|
+
];
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<Story name="Now" args={{ date: new Date() }} />
|
|
22
|
+
|
|
23
|
+
<Story name="Past Dates" argTypes={{ date: { control: false } }}>
|
|
24
|
+
{#snippet template(args)}
|
|
25
|
+
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
|
|
26
|
+
{#each times as time (time)}
|
|
27
|
+
<RelativeDateTime {...args} date={Date.now() - time} />
|
|
28
|
+
{/each}
|
|
29
|
+
</div>
|
|
30
|
+
{/snippet}
|
|
31
|
+
</Story>
|
|
32
|
+
|
|
33
|
+
<Story name="Future Dates" argTypes={{ date: { control: false } }}>
|
|
34
|
+
{#snippet template(args)}
|
|
35
|
+
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
|
|
36
|
+
{#each times as time (time)}
|
|
37
|
+
<RelativeDateTime {...args} date={Date.now() + time} />
|
|
38
|
+
{/each}
|
|
39
|
+
</div>
|
|
40
|
+
{/snippet}
|
|
41
|
+
</Story>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import RelativeDateTime from "./RelativeDateTime.svelte";
|
|
2
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
3
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
4
|
+
$$bindings?: Bindings;
|
|
5
|
+
} & Exports;
|
|
6
|
+
(internal: unknown, props: {
|
|
7
|
+
$$events?: Events;
|
|
8
|
+
$$slots?: Slots;
|
|
9
|
+
}): Exports & {
|
|
10
|
+
$set?: any;
|
|
11
|
+
$on?: any;
|
|
12
|
+
};
|
|
13
|
+
z_$$bindings?: Bindings;
|
|
14
|
+
}
|
|
15
|
+
declare const RelativeDateTime: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
|
16
|
+
[evt: string]: CustomEvent<any>;
|
|
17
|
+
}, {}, {}, string>;
|
|
18
|
+
type RelativeDateTime = InstanceType<typeof RelativeDateTime>;
|
|
19
|
+
export default RelativeDateTime;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
<!-- @canonical/generator-ds 0.17.1 -->
|
|
2
|
+
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
import type {
|
|
5
|
+
RelativeDateTimeProps,
|
|
6
|
+
RelativeTimeFormatValue,
|
|
7
|
+
} from "./types.js";
|
|
8
|
+
import {
|
|
9
|
+
dateTimeFormatter,
|
|
10
|
+
relativeTimeFormatter,
|
|
11
|
+
} from "./utils/formatters.js";
|
|
12
|
+
import { getOptimalRelativeTimeFormatValue } from "./utils/getOptimalRelativeTimeFormatValue.js";
|
|
13
|
+
|
|
14
|
+
let {
|
|
15
|
+
date: dateProp,
|
|
16
|
+
nowThreshold = 60_000,
|
|
17
|
+
nowLabel = "now",
|
|
18
|
+
...rest
|
|
19
|
+
}: RelativeDateTimeProps = $props();
|
|
20
|
+
|
|
21
|
+
const date = $derived(new Date(dateProp));
|
|
22
|
+
|
|
23
|
+
const isOutsideThreshold = (elapsed: number) =>
|
|
24
|
+
Math.abs(elapsed) >= nowThreshold;
|
|
25
|
+
|
|
26
|
+
// We intentionally capture only the initial `rtfValue` here, and then update it in an effect below. If `date` changes, the effect will re-run and update `rtfValue` accordingly.
|
|
27
|
+
// Capturing the initial value here avoids flashing incorrect content during SSR.
|
|
28
|
+
// svelte-ignore state_referenced_locally
|
|
29
|
+
const initialElapsed = date.getTime() - Date.now();
|
|
30
|
+
let rtfValue = $state<RelativeTimeFormatValue | null>(
|
|
31
|
+
isOutsideThreshold(initialElapsed)
|
|
32
|
+
? getOptimalRelativeTimeFormatValue(initialElapsed)
|
|
33
|
+
: null,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const display = $derived.by(() => {
|
|
37
|
+
if (!rtfValue) return nowLabel;
|
|
38
|
+
return relativeTimeFormatter.format(rtfValue.value, rtfValue.unit);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Periodically recalculates `rtfValue` based on the current time, scheduling
|
|
43
|
+
* the next update at the interval most appropriate for the current magnitude
|
|
44
|
+
* of elapsed time. Cleans up the timeout on teardown.
|
|
45
|
+
*/
|
|
46
|
+
$effect(() => {
|
|
47
|
+
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
48
|
+
|
|
49
|
+
const tick = () => {
|
|
50
|
+
const newElapsed = date.getTime() - Date.now();
|
|
51
|
+
if (isOutsideThreshold(newElapsed)) {
|
|
52
|
+
const { nextUpdateIn, ...newRtfValue } =
|
|
53
|
+
getOptimalRelativeTimeFormatValue(newElapsed);
|
|
54
|
+
rtfValue = newRtfValue;
|
|
55
|
+
timeout = setTimeout(tick, nextUpdateIn);
|
|
56
|
+
} else {
|
|
57
|
+
rtfValue = null;
|
|
58
|
+
timeout = setTimeout(tick, nowThreshold - Math.abs(newElapsed));
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
tick();
|
|
63
|
+
|
|
64
|
+
return () => {
|
|
65
|
+
if (timeout) clearTimeout(timeout);
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
</script>
|
|
69
|
+
|
|
70
|
+
<time
|
|
71
|
+
datetime={date.toISOString()}
|
|
72
|
+
title={dateTimeFormatter.format(date)}
|
|
73
|
+
{...rest}
|
|
74
|
+
>
|
|
75
|
+
{display}
|
|
76
|
+
</time>
|
|
77
|
+
|
|
78
|
+
<!-- @component
|
|
79
|
+
`RelativeDateTime` component formats and displays dates and times in a user-friendly relative format, automatically updating the display as time passes.
|
|
80
|
+
|
|
81
|
+
## Example Usage
|
|
82
|
+
```svelte
|
|
83
|
+
<RelativeDateTime date={new Date()} />
|
|
84
|
+
<RelativeDateTime date="2023-10-01T12:00:00Z" nowThreshold={120_000} nowLabel="just now" />
|
|
85
|
+
```
|
|
86
|
+
-->
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RelativeDateTimeProps } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* `RelativeDateTime` component formats and displays dates and times in a user-friendly relative format, automatically updating the display as time passes.
|
|
4
|
+
*
|
|
5
|
+
* ## Example Usage
|
|
6
|
+
* ```svelte
|
|
7
|
+
* <RelativeDateTime date={new Date()} />
|
|
8
|
+
* <RelativeDateTime date="2023-10-01T12:00:00Z" nowThreshold={120_000} nowLabel="just now" />
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
declare const RelativeDateTime: import("svelte").Component<RelativeDateTimeProps, {}, "">;
|
|
12
|
+
type RelativeDateTime = ReturnType<typeof RelativeDateTime>;
|
|
13
|
+
export default RelativeDateTime;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/* @canonical/generator-ds 0.17.1 */
|
|
2
|
+
import { describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { render } from "vitest-browser-svelte";
|
|
4
|
+
import Component from "./RelativeDateTime.svelte";
|
|
5
|
+
vi.mock("./utils/formatters.js", () => {
|
|
6
|
+
return {
|
|
7
|
+
dateTimeFormatter: new Intl.DateTimeFormat("en-US", {
|
|
8
|
+
dateStyle: "short",
|
|
9
|
+
timeStyle: "short",
|
|
10
|
+
timeZone: "UTC",
|
|
11
|
+
}),
|
|
12
|
+
relativeTimeFormatter: new Intl.RelativeTimeFormat("en-US", {
|
|
13
|
+
numeric: "auto",
|
|
14
|
+
style: "long",
|
|
15
|
+
}),
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
const date = new Date("2024-01-01T12:00:00Z");
|
|
19
|
+
const timestamp = date.getTime();
|
|
20
|
+
const dateString = date.toISOString();
|
|
21
|
+
describe("RelativeDateTime component", () => {
|
|
22
|
+
const baseProps = {
|
|
23
|
+
date,
|
|
24
|
+
};
|
|
25
|
+
it("renders", async () => {
|
|
26
|
+
const page = render(Component, baseProps);
|
|
27
|
+
await expect.element(componentLocator(page)).toBeInTheDocument();
|
|
28
|
+
});
|
|
29
|
+
describe("basic attributes", () => {
|
|
30
|
+
it.each([
|
|
31
|
+
["id", "test-id"],
|
|
32
|
+
["aria-label", "test-aria-label"],
|
|
33
|
+
])("applies %s", async (attribute, value) => {
|
|
34
|
+
const page = render(Component, { ...baseProps, [attribute]: value });
|
|
35
|
+
await expect
|
|
36
|
+
.element(componentLocator(page))
|
|
37
|
+
.toHaveAttribute(attribute, value);
|
|
38
|
+
});
|
|
39
|
+
it("applies style", async () => {
|
|
40
|
+
const page = render(Component, { ...baseProps, style: "color: orange;" });
|
|
41
|
+
await expect
|
|
42
|
+
.element(componentLocator(page))
|
|
43
|
+
.toHaveStyle("color: orange;");
|
|
44
|
+
});
|
|
45
|
+
it("applies class", async () => {
|
|
46
|
+
const page = render(Component, { ...baseProps, class: "test-class" });
|
|
47
|
+
const element = componentLocator(page);
|
|
48
|
+
await expect.element(element).toHaveClass("test-class");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
describe("datetime attribute", () => {
|
|
52
|
+
it("applies correct datetime attribute for Date input", async () => {
|
|
53
|
+
const page = render(Component, baseProps);
|
|
54
|
+
await expect
|
|
55
|
+
.element(componentLocator(page))
|
|
56
|
+
.toHaveAttribute("datetime", date.toISOString());
|
|
57
|
+
});
|
|
58
|
+
it("applies correct datetime attribute for timestamp input", async () => {
|
|
59
|
+
const page = render(Component, { ...baseProps, date: timestamp });
|
|
60
|
+
await expect
|
|
61
|
+
.element(componentLocator(page))
|
|
62
|
+
.toHaveAttribute("datetime", date.toISOString());
|
|
63
|
+
});
|
|
64
|
+
it("applies correct datetime attribute for date string input", async () => {
|
|
65
|
+
const page = render(Component, { ...baseProps, date: dateString });
|
|
66
|
+
await expect
|
|
67
|
+
.element(componentLocator(page))
|
|
68
|
+
.toHaveAttribute("datetime", date.toISOString());
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe("Content", () => {
|
|
72
|
+
describe("now label", () => {
|
|
73
|
+
it("renders nowLabel when within nowThreshold", async () => {
|
|
74
|
+
const now = Date.now();
|
|
75
|
+
const page = render(Component, {
|
|
76
|
+
...baseProps,
|
|
77
|
+
date: now,
|
|
78
|
+
nowThreshold: 999999,
|
|
79
|
+
});
|
|
80
|
+
await expect.element(componentLocator(page)).toHaveTextContent("now");
|
|
81
|
+
});
|
|
82
|
+
it("does not render nowLabel when outside nowThreshold", async () => {
|
|
83
|
+
const now = Date.now();
|
|
84
|
+
const page = render(Component, {
|
|
85
|
+
...baseProps,
|
|
86
|
+
date: now - 1000,
|
|
87
|
+
nowThreshold: 10,
|
|
88
|
+
});
|
|
89
|
+
await expect
|
|
90
|
+
.element(componentLocator(page))
|
|
91
|
+
.not.toHaveTextContent("now");
|
|
92
|
+
});
|
|
93
|
+
it("renders custom nowLabel when within nowThreshold", async () => {
|
|
94
|
+
const now = Date.now();
|
|
95
|
+
const page = render(Component, {
|
|
96
|
+
...baseProps,
|
|
97
|
+
date: now,
|
|
98
|
+
nowThreshold: 999999,
|
|
99
|
+
nowLabel: "just now",
|
|
100
|
+
});
|
|
101
|
+
await expect
|
|
102
|
+
.element(componentLocator(page))
|
|
103
|
+
.toHaveTextContent("just now");
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe("Relative time", () => {
|
|
107
|
+
it("renders relative time when outside nowThreshold", async () => {
|
|
108
|
+
const pastDate = new Date(Date.now() - 1000 * 60 * 60 * 3); // 3 hours ago
|
|
109
|
+
const page = render(Component, {
|
|
110
|
+
...baseProps,
|
|
111
|
+
date: pastDate,
|
|
112
|
+
nowThreshold: 0,
|
|
113
|
+
});
|
|
114
|
+
await expect
|
|
115
|
+
.element(componentLocator(page))
|
|
116
|
+
.toHaveTextContent("3 hours ago");
|
|
117
|
+
});
|
|
118
|
+
it("updates over time", async () => {
|
|
119
|
+
vi.useFakeTimers();
|
|
120
|
+
const pastDate = new Date(Date.now() - 1000 * 60 * 3); // 3 minutes ago
|
|
121
|
+
const page = render(Component, {
|
|
122
|
+
...baseProps,
|
|
123
|
+
date: pastDate,
|
|
124
|
+
nowThreshold: 0,
|
|
125
|
+
});
|
|
126
|
+
await expect
|
|
127
|
+
.element(componentLocator(page))
|
|
128
|
+
.toHaveTextContent("3 minutes ago");
|
|
129
|
+
vi.advanceTimersByTime(1000 * 60);
|
|
130
|
+
await expect
|
|
131
|
+
.element(componentLocator(page))
|
|
132
|
+
.toHaveTextContent("4 minutes ago");
|
|
133
|
+
vi.advanceTimersByTime(1000 * 60 * 60);
|
|
134
|
+
await expect
|
|
135
|
+
.element(componentLocator(page))
|
|
136
|
+
.toHaveTextContent("1 hour ago");
|
|
137
|
+
vi.useRealTimers();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
describe("title attribute", () => {
|
|
142
|
+
it("applies formatted date as title", async () => {
|
|
143
|
+
const page = render(Component, baseProps);
|
|
144
|
+
await expect
|
|
145
|
+
.element(componentLocator(page))
|
|
146
|
+
.toHaveAttribute("title", "1/1/24, 12:00 PM");
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
function componentLocator(page) {
|
|
151
|
+
return page.getByRole("time");
|
|
152
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SvelteHTMLElements } from "svelte/elements";
|
|
2
|
+
type BaseProps = Omit<SvelteHTMLElements["time"], "children">;
|
|
3
|
+
export interface RelativeDateTimeProps extends BaseProps {
|
|
4
|
+
/**
|
|
5
|
+
* The moment in time to display.
|
|
6
|
+
* This can be a Date object, a timestamp, or a date string.
|
|
7
|
+
*/
|
|
8
|
+
date: Date | string | number;
|
|
9
|
+
/**
|
|
10
|
+
* The threshold in milliseconds below which to display the `nowLabel` label.
|
|
11
|
+
*
|
|
12
|
+
* @default 60_000 (1 minute)
|
|
13
|
+
*/
|
|
14
|
+
nowThreshold?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The label to display when the time is within the `nowThreshold`.
|
|
17
|
+
*
|
|
18
|
+
* @default "now"
|
|
19
|
+
*/
|
|
20
|
+
nowLabel?: string;
|
|
21
|
+
}
|
|
22
|
+
export type RelativeTimeFormatValue = {
|
|
23
|
+
value: number;
|
|
24
|
+
unit: Intl.RelativeTimeFormatUnit;
|
|
25
|
+
};
|
|
26
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const units: (["year", number] | ["month", number] | ["week", number] | ["day", number] | ["hour", number] | ["minute", number] | ["second", number])[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RelativeTimeFormatValue } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Computes a relative time (value + unit) and milliseconds until it changes.
|
|
4
|
+
* Chooses the largest unit with ms <= |elapsed|.
|
|
5
|
+
* `nextUpdateIn` is the ms until the value/unit would differ.
|
|
6
|
+
*
|
|
7
|
+
* @param elapsed Milliseconds relative to now (+future, -past).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* getOptimalRelativeTimeFormatValue(-90 * 60 * 1000);
|
|
11
|
+
* // { value: -1, unit: "hour", nextUpdateIn: 1800000 }
|
|
12
|
+
*/
|
|
13
|
+
export declare function getOptimalRelativeTimeFormatValue(elapsed: number): RelativeTimeFormatValue & {
|
|
14
|
+
nextUpdateIn: number;
|
|
15
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { units } from "./constants.js";
|
|
2
|
+
/**
|
|
3
|
+
* Computes a relative time (value + unit) and milliseconds until it changes.
|
|
4
|
+
* Chooses the largest unit with ms <= |elapsed|.
|
|
5
|
+
* `nextUpdateIn` is the ms until the value/unit would differ.
|
|
6
|
+
*
|
|
7
|
+
* @param elapsed Milliseconds relative to now (+future, -past).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* getOptimalRelativeTimeFormatValue(-90 * 60 * 1000);
|
|
11
|
+
* // { value: -1, unit: "hour", nextUpdateIn: 1800000 }
|
|
12
|
+
*/
|
|
13
|
+
export function getOptimalRelativeTimeFormatValue(elapsed) {
|
|
14
|
+
const absElapsed = Math.abs(elapsed);
|
|
15
|
+
const match = units.find(([_, millis]) => absElapsed >= millis) ??
|
|
16
|
+
units[units.length - 1];
|
|
17
|
+
const [unit, millis] = match;
|
|
18
|
+
const value = Math.trunc(elapsed / millis);
|
|
19
|
+
const nextUpdateIn = millis - (absElapsed % millis);
|
|
20
|
+
return { value, unit, nextUpdateIn };
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getOptimalRelativeTimeFormatValue } from "./getOptimalRelativeTimeFormatValue.js";
|
|
3
|
+
describe("getOptimalRtfValue", () => {
|
|
4
|
+
describe.each([
|
|
5
|
+
["for the future", 1],
|
|
6
|
+
["for the past", -1],
|
|
7
|
+
])("%s", (_, multiplier) => {
|
|
8
|
+
describe("unit", () => {
|
|
9
|
+
it("returns the largest unit that fits within the elapsed time", () => {
|
|
10
|
+
const oneHour = 1000 * 60 * 60 * multiplier;
|
|
11
|
+
let { unit } = getOptimalRelativeTimeFormatValue(oneHour);
|
|
12
|
+
expect(unit).toBe("hour");
|
|
13
|
+
const thirtyMinutes = 1000 * 60 * 30 * multiplier;
|
|
14
|
+
({ unit } = getOptimalRelativeTimeFormatValue(thirtyMinutes));
|
|
15
|
+
expect(unit).toBe("minute");
|
|
16
|
+
const fiveYears = 1000 * 60 * 60 * 24 * 365 * 5 * multiplier;
|
|
17
|
+
({ unit } = getOptimalRelativeTimeFormatValue(fiveYears));
|
|
18
|
+
expect(unit).toBe("year");
|
|
19
|
+
});
|
|
20
|
+
it("returns the smallest unit if none fit within the elapsed time", () => {
|
|
21
|
+
const halfSecond = 500 * multiplier;
|
|
22
|
+
const { unit } = getOptimalRelativeTimeFormatValue(halfSecond);
|
|
23
|
+
expect(unit).toBe("second");
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe("value", () => {
|
|
27
|
+
it("returns the number of largest fitting units within the elapsed time, preserving elapsed sign", () => {
|
|
28
|
+
const threeHours = 1000 * 60 * 60 * 3 * multiplier;
|
|
29
|
+
let { value } = getOptimalRelativeTimeFormatValue(threeHours);
|
|
30
|
+
expect(value).toBe(3 * multiplier);
|
|
31
|
+
const ninetyMinutes = 1000 * 60 * 90 * multiplier;
|
|
32
|
+
({ value } = getOptimalRelativeTimeFormatValue(ninetyMinutes));
|
|
33
|
+
expect(value).toBe(1 * multiplier);
|
|
34
|
+
const twoYears = 1000 * 60 * 60 * 24 * 365 * 2 * multiplier;
|
|
35
|
+
({ value } = getOptimalRelativeTimeFormatValue(twoYears));
|
|
36
|
+
expect(value).toBe(2 * multiplier);
|
|
37
|
+
});
|
|
38
|
+
it("returns signed 0 for elapsed times smaller than the smallest unit", () => {
|
|
39
|
+
const halfSecond = 500 * multiplier;
|
|
40
|
+
const { value } = getOptimalRelativeTimeFormatValue(halfSecond);
|
|
41
|
+
expect(value).toBe(0 * multiplier);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe("nextUpdateIn", () => {
|
|
45
|
+
it("returns the milliseconds until the value would change", () => {
|
|
46
|
+
const threeHours = 1000 * 60 * 60 * 3 * multiplier;
|
|
47
|
+
let { nextUpdateIn } = getOptimalRelativeTimeFormatValue(threeHours);
|
|
48
|
+
expect(nextUpdateIn).toBe(1000 * 60 * 60); // 1 hour
|
|
49
|
+
const ninetyMinutes = 1000 * 60 * 90 * multiplier;
|
|
50
|
+
({ nextUpdateIn } = getOptimalRelativeTimeFormatValue(ninetyMinutes));
|
|
51
|
+
expect(nextUpdateIn).toBe(1000 * 60 * 30); // 30 minutes
|
|
52
|
+
const twoYears = 1000 * 60 * 60 * 24 * 365 * 2 * multiplier;
|
|
53
|
+
({ nextUpdateIn } = getOptimalRelativeTimeFormatValue(twoYears));
|
|
54
|
+
expect(nextUpdateIn).toBe(1000 * 60 * 60 * 24 * 365); // 1 year
|
|
55
|
+
const oneSecondAndHalf = 1500 * multiplier;
|
|
56
|
+
({ nextUpdateIn } =
|
|
57
|
+
getOptimalRelativeTimeFormatValue(oneSecondAndHalf));
|
|
58
|
+
expect(nextUpdateIn).toBe(500); // 0.5 second
|
|
59
|
+
const tenMilliseconds = 10 * multiplier;
|
|
60
|
+
({ nextUpdateIn } = getOptimalRelativeTimeFormatValue(tenMilliseconds));
|
|
61
|
+
expect(nextUpdateIn).toBe(990);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -3,11 +3,13 @@ export * from "./Breadcrumbs/index.js";
|
|
|
3
3
|
export * from "./Button/index.js";
|
|
4
4
|
export * from "./Checkbox/index.js";
|
|
5
5
|
export * from "./Chip/index.js";
|
|
6
|
+
export * from "./DateTime/index.js";
|
|
6
7
|
export * from "./DescriptionList/index.js";
|
|
7
8
|
export * from "./Link/index.js";
|
|
8
9
|
export * from "./NumberInput/index.js";
|
|
9
10
|
export * from "./Popover/index.js";
|
|
10
11
|
export * from "./Radio/index.js";
|
|
12
|
+
export * from "./RelativeDateTime/index.js";
|
|
11
13
|
export * from "./Spinner/index.js";
|
|
12
14
|
export * from "./Switch/index.js";
|
|
13
15
|
export * from "./Textarea/index.js";
|
package/dist/components/index.js
CHANGED
|
@@ -3,11 +3,13 @@ export * from "./Breadcrumbs/index.js";
|
|
|
3
3
|
export * from "./Button/index.js";
|
|
4
4
|
export * from "./Checkbox/index.js";
|
|
5
5
|
export * from "./Chip/index.js";
|
|
6
|
+
export * from "./DateTime/index.js";
|
|
6
7
|
export * from "./DescriptionList/index.js";
|
|
7
8
|
export * from "./Link/index.js";
|
|
8
9
|
export * from "./NumberInput/index.js";
|
|
9
10
|
export * from "./Popover/index.js";
|
|
10
11
|
export * from "./Radio/index.js";
|
|
12
|
+
export * from "./RelativeDateTime/index.js";
|
|
11
13
|
export * from "./Spinner/index.js";
|
|
12
14
|
export * from "./Switch/index.js";
|
|
13
15
|
export * from "./Textarea/index.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canonical/svelte-ds-app-launchpad",
|
|
3
3
|
"description": "Launchpad-specific components for the Pragma design system.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.20.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@biomejs/biome": "2.4.6",
|
|
55
|
-
"@canonical/biome-config": "^0.
|
|
56
|
-
"@canonical/storybook-helpers": "^0.
|
|
57
|
-
"@canonical/svelte-ssr-test": "^0.
|
|
58
|
-
"@canonical/typescript-config-svelte": "^0.
|
|
59
|
-
"@canonical/webarchitect": "^0.
|
|
55
|
+
"@canonical/biome-config": "^0.20.0",
|
|
56
|
+
"@canonical/storybook-helpers": "^0.20.0",
|
|
57
|
+
"@canonical/svelte-ssr-test": "^0.20.0",
|
|
58
|
+
"@canonical/typescript-config-svelte": "^0.20.0",
|
|
59
|
+
"@canonical/webarchitect": "^0.20.0",
|
|
60
60
|
"@storybook/addon-docs": "^10.3.1",
|
|
61
61
|
"@storybook/addon-svelte-csf": "^5.0.11",
|
|
62
62
|
"@sveltejs/package": "^2.5.7",
|
|
@@ -74,11 +74,12 @@
|
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
76
|
"@canonical/launchpad-design-tokens": "^1.0.23",
|
|
77
|
-
"@canonical/storybook-config": "^0.
|
|
77
|
+
"@canonical/storybook-config": "^0.20.0",
|
|
78
78
|
"@canonical/svelte-icons": "^0.0.9",
|
|
79
79
|
"@floating-ui/dom": "^1.7.6"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
82
|
"svelte": "^5.47.1"
|
|
83
|
-
}
|
|
83
|
+
},
|
|
84
|
+
"gitHead": "1aee634b278922f2aa1cfe2a11d3b14e64280905"
|
|
84
85
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|