@overdoser/react-toolkit 0.17.0 → 0.19.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/AGENTS.md +51 -7
- package/CHANGELOG.md +149 -0
- package/components/Accordion/Accordion.d.ts +94 -0
- package/components/Accordion/Accordion.js +180 -0
- package/components/Accordion/Accordion.module.scss.js +36 -0
- package/components/Accordion/index.d.ts +2 -0
- package/components/Alert/Alert.d.ts +29 -1
- package/components/Alert/Alert.js +44 -30
- package/components/Alert/Alert.module.scss.js +24 -16
- package/components/Alert/index.d.ts +1 -1
- package/components/Card/Card.d.ts +86 -0
- package/components/Card/Card.js +132 -0
- package/components/Card/Card.module.scss.js +60 -0
- package/components/Card/index.d.ts +2 -0
- package/components/Chip/Chip.d.ts +86 -0
- package/components/Chip/Chip.js +141 -0
- package/components/Chip/Chip.module.scss.js +59 -0
- package/components/Chip/index.d.ts +2 -0
- package/components/CodeBlock/CodeBlock.d.ts +78 -0
- package/components/CodeBlock/CodeBlock.js +120 -0
- package/components/CodeBlock/CodeBlock.module.scss.js +43 -0
- package/components/CodeBlock/index.d.ts +2 -0
- package/components/CommandPalette/CommandPalette.d.ts +141 -0
- package/components/CommandPalette/CommandPalette.js +397 -0
- package/components/CommandPalette/CommandPalette.module.scss.js +49 -0
- package/components/CommandPalette/index.d.ts +2 -0
- package/components/ConfirmDialog/ConfirmDialog.d.ts +119 -0
- package/components/ConfirmDialog/ConfirmDialog.js +201 -0
- package/components/ConfirmDialog/ConfirmDialog.module.scss.js +21 -0
- package/components/ConfirmDialog/index.d.ts +2 -0
- package/components/Drawer/Drawer.d.ts +1 -0
- package/components/Drawer/Drawer.js +35 -36
- package/components/Drawer/Drawer.module.scss.js +20 -18
- package/components/Dropdown/Dropdown.d.ts +47 -7
- package/components/Dropdown/Dropdown.js +174 -81
- package/components/Dropdown/Dropdown.module.scss.js +40 -22
- package/components/Dropdown/DropdownItem.d.ts +31 -2
- package/components/Dropdown/DropdownItem.js +54 -27
- package/components/Dropdown/DropdownLabel.d.ts +11 -0
- package/components/Dropdown/DropdownLabel.js +11 -0
- package/components/Dropdown/DropdownSeparator.d.ts +12 -0
- package/components/Dropdown/DropdownSeparator.js +11 -0
- package/components/Dropdown/dropdownContext.d.ts +6 -0
- package/components/Dropdown/dropdownContext.js +5 -0
- package/components/Dropdown/index.d.ts +8 -2
- package/components/Dropdown/useContextMenu.d.ts +32 -0
- package/components/Dropdown/useContextMenu.js +18 -0
- package/components/EmptyState/EmptyState.d.ts +53 -0
- package/components/EmptyState/EmptyState.js +55 -0
- package/components/EmptyState/EmptyState.module.scss.js +25 -0
- package/components/EmptyState/index.d.ts +2 -0
- package/components/Form/FormField.js +25 -24
- package/components/ImageViewer/ImageViewer.d.ts +105 -0
- package/components/ImageViewer/ImageViewer.js +201 -0
- package/components/ImageViewer/ImageViewer.module.scss.js +47 -0
- package/components/ImageViewer/index.d.ts +4 -0
- package/components/ImageViewer/useZoomPan.d.ts +94 -0
- package/components/ImageViewer/useZoomPan.js +141 -0
- package/components/Kbd/Kbd.d.ts +26 -0
- package/components/Kbd/Kbd.js +22 -0
- package/components/Kbd/Kbd.module.scss.js +17 -0
- package/components/Kbd/index.d.ts +2 -0
- package/components/KeyValueList/KeyValueList.d.ts +84 -0
- package/components/KeyValueList/KeyValueList.js +127 -0
- package/components/KeyValueList/KeyValueList.module.scss.js +43 -0
- package/components/KeyValueList/index.d.ts +2 -0
- package/components/Loader/Loader.d.ts +104 -0
- package/components/Loader/Loader.js +186 -0
- package/components/Loader/Loader.module.scss.js +70 -0
- package/components/Loader/index.d.ts +2 -0
- package/components/Modal/Modal.d.ts +1 -0
- package/components/Modal/Modal.js +40 -41
- package/components/Modal/Modal.module.scss.js +18 -16
- package/components/PageHeader/PageHeader.d.ts +61 -0
- package/components/PageHeader/PageHeader.js +73 -0
- package/components/PageHeader/PageHeader.module.scss.js +43 -0
- package/components/PageHeader/index.d.ts +2 -0
- package/components/ProgressBar/ProgressBar.d.ts +74 -0
- package/components/ProgressBar/ProgressBar.js +90 -0
- package/components/ProgressBar/ProgressBar.module.scss.js +51 -0
- package/components/ProgressBar/index.d.ts +2 -0
- package/components/RelativeTime/RelativeTime.d.ts +45 -0
- package/components/RelativeTime/RelativeTime.js +43 -0
- package/components/RelativeTime/RelativeTime.module.scss.js +7 -0
- package/components/RelativeTime/index.d.ts +2 -0
- package/components/SegmentedControl/SegmentedControl.d.ts +84 -0
- package/components/SegmentedControl/SegmentedControl.js +131 -0
- package/components/SegmentedControl/SegmentedControl.module.scss.js +39 -0
- package/components/SegmentedControl/index.d.ts +2 -0
- package/components/SplitPane/SplitPane.d.ts +61 -0
- package/components/SplitPane/SplitPane.js +171 -0
- package/components/SplitPane/SplitPane.module.scss.js +25 -0
- package/components/SplitPane/index.d.ts +2 -0
- package/components/StatCard/StatCard.d.ts +105 -0
- package/components/StatCard/StatCard.js +127 -0
- package/components/StatCard/StatCard.module.scss.js +72 -0
- package/components/StatCard/index.d.ts +2 -0
- package/components/StatusDot/StatusDot.d.ts +33 -0
- package/components/StatusDot/StatusDot.js +39 -0
- package/components/StatusDot/StatusDot.module.scss.js +32 -0
- package/components/StatusDot/index.d.ts +2 -0
- package/components/Table/Table.d.ts +100 -2
- package/components/Table/Table.js +371 -210
- package/components/Table/Table.module.scss.js +68 -46
- package/components/Table/index.d.ts +1 -1
- package/components/ThemeToggle/ThemeToggle.d.ts +38 -0
- package/components/ThemeToggle/ThemeToggle.js +97 -0
- package/components/ThemeToggle/ThemeToggle.module.scss.js +23 -0
- package/components/ThemeToggle/index.d.ts +2 -0
- package/components/inputs/Checkbox/Checkbox.d.ts +2 -0
- package/components/inputs/Checkbox/Checkbox.js +29 -28
- package/components/inputs/Checkbox/Checkbox.module.scss.js +8 -6
- package/components/inputs/Input/Input.d.ts +33 -1
- package/components/inputs/Input/Input.js +94 -35
- package/components/inputs/Input/Input.module.scss.js +14 -12
- package/components/inputs/Radio/Radio.d.ts +5 -4
- package/components/inputs/Radio/Radio.js +55 -50
- package/components/inputs/Radio/Radio.module.scss.js +12 -10
- package/components/inputs/SearchInput/SearchInput.d.ts +57 -0
- package/components/inputs/SearchInput/SearchInput.js +116 -0
- package/components/inputs/SearchInput/SearchInput.module.scss.js +13 -0
- package/components/inputs/SearchInput/index.d.ts +2 -0
- package/components/inputs/Select/Select.d.ts +52 -0
- package/components/inputs/Select/Select.js +494 -385
- package/components/inputs/Select/Select.module.scss.js +34 -30
- package/components/inputs/Switch/Switch.d.ts +15 -0
- package/components/inputs/Switch/Switch.js +8 -0
- package/components/inputs/Switch/index.d.ts +2 -0
- package/hooks/useDebouncedValue.d.ts +15 -0
- package/hooks/useDebouncedValue.js +15 -0
- package/hooks/useElementSize.d.ts +20 -0
- package/hooks/useElementSize.js +23 -0
- package/hooks/useKeyboard.d.ts +49 -5
- package/hooks/useKeyboard.js +66 -16
- package/hooks/useMediaQuery.d.ts +14 -0
- package/hooks/useMediaQuery.js +16 -0
- package/hooks/useNow.d.ts +14 -0
- package/hooks/useNow.js +35 -0
- package/hooks/usePersistedState.d.ts +30 -0
- package/hooks/usePersistedState.js +96 -0
- package/hooks/useResizable.d.ts +66 -0
- package/hooks/useResizable.js +105 -0
- package/hooks/useTheme.d.ts +33 -0
- package/hooks/useTheme.js +27 -0
- package/index.css +1 -1
- package/index.d.ts +60 -4
- package/index.js +208 -130
- package/index.layered.css +1 -1
- package/llms.txt +659 -25
- package/manifest.json +1797 -76
- package/package.json +1 -1
- package/recipes/confirm-modal.tsx +65 -53
- package/recipes/floating-copy-button.tsx +32 -28
- package/utils/formatBytes.d.ts +21 -0
- package/utils/formatBytes.js +14 -0
- package/utils/index.d.ts +6 -0
- package/utils/themeScript.d.ts +17 -0
- package/utils/themeScript.js +9 -0
- package/utils/timeAgo.d.ts +33 -0
- package/utils/timeAgo.js +37 -0
package/package.json
CHANGED
|
@@ -1,63 +1,75 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { Button, ConfirmDialog, ConfirmProvider, toast, useConfirm } from '@overdoser/react-toolkit';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Confirm prompts without `window.confirm`.
|
|
6
|
+
*
|
|
7
|
+
* Mount `<ConfirmProvider>` once near the root (inside `<ToastProvider>` if
|
|
8
|
+
* you use toasts from `onConfirm`). Anywhere below it, `useConfirm()` returns
|
|
9
|
+
* a function that opens the dialog and resolves `true` / `false`:
|
|
10
|
+
*
|
|
11
|
+
* - `onConfirm` may be async: the confirm button shows `loading`, Cancel /
|
|
12
|
+
* Escape / backdrop are locked, and a rejection is shown inside the dialog
|
|
13
|
+
* (the promise stays pending until the user confirms successfully or cancels).
|
|
14
|
+
* - `danger` flips the confirm button to the danger variant and puts initial
|
|
15
|
+
* focus on Cancel.
|
|
16
|
+
* - `requireText` adds a type-to-confirm field.
|
|
17
|
+
*
|
|
18
|
+
* For a flow that needs its own state (multi-step, custom body), use the
|
|
19
|
+
* controlled `<ConfirmDialog>` directly — see `ControlledExample`.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export function AppRoot({ children }: { children: React.ReactNode }) {
|
|
23
|
+
return <ConfirmProvider defaults={{ cancelLabel: 'Keep' }}>{children}</ConfirmProvider>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface DeleteProjectButtonProps {
|
|
27
|
+
project: { id: string; name: string; slug: string };
|
|
28
|
+
remove: (id: string) => Promise<void>;
|
|
13
29
|
}
|
|
14
30
|
|
|
15
|
-
export function
|
|
16
|
-
|
|
17
|
-
title,
|
|
18
|
-
message,
|
|
19
|
-
confirmLabel = 'Confirm',
|
|
20
|
-
cancelLabel = 'Cancel',
|
|
21
|
-
destructive = false,
|
|
22
|
-
onConfirm,
|
|
23
|
-
onClose,
|
|
24
|
-
}: ConfirmModalProps) {
|
|
25
|
-
const [busy, setBusy] = useState(false);
|
|
31
|
+
export function DeleteProjectButton({ project, remove }: DeleteProjectButtonProps) {
|
|
32
|
+
const confirm = useConfirm();
|
|
26
33
|
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
const handleClick = async () => {
|
|
35
|
+
const ok = await confirm({
|
|
36
|
+
title: `Delete “${project.name}”?`,
|
|
37
|
+
description: 'All environments and deploy history go with it. This cannot be undone.',
|
|
38
|
+
danger: true,
|
|
39
|
+
confirmLabel: 'Delete project',
|
|
40
|
+
requireText: project.slug,
|
|
41
|
+
// Awaited by the dialog; a thrown error is displayed inline.
|
|
42
|
+
onConfirm: () => remove(project.id),
|
|
43
|
+
});
|
|
44
|
+
if (ok) toast({ variant: 'success', title: 'Project deleted' });
|
|
35
45
|
};
|
|
36
46
|
|
|
37
47
|
return (
|
|
38
|
-
<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
<
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
48
|
+
<Button variant="danger" onClick={handleClick}>
|
|
49
|
+
Delete
|
|
50
|
+
</Button>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Controlled variant for custom flows. */
|
|
55
|
+
export function ControlledExample({ onArchive }: { onArchive: () => Promise<void> }) {
|
|
56
|
+
const [open, setOpen] = useState(false);
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
<Button variant="secondary" onClick={() => setOpen(true)}>
|
|
60
|
+
Archive
|
|
61
|
+
</Button>
|
|
62
|
+
<ConfirmDialog
|
|
63
|
+
open={open}
|
|
64
|
+
title="Archive this workspace?"
|
|
65
|
+
description="Members lose access until it is restored."
|
|
66
|
+
confirmLabel="Archive"
|
|
67
|
+
onConfirm={async () => {
|
|
68
|
+
await onArchive();
|
|
69
|
+
setOpen(false);
|
|
70
|
+
}}
|
|
71
|
+
onCancel={() => setOpen(false)}
|
|
72
|
+
/>
|
|
73
|
+
</>
|
|
62
74
|
);
|
|
63
75
|
}
|
|
@@ -1,47 +1,43 @@
|
|
|
1
|
-
import { CopyButton } from '@overdoser/react-toolkit';
|
|
1
|
+
import { CopyButton, CodeBlock } from '@overdoser/react-toolkit';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Copy button floating over
|
|
4
|
+
* Copy button floating over content.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* For **code**, don't build this — `<CodeBlock>` ships the floating copy
|
|
7
|
+
* button (plus header / line numbers / wrap). This recipe is the generic
|
|
8
|
+
* version for any other box: a JSON preview, a generated URL, a log excerpt.
|
|
9
|
+
*
|
|
10
|
+
* The two things it encodes:
|
|
7
11
|
*
|
|
8
12
|
* 1. **Solid wrapper surface.** The `secondary` and `ghost` variants are
|
|
9
13
|
* transparent at rest by design, so a button floated over content would
|
|
10
|
-
* let the
|
|
11
|
-
*
|
|
12
|
-
*
|
|
14
|
+
* let the text run straight under the glyph. Give the *wrapper* a solid
|
|
15
|
+
* `--crk-color-bg` fill (with the button's border radius) — every button
|
|
16
|
+
* state then composites over that surface instead of the content.
|
|
13
17
|
* 2. **`secondary` over `ghost` for floating use.** Ghost has no border in
|
|
14
18
|
* its resting state; secondary's inset outline gives the floating button
|
|
15
|
-
* a visible affordance against the
|
|
16
|
-
*
|
|
17
|
-
* The hover/success fills themselves are opaque (they mix the accent into
|
|
18
|
-
* `--crk-color-bg`), so mid-morph the code never shines through the button.
|
|
19
|
+
* a visible affordance against the box background.
|
|
19
20
|
*/
|
|
20
21
|
|
|
21
|
-
interface
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
interface CopyableBoxProps {
|
|
23
|
+
/** Text the button copies. */
|
|
24
|
+
text: string;
|
|
25
|
+
children: React.ReactNode;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
export function
|
|
28
|
+
export function CopyableBox({ text, children }: CopyableBoxProps) {
|
|
28
29
|
return (
|
|
29
30
|
<div style={{ position: 'relative' }}>
|
|
30
|
-
<
|
|
31
|
+
<div
|
|
31
32
|
style={{
|
|
32
|
-
margin: 0,
|
|
33
33
|
padding: '16px 20px',
|
|
34
|
-
// Keep long lines scrollable; the button stays pinned to the box.
|
|
35
|
-
overflowX: 'auto',
|
|
36
34
|
background: 'var(--crk-color-bg-muted)',
|
|
37
35
|
border: '1px solid var(--crk-color-border-hover)',
|
|
38
36
|
borderRadius: 'var(--crk-border-radius-md)',
|
|
39
|
-
fontSize: 'var(--crk-font-size-sm)',
|
|
40
|
-
lineHeight: 1.6,
|
|
41
37
|
}}
|
|
42
38
|
>
|
|
43
|
-
|
|
44
|
-
</
|
|
39
|
+
{children}
|
|
40
|
+
</div>
|
|
45
41
|
{/* Solid layer under the (transparent-at-rest) button — see note above. */}
|
|
46
42
|
<div
|
|
47
43
|
style={{
|
|
@@ -52,17 +48,25 @@ export function CodeSnippet({ code, language }: CodeSnippetProps) {
|
|
|
52
48
|
borderRadius: 'var(--crk-border-radius-md)',
|
|
53
49
|
}}
|
|
54
50
|
>
|
|
55
|
-
<CopyButton value={
|
|
51
|
+
<CopyButton value={text} variant="secondary" iconOnly aria-label="Copy" />
|
|
56
52
|
</div>
|
|
57
53
|
</div>
|
|
58
54
|
);
|
|
59
55
|
}
|
|
60
56
|
|
|
61
57
|
export function Example() {
|
|
58
|
+
const url = 'https://example.com/invite/8f3a-ab12';
|
|
62
59
|
return (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
<>
|
|
61
|
+
<CopyableBox text={url}>
|
|
62
|
+
Share this link: <strong>{url}</strong>
|
|
63
|
+
</CopyableBox>
|
|
64
|
+
|
|
65
|
+
{/* Code goes through CodeBlock instead. */}
|
|
66
|
+
<CodeBlock
|
|
67
|
+
language="bash"
|
|
68
|
+
code={'npm install @overdoser/react-toolkit\n# then import the theme once:\n# import "@overdoser/react-toolkit/theme.css"'}
|
|
69
|
+
/>
|
|
70
|
+
</>
|
|
67
71
|
);
|
|
68
72
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface FormatBytesOptions {
|
|
2
|
+
/** Fraction digits. @default 1 */
|
|
3
|
+
decimals?: number;
|
|
4
|
+
/** `1024` for binary (KiB-style steps labelled KB), `1000` for decimal. @default 1024 */
|
|
5
|
+
base?: 1000 | 1024;
|
|
6
|
+
/** Use IEC unit labels (KiB, MiB) when `base` is 1024. @default false */
|
|
7
|
+
iec?: boolean;
|
|
8
|
+
/** Locale for number formatting; `undefined` = runtime default. */
|
|
9
|
+
locale?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Human-readable byte count: `formatBytes(1536)` → `"1.5 KB"`.
|
|
13
|
+
* Whole bytes never get decimals (`"512 B"`). Negative values keep their sign.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* formatBytes(0) // "0 B"
|
|
17
|
+
* formatBytes(1_048_576) // "1.0 MB"
|
|
18
|
+
* formatBytes(1_500_000, { base: 1000 }) // "1.5 MB"
|
|
19
|
+
* formatBytes(2048, { iec: true }) // "2.0 KiB"
|
|
20
|
+
*/
|
|
21
|
+
export declare function formatBytes(bytes: number, options?: FormatBytesOptions): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const u = ["B", "KB", "MB", "GB", "TB", "PB", "EB"], g = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
|
|
2
|
+
function f(t, B = {}) {
|
|
3
|
+
const { decimals: c = 1, base: n = 1024, iec: a = !1, locale: m } = B;
|
|
4
|
+
if (!Number.isFinite(t)) return "—";
|
|
5
|
+
const e = n === 1024 && a ? g : u, r = t < 0 ? "-" : "";
|
|
6
|
+
let s = Math.abs(t), i = 0;
|
|
7
|
+
for (; s >= n && i < e.length - 1; )
|
|
8
|
+
s /= n, i++;
|
|
9
|
+
const o = i === 0 ? 0 : Math.max(0, c), l = s.toLocaleString(m, { minimumFractionDigits: o, maximumFractionDigits: o });
|
|
10
|
+
return `${r}${l} ${e[i]}`;
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
f as formatBytes
|
|
14
|
+
};
|
package/utils/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { formatBytes } from './formatBytes';
|
|
2
|
+
export type { FormatBytesOptions } from './formatBytes';
|
|
3
|
+
export { timeAgo } from './timeAgo';
|
|
4
|
+
export type { TimeAgoOptions, TimeAgoUnit } from './timeAgo';
|
|
5
|
+
export { THEME_INIT_SCRIPT, getThemeInitScript } from './themeScript';
|
|
6
|
+
export type { ThemeInitScriptOptions } from './themeScript';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface ThemeInitScriptOptions {
|
|
2
|
+
/** localStorage key `useTheme` persists to. @default 'crk-theme' */
|
|
3
|
+
storageKey?: string;
|
|
4
|
+
/** Attribute set on `<html>`. @default 'data-theme' */
|
|
5
|
+
attribute?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Builds the pre-paint theme script: reads the persisted theme and sets the
|
|
9
|
+
* attribute on `<html>` before React renders, so a dark page never flashes
|
|
10
|
+
* light. Inline it in `<head>` (it has no dependencies):
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* <script dangerouslySetInnerHTML={{ __html: getThemeInitScript() }} />
|
|
14
|
+
*/
|
|
15
|
+
export declare function getThemeInitScript(options?: ThemeInitScriptOptions): string;
|
|
16
|
+
/** `getThemeInitScript()` with the defaults — drop it into `<head>`. */
|
|
17
|
+
export declare const THEME_INIT_SCRIPT: string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function a(t = {}) {
|
|
2
|
+
const e = JSON.stringify(t.storageKey ?? "crk-theme"), r = JSON.stringify(t.attribute ?? "data-theme");
|
|
3
|
+
return "(function(){try{var k=" + e + ",a=" + r + ',d=document.documentElement,v=localStorage.getItem(k);if(v){try{v=JSON.parse(v)}catch(e){}}if(v==="dark"||v==="light"){d.setAttribute(a,v)}else{d.removeAttribute(a)}}catch(e){}})();';
|
|
4
|
+
}
|
|
5
|
+
const c = a();
|
|
6
|
+
export {
|
|
7
|
+
c as THEME_INIT_SCRIPT,
|
|
8
|
+
a as getThemeInitScript
|
|
9
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface TimeAgoOptions {
|
|
2
|
+
/** Reference instant (ms). @default Date.now() */
|
|
3
|
+
now?: number;
|
|
4
|
+
/** BCP 47 locale; `undefined` = runtime default. */
|
|
5
|
+
locale?: string;
|
|
6
|
+
/** `Intl.RelativeTimeFormat` style. @default 'long' */
|
|
7
|
+
format?: 'long' | 'short' | 'narrow';
|
|
8
|
+
/** `'auto'` says "yesterday" / "now"; `'always'` says "1 day ago". @default 'auto' */
|
|
9
|
+
numeric?: 'always' | 'auto';
|
|
10
|
+
/** Beyond this many ms (absolute), return `null` so the caller can show a date instead. */
|
|
11
|
+
maxAge?: number;
|
|
12
|
+
}
|
|
13
|
+
export type TimeAgoUnit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year';
|
|
14
|
+
export declare function toTimestamp(value: Date | number | string): number;
|
|
15
|
+
/** Pick the unit and signed count for a delta (ms, positive = future). */
|
|
16
|
+
export declare function relativeParts(deltaMs: number): {
|
|
17
|
+
unit: TimeAgoUnit;
|
|
18
|
+
value: number;
|
|
19
|
+
};
|
|
20
|
+
/** The tick period that keeps a relative label accurate for this delta. */
|
|
21
|
+
export declare function refreshIntervalFor(deltaMs: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* Relative time text via `Intl.RelativeTimeFormat`:
|
|
24
|
+
* `timeAgo(Date.now() - 3 * 60_000)` → `"3 minutes ago"`.
|
|
25
|
+
*
|
|
26
|
+
* Returns `null` when `maxAge` is set and exceeded (show an absolute date).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* timeAgo(past) // "3 minutes ago"
|
|
30
|
+
* timeAgo(future, { format: 'short' }) // "in 2 hr."
|
|
31
|
+
* timeAgo(yesterday, { numeric: 'auto' }) // "yesterday"
|
|
32
|
+
*/
|
|
33
|
+
export declare function timeAgo(value: Date | number | string, options?: TimeAgoOptions): string | null;
|
package/utils/timeAgo.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const f = [
|
|
2
|
+
{ unit: "year", ms: 31536e6 },
|
|
3
|
+
{ unit: "month", ms: 2592e6 },
|
|
4
|
+
{ unit: "week", ms: 6048e5 },
|
|
5
|
+
{ unit: "day", ms: 864e5 },
|
|
6
|
+
{ unit: "hour", ms: 36e5 },
|
|
7
|
+
{ unit: "minute", ms: 6e4 },
|
|
8
|
+
{ unit: "second", ms: 1e3 }
|
|
9
|
+
];
|
|
10
|
+
function l(t) {
|
|
11
|
+
return t instanceof Date ? t.getTime() : typeof t == "number" ? t : new Date(t).getTime();
|
|
12
|
+
}
|
|
13
|
+
function h(t) {
|
|
14
|
+
const e = Math.abs(t);
|
|
15
|
+
for (const n of f)
|
|
16
|
+
if (e >= n.ms || n.unit === "second")
|
|
17
|
+
return { unit: n.unit, value: Math.sign(t) * Math.round(e / n.ms) };
|
|
18
|
+
return { unit: "second", value: 0 };
|
|
19
|
+
}
|
|
20
|
+
function b(t) {
|
|
21
|
+
const e = Math.abs(t);
|
|
22
|
+
return e < 6e4 ? 1e3 : e < 36e5 ? 3e4 : 6e4;
|
|
23
|
+
}
|
|
24
|
+
function d(t, e = {}) {
|
|
25
|
+
const { now: n = Date.now(), locale: r, format: i = "long", numeric: o = "auto", maxAge: u } = e, s = l(t);
|
|
26
|
+
if (!Number.isFinite(s)) return null;
|
|
27
|
+
const a = s - n;
|
|
28
|
+
if (u !== void 0 && Math.abs(a) > u) return null;
|
|
29
|
+
const { unit: m, value: c } = h(a);
|
|
30
|
+
return m === "second" && Math.abs(c) < 5 && o === "auto" ? new Intl.RelativeTimeFormat(r, { style: i, numeric: "auto" }).format(0, "second") : new Intl.RelativeTimeFormat(r, { style: i, numeric: o }).format(c, m);
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
b as refreshIntervalFor,
|
|
34
|
+
h as relativeParts,
|
|
35
|
+
d as timeAgo,
|
|
36
|
+
l as toTimestamp
|
|
37
|
+
};
|