shelving 1.300.0 → 1.301.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.300.0",
3
+ "version": "1.301.0",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,27 @@
1
+ import { type ReactElement } from "react";
2
+ import type { FileTypes } from "../../util/file.js";
3
+ import { type BlockVariants } from "../style/Block.js";
4
+ import type { ValueInputProps } from "./Input.js";
5
+ /**
6
+ * Props for `<ImageInput>`, a square image picker that emits the picked `File`.
7
+ *
8
+ * @see https://shelving.cc/ui/ImageInputProps
9
+ */
10
+ export interface ImageInputProps extends ValueInputProps<File, string>, BlockVariants {
11
+ /**
12
+ * The image types the picker accepts.
13
+ * @default { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" }
14
+ */
15
+ types?: FileTypes | undefined;
16
+ }
17
+ /**
18
+ * Square image picker that shows the picked image in the square.
19
+ * - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
20
+ * - With an image, the image fills the square and an X button in the corner clears it (`onValue(undefined)`).
21
+ * - A tap anywhere on the square opens the file picker.
22
+ * - `value` is a picked `File`, or the URL of the current image.
23
+ *
24
+ * @kind component
25
+ * @see https://shelving.cc/ui/ImageInput
26
+ */
27
+ export declare function ImageInput({ name, title, placeholder, required, disabled, message, value, onValue, types, className, ...variants }: ImageInputProps): ReactElement;
@@ -0,0 +1,49 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { XMarkIcon } from "@heroicons/react/24/solid";
3
+ import { useEffect, useMemo } from "react";
4
+ import { getButtonClass } from "../button/Button.js";
5
+ import { getBlockClass } from "../style/Block.js";
6
+ import { getClass, getModuleClass } from "../util/css.js";
7
+ import IMAGE_INPUT_CSS from "./ImageInput.module.css";
8
+ const IMAGE_INPUT_CLASS = getModuleClass(IMAGE_INPUT_CSS, "image-input");
9
+ const IMAGE_INPUT_PICKER_CLASS = getModuleClass(IMAGE_INPUT_CSS, "picker");
10
+ const IMAGE_INPUT_PREVIEW_CLASS = getModuleClass(IMAGE_INPUT_CSS, "preview");
11
+ const IMAGE_INPUT_FILE_CLASS = getModuleClass(IMAGE_INPUT_CSS, "file");
12
+ const IMAGE_INPUT_CHOOSE_CLASS = getClass(getButtonClass({ solid: true }), getModuleClass(IMAGE_INPUT_CSS, "choose"));
13
+ 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
+ /**
17
+ * Square image picker that shows the picked image in the square.
18
+ * - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
19
+ * - With an image, the image fills the square and an X button in the corner clears it (`onValue(undefined)`).
20
+ * - A tap anywhere on the square opens the file picker.
21
+ * - `value` is a picked `File`, or the URL of the current image.
22
+ *
23
+ * @kind component
24
+ * @see https://shelving.cc/ui/ImageInput
25
+ */
26
+ export function ImageInput({ name, title = "Image", placeholder = "Choose…", required = false, disabled = false, message, value, onValue, types = IMAGE_TYPES, className, ...variants }) {
27
+ const onChange = (e) => {
28
+ const file = e.currentTarget.files?.[0];
29
+ if (file)
30
+ onValue(file);
31
+ };
32
+ const onClear = ({ currentTarget }) => {
33
+ // Empty the native input too, so picking the same file again still fires `change`.
34
+ const input = currentTarget.parentElement?.querySelector("input");
35
+ if (input)
36
+ input.value = "";
37
+ onValue(undefined);
38
+ };
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, {}) }))] }));
40
+ }
41
+ /** The image in the square: a URL as is, or a picked `File` through an object URL that is freed when it changes. */
42
+ function ImageInputPreview({ value }) {
43
+ const src = useMemo(() => (typeof value === "string" ? value : URL.createObjectURL(value)), [value]);
44
+ useEffect(() => () => {
45
+ if (typeof value !== "string")
46
+ URL.revokeObjectURL(src);
47
+ }, [value, src]);
48
+ return _jsx("img", { src: src, alt: "", className: IMAGE_INPUT_PREVIEW_CLASS });
49
+ }
@@ -0,0 +1,48 @@
1
+ # ImageInput
2
+
3
+ A large square image picker. With no image, it shows a "Choose…" button in the middle. With an image, the image fills the square and an X button in the corner clears it.
4
+
5
+ ## Things to know
6
+
7
+ - A tap anywhere on the square opens the file picker. On a phone this is the built-in photo picker, which can also take a photo.
8
+ - `onValue` gets the picked `File`, or `undefined` when the X button clears the image.
9
+ - `value` is the image to show: a picked `File`, or the URL of the current image (for example, a saved profile photo).
10
+ - The square is as wide as its container. Put it in a narrow container to make it smaller.
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).
13
+
14
+ ## Usage
15
+
16
+ ```tsx
17
+ import { ImageInput } from "shelving/ui";
18
+
19
+ function PhotoPicker() {
20
+ const [photo, setPhoto] = useState<File>();
21
+ return <ImageInput name="photo" title="Photo" value={photo} onValue={setPhoto} />;
22
+ }
23
+ ```
24
+
25
+ ### Show the current image
26
+
27
+ ```tsx
28
+ import { ImageInput } from "shelving/ui";
29
+
30
+ // Shows the saved photo. A new pick calls `onValue` with the new `File`.
31
+ <ImageInput name="photo" value={user.photoURL} onValue={upload} />
32
+ ```
33
+
34
+ ## Styling
35
+
36
+ `ImageInput` paints from the [tint ladder](/ui/TINT_CLASS). The "Choose…" and X buttons are `<Button>` styles, so `--button-*` hooks also move them.
37
+
38
+ | Variable | Styles | Default |
39
+ |---|---|---|
40
+ | `--image-input-space` | Space above and below the square | `var(--space-small)` (12px) |
41
+ | `--image-input-radius` | Corner radius of the square | `var(--radius-small)` (12px) |
42
+ | `--image-input-background` | Square background when empty | `var(--tint-90)` |
43
+ | `--image-input-preview-background` | Background behind the picked image, seen through a transparent image | `#000` |
44
+ | `--image-input-focus-border` | Outline when the input has focus | `var(--stroke-focus) solid var(--color-focus)` |
45
+ | `--image-input-invalid-border` | Outline when `message` is set | `var(--stroke-focus) solid var(--color-failure)` |
46
+ | `--image-input-clear-inset` | Distance of the X button from the top and end edges | `var(--space-small)` (12px) |
47
+
48
+ Global tokens it reads: `--tint-90`, `--space-small`, `--radius-small`, `--stroke-focus`, `--color-focus`, `--color-failure`.
@@ -0,0 +1,79 @@
1
+ @import url("../style/layers.css");
2
+ @import url("../style/Color.module.css");
3
+ @import url("../style/Radius.module.css");
4
+ @import url("../style/Space.module.css");
5
+ @import url("../style/Stroke.module.css");
6
+ @import url("../style/Tint.module.css");
7
+
8
+ @layer components {
9
+ .image-input {
10
+ /* Box: a square as wide as its container. */
11
+ position: relative;
12
+ box-sizing: border-box;
13
+ inline-size: 100%;
14
+ aspect-ratio: 1;
15
+ margin-block: var(--image-input-space, var(--space-small));
16
+ }
17
+
18
+ /* The square itself: a tap anywhere opens the file picker. */
19
+ .picker {
20
+ position: absolute;
21
+ inset: 0;
22
+ display: flex;
23
+ align-items: center;
24
+ justify-content: center;
25
+ overflow: hidden;
26
+ border-radius: var(--image-input-radius, var(--radius-small));
27
+ background: var(--image-input-background, var(--tint-90));
28
+ cursor: pointer;
29
+
30
+ /* The native file input is not seen, so show its focus ring on the square. */
31
+ &:has(:focus-visible) {
32
+ outline: var(--image-input-focus-border, var(--stroke-focus) solid var(--color-focus));
33
+ }
34
+
35
+ &[aria-invalid="true"] {
36
+ outline: var(--image-input-invalid-border, var(--stroke-focus) solid var(--color-failure));
37
+ }
38
+
39
+ &:has(:disabled) {
40
+ cursor: default;
41
+ opacity: 0.5;
42
+ }
43
+ }
44
+
45
+ /* The picked image, covering the whole square. */
46
+ .preview {
47
+ position: absolute;
48
+ inset: 0;
49
+ inline-size: 100%;
50
+ block-size: 100%;
51
+ object-fit: cover;
52
+
53
+ /* A transparent image shows on black, not on the empty square's tint. */
54
+ background: var(--image-input-preview-background, #000);
55
+ }
56
+
57
+ /* The native file input: still focusable and opened by its label, but not seen. */
58
+ .file {
59
+ position: absolute;
60
+ inline-size: 1px;
61
+ block-size: 1px;
62
+ overflow: hidden;
63
+ clip-path: inset(50%);
64
+ white-space: nowrap;
65
+ }
66
+
67
+ /* The "Choose…" button only shows the label's action, so taps go through to the label. */
68
+ .choose {
69
+ pointer-events: none;
70
+ }
71
+
72
+ /* The X button that clears the image, in the top corner. */
73
+ .clear {
74
+ position: absolute;
75
+ inset-block-start: var(--image-input-clear-inset, var(--space-small));
76
+ inset-inline-end: var(--image-input-clear-inset, var(--space-small));
77
+ margin: 0;
78
+ }
79
+ }
@@ -0,0 +1,100 @@
1
+ import { XMarkIcon } from "@heroicons/react/24/solid";
2
+ import { type MouseEvent, type ReactElement, type SyntheticEvent, useEffect, useMemo } from "react";
3
+ import type { FileTypes } from "../../util/file.js";
4
+ import { getButtonClass } from "../button/Button.js";
5
+ import { type BlockVariants, getBlockClass } from "../style/Block.js";
6
+ import { getClass, getModuleClass } from "../util/css.js";
7
+ import IMAGE_INPUT_CSS from "./ImageInput.module.css";
8
+ import type { ValueInputProps } from "./Input.js";
9
+
10
+ const IMAGE_INPUT_CLASS = getModuleClass(IMAGE_INPUT_CSS, "image-input");
11
+ const IMAGE_INPUT_PICKER_CLASS = getModuleClass(IMAGE_INPUT_CSS, "picker");
12
+ const IMAGE_INPUT_PREVIEW_CLASS = getModuleClass(IMAGE_INPUT_CSS, "preview");
13
+ const IMAGE_INPUT_FILE_CLASS = getModuleClass(IMAGE_INPUT_CSS, "file");
14
+ const IMAGE_INPUT_CHOOSE_CLASS = getClass(getButtonClass({ solid: true }), getModuleClass(IMAGE_INPUT_CSS, "choose"));
15
+ const IMAGE_INPUT_CLEAR_CLASS = getClass(getButtonClass({ solid: true, small: true }), getModuleClass(IMAGE_INPUT_CSS, "clear"));
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
+ /**
21
+ * Props for `<ImageInput>`, a square image picker that emits the picked `File`.
22
+ *
23
+ * @see https://shelving.cc/ui/ImageInputProps
24
+ */
25
+ export interface ImageInputProps extends ValueInputProps<File, string>, BlockVariants {
26
+ /**
27
+ * The image types the picker accepts.
28
+ * @default { jpg: "image/jpeg", png: "image/png", webp: "image/webp", gif: "image/gif" }
29
+ */
30
+ types?: FileTypes | undefined;
31
+ }
32
+
33
+ /**
34
+ * Square image picker that shows the picked image in the square.
35
+ * - With no image, the square shows a "Choose…" button in the middle (change it with `placeholder`).
36
+ * - With an image, the image fills the square and an X button in the corner clears it (`onValue(undefined)`).
37
+ * - A tap anywhere on the square opens the file picker.
38
+ * - `value` is a picked `File`, or the URL of the current image.
39
+ *
40
+ * @kind component
41
+ * @see https://shelving.cc/ui/ImageInput
42
+ */
43
+ export function ImageInput({
44
+ name,
45
+ title = "Image",
46
+ placeholder = "Choose…",
47
+ required = false,
48
+ disabled = false,
49
+ message,
50
+ value,
51
+ onValue,
52
+ types = IMAGE_TYPES,
53
+ className,
54
+ ...variants
55
+ }: ImageInputProps): ReactElement {
56
+ const onChange = (e: SyntheticEvent<HTMLInputElement>) => {
57
+ const file = e.currentTarget.files?.[0];
58
+ if (file) onValue(file);
59
+ };
60
+ const onClear = ({ currentTarget }: MouseEvent<HTMLButtonElement>) => {
61
+ // Empty the native input too, so picking the same file again still fires `change`.
62
+ const input = currentTarget.parentElement?.querySelector("input");
63
+ if (input) input.value = "";
64
+ onValue(undefined);
65
+ };
66
+ return (
67
+ <div className={getClass(IMAGE_INPUT_CLASS, getBlockClass(variants), className)}>
68
+ <label className={IMAGE_INPUT_PICKER_CLASS} title={message} aria-invalid={!!message}>
69
+ {value ? <ImageInputPreview value={value} /> : <span className={IMAGE_INPUT_CHOOSE_CLASS}>{placeholder}</span>}
70
+ <input
71
+ name={name}
72
+ type="file"
73
+ accept={Object.values(types).join(",")}
74
+ required={required && !value}
75
+ disabled={disabled}
76
+ aria-label={title}
77
+ className={IMAGE_INPUT_FILE_CLASS}
78
+ onChange={onChange}
79
+ />
80
+ </label>
81
+ {value && !disabled && (
82
+ <button type="button" title="Clear" className={IMAGE_INPUT_CLEAR_CLASS} onClick={onClear}>
83
+ <XMarkIcon />
84
+ </button>
85
+ )}
86
+ </div>
87
+ );
88
+ }
89
+
90
+ /** The image in the square: a URL as is, or a picked `File` through an object URL that is freed when it changes. */
91
+ function ImageInputPreview({ value }: { readonly value: File | string }): ReactElement {
92
+ const src = useMemo(() => (typeof value === "string" ? value : URL.createObjectURL(value)), [value]);
93
+ useEffect(
94
+ () => () => {
95
+ if (typeof value !== "string") URL.revokeObjectURL(src);
96
+ },
97
+ [value, src],
98
+ );
99
+ return <img src={src} alt="" className={IMAGE_INPUT_PREVIEW_CLASS} />;
100
+ }
@@ -101,7 +101,16 @@
101
101
  white-space: pre-wrap;
102
102
  overflow-wrap: break-word;
103
103
  field-sizing: content;
104
- min-height: calc(1lh * attr(rows number, 2));
104
+
105
+ /* `field-sizing` ignores `rows`, so set the minimum height from `--input-rows` (set inline from the `rows` prop). */
106
+ min-block-size: calc(
107
+ var(--input-rows, 2) *
108
+ 1lh +
109
+ 2 *
110
+ var(--input-padding, var(--space-small)) +
111
+ 2 *
112
+ var(--input-stroke, var(--stroke-normal))
113
+ );
105
114
  }
106
115
  }
107
116
 
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
  import type { ReactElement } from "react";
3
- import { DateInput, FileInput, NumberInput, TextInput } from "shelving/ui";
3
+ import { DateInput, FileInput, ImageInput, NumberInput, TextInput } from "shelving/ui";
4
4
  import { PASSTHROUGH } from "shelving/util/function";
5
5
 
6
6
  /** The props of the rendered element that the tests read. */
@@ -36,3 +36,35 @@ describe("FileInput", () => {
36
36
  expect(values).toEqual([file]);
37
37
  });
38
38
  });
39
+
40
+ describe("ImageInput", () => {
41
+ type _Element = ReactElement<{ children: ReactElement<{ children: ReactElement[] }>[] }>;
42
+ function _parts(element: ReactElement) {
43
+ const [label, clear] = (element as _Element).props.children;
44
+ const [shown, input] = (label as ReactElement<{ children: ReactElement[] }>).props.children;
45
+ return {
46
+ shown: shown as ReactElement<Record<string, unknown>>,
47
+ input: input as ReactElement<Record<string, unknown>>,
48
+ clear: clear as unknown as ReactElement<Record<string, unknown>> | false | undefined,
49
+ };
50
+ }
51
+ test("shows the Choose… button and no clear button when empty", () => {
52
+ const { shown, clear } = _parts(ImageInput({ name: "x", onValue: PASSTHROUGH }));
53
+ expect(shown.type).toBe("span");
54
+ expect(shown.props.children).toBe("Choose…");
55
+ expect(clear).toBeFalsy();
56
+ });
57
+ test("shows the image and a clear button for a URL value", () => {
58
+ const { shown, clear } = _parts(ImageInput({ name: "x", value: "https://x.com/a.jpg", onValue: PASSTHROUGH }));
59
+ expect(shown.props.value).toBe("https://x.com/a.jpg");
60
+ expect(clear).toBeTruthy();
61
+ });
62
+ test("calls onValue with the picked file, and with undefined on clear", () => {
63
+ const values: (File | undefined)[] = [];
64
+ const file = new File(["abc"], "a.jpg");
65
+ const { input, clear } = _parts(ImageInput({ name: "x", value: "https://x.com/a.jpg", onValue: v => void values.push(v) }));
66
+ (input.props.onChange as (e: { currentTarget: { files: File[] } }) => void)({ currentTarget: { files: [file] } });
67
+ ((clear as ReactElement<Record<string, unknown>>).props.onClick as (e: unknown) => void)({ currentTarget: { parentElement: null } });
68
+ expect(values).toEqual([file, undefined]);
69
+ });
70
+ });
@@ -22,7 +22,7 @@ required = false, disabled = false, message = "", value, onValue, input = "text"
22
22
  onValue?.(e.currentTarget.value);
23
23
  _resize(e.currentTarget, rows);
24
24
  };
25
- return (_jsx("textarea", { ref: el => void (el && _resize(el, rows)), name: name, defaultValue: value !== undefined ? formatter(value) : "", minLength: Number.isFinite(min) ? min : 0, maxLength: Number.isFinite(max) ? max : undefined, rows: rows, required: required && min > 0, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), getModuleClass(INPUT_CSS, "multiline"), className), onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
25
+ return (_jsx("textarea", { ref: el => void (el && _resize(el, rows)), name: name, defaultValue: value !== undefined ? formatter(value) : "", minLength: Number.isFinite(min) ? min : 0, maxLength: Number.isFinite(max) ? max : undefined, rows: rows, style: { "--input-rows": rows }, required: required && min > 0, disabled: disabled, placeholder: placeholder || " ", className: getClass(getInputClass(variants), getModuleClass(INPUT_CSS, "text"), getModuleClass(INPUT_CSS, "multiline"), className), onChange: onChange, onBlur: onBlur, title: message, "aria-invalid": !!message }));
26
26
  }
27
27
  const onChange = (e) => {
28
28
  onValue?.(e.currentTarget.value);
@@ -1,4 +1,4 @@
1
- import type { ReactElement, SyntheticEvent } from "react";
1
+ import type { CSSProperties, ReactElement, SyntheticEvent } from "react";
2
2
  import type { StringInputType } from "../../schema/StringSchema.js";
3
3
  import { PASSTHROUGH } from "../../util/function.js";
4
4
  import { getInputClass, type InputVariants, type ValueInputProps } from "../input/Input.js";
@@ -66,6 +66,7 @@ export function TextInput({
66
66
  minLength={Number.isFinite(min) ? min : 0}
67
67
  maxLength={Number.isFinite(max) ? max : undefined}
68
68
  rows={rows}
69
+ style={{ "--input-rows": rows } as CSSProperties}
69
70
  required={required && min > 0}
70
71
  disabled={disabled}
71
72
  placeholder={placeholder || " "}
@@ -8,6 +8,7 @@ export * from "./DataInput.js";
8
8
  export * from "./DateInput.js";
9
9
  export * from "./DictionaryInput.js";
10
10
  export * from "./FileInput.js";
11
+ export * from "./ImageInput.js";
11
12
  export * from "./Input.js";
12
13
  export * from "./NumberInput.js";
13
14
  export * from "./OutputInput.js";
package/ui/input/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./DataInput.js";
8
8
  export * from "./DateInput.js";
9
9
  export * from "./DictionaryInput.js";
10
10
  export * from "./FileInput.js";
11
+ export * from "./ImageInput.js";
11
12
  export * from "./Input.js";
12
13
  export * from "./NumberInput.js";
13
14
  export * from "./OutputInput.js";
package/ui/input/index.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./DataInput.js";
8
8
  export * from "./DateInput.js";
9
9
  export * from "./DictionaryInput.js";
10
10
  export * from "./FileInput.js";
11
+ export * from "./ImageInput.js";
11
12
  export * from "./Input.js";
12
13
  export * from "./NumberInput.js";
13
14
  export * from "./OutputInput.js";