shelving 1.303.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 +1 -1
- package/schema/ImageSchema.d.ts +25 -0
- package/schema/ImageSchema.js +29 -0
- package/schema/MediaSchema.d.ts +38 -0
- package/schema/MediaSchema.js +33 -0
- package/schema/VideoSchema.d.ts +25 -0
- package/schema/VideoSchema.js +29 -0
- package/schema/index.d.ts +3 -0
- package/schema/index.js +3 -0
- package/ui/input/FileInput.d.ts +6 -2
- package/ui/input/FileInput.js +4 -2
- package/ui/input/FileInput.tsx +8 -2
- package/ui/input/ImageInput.d.ts +5 -3
- package/ui/input/ImageInput.js +3 -4
- package/ui/input/ImageInput.md +2 -1
- package/ui/input/ImageInput.tsx +7 -6
- package/util/file.d.ts +35 -0
- package/util/file.js +41 -0
package/package.json
CHANGED
|
@@ -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";
|
package/ui/input/FileInput.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ReactElement } from "react";
|
|
2
|
-
import type
|
|
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;
|
package/ui/input/FileInput.js
CHANGED
|
@@ -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:
|
|
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
|
}
|
package/ui/input/FileInput.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ReactElement, SyntheticEvent } from "react";
|
|
2
|
-
import type
|
|
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={
|
|
50
|
+
accept={getFileAccept(types)}
|
|
51
|
+
capture={capture}
|
|
46
52
|
required={required}
|
|
47
53
|
disabled={disabled}
|
|
48
54
|
placeholder={placeholder}
|
package/ui/input/ImageInput.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ReactElement } from "react";
|
|
2
|
-
import type
|
|
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
|
|
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;
|
package/ui/input/ImageInput.js
CHANGED
|
@@ -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:
|
|
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 }) {
|
package/ui/input/ImageInput.md
CHANGED
|
@@ -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
|
|
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
|
|
package/ui/input/ImageInput.tsx
CHANGED
|
@@ -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
|
|
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
|
|
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={
|
|
73
|
+
accept={getFileAccept(types)}
|
|
74
|
+
capture={capture}
|
|
74
75
|
required={required && !value}
|
|
75
76
|
disabled={disabled}
|
|
76
77
|
aria-label={title}
|
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"`.
|