shelving 1.302.0 → 1.303.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.302.0",
3
+ "version": "1.303.0",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,12 +1,14 @@
1
1
  import type { ReactElement } from "react";
2
2
  import { type PossibleDate } from "../../util/date.js";
3
+ import { type UnitFormatOptions } from "../../util/format.js";
3
4
  import type { OptionalChildProps } from "../util/props.js";
4
5
  /**
5
6
  * Props for `When`, `Ago`, and `Until` — a `target` date plus an optional `current` reference date and `full` toggle.
7
+ * - Unit format options go straight on the props, e.g. `<Ago target={when} unitDisplay="short" />`. `unitDisplay` defaults to `"narrow"` (e.g. `3h`).
6
8
  *
7
9
  * @see https://shelving.cc/ui/WhenProps
8
10
  */
9
- export interface WhenProps extends OptionalChildProps {
11
+ export interface WhenProps extends OptionalChildProps, Omit<UnitFormatOptions, "one" | "many" | "abbr"> {
10
12
  target: PossibleDate | undefined;
11
13
  /** Reference date the relative time is measured against.
12
14
  * @default "now" */
@@ -14,7 +16,7 @@ export interface WhenProps extends OptionalChildProps {
14
16
  full?: boolean | undefined;
15
17
  }
16
18
  /**
17
- * Relative time — shows a signed string like `in 6d` or `3w ago` wrapped in a `<time>` element carrying the machine-readable date.
19
+ * Relative time — shows a signed string like `in 6d`, `3w ago` or `just now` wrapped in a `<time>` element carrying the machine-readable date.
18
20
  *
19
21
  * @throws {RequiredError} If `target` (or `current`) cannot be coerced to a valid date.
20
22
  * @kind component
@@ -30,7 +32,7 @@ export declare function When(props: WhenProps): ReactElement;
30
32
  export interface AgoProps extends WhenProps {
31
33
  }
32
34
  /**
33
- * Elapsed time — shows an unsigned string like `6d` or `3w` for a past date wrapped in a `<time>` element carrying the machine-readable date.
35
+ * Elapsed time — shows an unsigned string like `3m`, `6mo` or `just now` for a past date wrapped in a `<time>` element carrying the machine-readable date.
34
36
  *
35
37
  * @throws {RequiredError} If `target` (or `current`) cannot be coerced to a valid date.
36
38
  * @kind component
package/ui/inline/When.js CHANGED
@@ -2,16 +2,16 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { requireDate } from "../../util/date.js";
3
3
  import { formatAgo, formatUntil, formatWhen, getSecondsAgo } from "../../util/duration.js";
4
4
  import { formatDateTime } from "../../util/format.js";
5
- const _OPTIONS = { unitDisplay: "narrow" };
6
- function _getWhen(formatter, { target: possibleTarget, current: possibleCurrent = "now", full = false, children }, caller) {
5
+ function _getWhen(formatter, { target: possibleTarget, current: possibleCurrent = "now", full = false, children, unitDisplay = "narrow", ...rest }, caller) {
7
6
  const target = requireDate(possibleTarget, caller);
8
7
  const current = requireDate(possibleCurrent, caller);
9
8
  const title = possibleCurrent === "now" ? formatDateTime(target) : `${formatDateTime(target)} to ${formatDateTime(current)}`;
10
- const formatted = children ?? (full ? `${title} (${formatter(target, current, _OPTIONS)})` : formatter(target, current, _OPTIONS));
9
+ const options = { ...rest, unitDisplay };
10
+ const formatted = children ?? (full ? `${title} (${formatter(target, current, options)})` : formatter(target, current, options));
11
11
  return (_jsx("time", { dateTime: possibleCurrent === "now" ? target.toISOString() : `PT${getSecondsAgo(target, current)}S`, title: title, children: formatted }));
12
12
  }
13
13
  /**
14
- * Relative time — shows a signed string like `in 6d` or `3w ago` wrapped in a `<time>` element carrying the machine-readable date.
14
+ * Relative time — shows a signed string like `in 6d`, `3w ago` or `just now` wrapped in a `<time>` element carrying the machine-readable date.
15
15
  *
16
16
  * @throws {RequiredError} If `target` (or `current`) cannot be coerced to a valid date.
17
17
  * @kind component
@@ -22,7 +22,7 @@ export function When(props) {
22
22
  return _getWhen(formatWhen, props, When);
23
23
  }
24
24
  /**
25
- * Elapsed time — shows an unsigned string like `6d` or `3w` for a past date wrapped in a `<time>` element carrying the machine-readable date.
25
+ * Elapsed time — shows an unsigned string like `3m`, `6mo` or `just now` for a past date wrapped in a `<time>` element carrying the machine-readable date.
26
26
  *
27
27
  * @throws {RequiredError} If `target` (or `current`) cannot be coerced to a valid date.
28
28
  * @kind component
@@ -0,0 +1,22 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { renderToStaticMarkup } from "react-dom/server";
3
+ import { Ago, When } from "shelving/ui";
4
+ import { HOUR, MINUTE, SECOND } from "shelving/util/constants";
5
+
6
+ describe("Ago", () => {
7
+ test("shows narrow units by default", () => {
8
+ expect(renderToStaticMarkup(<Ago target={0} current={HOUR * 3} />)).toContain(">3h<");
9
+ });
10
+ test("takes format options as props", () => {
11
+ expect(renderToStaticMarkup(<Ago target={0} current={HOUR * 3} unitDisplay="short" />)).toContain(">3 hr<");
12
+ });
13
+ test("shows just now under a minute", () => {
14
+ expect(renderToStaticMarkup(<Ago target={0} current={SECOND * 5} />)).toContain(">just now<");
15
+ });
16
+ });
17
+
18
+ describe("When", () => {
19
+ test("takes format options as props", () => {
20
+ expect(renderToStaticMarkup(<When target={0} current={MINUTE * 3} unitDisplay="long" />)).toContain(">3 minutes ago<");
21
+ });
22
+ });
@@ -1,21 +1,20 @@
1
1
  import type { ReactElement } from "react";
2
2
  import { type PossibleDate, requireDate } from "../../util/date.js";
3
3
  import { formatAgo, formatUntil, formatWhen, getSecondsAgo } from "../../util/duration.js";
4
- import { formatDateTime } from "../../util/format.js";
4
+ import { formatDateTime, type UnitFormatOptions } from "../../util/format.js";
5
5
  import type { AnyCaller } from "../../util/function.js";
6
6
  import type { OptionalChildProps } from "../util/props.js";
7
7
 
8
- const _OPTIONS: Intl.NumberFormatOptions = { unitDisplay: "narrow" };
9
-
10
8
  function _getWhen(
11
9
  formatter: typeof formatWhen,
12
- { target: possibleTarget, current: possibleCurrent = "now", full = false, children }: WhenProps,
10
+ { target: possibleTarget, current: possibleCurrent = "now", full = false, children, unitDisplay = "narrow", ...rest }: WhenProps,
13
11
  caller: AnyCaller,
14
12
  ): ReactElement {
15
13
  const target = requireDate(possibleTarget, caller);
16
14
  const current = requireDate(possibleCurrent, caller);
17
15
  const title = possibleCurrent === "now" ? formatDateTime(target) : `${formatDateTime(target)} to ${formatDateTime(current)}`;
18
- const formatted = children ?? (full ? `${title} (${formatter(target, current, _OPTIONS)})` : formatter(target, current, _OPTIONS));
16
+ const options: UnitFormatOptions = { ...rest, unitDisplay };
17
+ const formatted = children ?? (full ? `${title} (${formatter(target, current, options)})` : formatter(target, current, options));
19
18
  return (
20
19
  <time dateTime={possibleCurrent === "now" ? target.toISOString() : `PT${getSecondsAgo(target, current)}S`} title={title}>
21
20
  {formatted}
@@ -25,10 +24,11 @@ function _getWhen(
25
24
 
26
25
  /**
27
26
  * Props for `When`, `Ago`, and `Until` — a `target` date plus an optional `current` reference date and `full` toggle.
27
+ * - Unit format options go straight on the props, e.g. `<Ago target={when} unitDisplay="short" />`. `unitDisplay` defaults to `"narrow"` (e.g. `3h`).
28
28
  *
29
29
  * @see https://shelving.cc/ui/WhenProps
30
30
  */
31
- export interface WhenProps extends OptionalChildProps {
31
+ export interface WhenProps extends OptionalChildProps, Omit<UnitFormatOptions, "one" | "many" | "abbr"> {
32
32
  target: PossibleDate | undefined;
33
33
  /** Reference date the relative time is measured against.
34
34
  * @default "now" */
@@ -37,7 +37,7 @@ export interface WhenProps extends OptionalChildProps {
37
37
  }
38
38
 
39
39
  /**
40
- * Relative time — shows a signed string like `in 6d` or `3w ago` wrapped in a `<time>` element carrying the machine-readable date.
40
+ * Relative time — shows a signed string like `in 6d`, `3w ago` or `just now` wrapped in a `<time>` element carrying the machine-readable date.
41
41
  *
42
42
  * @throws {RequiredError} If `target` (or `current`) cannot be coerced to a valid date.
43
43
  * @kind component
@@ -56,7 +56,7 @@ export function When(props: WhenProps): ReactElement {
56
56
  export interface AgoProps extends WhenProps {}
57
57
 
58
58
  /**
59
- * Elapsed time — shows an unsigned string like `6d` or `3w` for a past date wrapped in a `<time>` element carrying the machine-readable date.
59
+ * Elapsed time — shows an unsigned string like `3m`, `6mo` or `just now` for a past date wrapped in a `<time>` element carrying the machine-readable date.
60
60
  *
61
61
  * @throws {RequiredError} If `target` (or `current`) cannot be coerced to a valid date.
62
62
  * @kind component
@@ -102,10 +102,9 @@
102
102
  overflow-wrap: break-word;
103
103
  field-sizing: content;
104
104
 
105
- /* `field-sizing` ignores `rows`, so set the minimum height from `--input-rows` (set inline from the `rows` prop). */
105
+ /* `field-sizing` ignores `rows`, so set the minimum height from `--input-rows` (set inline from the `rows` prop). Never shorter than a single-line input, so a one-row textarea matches a button with an icon. */
106
106
  min-block-size: calc(
107
- var(--input-rows, 2) *
108
- 1lh +
107
+ max(var(--input-icon-size, var(--size-icon)), var(--input-rows, 2) * 1lh) +
109
108
  2 *
110
109
  var(--input-padding, var(--space-small)) +
111
110
  2 *
@@ -310,7 +310,7 @@ export declare function getBestDurationUnit(ms: number): Unit<DurationUnitKey>;
310
310
  /**
311
311
  * Format a compact best-fit description of when a date happens or happened, e.g. `in 10d` or `2h ago` or `in 1w` or `just now`
312
312
  * - See `getBestDurationUnit()` for details on how the best-fit unit is chosen.
313
- * - But: anything under 30 seconds will show `just now`, which makes more sense in most UIs.
313
+ * - But: anything under a minute will show `just now`, which makes more sense in most UIs.
314
314
  *
315
315
  * @param target Date the duration is measured to.
316
316
  * @param current Date the duration is measured from (defaults to now).
@@ -335,14 +335,15 @@ export declare function formatWhen(target: PossibleDate, current?: PossibleDate,
335
335
  */
336
336
  export declare function formatUntil(target: PossibleDate, current?: PossibleDate, options?: UnitFormatOptions, caller?: AnyCaller): string;
337
337
  /**
338
- * Format a compact best-fit description of how long since a date, e.g. `10d` or `2h` or `-1w`
338
+ * Format a compact best-fit description of how long since a date, e.g. `10d` or `2h` or `just now`
339
339
  * - See `getBestDurationUnit()` for details on how the best-fit unit is chosen.
340
+ * - Anything under a minute (either way) shows `just now`.
340
341
  *
341
342
  * @param target Date to count back from.
342
343
  * @param current Date to count to (defaults to now).
343
344
  * @param options Formatting options for the chosen duration unit.
344
345
  * @param caller Function to attribute a thrown error to (defaults to `formatAgo` itself).
345
- * @returns Compact string like `10d` or `2h` (negative if `target` is in the future).
346
+ * @returns Compact string like `10d`, `2h` or `just now` (negative if `target` is in the future).
346
347
  * @throws {RequiredError} If `target` or `current` cannot be converted to a valid date.
347
348
  * @see https://shelving.cc/util/duration/formatAgo
348
349
  */
package/util/duration.js CHANGED
@@ -380,7 +380,7 @@ export function getBestDurationUnit(ms) {
380
380
  /**
381
381
  * Format a compact best-fit description of when a date happens or happened, e.g. `in 10d` or `2h ago` or `in 1w` or `just now`
382
382
  * - See `getBestDurationUnit()` for details on how the best-fit unit is chosen.
383
- * - But: anything under 30 seconds will show `just now`, which makes more sense in most UIs.
383
+ * - But: anything under a minute will show `just now`, which makes more sense in most UIs.
384
384
  *
385
385
  * @param target Date the duration is measured to.
386
386
  * @param current Date the duration is measured from (defaults to now).
@@ -393,10 +393,11 @@ export function getBestDurationUnit(ms) {
393
393
  export function formatWhen(target, current, options, caller = formatWhen) {
394
394
  const ms = getMilliseconds(current, target, caller);
395
395
  const abs = Math.abs(ms);
396
- if (abs < 30 * SECOND)
396
+ if (abs < MINUTE)
397
397
  return "just now";
398
398
  const unit = getBestDurationUnit(ms);
399
- return ms > 0 ? `in ${unit.format(unit.from(abs), options)}` : `${unit.format(unit.from(abs), options)} ago`;
399
+ const str = unit.format(unit.from(abs), options);
400
+ return ms > 0 ? `in ${str}` : `${str} ago`;
400
401
  }
401
402
  /**
402
403
  * Format a compact best-fit description of how long until a date, e.g. `10d` or `2h` or `-1w`
@@ -416,19 +417,22 @@ export function formatUntil(target, current, options, caller = formatUntil) {
416
417
  return unit.format(unit.from(ms), options);
417
418
  }
418
419
  /**
419
- * Format a compact best-fit description of how long since a date, e.g. `10d` or `2h` or `-1w`
420
+ * Format a compact best-fit description of how long since a date, e.g. `10d` or `2h` or `just now`
420
421
  * - See `getBestDurationUnit()` for details on how the best-fit unit is chosen.
422
+ * - Anything under a minute (either way) shows `just now`.
421
423
  *
422
424
  * @param target Date to count back from.
423
425
  * @param current Date to count to (defaults to now).
424
426
  * @param options Formatting options for the chosen duration unit.
425
427
  * @param caller Function to attribute a thrown error to (defaults to `formatAgo` itself).
426
- * @returns Compact string like `10d` or `2h` (negative if `target` is in the future).
428
+ * @returns Compact string like `10d`, `2h` or `just now` (negative if `target` is in the future).
427
429
  * @throws {RequiredError} If `target` or `current` cannot be converted to a valid date.
428
430
  * @see https://shelving.cc/util/duration/formatAgo
429
431
  */
430
432
  export function formatAgo(target, current, options, caller = formatAgo) {
431
433
  const ms = getMilliseconds(target, current, caller);
434
+ if (Math.abs(ms) < MINUTE)
435
+ return "just now";
432
436
  const unit = getBestDurationUnit(ms);
433
437
  return unit.format(unit.from(ms), options);
434
438
  }