shelving 1.302.0 → 1.304.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.304.0",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,25 @@
1
+ import { MediaSchema, type MediaSchemaOptions } from "./MediaSchema.js";
2
+ /**
3
+ * Schema that validates the file name of an image.
4
+ *
5
+ * - The extension must be one of `types` (default `IMAGE_TYPES`).
6
+ *
7
+ * @see https://shelving.cc/schema/ImageSchema
8
+ */
9
+ export declare class ImageSchema extends MediaSchema {
10
+ constructor({ one, title, types, ...options }: MediaSchemaOptions);
11
+ }
12
+ /**
13
+ * Sugar instance of `ImageSchema` for an image file name, e.g. `photo.jpg`. Equivalent to `new ImageSchema({})`.
14
+ *
15
+ * @example IMAGE.validate("photo.jpg"); // Returns "photo.jpg"
16
+ * @see https://shelving.cc/schema/IMAGE
17
+ */
18
+ export declare const IMAGE: ImageSchema;
19
+ /**
20
+ * Sugar instance allowing an `IMAGE` or `null`. Equivalent to `NULLABLE(IMAGE)`.
21
+ *
22
+ * @example NULLABLE_IMAGE.validate(null); // Returns null
23
+ * @see https://shelving.cc/schema/NULLABLE_IMAGE
24
+ */
25
+ export declare const NULLABLE_IMAGE: import("./NullableSchema.js").NullableSchema<string>;
@@ -0,0 +1,29 @@
1
+ import { IMAGE_TYPES } from "../util/file.js";
2
+ import { MediaSchema } from "./MediaSchema.js";
3
+ import { NULLABLE } from "./NullableSchema.js";
4
+ /**
5
+ * Schema that validates the file name of an image.
6
+ *
7
+ * - The extension must be one of `types` (default `IMAGE_TYPES`).
8
+ *
9
+ * @see https://shelving.cc/schema/ImageSchema
10
+ */
11
+ export class ImageSchema extends MediaSchema {
12
+ constructor({ one = "image", title = "Image", types = IMAGE_TYPES, ...options }) {
13
+ super({ one, title, types, ...options });
14
+ }
15
+ }
16
+ /**
17
+ * Sugar instance of `ImageSchema` for an image file name, e.g. `photo.jpg`. Equivalent to `new ImageSchema({})`.
18
+ *
19
+ * @example IMAGE.validate("photo.jpg"); // Returns "photo.jpg"
20
+ * @see https://shelving.cc/schema/IMAGE
21
+ */
22
+ export const IMAGE = new ImageSchema({});
23
+ /**
24
+ * Sugar instance allowing an `IMAGE` or `null`. Equivalent to `NULLABLE(IMAGE)`.
25
+ *
26
+ * @example NULLABLE_IMAGE.validate(null); // Returns null
27
+ * @see https://shelving.cc/schema/NULLABLE_IMAGE
28
+ */
29
+ export const NULLABLE_IMAGE = NULLABLE(IMAGE);
@@ -0,0 +1,38 @@
1
+ import { type FileCapture } from "../util/file.js";
2
+ import { FileSchema, type FileSchemaOptions } from "./FileSchema.js";
3
+ /**
4
+ * Allowed options for `MediaSchema`.
5
+ *
6
+ * @see https://shelving.cc/schema/MediaSchemaOptions
7
+ */
8
+ export interface MediaSchemaOptions extends FileSchemaOptions {
9
+ /** Which camera a file picker for this schema opens straight away, or `undefined` for the normal picker. */
10
+ readonly capture?: FileCapture | undefined;
11
+ }
12
+ /**
13
+ * Schema that validates the file name of an image or a video.
14
+ *
15
+ * - The extension must be one of `types` (default `MEDIA_TYPES`).
16
+ * - `capture` tells a file picker to open the camera straight away.
17
+ *
18
+ * @see https://shelving.cc/schema/MediaSchema
19
+ */
20
+ export declare class MediaSchema extends FileSchema {
21
+ /** Which camera a file picker for this schema opens straight away, or `undefined` for the normal picker. */
22
+ readonly capture: FileCapture | undefined;
23
+ constructor({ one, title, types, capture, ...options }: MediaSchemaOptions);
24
+ }
25
+ /**
26
+ * Sugar instance of `MediaSchema` for an image or video file name, e.g. `photo.jpg`. Equivalent to `new MediaSchema({})`.
27
+ *
28
+ * @example MEDIA.validate("clip.mp4"); // Returns "clip.mp4"
29
+ * @see https://shelving.cc/schema/MEDIA
30
+ */
31
+ export declare const MEDIA: MediaSchema;
32
+ /**
33
+ * Sugar instance allowing a `MEDIA` or `null`. Equivalent to `NULLABLE(MEDIA)`.
34
+ *
35
+ * @example NULLABLE_MEDIA.validate(null); // Returns null
36
+ * @see https://shelving.cc/schema/NULLABLE_MEDIA
37
+ */
38
+ export declare const NULLABLE_MEDIA: import("./NullableSchema.js").NullableSchema<string>;
@@ -0,0 +1,33 @@
1
+ import { MEDIA_TYPES } from "../util/file.js";
2
+ import { FileSchema } from "./FileSchema.js";
3
+ import { NULLABLE } from "./NullableSchema.js";
4
+ /**
5
+ * Schema that validates the file name of an image or a video.
6
+ *
7
+ * - The extension must be one of `types` (default `MEDIA_TYPES`).
8
+ * - `capture` tells a file picker to open the camera straight away.
9
+ *
10
+ * @see https://shelving.cc/schema/MediaSchema
11
+ */
12
+ export class MediaSchema extends FileSchema {
13
+ /** Which camera a file picker for this schema opens straight away, or `undefined` for the normal picker. */
14
+ capture;
15
+ constructor({ one = "media", title = "Media", types = MEDIA_TYPES, capture, ...options }) {
16
+ super({ one, title, types, ...options });
17
+ this.capture = capture;
18
+ }
19
+ }
20
+ /**
21
+ * Sugar instance of `MediaSchema` for an image or video file name, e.g. `photo.jpg`. Equivalent to `new MediaSchema({})`.
22
+ *
23
+ * @example MEDIA.validate("clip.mp4"); // Returns "clip.mp4"
24
+ * @see https://shelving.cc/schema/MEDIA
25
+ */
26
+ export const MEDIA = new MediaSchema({});
27
+ /**
28
+ * Sugar instance allowing a `MEDIA` or `null`. Equivalent to `NULLABLE(MEDIA)`.
29
+ *
30
+ * @example NULLABLE_MEDIA.validate(null); // Returns null
31
+ * @see https://shelving.cc/schema/NULLABLE_MEDIA
32
+ */
33
+ export const NULLABLE_MEDIA = NULLABLE(MEDIA);
@@ -0,0 +1,25 @@
1
+ import { MediaSchema, type MediaSchemaOptions } from "./MediaSchema.js";
2
+ /**
3
+ * Schema that validates the file name of a video.
4
+ *
5
+ * - The extension must be one of `types` (default `VIDEO_TYPES`).
6
+ *
7
+ * @see https://shelving.cc/schema/VideoSchema
8
+ */
9
+ export declare class VideoSchema extends MediaSchema {
10
+ constructor({ one, title, types, ...options }: MediaSchemaOptions);
11
+ }
12
+ /**
13
+ * Sugar instance of `VideoSchema` for a video file name, e.g. `clip.mp4`. Equivalent to `new VideoSchema({})`.
14
+ *
15
+ * @example VIDEO.validate("clip.mp4"); // Returns "clip.mp4"
16
+ * @see https://shelving.cc/schema/VIDEO
17
+ */
18
+ export declare const VIDEO: VideoSchema;
19
+ /**
20
+ * Sugar instance allowing a `VIDEO` or `null`. Equivalent to `NULLABLE(VIDEO)`.
21
+ *
22
+ * @example NULLABLE_VIDEO.validate(null); // Returns null
23
+ * @see https://shelving.cc/schema/NULLABLE_VIDEO
24
+ */
25
+ export declare const NULLABLE_VIDEO: import("./NullableSchema.js").NullableSchema<string>;
@@ -0,0 +1,29 @@
1
+ import { VIDEO_TYPES } from "../util/file.js";
2
+ import { MediaSchema } from "./MediaSchema.js";
3
+ import { NULLABLE } from "./NullableSchema.js";
4
+ /**
5
+ * Schema that validates the file name of a video.
6
+ *
7
+ * - The extension must be one of `types` (default `VIDEO_TYPES`).
8
+ *
9
+ * @see https://shelving.cc/schema/VideoSchema
10
+ */
11
+ export class VideoSchema extends MediaSchema {
12
+ constructor({ one = "video", title = "Video", types = VIDEO_TYPES, ...options }) {
13
+ super({ one, title, types, ...options });
14
+ }
15
+ }
16
+ /**
17
+ * Sugar instance of `VideoSchema` for a video file name, e.g. `clip.mp4`. Equivalent to `new VideoSchema({})`.
18
+ *
19
+ * @example VIDEO.validate("clip.mp4"); // Returns "clip.mp4"
20
+ * @see https://shelving.cc/schema/VIDEO
21
+ */
22
+ export const VIDEO = new VideoSchema({});
23
+ /**
24
+ * Sugar instance allowing a `VIDEO` or `null`. Equivalent to `NULLABLE(VIDEO)`.
25
+ *
26
+ * @example NULLABLE_VIDEO.validate(null); // Returns null
27
+ * @see https://shelving.cc/schema/NULLABLE_VIDEO
28
+ */
29
+ export const NULLABLE_VIDEO = NULLABLE(VIDEO);
package/schema/index.d.ts CHANGED
@@ -13,7 +13,9 @@ export * from "./DictionarySchema.js";
13
13
  export * from "./EmailSchema.js";
14
14
  export * from "./EntitySchema.js";
15
15
  export * from "./FileSchema.js";
16
+ export * from "./ImageSchema.js";
16
17
  export * from "./KeySchema.js";
18
+ export * from "./MediaSchema.js";
17
19
  export * from "./NullableSchema.js";
18
20
  export * from "./NumberSchema.js";
19
21
  export * from "./OptionalSchema.js";
@@ -28,3 +30,4 @@ export * from "./TimeSchema.js";
28
30
  export * from "./URISchema.js";
29
31
  export * from "./URLSchema.js";
30
32
  export * from "./UUIDSchema.js";
33
+ export * from "./VideoSchema.js";
package/schema/index.js CHANGED
@@ -13,7 +13,9 @@ export * from "./DictionarySchema.js";
13
13
  export * from "./EmailSchema.js";
14
14
  export * from "./EntitySchema.js";
15
15
  export * from "./FileSchema.js";
16
+ export * from "./ImageSchema.js";
16
17
  export * from "./KeySchema.js";
18
+ export * from "./MediaSchema.js";
17
19
  export * from "./NullableSchema.js";
18
20
  export * from "./NumberSchema.js";
19
21
  export * from "./OptionalSchema.js";
@@ -28,3 +30,4 @@ export * from "./TimeSchema.js";
28
30
  export * from "./URISchema.js";
29
31
  export * from "./URLSchema.js";
30
32
  export * from "./UUIDSchema.js";
33
+ export * from "./VideoSchema.js";
@@ -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
@@ -1,5 +1,5 @@
1
1
  import type { ReactElement } from "react";
2
- import type { FileTypes } from "../../util/file.js";
2
+ import { type FileCapture, type FileTypes } from "../../util/file.js";
3
3
  import { type InputVariants, type ValueInputProps } from "./Input.js";
4
4
  /**
5
5
  * Props for `FileInput`, an `<input type="file">` that emits the selected `File`.
@@ -7,14 +7,18 @@ import { type InputVariants, type ValueInputProps } from "./Input.js";
7
7
  * @see https://shelving.cc/ui/FileInputProps
8
8
  */
9
9
  export interface FileInputProps extends ValueInputProps<string | File>, InputVariants {
10
+ /** The file types the picker accepts. */
10
11
  types?: FileTypes | undefined;
12
+ /** Which camera the picker opens straight away. On phones this skips the photo library. */
13
+ capture?: FileCapture | undefined;
11
14
  }
12
15
  /**
13
16
  * File picker input that emits the selected `File` through `onValue`.
14
17
  * - The optional `types` dictionary restricts the picker's accepted file types.
18
+ * - The optional `capture` opens the front (`"user"`) or back (`"environment"`) camera straight away.
15
19
  *
16
20
  * @returns A native `<input type="file">` element.
17
21
  * @example <FileInput name="avatar" types={{ png: "image/png" }} onValue={setFile} />
18
22
  * @see https://shelving.cc/ui/FileInput
19
23
  */
20
- export declare function FileInput({ name, title, placeholder, required, disabled, message, onValue, types, className, ...variants }: FileInputProps): ReactElement;
24
+ export declare function FileInput({ name, title, placeholder, required, disabled, message, onValue, types, capture, className, ...variants }: FileInputProps): ReactElement;
@@ -1,9 +1,11 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { getFileAccept } from "../../util/file.js";
2
3
  import { getClass } from "../util/css.js";
3
4
  import { getInputClass } from "./Input.js";
4
5
  /**
5
6
  * File picker input that emits the selected `File` through `onValue`.
6
7
  * - The optional `types` dictionary restricts the picker's accepted file types.
8
+ * - The optional `capture` opens the front (`"user"`) or back (`"environment"`) camera straight away.
7
9
  *
8
10
  * @returns A native `<input type="file">` element.
9
11
  * @example <FileInput name="avatar" types={{ png: "image/png" }} onValue={setFile} />
@@ -11,12 +13,12 @@ import { getInputClass } from "./Input.js";
11
13
  */
12
14
  export function FileInput({ name, title, placeholder = title, required = false, disabled = false, message,
13
15
  // value,
14
- onValue, types = {}, className, ...variants }) {
16
+ onValue, types = {}, capture, className, ...variants }) {
15
17
  const onChange = (e) => {
16
18
  const files = e.currentTarget.files;
17
19
  if (files)
18
20
  for (const file of files)
19
21
  onValue(file);
20
22
  };
21
- return (_jsx("input", { name: name, type: "file", accept: Object.values(types).join(","), required: required, disabled: disabled, placeholder: placeholder, className: getClass(getInputClass(variants), className), onChange: onChange, multiple: false, title: message, "aria-invalid": !!message }));
23
+ return (_jsx("input", { name: name, type: "file", accept: getFileAccept(types), capture: capture, required: required, disabled: disabled, placeholder: placeholder, className: getClass(getInputClass(variants), className), onChange: onChange, multiple: false, title: message, "aria-invalid": !!message }));
22
24
  }
@@ -1,5 +1,5 @@
1
1
  import type { ReactElement, SyntheticEvent } from "react";
2
- import type { FileTypes } from "../../util/file.js";
2
+ import { type FileCapture, type FileTypes, getFileAccept } from "../../util/file.js";
3
3
  import { getClass } from "../util/css.js";
4
4
  import { getInputClass, type InputVariants, type ValueInputProps } from "./Input.js";
5
5
 
@@ -9,12 +9,16 @@ import { getInputClass, type InputVariants, type ValueInputProps } from "./Input
9
9
  * @see https://shelving.cc/ui/FileInputProps
10
10
  */
11
11
  export interface FileInputProps extends ValueInputProps<string | File>, InputVariants {
12
+ /** The file types the picker accepts. */
12
13
  types?: FileTypes | undefined;
14
+ /** Which camera the picker opens straight away. On phones this skips the photo library. */
15
+ capture?: FileCapture | undefined;
13
16
  }
14
17
 
15
18
  /**
16
19
  * File picker input that emits the selected `File` through `onValue`.
17
20
  * - The optional `types` dictionary restricts the picker's accepted file types.
21
+ * - The optional `capture` opens the front (`"user"`) or back (`"environment"`) camera straight away.
18
22
  *
19
23
  * @returns A native `<input type="file">` element.
20
24
  * @example <FileInput name="avatar" types={{ png: "image/png" }} onValue={setFile} />
@@ -30,6 +34,7 @@ export function FileInput({
30
34
  // value,
31
35
  onValue,
32
36
  types = {},
37
+ capture,
33
38
  className,
34
39
  ...variants
35
40
  }: FileInputProps): ReactElement {
@@ -42,7 +47,8 @@ export function FileInput({
42
47
  <input
43
48
  name={name}
44
49
  type="file"
45
- accept={Object.values(types).join(",")}
50
+ accept={getFileAccept(types)}
51
+ capture={capture}
46
52
  required={required}
47
53
  disabled={disabled}
48
54
  placeholder={placeholder}
@@ -1,5 +1,5 @@
1
1
  import { type ReactElement } from "react";
2
- import type { FileTypes } from "../../util/file.js";
2
+ import { type FileCapture, type FileTypes } from "../../util/file.js";
3
3
  import { type BlockVariants } from "../style/Block.js";
4
4
  import type { ValueInputProps } from "./Input.js";
5
5
  /**
@@ -10,9 +10,11 @@ import type { ValueInputProps } from "./Input.js";
10
10
  export interface ImageInputProps extends ValueInputProps<File, string>, BlockVariants {
11
11
  /**
12
12
  * The image types the picker accepts.
13
- * @default { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" }
13
+ * @default IMAGE_TYPES
14
14
  */
15
15
  types?: FileTypes | undefined;
16
+ /** Which camera the picker opens straight away. On phones this skips the photo library. */
17
+ capture?: FileCapture | undefined;
16
18
  }
17
19
  /**
18
20
  * Square image picker that shows the picked image in the square.
@@ -24,4 +26,4 @@ export interface ImageInputProps extends ValueInputProps<File, string>, BlockVar
24
26
  * @kind component
25
27
  * @see https://shelving.cc/ui/ImageInput
26
28
  */
27
- export declare function ImageInput({ name, title, placeholder, required, disabled, message, value, onValue, types, className, ...variants }: ImageInputProps): ReactElement;
29
+ export declare function ImageInput({ name, title, placeholder, required, disabled, message, value, onValue, types, capture, className, ...variants }: ImageInputProps): ReactElement;
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { XMarkIcon } from "@heroicons/react/24/solid";
3
3
  import { useEffect, useMemo } from "react";
4
+ import { getFileAccept, IMAGE_TYPES } from "../../util/file.js";
4
5
  import { getButtonClass } from "../button/Button.js";
5
6
  import { getBlockClass } from "../style/Block.js";
6
7
  import { getClass, getModuleClass } from "../util/css.js";
@@ -11,8 +12,6 @@ const IMAGE_INPUT_PREVIEW_CLASS = getModuleClass(IMAGE_INPUT_CSS, "preview");
11
12
  const IMAGE_INPUT_FILE_CLASS = getModuleClass(IMAGE_INPUT_CSS, "file");
12
13
  const IMAGE_INPUT_CHOOSE_CLASS = getClass(getButtonClass({ solid: true }), getModuleClass(IMAGE_INPUT_CSS, "choose"));
13
14
  const IMAGE_INPUT_CLEAR_CLASS = getClass(getButtonClass({ solid: true, small: true }), getModuleClass(IMAGE_INPUT_CSS, "clear"));
14
- /** The image types the picker accepts by default. */
15
- const IMAGE_TYPES = { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" };
16
15
  /**
17
16
  * Square image picker that shows the picked image in the square.
18
17
  * - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
@@ -23,7 +22,7 @@ const IMAGE_TYPES = { jpg: "image/jpeg", png: "image/png", webp: "image/webp", g
23
22
  * @kind component
24
23
  * @see https://shelving.cc/ui/ImageInput
25
24
  */
26
- export function ImageInput({ name, title = "Image", placeholder = "Choose…", required = false, disabled = false, message, value, onValue, types = IMAGE_TYPES, className, ...variants }) {
25
+ export function ImageInput({ name, title = "Image", placeholder = "Choose…", required = false, disabled = false, message, value, onValue, types = IMAGE_TYPES, capture, className, ...variants }) {
27
26
  const onChange = (e) => {
28
27
  const file = e.currentTarget.files?.[0];
29
28
  if (file)
@@ -36,7 +35,7 @@ export function ImageInput({ name, title = "Image", placeholder = "Choose…", r
36
35
  input.value = "";
37
36
  onValue(undefined);
38
37
  };
39
- return (_jsxs("div", { className: getClass(IMAGE_INPUT_CLASS, getBlockClass(variants), className), children: [_jsxs("label", { className: IMAGE_INPUT_PICKER_CLASS, title: message, "aria-invalid": !!message, children: [value ? _jsx(ImageInputPreview, { value: value }) : _jsx("span", { className: IMAGE_INPUT_CHOOSE_CLASS, children: placeholder }), _jsx("input", { name: name, type: "file", accept: Object.values(types).join(","), required: required && !value, disabled: disabled, "aria-label": title, className: IMAGE_INPUT_FILE_CLASS, onChange: onChange })] }), value && !disabled && (_jsx("button", { type: "button", title: "Clear", className: IMAGE_INPUT_CLEAR_CLASS, onClick: onClear, children: _jsx(XMarkIcon, {}) }))] }));
38
+ return (_jsxs("div", { className: getClass(IMAGE_INPUT_CLASS, getBlockClass(variants), className), children: [_jsxs("label", { className: IMAGE_INPUT_PICKER_CLASS, title: message, "aria-invalid": !!message, children: [value ? _jsx(ImageInputPreview, { value: value }) : _jsx("span", { className: IMAGE_INPUT_CHOOSE_CLASS, children: placeholder }), _jsx("input", { name: name, type: "file", accept: getFileAccept(types), capture: capture, required: required && !value, disabled: disabled, "aria-label": title, className: IMAGE_INPUT_FILE_CLASS, onChange: onChange })] }), value && !disabled && (_jsx("button", { type: "button", title: "Clear", className: IMAGE_INPUT_CLEAR_CLASS, onClick: onClear, children: _jsx(XMarkIcon, {}) }))] }));
40
39
  }
41
40
  /** The image in the square: a URL as is, or a picked `File` through an object URL that is freed when it changes. */
42
41
  function ImageInputPreview({ value }) {
@@ -9,7 +9,8 @@ A large square image picker. With no image, it shows a "Choose…" button in the
9
9
  - `value` is the image to show: a picked `File`, or the URL of the current image (for example, a saved profile photo).
10
10
  - The square is as wide as its container. Put it in a narrow container to make it smaller.
11
11
  - `placeholder` sets the button text (default "Choose…"). `title` labels the input for screen readers.
12
- - `types` sets the image types the picker accepts (default JPEG, PNG, WebP, and GIF).
12
+ - `types` sets the image types the picker accepts (default `IMAGE_TYPES`).
13
+ - `capture` opens the front (`"user"`) or back (`"environment"`) camera straight away. On phones this skips the photo library, so people can only take a new photo. Leave it out to get the normal picker, which on a phone also offers to take a photo.
13
14
 
14
15
  ## Usage
15
16
 
@@ -1,6 +1,6 @@
1
1
  import { XMarkIcon } from "@heroicons/react/24/solid";
2
2
  import { type MouseEvent, type ReactElement, type SyntheticEvent, useEffect, useMemo } from "react";
3
- import type { FileTypes } from "../../util/file.js";
3
+ import { type FileCapture, type FileTypes, getFileAccept, IMAGE_TYPES } from "../../util/file.js";
4
4
  import { getButtonClass } from "../button/Button.js";
5
5
  import { type BlockVariants, getBlockClass } from "../style/Block.js";
6
6
  import { getClass, getModuleClass } from "../util/css.js";
@@ -14,9 +14,6 @@ const IMAGE_INPUT_FILE_CLASS = getModuleClass(IMAGE_INPUT_CSS, "file");
14
14
  const IMAGE_INPUT_CHOOSE_CLASS = getClass(getButtonClass({ solid: true }), getModuleClass(IMAGE_INPUT_CSS, "choose"));
15
15
  const IMAGE_INPUT_CLEAR_CLASS = getClass(getButtonClass({ solid: true, small: true }), getModuleClass(IMAGE_INPUT_CSS, "clear"));
16
16
 
17
- /** The image types the picker accepts by default. */
18
- const IMAGE_TYPES: FileTypes = { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" };
19
-
20
17
  /**
21
18
  * Props for `<ImageInput>`, a square image picker that emits the picked `File`.
22
19
  *
@@ -25,9 +22,11 @@ const IMAGE_TYPES: FileTypes = { jpg: "image/jpeg", png: "image/png", webp: "ima
25
22
  export interface ImageInputProps extends ValueInputProps<File, string>, BlockVariants {
26
23
  /**
27
24
  * The image types the picker accepts.
28
- * @default { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" }
25
+ * @default IMAGE_TYPES
29
26
  */
30
27
  types?: FileTypes | undefined;
28
+ /** Which camera the picker opens straight away. On phones this skips the photo library. */
29
+ capture?: FileCapture | undefined;
31
30
  }
32
31
 
33
32
  /**
@@ -50,6 +49,7 @@ export function ImageInput({
50
49
  value,
51
50
  onValue,
52
51
  types = IMAGE_TYPES,
52
+ capture,
53
53
  className,
54
54
  ...variants
55
55
  }: ImageInputProps): ReactElement {
@@ -70,7 +70,8 @@ export function ImageInput({
70
70
  <input
71
71
  name={name}
72
72
  type="file"
73
- accept={Object.values(types).join(",")}
73
+ accept={getFileAccept(types)}
74
+ capture={capture}
74
75
  required={required && !value}
75
76
  disabled={disabled}
76
77
  aria-label={title}
@@ -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
  }
package/util/file.d.ts CHANGED
@@ -8,6 +8,41 @@ import type { Nullish } from "./null.js";
8
8
  export type FileTypes = {
9
9
  [extension: string]: string;
10
10
  };
11
+ /**
12
+ * Which camera a file picker opens straight away: `"user"` is the front (selfie) camera, `"environment"` the back camera.
13
+ * - Sets the `capture` attribute of `<input type="file">`.
14
+ * - On phones, the picker then opens the camera only, with no photo library.
15
+ *
16
+ * @see https://shelving.cc/util/file/FileCapture
17
+ */
18
+ export type FileCapture = "user" | "environment";
19
+ /**
20
+ * Image file types in `extension: mime` format: JPEG, PNG, WebP, GIF, and AVIF.
21
+ *
22
+ * @see https://shelving.cc/util/file/IMAGE_TYPES
23
+ */
24
+ export declare const IMAGE_TYPES: FileTypes;
25
+ /**
26
+ * Video file types in `extension: mime` format: MP4, QuickTime, and WebM.
27
+ *
28
+ * @see https://shelving.cc/util/file/VIDEO_TYPES
29
+ */
30
+ export declare const VIDEO_TYPES: FileTypes;
31
+ /**
32
+ * Media file types in `extension: mime` format: every type in `IMAGE_TYPES` and `VIDEO_TYPES`.
33
+ *
34
+ * @see https://shelving.cc/util/file/MEDIA_TYPES
35
+ */
36
+ export declare const MEDIA_TYPES: FileTypes;
37
+ /**
38
+ * Get the `accept` attribute of a file input for a set of file types, e.g. `"image/jpeg,image/png"`.
39
+ * - Each MIME type shows once, even when two extensions share it.
40
+ *
41
+ * @param types The allowed file types.
42
+ * @returns A comma-separated list of MIME types.
43
+ * @see https://shelving.cc/util/file/getFileAccept
44
+ */
45
+ export declare function getFileAccept(types: FileTypes): string;
11
46
  /**
12
47
  * Split a filename into its base name and extension in a single pass.
13
48
  * - Extension with no leading dot, e.g. `"ts"`.
package/util/file.js CHANGED
@@ -1,4 +1,45 @@
1
1
  import { RequiredError } from "../error/RequiredError.js";
2
+ /**
3
+ * Image file types in `extension: mime` format: JPEG, PNG, WebP, GIF, and AVIF.
4
+ *
5
+ * @see https://shelving.cc/util/file/IMAGE_TYPES
6
+ */
7
+ export const IMAGE_TYPES = {
8
+ jpg: "image/jpeg",
9
+ jpeg: "image/jpeg",
10
+ png: "image/png",
11
+ webp: "image/webp",
12
+ gif: "image/gif",
13
+ avif: "image/avif",
14
+ };
15
+ /**
16
+ * Video file types in `extension: mime` format: MP4, QuickTime, and WebM.
17
+ *
18
+ * @see https://shelving.cc/util/file/VIDEO_TYPES
19
+ */
20
+ export const VIDEO_TYPES = {
21
+ mp4: "video/mp4",
22
+ m4v: "video/mp4",
23
+ mov: "video/quicktime",
24
+ webm: "video/webm",
25
+ };
26
+ /**
27
+ * Media file types in `extension: mime` format: every type in `IMAGE_TYPES` and `VIDEO_TYPES`.
28
+ *
29
+ * @see https://shelving.cc/util/file/MEDIA_TYPES
30
+ */
31
+ export const MEDIA_TYPES = { ...IMAGE_TYPES, ...VIDEO_TYPES };
32
+ /**
33
+ * Get the `accept` attribute of a file input for a set of file types, e.g. `"image/jpeg,image/png"`.
34
+ * - Each MIME type shows once, even when two extensions share it.
35
+ *
36
+ * @param types The allowed file types.
37
+ * @returns A comma-separated list of MIME types.
38
+ * @see https://shelving.cc/util/file/getFileAccept
39
+ */
40
+ export function getFileAccept(types) {
41
+ return Array.from(new Set(Object.values(types))).join(",");
42
+ }
2
43
  /**
3
44
  * Split a filename into its base name and extension in a single pass.
4
45
  * - Extension with no leading dot, e.g. `"ts"`.