@webstudio-is/react-sdk 0.7.0 → 0.7.2
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/lib/arg-types/utils.d.ts +4 -18
- package/lib/arg-types/utils.d.ts.map +1 -1
- package/lib/arg-types/utils.js +23 -16
- package/lib/component-utils/image.d.ts +107 -0
- package/lib/component-utils/image.d.ts.map +1 -0
- package/lib/component-utils/image.js +198 -0
- package/lib/component-utils/image.test.d.ts +2 -0
- package/lib/component-utils/image.test.d.ts.map +1 -0
- package/lib/component-utils/image.test.js +132 -0
- package/lib/components/body.props.json +104 -1847
- package/lib/components/body.stories.d.ts +2 -2
- package/lib/components/body.stories.d.ts.map +1 -1
- package/lib/components/body.stories.js +0 -5
- package/lib/components/bold.d.ts +2 -2
- package/lib/components/bold.d.ts.map +1 -1
- package/lib/components/bold.props.json +104 -1847
- package/lib/components/bold.stories.d.ts +3 -3
- package/lib/components/bold.stories.d.ts.map +1 -1
- package/lib/components/bold.stories.js +0 -5
- package/lib/components/box.props.json +108 -1899
- package/lib/components/box.stories.d.ts +5 -5
- package/lib/components/box.stories.d.ts.map +1 -1
- package/lib/components/box.stories.js +0 -5
- package/lib/components/button.d.ts +2 -2
- package/lib/components/button.d.ts.map +1 -1
- package/lib/components/button.props.json +117 -2065
- package/lib/components/button.stories.d.ts +3 -3
- package/lib/components/button.stories.d.ts.map +1 -1
- package/lib/components/button.stories.js +0 -5
- package/lib/components/component-type.d.ts +1 -1
- package/lib/components/component-type.d.ts.map +1 -1
- package/lib/components/form.d.ts +2 -2
- package/lib/components/form.d.ts.map +1 -1
- package/lib/components/form.props.json +112 -1991
- package/lib/components/form.stories.d.ts +3 -3
- package/lib/components/form.stories.d.ts.map +1 -1
- package/lib/components/form.stories.js +0 -5
- package/lib/components/heading.props.json +107 -1893
- package/lib/components/heading.stories.d.ts +5 -5
- package/lib/components/heading.stories.d.ts.map +1 -1
- package/lib/components/heading.stories.js +0 -5
- package/lib/components/image-dev.stories.d.ts +36 -0
- package/lib/components/image-dev.stories.d.ts.map +1 -0
- package/lib/components/image-dev.stories.js +102 -0
- package/lib/components/image.d.ts +10 -2
- package/lib/components/image.d.ts.map +1 -1
- package/lib/components/image.js +19 -9
- package/lib/components/image.props.json +140 -2098
- package/lib/components/image.stories.d.ts +11 -3
- package/lib/components/image.stories.d.ts.map +1 -1
- package/lib/components/image.stories.js +0 -5
- package/lib/components/image.ws.d.ts.map +1 -1
- package/lib/components/image.ws.js +15 -0
- package/lib/components/index.d.ts +3 -0
- package/lib/components/index.d.ts.map +1 -1
- package/lib/components/index.js +7 -1
- package/lib/components/input.d.ts +2 -2
- package/lib/components/input.d.ts.map +1 -1
- package/lib/components/input.props.json +138 -2474
- package/lib/components/input.stories.d.ts +3 -3
- package/lib/components/input.stories.d.ts.map +1 -1
- package/lib/components/input.stories.js +0 -5
- package/lib/components/italic.d.ts +2 -2
- package/lib/components/italic.d.ts.map +1 -1
- package/lib/components/italic.props.json +104 -1847
- package/lib/components/italic.stories.d.ts +3 -3
- package/lib/components/italic.stories.d.ts.map +1 -1
- package/lib/components/italic.stories.js +0 -5
- package/lib/components/link.d.ts +2 -2
- package/lib/components/link.d.ts.map +1 -1
- package/lib/components/link.props.json +116 -2040
- package/lib/components/link.stories.d.ts +5 -5
- package/lib/components/link.stories.d.ts.map +1 -1
- package/lib/components/link.stories.js +0 -5
- package/lib/components/meta.d.ts +7469 -12
- package/lib/components/meta.d.ts.map +1 -1
- package/lib/components/meta.js +34 -25
- package/lib/components/paragraph.d.ts +2 -2
- package/lib/components/paragraph.d.ts.map +1 -1
- package/lib/components/paragraph.props.json +104 -1847
- package/lib/components/paragraph.stories.d.ts +3 -3
- package/lib/components/paragraph.stories.d.ts.map +1 -1
- package/lib/components/paragraph.stories.js +0 -5
- package/lib/components/span.d.ts +3 -0
- package/lib/components/span.d.ts.map +1 -0
- package/lib/components/span.js +8 -0
- package/lib/components/span.props.json +491 -0
- package/lib/components/span.stories.d.ts +6 -0
- package/lib/components/span.stories.d.ts.map +1 -0
- package/lib/components/span.stories.js +14 -0
- package/lib/components/span.ws.d.ts +5 -0
- package/lib/components/span.ws.d.ts.map +1 -0
- package/lib/components/span.ws.js +14 -0
- package/lib/components/subscript.d.ts +3 -0
- package/lib/components/subscript.d.ts.map +1 -0
- package/lib/components/subscript.js +8 -0
- package/lib/components/subscript.props.json +491 -0
- package/lib/components/subscript.stories.d.ts +6 -0
- package/lib/components/subscript.stories.d.ts.map +1 -0
- package/lib/components/subscript.stories.js +14 -0
- package/lib/components/subscript.ws.d.ts +5 -0
- package/lib/components/subscript.ws.d.ts.map +1 -0
- package/lib/components/subscript.ws.js +14 -0
- package/lib/components/superscript.d.ts +3 -0
- package/lib/components/superscript.d.ts.map +1 -0
- package/lib/components/superscript.js +8 -0
- package/lib/components/superscript.props.json +491 -0
- package/lib/components/superscript.stories.d.ts +6 -0
- package/lib/components/superscript.stories.d.ts.map +1 -0
- package/lib/components/superscript.stories.js +14 -0
- package/lib/components/superscript.ws.d.ts +5 -0
- package/lib/components/superscript.ws.d.ts.map +1 -0
- package/lib/components/superscript.ws.js +14 -0
- package/lib/components/text-block.d.ts +2 -2
- package/lib/components/text-block.d.ts.map +1 -1
- package/lib/components/text-block.props.json +104 -1847
- package/lib/components/text-block.stories.d.ts +3 -3
- package/lib/components/text-block.stories.d.ts.map +1 -1
- package/lib/components/text-block.stories.js +0 -5
- package/lib/css/breakpoints.d.ts +1 -1
- package/lib/css/breakpoints.d.ts.map +1 -1
- package/lib/css/categories.d.ts +2 -2
- package/lib/css/get-browser-style.d.ts +1 -1
- package/lib/css/get-browser-style.d.ts.map +1 -1
- package/lib/css/get-browser-style.js +10 -5
- package/lib/css/index.d.ts +0 -5
- package/lib/css/index.d.ts.map +1 -1
- package/lib/css/index.js +0 -5
- package/lib/db/instance.d.ts +1 -1
- package/lib/db/instance.d.ts.map +1 -1
- package/lib/db/instance.js +2 -2
- package/lib/index.d.ts +1 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -3
- package/lib/pubsub/create.d.ts +1 -0
- package/lib/pubsub/create.d.ts.map +1 -1
- package/lib/pubsub/create.js +8 -0
- package/lib/remix/handle-request.server.d.ts.map +1 -1
- package/lib/remix/handle-request.server.js +1 -2
- package/lib/remix/root.d.ts.map +1 -1
- package/lib/remix/root.js +2 -2
- package/lib/tree/create-elements-tree.d.ts +7 -11
- package/lib/tree/create-elements-tree.d.ts.map +1 -1
- package/lib/tree/create-elements-tree.js +5 -10
- package/lib/tree/root.d.ts +4 -6
- package/lib/tree/root.d.ts.map +1 -1
- package/lib/tree/root.js +1 -23
- package/lib/tree/session-storage-polyfill.d.ts +2 -0
- package/lib/tree/session-storage-polyfill.d.ts.map +1 -0
- package/lib/tree/session-storage-polyfill.js +39 -0
- package/lib/tree/wrapper-component.d.ts +4 -4
- package/lib/tree/wrapper-component.d.ts.map +1 -1
- package/lib/tree/wrapper-component.js +5 -5
- package/package.json +21 -13
- package/lib/css/keyword-values.d.ts +0 -302
- package/lib/css/keyword-values.d.ts.map +0 -1
- package/lib/css/keyword-values.js +0 -6619
- package/lib/css/properties.d.ts +0 -3199
- package/lib/css/properties.d.ts.map +0 -1
- package/lib/css/properties.js +0 -3202
- package/lib/css/schema.d.ts +0 -283
- package/lib/css/schema.d.ts.map +0 -1
- package/lib/css/schema.js +0 -48
- package/lib/css/types.d.ts +0 -6
- package/lib/css/types.d.ts.map +0 -1
- package/lib/css/types.js +0 -2
- package/lib/css/units.d.ts +0 -2
- package/lib/css/units.d.ts.map +0 -1
- package/lib/css/units.js +0 -35
- package/lib/remix/constants.d.ts +0 -2
- package/lib/remix/constants.d.ts.map +0 -1
- package/lib/remix/constants.js +0 -4
- package/lib/remix/insert-critical-css.d.ts +0 -2
- package/lib/remix/insert-critical-css.d.ts.map +0 -1
- package/lib/remix/insert-critical-css.js +0 -9
- package/lib/stitches/index.d.ts +0 -3
- package/lib/stitches/index.d.ts.map +0 -1
- package/lib/stitches/index.js +0 -18
- package/lib/stitches/stitches.d.ts +0 -8
- package/lib/stitches/stitches.d.ts.map +0 -1
- package/lib/stitches/stitches.js +0 -38
- package/lib/stitches/to-css.d.ts +0 -8
- package/lib/stitches/to-css.d.ts.map +0 -1
- package/lib/stitches/to-css.js +0 -52
- package/lib/stitches/to-css.test.d.ts +0 -2
- package/lib/stitches/to-css.test.d.ts.map +0 -1
- package/lib/stitches/to-css.test.js +0 -82
package/lib/arg-types/utils.d.ts
CHANGED
|
@@ -1,24 +1,10 @@
|
|
|
1
|
-
import type { ArgTypes } from "@storybook/csf";
|
|
2
1
|
import { PropItem } from "react-docgen-typescript";
|
|
3
2
|
export declare type FilterPredicate = (prop: PropItem) => boolean;
|
|
4
|
-
export declare const propsToArgTypes: (props: Record<string, PropItem>, filter?: FilterPredicate) =>
|
|
5
|
-
export declare const
|
|
6
|
-
|
|
7
|
-
type: string;
|
|
8
|
-
};
|
|
3
|
+
export declare const propsToArgTypes: (props: Record<string, PropItem>, filter?: FilterPredicate) => Record<string, any>;
|
|
4
|
+
export declare const getArgType: (propItem: any) => {
|
|
5
|
+
type: string;
|
|
9
6
|
defaultValue: any;
|
|
10
|
-
options?: undefined;
|
|
11
|
-
} | {
|
|
12
|
-
control: {
|
|
13
|
-
type: string;
|
|
14
|
-
};
|
|
15
|
-
defaultValue?: undefined;
|
|
16
|
-
options?: undefined;
|
|
17
|
-
} | {
|
|
18
|
-
control: {
|
|
19
|
-
type: string;
|
|
20
|
-
};
|
|
21
7
|
options: any;
|
|
22
|
-
|
|
8
|
+
required: any;
|
|
23
9
|
} | null | undefined;
|
|
24
10
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/arg-types/utils.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/arg-types/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAEnD,oBAAY,eAAe,GAAG,CAAC,IAAI,EAAE,QAAQ,KAAK,OAAO,CAAC;AAa1D,eAAO,MAAM,eAAe,UACnB,OAAO,MAAM,EAAE,QAAQ,CAAC,WACtB,eAAe,wBAqBzB,CAAC;AAQF,eAAO,MAAM,UAAU,aAAc,GAAG;;;;;oBAqDvC,CAAC"}
|
package/lib/arg-types/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getArgType = exports.propsToArgTypes = void 0;
|
|
4
4
|
const validAttributes = (prop) => {
|
|
5
5
|
if (prop.parent) {
|
|
6
6
|
// Pass *HTML (both ButtonHTMLAttributes and HTMLAttributes), Aria, and SVG attributes through
|
|
@@ -22,9 +22,12 @@ const propsToArgTypes = (props, filter) => {
|
|
|
22
22
|
if (!filterFn(prop)) {
|
|
23
23
|
return result;
|
|
24
24
|
}
|
|
25
|
-
const
|
|
26
|
-
|
|
25
|
+
const argType = (0, exports.getArgType)(prop);
|
|
26
|
+
if (argType != null) {
|
|
27
|
+
result[propName] = argType;
|
|
28
|
+
}
|
|
27
29
|
return result;
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
31
|
}, {});
|
|
29
32
|
};
|
|
30
33
|
exports.propsToArgTypes = propsToArgTypes;
|
|
@@ -32,42 +35,46 @@ const matchers = {
|
|
|
32
35
|
color: new RegExp("(background|color)", "i"),
|
|
33
36
|
date: /Date$/,
|
|
34
37
|
};
|
|
35
|
-
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
+
const getArgType = (propItem) => {
|
|
36
40
|
const { type, name } = propItem;
|
|
37
41
|
if (!type) {
|
|
38
42
|
return undefined;
|
|
39
43
|
}
|
|
44
|
+
const overrides = {
|
|
45
|
+
defaultValue: propItem.defaultValue?.value ?? null,
|
|
46
|
+
options: propItem.options,
|
|
47
|
+
required: propItem.required,
|
|
48
|
+
};
|
|
40
49
|
// args that end with background or color e.g. iconColor
|
|
41
50
|
if (matchers.color && matchers.color.test(name)) {
|
|
42
51
|
const controlType = propItem.type.name;
|
|
43
52
|
if (controlType === "string") {
|
|
44
|
-
return {
|
|
45
|
-
control: { type: "color" },
|
|
46
|
-
defaultValue: propItem.defaultValue?.value,
|
|
47
|
-
};
|
|
53
|
+
return { ...overrides, type: "color" };
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
56
|
// args that end with date e.g. purchaseDate
|
|
51
57
|
if (matchers.date && matchers.date.test(name)) {
|
|
52
|
-
return {
|
|
58
|
+
return { ...overrides, type: "date" };
|
|
53
59
|
}
|
|
54
60
|
switch (type?.name) {
|
|
55
61
|
case "array":
|
|
56
|
-
return {
|
|
62
|
+
return { ...overrides, type: "object" };
|
|
57
63
|
case "boolean":
|
|
58
64
|
case "Booleanish":
|
|
59
|
-
return {
|
|
65
|
+
return { ...overrides, type: "boolean" };
|
|
60
66
|
case "string":
|
|
61
|
-
return {
|
|
67
|
+
return { ...overrides, type: "text" };
|
|
62
68
|
case "number":
|
|
63
|
-
return {
|
|
69
|
+
return { ...overrides, type: "number" };
|
|
64
70
|
case "enum": {
|
|
65
71
|
const { value } = type;
|
|
66
72
|
// Remove additional quotes from enum values
|
|
67
73
|
// @ts-expect-error Original type has `any` type
|
|
68
74
|
const values = value.map((val) => val.value.replace(/^"(.+)"$/, "$1"));
|
|
69
75
|
return {
|
|
70
|
-
|
|
76
|
+
...overrides,
|
|
77
|
+
type: values?.length <= 5 ? "radio" : "select",
|
|
71
78
|
options: values,
|
|
72
79
|
};
|
|
73
80
|
}
|
|
@@ -75,7 +82,7 @@ const mapControlForType = (propItem) => {
|
|
|
75
82
|
case "symbol":
|
|
76
83
|
return null;
|
|
77
84
|
default:
|
|
78
|
-
return {
|
|
85
|
+
return { ...overrides, type: "text" };
|
|
79
86
|
}
|
|
80
87
|
};
|
|
81
|
-
exports.
|
|
88
|
+
exports.getArgType = getArgType;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # Responsive Image component helpers.
|
|
3
|
+
*
|
|
4
|
+
* ## Quick summary about img srcset and sizes attributes:
|
|
5
|
+
*
|
|
6
|
+
* There are 2 ways to define what image will be loaded in the img property srcset.
|
|
7
|
+
*
|
|
8
|
+
* 1. via pixel density descriptor 'x', like `srcset="photo-small.jpg 1x, photo-medium.jpg 1.5x, photo-huge.jpg 2x"`
|
|
9
|
+
* src will be selected depending on `device-pixel-ratio`.
|
|
10
|
+
*
|
|
11
|
+
* 2. via viewport width descriptor 'w' and sizes property containing source size descriptors, like
|
|
12
|
+
* `srcset="photo-small.jpg 320w, photo-medium.jpg 640w, photo-huge.jpg 1280w"`
|
|
13
|
+
* `sizes="(max-width: 600px) 400px, (max-width: 1200px) 70vw, 50vw"`
|
|
14
|
+
*
|
|
15
|
+
* The browser finds the first matching media query from source size descriptors,
|
|
16
|
+
* then use source size value to generate internally srcset
|
|
17
|
+
* with pixel density descriptors dividing width descriptor value by source size value.
|
|
18
|
+
*
|
|
19
|
+
* Using the example above for viewport width 800px.
|
|
20
|
+
* The first matching media query is (max-width: 1200px)
|
|
21
|
+
* source size value is 70vw equal to 800px * 0,7 = 560px
|
|
22
|
+
*
|
|
23
|
+
* browser internal srcset will be (we divide `w` descriptor by source size value):
|
|
24
|
+
* photo-small.jpg 320/560x, photo-medium.jpg 640/560x, photo-huge.jpg 1280/560x =>
|
|
25
|
+
* photo-small.jpg 0.57x, photo-medium.jpg 1.14x, photo-huge.jpg 2.28x
|
|
26
|
+
*
|
|
27
|
+
* Finally same rules as for pixel density descriptor 'x' are applied.
|
|
28
|
+
*
|
|
29
|
+
* ## Algorithm (without optimizations):
|
|
30
|
+
*
|
|
31
|
+
* We have a predefined array of all supported image sizes allSizes, this is the real width of an image in pixels.
|
|
32
|
+
* This is good for caching, as we can cache image with specific width and then use it for different devices.
|
|
33
|
+
*
|
|
34
|
+
* > allSizes array is a tradeoff between cache and the best possible image size you deliver to the user.
|
|
35
|
+
* > If allSizes.length is too small, you will deliver too big images to the user,
|
|
36
|
+
* > if allSizes.length is too big, you will have many caches misses.
|
|
37
|
+
*
|
|
38
|
+
* If img has a defined width property.
|
|
39
|
+
* 1. find the first value from allSizes which is greater or equal to the width property
|
|
40
|
+
* 2. use found value to generate srcset with pixel density descriptor 'x'
|
|
41
|
+
*
|
|
42
|
+
*
|
|
43
|
+
* If img has no defined width property.
|
|
44
|
+
* 1. Generate srcset = allSizes.map((w) => `${getImageSrcAtWidth(w)} ${w}w`)
|
|
45
|
+
* 2. Use sizes property, or if it is not defined use opinionated DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";
|
|
46
|
+
*
|
|
47
|
+
* Optimizations applied now:
|
|
48
|
+
*
|
|
49
|
+
* - If the sizes property is defined, we can exclude from `srcsets` all images
|
|
50
|
+
* which are smaller than the `smallestRatio * smallesDeviceSize`
|
|
51
|
+
*
|
|
52
|
+
* Future (not implemented) optimizations and improvements:
|
|
53
|
+
*
|
|
54
|
+
* - Knowing image size on different viewport widths we can provide nondefault sizes property
|
|
55
|
+
* - Knowledge of Image aspect-ratio would allow cropping images serverside.
|
|
56
|
+
* - Early hints for high priority images https://blog.cloudflare.com/early-hints/
|
|
57
|
+
* - Slow networks optimizations
|
|
58
|
+
* - 404 etc processing with CSS - https://bitsofco.de/styling-broken-images/ (has some opinionated issues) or js solution with custom user fallback.
|
|
59
|
+
*
|
|
60
|
+
* # Attributions
|
|
61
|
+
*
|
|
62
|
+
* The MIT License (MIT)
|
|
63
|
+
*
|
|
64
|
+
* applies to:
|
|
65
|
+
*
|
|
66
|
+
* - https://github.com/vercel/next.js, Copyright (c) 2022 Vercel, Inc.
|
|
67
|
+
*
|
|
68
|
+
* The MIT License (MIT)
|
|
69
|
+
*
|
|
70
|
+
* Copyright (c) 2022 Vercel, Inc.
|
|
71
|
+
*
|
|
72
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
73
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
74
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
75
|
+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
|
76
|
+
* is furnished to do so, subject to the following conditions:
|
|
77
|
+
*
|
|
78
|
+
* The above copyright notice and this permission notice shall be included in all copies
|
|
79
|
+
* or substantial portions of the Software.
|
|
80
|
+
*
|
|
81
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
82
|
+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
83
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
84
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
85
|
+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
86
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
87
|
+
**/
|
|
88
|
+
export declare type ImageLoader = (props: {
|
|
89
|
+
width: number;
|
|
90
|
+
quality: number;
|
|
91
|
+
src: string;
|
|
92
|
+
}) => string;
|
|
93
|
+
export declare const allSizes: number[];
|
|
94
|
+
export declare const getImageAttributes: (props: {
|
|
95
|
+
src: string | undefined;
|
|
96
|
+
srcSet: string | undefined;
|
|
97
|
+
sizes: string | undefined;
|
|
98
|
+
width: string | number | undefined;
|
|
99
|
+
quality: number | undefined;
|
|
100
|
+
loader?: ImageLoader;
|
|
101
|
+
optimize: boolean;
|
|
102
|
+
}) => {
|
|
103
|
+
src: string;
|
|
104
|
+
srcSet: string | undefined;
|
|
105
|
+
sizes: string | undefined;
|
|
106
|
+
} | null;
|
|
107
|
+
//# sourceMappingURL=image.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/component-utils/image.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAsFI;AAIJ,oBAAY,WAAW,GAAG,CAAC,KAAK,EAAE;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;CACb,KAAK,MAAM,CAAC;AAQb,eAAO,MAAM,QAAQ,UAAkC,CAAC;AA8HxD,eAAO,MAAM,kBAAkB,UAAW;IACxC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;IACxB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IACnC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAC;CACnB,KAAG;IACF,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;CAC3B,GAAG,IA2BH,CAAC"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* # Responsive Image component helpers.
|
|
4
|
+
*
|
|
5
|
+
* ## Quick summary about img srcset and sizes attributes:
|
|
6
|
+
*
|
|
7
|
+
* There are 2 ways to define what image will be loaded in the img property srcset.
|
|
8
|
+
*
|
|
9
|
+
* 1. via pixel density descriptor 'x', like `srcset="photo-small.jpg 1x, photo-medium.jpg 1.5x, photo-huge.jpg 2x"`
|
|
10
|
+
* src will be selected depending on `device-pixel-ratio`.
|
|
11
|
+
*
|
|
12
|
+
* 2. via viewport width descriptor 'w' and sizes property containing source size descriptors, like
|
|
13
|
+
* `srcset="photo-small.jpg 320w, photo-medium.jpg 640w, photo-huge.jpg 1280w"`
|
|
14
|
+
* `sizes="(max-width: 600px) 400px, (max-width: 1200px) 70vw, 50vw"`
|
|
15
|
+
*
|
|
16
|
+
* The browser finds the first matching media query from source size descriptors,
|
|
17
|
+
* then use source size value to generate internally srcset
|
|
18
|
+
* with pixel density descriptors dividing width descriptor value by source size value.
|
|
19
|
+
*
|
|
20
|
+
* Using the example above for viewport width 800px.
|
|
21
|
+
* The first matching media query is (max-width: 1200px)
|
|
22
|
+
* source size value is 70vw equal to 800px * 0,7 = 560px
|
|
23
|
+
*
|
|
24
|
+
* browser internal srcset will be (we divide `w` descriptor by source size value):
|
|
25
|
+
* photo-small.jpg 320/560x, photo-medium.jpg 640/560x, photo-huge.jpg 1280/560x =>
|
|
26
|
+
* photo-small.jpg 0.57x, photo-medium.jpg 1.14x, photo-huge.jpg 2.28x
|
|
27
|
+
*
|
|
28
|
+
* Finally same rules as for pixel density descriptor 'x' are applied.
|
|
29
|
+
*
|
|
30
|
+
* ## Algorithm (without optimizations):
|
|
31
|
+
*
|
|
32
|
+
* We have a predefined array of all supported image sizes allSizes, this is the real width of an image in pixels.
|
|
33
|
+
* This is good for caching, as we can cache image with specific width and then use it for different devices.
|
|
34
|
+
*
|
|
35
|
+
* > allSizes array is a tradeoff between cache and the best possible image size you deliver to the user.
|
|
36
|
+
* > If allSizes.length is too small, you will deliver too big images to the user,
|
|
37
|
+
* > if allSizes.length is too big, you will have many caches misses.
|
|
38
|
+
*
|
|
39
|
+
* If img has a defined width property.
|
|
40
|
+
* 1. find the first value from allSizes which is greater or equal to the width property
|
|
41
|
+
* 2. use found value to generate srcset with pixel density descriptor 'x'
|
|
42
|
+
*
|
|
43
|
+
*
|
|
44
|
+
* If img has no defined width property.
|
|
45
|
+
* 1. Generate srcset = allSizes.map((w) => `${getImageSrcAtWidth(w)} ${w}w`)
|
|
46
|
+
* 2. Use sizes property, or if it is not defined use opinionated DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";
|
|
47
|
+
*
|
|
48
|
+
* Optimizations applied now:
|
|
49
|
+
*
|
|
50
|
+
* - If the sizes property is defined, we can exclude from `srcsets` all images
|
|
51
|
+
* which are smaller than the `smallestRatio * smallesDeviceSize`
|
|
52
|
+
*
|
|
53
|
+
* Future (not implemented) optimizations and improvements:
|
|
54
|
+
*
|
|
55
|
+
* - Knowing image size on different viewport widths we can provide nondefault sizes property
|
|
56
|
+
* - Knowledge of Image aspect-ratio would allow cropping images serverside.
|
|
57
|
+
* - Early hints for high priority images https://blog.cloudflare.com/early-hints/
|
|
58
|
+
* - Slow networks optimizations
|
|
59
|
+
* - 404 etc processing with CSS - https://bitsofco.de/styling-broken-images/ (has some opinionated issues) or js solution with custom user fallback.
|
|
60
|
+
*
|
|
61
|
+
* # Attributions
|
|
62
|
+
*
|
|
63
|
+
* The MIT License (MIT)
|
|
64
|
+
*
|
|
65
|
+
* applies to:
|
|
66
|
+
*
|
|
67
|
+
* - https://github.com/vercel/next.js, Copyright (c) 2022 Vercel, Inc.
|
|
68
|
+
*
|
|
69
|
+
* The MIT License (MIT)
|
|
70
|
+
*
|
|
71
|
+
* Copyright (c) 2022 Vercel, Inc.
|
|
72
|
+
*
|
|
73
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
74
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
75
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
76
|
+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
|
77
|
+
* is furnished to do so, subject to the following conditions:
|
|
78
|
+
*
|
|
79
|
+
* The above copyright notice and this permission notice shall be included in all copies
|
|
80
|
+
* or substantial portions of the Software.
|
|
81
|
+
*
|
|
82
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
83
|
+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
84
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
85
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
86
|
+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
87
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
88
|
+
**/
|
|
89
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
90
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
91
|
+
};
|
|
92
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
93
|
+
exports.getImageAttributes = exports.allSizes = void 0;
|
|
94
|
+
const warn_once_1 = __importDefault(require("warn-once"));
|
|
95
|
+
/**
|
|
96
|
+
* max(...imageSizes) must be less then min(...deviceSizes)
|
|
97
|
+
**/
|
|
98
|
+
const imageSizes = [16, 32, 48, 64, 96, 128, 256, 384];
|
|
99
|
+
const deviceSizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840];
|
|
100
|
+
exports.allSizes = [...imageSizes, ...deviceSizes];
|
|
101
|
+
/**
|
|
102
|
+
* https://github.com/vercel/next.js/blob/canary/packages/next/client/image.tsx
|
|
103
|
+
**/
|
|
104
|
+
const getWidths = (width, sizes) => {
|
|
105
|
+
if (sizes) {
|
|
106
|
+
// Find all the "vw" percent sizes used in the sizes prop
|
|
107
|
+
const viewportWidthRe = /(^|\s)(1?\d?\d)vw/g;
|
|
108
|
+
const percentSizes = [];
|
|
109
|
+
for (let match; (match = viewportWidthRe.exec(sizes)); match) {
|
|
110
|
+
percentSizes.push(parseInt(match[2], 10));
|
|
111
|
+
}
|
|
112
|
+
if (percentSizes.length) {
|
|
113
|
+
// we can exclude from srcSets all images which are smaller than the smallestRatio * smallesDeviceSize
|
|
114
|
+
const smallestRatio = Math.min(...percentSizes) * 0.01;
|
|
115
|
+
return {
|
|
116
|
+
widths: exports.allSizes.filter((s) => s >= deviceSizes[0] * smallestRatio),
|
|
117
|
+
kind: "w",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return { widths: exports.allSizes, kind: "w" };
|
|
121
|
+
}
|
|
122
|
+
if (width == null) {
|
|
123
|
+
return { widths: deviceSizes, kind: "w" };
|
|
124
|
+
}
|
|
125
|
+
const widths = [
|
|
126
|
+
...new Set([width, width * 2].map((w) => exports.allSizes.find((p) => p >= w) || exports.allSizes[exports.allSizes.length - 1])),
|
|
127
|
+
];
|
|
128
|
+
return { widths, kind: "x" };
|
|
129
|
+
};
|
|
130
|
+
const generateImgAttrs = ({ src, width, quality, sizes, loader, }) => {
|
|
131
|
+
const { widths, kind } = getWidths(width, sizes);
|
|
132
|
+
return {
|
|
133
|
+
sizes: !sizes && kind === "w" ? "100vw" : sizes,
|
|
134
|
+
srcSet: widths
|
|
135
|
+
.map((w, i) => `${loader({ src, quality, width: w })} ${kind === "w" ? w : i + 1}${kind}`)
|
|
136
|
+
.join(", "),
|
|
137
|
+
// Must be last, to prevent Safari to load images twice
|
|
138
|
+
src: loader({
|
|
139
|
+
src,
|
|
140
|
+
quality,
|
|
141
|
+
width: widths[widths.length - 1],
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Default image loader in case of no loader provided
|
|
147
|
+
* https://developers.cloudflare.com/images/image-resizing/url-format/
|
|
148
|
+
**/
|
|
149
|
+
const cloudflareImageLoader = ({ width, src, quality }) => {
|
|
150
|
+
if (process.env.NODE_ENV !== "production") {
|
|
151
|
+
(0, warn_once_1.default)(!exports.allSizes.includes(width), "Width must be only from allowed values");
|
|
152
|
+
}
|
|
153
|
+
const options = `width=${width},quality=${quality},format=auto`;
|
|
154
|
+
// Cloudflare docs say that we don't need to urlencode the path params
|
|
155
|
+
const pathname = `/cdn-cgi/image/${options}/${src}`;
|
|
156
|
+
return pathname;
|
|
157
|
+
};
|
|
158
|
+
const getInt = (value) => {
|
|
159
|
+
if (typeof value === "number") {
|
|
160
|
+
return Math.round(value);
|
|
161
|
+
}
|
|
162
|
+
if (typeof value === "string") {
|
|
163
|
+
const vNum = Number.parseFloat(value);
|
|
164
|
+
if (!Number.isNaN(vNum)) {
|
|
165
|
+
return Math.round(vNum);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* DEFAULT_SIZES Just an assumption that most images (except hero and icons) are 100% wide on mobile and 50% on desktop.
|
|
172
|
+
* For icons width are usually set explicitly so DEFAULT_SIZES is not applied.
|
|
173
|
+
* For hero images, we can allow in UI to select sizes=100vw explicitly.
|
|
174
|
+
* Anyway, the best would be to calculate this based on canvas data from different breakpoints.
|
|
175
|
+
* See ../component-utils/image for detailed description
|
|
176
|
+
**/
|
|
177
|
+
const DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";
|
|
178
|
+
const DEFAULT_QUALITY = 80;
|
|
179
|
+
const getImageAttributes = (props) => {
|
|
180
|
+
const width = getInt(props.width);
|
|
181
|
+
const quality = Math.max(Math.min(getInt(props.quality) ?? DEFAULT_QUALITY, 100), 0);
|
|
182
|
+
if (props.src != null && props.src != "") {
|
|
183
|
+
if (props.srcSet == null && props.optimize) {
|
|
184
|
+
const sizes = props.sizes ?? (props.width == null ? DEFAULT_SIZES : undefined);
|
|
185
|
+
const loader = props.loader ?? cloudflareImageLoader;
|
|
186
|
+
return generateImgAttrs({
|
|
187
|
+
src: props.src,
|
|
188
|
+
width,
|
|
189
|
+
quality,
|
|
190
|
+
sizes,
|
|
191
|
+
loader,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
return { src: props.src, srcSet: props.srcSet, sizes: props.sizes };
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
};
|
|
198
|
+
exports.getImageAttributes = getImageAttributes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.test.d.ts","sourceRoot":"","sources":["../../src/component-utils/image.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const image_1 = require("./image");
|
|
4
|
+
describe("Image optimizations applied", () => {
|
|
5
|
+
test("width is number, create pixel density descriptor 'x'", () => {
|
|
6
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
7
|
+
optimize: true,
|
|
8
|
+
width: 100,
|
|
9
|
+
src: "https://webstudio.is/logo.webp",
|
|
10
|
+
srcSet: undefined,
|
|
11
|
+
sizes: undefined,
|
|
12
|
+
quality: 100,
|
|
13
|
+
});
|
|
14
|
+
expect(imgAttr).toMatchInlineSnapshot(`
|
|
15
|
+
Object {
|
|
16
|
+
"sizes": undefined,
|
|
17
|
+
"src": "/cdn-cgi/image/width=256,quality=100,format=auto/https://webstudio.is/logo.webp",
|
|
18
|
+
"srcSet": "/cdn-cgi/image/width=128,quality=100,format=auto/https://webstudio.is/logo.webp 1x, /cdn-cgi/image/width=256,quality=100,format=auto/https://webstudio.is/logo.webp 2x",
|
|
19
|
+
}
|
|
20
|
+
`);
|
|
21
|
+
});
|
|
22
|
+
test("width is undefined, create 'w' descriptor and sizes prop", () => {
|
|
23
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
24
|
+
optimize: true,
|
|
25
|
+
width: undefined,
|
|
26
|
+
src: "https://webstudio.is/logo.webp",
|
|
27
|
+
srcSet: undefined,
|
|
28
|
+
sizes: undefined,
|
|
29
|
+
quality: 90,
|
|
30
|
+
});
|
|
31
|
+
expect(imgAttr).toMatchInlineSnapshot(`
|
|
32
|
+
Object {
|
|
33
|
+
"sizes": "(min-width: 1280px) 50vw, 100vw",
|
|
34
|
+
"src": "/cdn-cgi/image/width=3840,quality=90,format=auto/https://webstudio.is/logo.webp",
|
|
35
|
+
"srcSet": "/cdn-cgi/image/width=384,quality=90,format=auto/https://webstudio.is/logo.webp 384w, /cdn-cgi/image/width=640,quality=90,format=auto/https://webstudio.is/logo.webp 640w, /cdn-cgi/image/width=750,quality=90,format=auto/https://webstudio.is/logo.webp 750w, /cdn-cgi/image/width=828,quality=90,format=auto/https://webstudio.is/logo.webp 828w, /cdn-cgi/image/width=1080,quality=90,format=auto/https://webstudio.is/logo.webp 1080w, /cdn-cgi/image/width=1200,quality=90,format=auto/https://webstudio.is/logo.webp 1200w, /cdn-cgi/image/width=1920,quality=90,format=auto/https://webstudio.is/logo.webp 1920w, /cdn-cgi/image/width=2048,quality=90,format=auto/https://webstudio.is/logo.webp 2048w, /cdn-cgi/image/width=3840,quality=90,format=auto/https://webstudio.is/logo.webp 3840w",
|
|
36
|
+
}
|
|
37
|
+
`);
|
|
38
|
+
});
|
|
39
|
+
test("width is undefined and size defined, creates 'w' descriptor and use input sizes props", () => {
|
|
40
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
41
|
+
optimize: true,
|
|
42
|
+
width: undefined,
|
|
43
|
+
src: "https://webstudio.is/logo.webp",
|
|
44
|
+
srcSet: undefined,
|
|
45
|
+
sizes: "100vw",
|
|
46
|
+
quality: 70,
|
|
47
|
+
});
|
|
48
|
+
expect(imgAttr).toMatchInlineSnapshot(`
|
|
49
|
+
Object {
|
|
50
|
+
"sizes": "100vw",
|
|
51
|
+
"src": "/cdn-cgi/image/width=3840,quality=70,format=auto/https://webstudio.is/logo.webp",
|
|
52
|
+
"srcSet": "/cdn-cgi/image/width=640,quality=70,format=auto/https://webstudio.is/logo.webp 640w, /cdn-cgi/image/width=750,quality=70,format=auto/https://webstudio.is/logo.webp 750w, /cdn-cgi/image/width=828,quality=70,format=auto/https://webstudio.is/logo.webp 828w, /cdn-cgi/image/width=1080,quality=70,format=auto/https://webstudio.is/logo.webp 1080w, /cdn-cgi/image/width=1200,quality=70,format=auto/https://webstudio.is/logo.webp 1200w, /cdn-cgi/image/width=1920,quality=70,format=auto/https://webstudio.is/logo.webp 1920w, /cdn-cgi/image/width=2048,quality=70,format=auto/https://webstudio.is/logo.webp 2048w, /cdn-cgi/image/width=3840,quality=70,format=auto/https://webstudio.is/logo.webp 3840w",
|
|
53
|
+
}
|
|
54
|
+
`);
|
|
55
|
+
});
|
|
56
|
+
test("custom loader", () => {
|
|
57
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
58
|
+
optimize: true,
|
|
59
|
+
width: undefined,
|
|
60
|
+
src: "https://webstudio.is/logo.webp",
|
|
61
|
+
srcSet: undefined,
|
|
62
|
+
sizes: "100vw",
|
|
63
|
+
quality: 70,
|
|
64
|
+
loader: ({ width, src, quality }) => `${new URL(src).pathname}?w=${width}&q=${quality}`,
|
|
65
|
+
});
|
|
66
|
+
expect(imgAttr).toMatchInlineSnapshot(`
|
|
67
|
+
Object {
|
|
68
|
+
"sizes": "100vw",
|
|
69
|
+
"src": "/logo.webp?w=3840&q=70",
|
|
70
|
+
"srcSet": "/logo.webp?w=640&q=70 640w, /logo.webp?w=750&q=70 750w, /logo.webp?w=828&q=70 828w, /logo.webp?w=1080&q=70 1080w, /logo.webp?w=1200&q=70 1200w, /logo.webp?w=1920&q=70 1920w, /logo.webp?w=2048&q=70 2048w, /logo.webp?w=3840&q=70 3840w",
|
|
71
|
+
}
|
|
72
|
+
`);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("Image optimizations not applied", () => {
|
|
76
|
+
test("optimize is false", () => {
|
|
77
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
78
|
+
optimize: false,
|
|
79
|
+
width: 100,
|
|
80
|
+
src: "https://webstudio.is/logo.webp",
|
|
81
|
+
srcSet: undefined,
|
|
82
|
+
sizes: undefined,
|
|
83
|
+
quality: 100,
|
|
84
|
+
});
|
|
85
|
+
expect(imgAttr).toMatchInlineSnapshot(`
|
|
86
|
+
Object {
|
|
87
|
+
"sizes": undefined,
|
|
88
|
+
"src": "https://webstudio.is/logo.webp",
|
|
89
|
+
"srcSet": undefined,
|
|
90
|
+
}
|
|
91
|
+
`);
|
|
92
|
+
});
|
|
93
|
+
test("srcSet is defined", () => {
|
|
94
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
95
|
+
optimize: true,
|
|
96
|
+
width: 100,
|
|
97
|
+
src: "https://webstudio.is/logo.webp",
|
|
98
|
+
srcSet: "user-defined-srcset",
|
|
99
|
+
sizes: undefined,
|
|
100
|
+
quality: 100,
|
|
101
|
+
});
|
|
102
|
+
expect(imgAttr).toMatchInlineSnapshot(`
|
|
103
|
+
Object {
|
|
104
|
+
"sizes": undefined,
|
|
105
|
+
"src": "https://webstudio.is/logo.webp",
|
|
106
|
+
"srcSet": "user-defined-srcset",
|
|
107
|
+
}
|
|
108
|
+
`);
|
|
109
|
+
});
|
|
110
|
+
test("src is empty", () => {
|
|
111
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
112
|
+
optimize: true,
|
|
113
|
+
width: 100,
|
|
114
|
+
src: "",
|
|
115
|
+
srcSet: undefined,
|
|
116
|
+
sizes: undefined,
|
|
117
|
+
quality: 100,
|
|
118
|
+
});
|
|
119
|
+
expect(imgAttr).toMatchInlineSnapshot(`null`);
|
|
120
|
+
});
|
|
121
|
+
test("src is undefined", () => {
|
|
122
|
+
const imgAttr = (0, image_1.getImageAttributes)({
|
|
123
|
+
optimize: true,
|
|
124
|
+
width: 100,
|
|
125
|
+
src: undefined,
|
|
126
|
+
srcSet: undefined,
|
|
127
|
+
sizes: undefined,
|
|
128
|
+
quality: 100,
|
|
129
|
+
});
|
|
130
|
+
expect(imgAttr).toMatchInlineSnapshot(`null`);
|
|
131
|
+
});
|
|
132
|
+
});
|