@azerion/bunosaurus-plugin-ideal-image 0.0.0-stage → 4.0.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/README.md +6 -2
- package/lib/index.d.ts +10 -0
- package/lib/index.js +75 -0
- package/lib/theme/IdealImage/index.d.ts +9 -0
- package/lib/theme/IdealImage/index.js +96 -0
- package/lib/theme/IdealImageLegacy/LICENSE +20 -0
- package/lib/theme/IdealImageLegacy/README.md +13 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Download.d.ts +2 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Download.js +14 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Loading.d.ts +2 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Loading.js +14 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Offline.d.ts +2 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Offline.js +14 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Warning.d.ts +2 -0
- package/lib/theme/IdealImageLegacy/components/Icon/Warning.js +14 -0
- package/lib/theme/IdealImageLegacy/components/Icon/index.d.ts +7 -0
- package/lib/theme/IdealImageLegacy/components/Icon/index.js +24 -0
- package/lib/theme/IdealImageLegacy/components/IdealImage/index.d.ts +46 -0
- package/lib/theme/IdealImageLegacy/components/IdealImage/index.js +352 -0
- package/lib/theme/IdealImageLegacy/components/IdealImage/waypoint.d.ts +10 -0
- package/lib/theme/IdealImageLegacy/components/IdealImage/waypoint.js +188 -0
- package/lib/theme/IdealImageLegacy/components/IdealImageWithDefaults/index.d.ts +32 -0
- package/lib/theme/IdealImageLegacy/components/IdealImageWithDefaults/index.js +11 -0
- package/lib/theme/IdealImageLegacy/components/Media/index.d.ts +20 -0
- package/lib/theme/IdealImageLegacy/components/Media/index.js +169 -0
- package/lib/theme/IdealImageLegacy/components/MediaWithDefaults/README.md +89 -0
- package/lib/theme/IdealImageLegacy/components/MediaWithDefaults/index.d.ts +32 -0
- package/lib/theme/IdealImageLegacy/components/MediaWithDefaults/index.js +11 -0
- package/lib/theme/IdealImageLegacy/components/composeStyle.d.ts +23 -0
- package/lib/theme/IdealImageLegacy/components/composeStyle.js +37 -0
- package/lib/theme/IdealImageLegacy/components/constants.d.ts +14 -0
- package/lib/theme/IdealImageLegacy/components/constants.js +24 -0
- package/lib/theme/IdealImageLegacy/components/helpers.d.ts +22 -0
- package/lib/theme/IdealImageLegacy/components/helpers.js +141 -0
- package/lib/theme/IdealImageLegacy/components/icons.d.ts +14 -0
- package/lib/theme/IdealImageLegacy/components/icons.js +16 -0
- package/lib/theme/IdealImageLegacy/components/loaders.d.ts +11 -0
- package/lib/theme/IdealImageLegacy/components/loaders.js +112 -0
- package/lib/theme/IdealImageLegacy/components/theme.d.ts +26 -0
- package/lib/theme/IdealImageLegacy/components/theme.js +26 -0
- package/lib/theme/IdealImageLegacy/components/theme.module.css +35 -0
- package/lib/theme/IdealImageLegacy/components/unfetch.d.ts +8 -0
- package/lib/theme/IdealImageLegacy/components/unfetch.js +74 -0
- package/lib/theme/IdealImageLegacy/index.d.ts +2 -0
- package/lib/theme/IdealImageLegacy/index.js +2 -0
- package/package.json +46 -4
- package/src/index.ts +94 -0
- package/src/plugin-ideal-image.d.ts +199 -0
- package/src/theme/IdealImage/index.tsx +108 -0
- package/src/theme/IdealImageLegacy/LICENSE +20 -0
- package/src/theme/IdealImageLegacy/README.md +13 -0
- package/src/theme/IdealImageLegacy/components/Icon/Download.js +14 -0
- package/src/theme/IdealImageLegacy/components/Icon/Loading.js +14 -0
- package/src/theme/IdealImageLegacy/components/Icon/Offline.js +14 -0
- package/src/theme/IdealImageLegacy/components/Icon/Warning.js +14 -0
- package/src/theme/IdealImageLegacy/components/Icon/index.js +24 -0
- package/src/theme/IdealImageLegacy/components/IdealImage/index.js +352 -0
- package/src/theme/IdealImageLegacy/components/IdealImage/waypoint.tsx +254 -0
- package/src/theme/IdealImageLegacy/components/IdealImageWithDefaults/index.js +11 -0
- package/src/theme/IdealImageLegacy/components/Media/index.js +169 -0
- package/src/theme/IdealImageLegacy/components/MediaWithDefaults/README.md +89 -0
- package/src/theme/IdealImageLegacy/components/MediaWithDefaults/index.js +11 -0
- package/src/theme/IdealImageLegacy/components/composeStyle.js +37 -0
- package/src/theme/IdealImageLegacy/components/constants.js +24 -0
- package/src/theme/IdealImageLegacy/components/helpers.js +141 -0
- package/src/theme/IdealImageLegacy/components/icons.js +16 -0
- package/src/theme/IdealImageLegacy/components/loaders.js +112 -0
- package/src/theme/IdealImageLegacy/components/theme.js +26 -0
- package/src/theme/IdealImageLegacy/components/theme.module.css +35 -0
- package/src/theme/IdealImageLegacy/components/unfetch.js +74 -0
- package/src/theme/IdealImageLegacy/index.tsx +3 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import React, {PureComponent} from 'react';
|
|
2
|
+
// import PropTypes from 'prop-types'
|
|
3
|
+
import compose from '../composeStyle';
|
|
4
|
+
import {icons as defaultIcons} from '../constants';
|
|
5
|
+
|
|
6
|
+
const {load, loading, loaded, error, noicon, offline} = defaultIcons;
|
|
7
|
+
|
|
8
|
+
export default class Media extends PureComponent {
|
|
9
|
+
/*static propTypes = {
|
|
10
|
+
/!** URL of the image *!/
|
|
11
|
+
src: PropTypes.string.isRequired,
|
|
12
|
+
/!** Width of the image in px *!/
|
|
13
|
+
width: PropTypes.number.isRequired,
|
|
14
|
+
/!** Height of the image in px *!/
|
|
15
|
+
height: PropTypes.number.isRequired,
|
|
16
|
+
placeholder: PropTypes.oneOfType([
|
|
17
|
+
PropTypes.shape({
|
|
18
|
+
/!** Solid color placeholder *!/
|
|
19
|
+
color: PropTypes.string.isRequired,
|
|
20
|
+
}),
|
|
21
|
+
PropTypes.shape({
|
|
22
|
+
/!**
|
|
23
|
+
* [Low Quality Image Placeholder](https://github.com/zouhir/lqip)
|
|
24
|
+
* [SVG-Based Image Placeholder](https://github.com/technopagan/sqip)
|
|
25
|
+
* base64 encoded image of low quality
|
|
26
|
+
*!/
|
|
27
|
+
lqip: PropTypes.string.isRequired,
|
|
28
|
+
}),
|
|
29
|
+
]).isRequired,
|
|
30
|
+
/!** display icon *!/
|
|
31
|
+
icon: PropTypes.oneOf([load, loading, loaded, error, noicon, offline])
|
|
32
|
+
.isRequired,
|
|
33
|
+
/!** Map of icons *!/
|
|
34
|
+
icons: PropTypes.object.isRequired,
|
|
35
|
+
/!** theme object - CSS Modules or React styles *!/
|
|
36
|
+
theme: PropTypes.object.isRequired,
|
|
37
|
+
/!** Alternative text *!/
|
|
38
|
+
alt: PropTypes.string,
|
|
39
|
+
/!** Color of the icon *!/
|
|
40
|
+
iconColor: PropTypes.string,
|
|
41
|
+
/!** Size of the icon in px *!/
|
|
42
|
+
iconSize: PropTypes.number,
|
|
43
|
+
/!** React's style attribute for root element of the component *!/
|
|
44
|
+
style: PropTypes.object,
|
|
45
|
+
/!** React's className attribute for root element of the component *!/
|
|
46
|
+
className: PropTypes.string,
|
|
47
|
+
/!** On click handler *!/
|
|
48
|
+
onClick: PropTypes.func,
|
|
49
|
+
/!** callback to get dimensions of the placeholder *!/
|
|
50
|
+
onDimensions: PropTypes.func,
|
|
51
|
+
/!** message to show below the icon *!/
|
|
52
|
+
message: PropTypes.node,
|
|
53
|
+
/!** reference for Waypoint *!/
|
|
54
|
+
innerRef: PropTypes.func,
|
|
55
|
+
/!** noscript image src *!/
|
|
56
|
+
nsSrc: PropTypes.string,
|
|
57
|
+
/!** noscript image srcSet *!/
|
|
58
|
+
nsSrcSet: PropTypes.string,
|
|
59
|
+
}*/
|
|
60
|
+
|
|
61
|
+
static defaultProps = {
|
|
62
|
+
iconColor: '#fff',
|
|
63
|
+
iconSize: 64,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
constructor(props) {
|
|
67
|
+
super(props);
|
|
68
|
+
this.state = {isMounted: false};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
componentDidMount() {
|
|
72
|
+
this.setState({isMounted: true});
|
|
73
|
+
|
|
74
|
+
if (this.props.onDimensions && this.dimensionElement)
|
|
75
|
+
/* Firefox returns 0 for both clientWidth and clientHeight.
|
|
76
|
+
To fix this we can check the parentNode's clientWidth and clientHeight as a fallback. */
|
|
77
|
+
this.props.onDimensions({
|
|
78
|
+
width:
|
|
79
|
+
this.dimensionElement.clientWidth ||
|
|
80
|
+
this.dimensionElement.parentNode.clientWidth,
|
|
81
|
+
height:
|
|
82
|
+
this.dimensionElement.clientHeight ||
|
|
83
|
+
this.dimensionElement.parentNode.clientHeight,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
renderIcon(props) {
|
|
88
|
+
const {icon, icons, iconColor: fill, iconSize: size, theme} = props;
|
|
89
|
+
const iconToRender = icons[icon];
|
|
90
|
+
if (!iconToRender) return null;
|
|
91
|
+
const styleOrClass = compose(
|
|
92
|
+
{width: size + 100, height: size, color: fill},
|
|
93
|
+
theme.icon,
|
|
94
|
+
);
|
|
95
|
+
return React.createElement('div', styleOrClass, [
|
|
96
|
+
React.createElement(iconToRender, {fill, size, key: 'icon'}),
|
|
97
|
+
React.createElement('br', {key: 'br'}),
|
|
98
|
+
this.props.message,
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
renderImage(props) {
|
|
103
|
+
return props.icon === loaded ? (
|
|
104
|
+
<img
|
|
105
|
+
{...compose(props.theme.img)}
|
|
106
|
+
src={props.src}
|
|
107
|
+
alt={props.alt}
|
|
108
|
+
width={props.width}
|
|
109
|
+
height={props.height}
|
|
110
|
+
/>
|
|
111
|
+
) : (
|
|
112
|
+
<svg
|
|
113
|
+
{...compose(props.theme.img)}
|
|
114
|
+
width={props.width}
|
|
115
|
+
height={props.height}
|
|
116
|
+
ref={(ref) => (this.dimensionElement = ref)}
|
|
117
|
+
/>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
renderNoscript(props) {
|
|
122
|
+
// render noscript in ssr + hydration to avoid hydration mismatch error
|
|
123
|
+
return this.state.isMounted ? null : (
|
|
124
|
+
<noscript>
|
|
125
|
+
<img
|
|
126
|
+
{...compose(props.theme.img, props.theme.noscript)}
|
|
127
|
+
src={props.nsSrc}
|
|
128
|
+
srcSet={props.nsSrcSet}
|
|
129
|
+
alt={props.alt}
|
|
130
|
+
width={props.width}
|
|
131
|
+
height={props.height}
|
|
132
|
+
/>
|
|
133
|
+
</noscript>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
render() {
|
|
138
|
+
const props = this.props;
|
|
139
|
+
const {placeholder, theme} = props;
|
|
140
|
+
let background;
|
|
141
|
+
if (props.icon === loaded) {
|
|
142
|
+
background = {};
|
|
143
|
+
} else if (placeholder.lqip) {
|
|
144
|
+
background = {
|
|
145
|
+
backgroundImage: `url("${placeholder.lqip}")`,
|
|
146
|
+
};
|
|
147
|
+
} else {
|
|
148
|
+
background = {
|
|
149
|
+
backgroundColor: placeholder.color,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return (
|
|
153
|
+
<div
|
|
154
|
+
{...compose(
|
|
155
|
+
theme.placeholder,
|
|
156
|
+
background,
|
|
157
|
+
props.style,
|
|
158
|
+
props.className,
|
|
159
|
+
)}
|
|
160
|
+
onClick={this.props.onClick}
|
|
161
|
+
onKeyPress={this.props.onClick}
|
|
162
|
+
ref={this.props.innerRef}>
|
|
163
|
+
{this.renderImage(props)}
|
|
164
|
+
{this.renderNoscript(props)}
|
|
165
|
+
{this.renderIcon(props)}
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
All possible states of the component
|
|
2
|
+
|
|
3
|
+
```js
|
|
4
|
+
const lqip =
|
|
5
|
+
'data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAA4DASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAUG/8QAIRAAAQQDAAEFAAAAAAAAAAAAAQIDBREABAYhEjEyQVH/xAAUAQEAAAAAAAAAAAAAAAAAAAAE/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQACIRH/2gAMAwEAAhEDEQA/AMJ2DG+7Dw0nz8gsx+uyhlxnWdLakOlfzpIF3aRf1WT5t96P5+N1ug9Tu7ZWS8q1gG6B8H2FDz+YxhjUrEOdZ//Z';
|
|
6
|
+
|
|
7
|
+
const sqip =
|
|
8
|
+
"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4774 3024'%3e%3cfilter id='b'%3e%3cfeGaussianBlur stdDeviation='12' /%3e%3c/filter%3e%3cpath fill='%23515a57' d='M0 0h4774v3021H0z'/%3e%3cg filter='url(%23b)' transform='translate(9.3 9.3) scale(18.64844)' fill-opacity='.5'%3e%3cellipse fill='whitefef' rx='1' ry='1' transform='matrix(74.55002 60.89891 -21.7939 26.67923 151.8 104.4)'/%3e%3cellipse fill='black80c' cx='216' cy='49' rx='59' ry='59'/%3e%3cellipse fill='black60e' cx='22' cy='60' rx='46' ry='89'/%3e%3cellipse fill='%23ffebd5' cx='110' cy='66' rx='42' ry='28'/%3e%3cellipse fill='whiteff9' rx='1' ry='1' transform='rotate(33.3 -113.2 392.6) scale(42.337 17.49703)'/%3e%3cellipse fill='%23031f1e' rx='1' ry='1' transform='matrix(163.4651 -64.93326 6.77862 17.06471 111 16.4)'/%3e%3cpath fill='whitefea' d='M66 74l9 39 16-44z'/%3e%3cellipse fill='%23a28364' rx='1' ry='1' transform='rotate(-32.4 253.2 -179) scale(30.79511 43.65381)'/%3e%3cpath fill='%231a232c' d='M40 139l61-57 33 95z'/%3e%3cpath fill='%230a222b' d='M249.8 153.3l-48.1-48 32.5-32.6 48.1 48z'/%3e%3c/g%3e%3c/svg%3e";
|
|
9
|
+
<table>
|
|
10
|
+
<tbody>
|
|
11
|
+
<tr>
|
|
12
|
+
<th align="left" width="100">
|
|
13
|
+
load
|
|
14
|
+
</th>
|
|
15
|
+
<td>
|
|
16
|
+
<MediaWithDefaults
|
|
17
|
+
width={3500}
|
|
18
|
+
height={2095}
|
|
19
|
+
placeholder={{lqip: lqip}}
|
|
20
|
+
src="andre-spieker-238-unsplash.jpg"
|
|
21
|
+
style={{maxWidth: 200}}
|
|
22
|
+
icon={'load'}
|
|
23
|
+
/>
|
|
24
|
+
</td>
|
|
25
|
+
<th align="left" width="100">
|
|
26
|
+
noicon
|
|
27
|
+
</th>
|
|
28
|
+
<td>
|
|
29
|
+
<MediaWithDefaults
|
|
30
|
+
width={3500}
|
|
31
|
+
height={2095}
|
|
32
|
+
placeholder={{lqip: lqip}}
|
|
33
|
+
src="andre-spieker-238-unsplash.jpg"
|
|
34
|
+
style={{maxWidth: 200}}
|
|
35
|
+
icon={'noicon'}
|
|
36
|
+
/>
|
|
37
|
+
</td>
|
|
38
|
+
</tr>
|
|
39
|
+
<tr>
|
|
40
|
+
<th align="left">loading</th>
|
|
41
|
+
<td>
|
|
42
|
+
<MediaWithDefaults
|
|
43
|
+
width={3500}
|
|
44
|
+
height={2095}
|
|
45
|
+
placeholder={{lqip: lqip}}
|
|
46
|
+
src="andre-spieker-238-unsplash.jpg"
|
|
47
|
+
style={{maxWidth: 200}}
|
|
48
|
+
icon={'loading'}
|
|
49
|
+
/>
|
|
50
|
+
</td>
|
|
51
|
+
<th align="left">offline</th>
|
|
52
|
+
<td>
|
|
53
|
+
<MediaWithDefaults
|
|
54
|
+
width={3500}
|
|
55
|
+
height={2095}
|
|
56
|
+
placeholder={{lqip: lqip}}
|
|
57
|
+
src="andre-spieker-238-unsplash.jpg"
|
|
58
|
+
style={{maxWidth: 200}}
|
|
59
|
+
icon={'offline'}
|
|
60
|
+
/>
|
|
61
|
+
</td>
|
|
62
|
+
</tr>
|
|
63
|
+
<tr>
|
|
64
|
+
<th align="left">loaded</th>
|
|
65
|
+
<td>
|
|
66
|
+
<MediaWithDefaults
|
|
67
|
+
width={3500}
|
|
68
|
+
height={2095}
|
|
69
|
+
placeholder={{lqip: lqip}}
|
|
70
|
+
src="andre-spieker-238-unsplash.jpg"
|
|
71
|
+
style={{maxWidth: 200}}
|
|
72
|
+
icon={'loaded'}
|
|
73
|
+
/>
|
|
74
|
+
</td>
|
|
75
|
+
<th align="left">error</th>
|
|
76
|
+
<td>
|
|
77
|
+
<MediaWithDefaults
|
|
78
|
+
width={3500}
|
|
79
|
+
height={2095}
|
|
80
|
+
placeholder={{lqip: lqip}}
|
|
81
|
+
src="andre-spieker-238-unsplash.jpg"
|
|
82
|
+
style={{maxWidth: 200}}
|
|
83
|
+
icon={'error'}
|
|
84
|
+
/>
|
|
85
|
+
</td>
|
|
86
|
+
</tr>
|
|
87
|
+
</tbody>
|
|
88
|
+
</table>;
|
|
89
|
+
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
declare const MediaWithDefaults: ({ icons: iconsProp, theme: themeProp, ...props }: {
|
|
2
|
+
[x: string]: any;
|
|
3
|
+
icons?: {
|
|
4
|
+
[x: string]: ((props: any) => import("react").JSX.Element) | null;
|
|
5
|
+
} | undefined;
|
|
6
|
+
theme?: {
|
|
7
|
+
placeholder: {
|
|
8
|
+
backgroundSize: string;
|
|
9
|
+
backgroundRepeat: string;
|
|
10
|
+
position: string;
|
|
11
|
+
};
|
|
12
|
+
img: {
|
|
13
|
+
width: string;
|
|
14
|
+
height: string;
|
|
15
|
+
maxWidth: string;
|
|
16
|
+
marginBottom: string;
|
|
17
|
+
};
|
|
18
|
+
icon: {
|
|
19
|
+
position: string;
|
|
20
|
+
top: string;
|
|
21
|
+
left: string;
|
|
22
|
+
transform: string;
|
|
23
|
+
textAlign: string;
|
|
24
|
+
};
|
|
25
|
+
noscript: {
|
|
26
|
+
position: string;
|
|
27
|
+
top: number;
|
|
28
|
+
left: number;
|
|
29
|
+
};
|
|
30
|
+
} | undefined;
|
|
31
|
+
}) => import("react").JSX.Element;
|
|
32
|
+
export default MediaWithDefaults;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Media from '../Media';
|
|
2
|
+
import icons from '../icons';
|
|
3
|
+
import theme from '../theme';
|
|
4
|
+
|
|
5
|
+
const MediaWithDefaults = ({
|
|
6
|
+
icons: iconsProp = icons,
|
|
7
|
+
theme: themeProp = theme,
|
|
8
|
+
...props
|
|
9
|
+
}) => <Media {...props} icons={iconsProp} theme={themeProp} />;
|
|
10
|
+
|
|
11
|
+
export default MediaWithDefaults;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default _default;
|
|
2
|
+
/**
|
|
3
|
+
* Composes styles and/or classes
|
|
4
|
+
*
|
|
5
|
+
* For classes it will concat them in in one string
|
|
6
|
+
* and return as `className` property.
|
|
7
|
+
* Alternative is https://github.com/JedWatson/classnames
|
|
8
|
+
*
|
|
9
|
+
* For objects it will merge them in one object
|
|
10
|
+
* and return as `style` property.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* Assume you have `theme` object, which can be css-module
|
|
14
|
+
* or object or other css-in-js compatible with css-module
|
|
15
|
+
*
|
|
16
|
+
* <a {...compose(theme.link, theme.active, {color: "#000"})}>link</a>
|
|
17
|
+
*
|
|
18
|
+
* @returns {{className: string, style: object}} - params for React component
|
|
19
|
+
*/
|
|
20
|
+
declare function _default(...stylesOrClasses: any[]): {
|
|
21
|
+
className: string;
|
|
22
|
+
style: object;
|
|
23
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composes styles and/or classes
|
|
3
|
+
*
|
|
4
|
+
* For classes it will concat them in in one string
|
|
5
|
+
* and return as `className` property.
|
|
6
|
+
* Alternative is https://github.com/JedWatson/classnames
|
|
7
|
+
*
|
|
8
|
+
* For objects it will merge them in one object
|
|
9
|
+
* and return as `style` property.
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* Assume you have `theme` object, which can be css-module
|
|
13
|
+
* or object or other css-in-js compatible with css-module
|
|
14
|
+
*
|
|
15
|
+
* <a {...compose(theme.link, theme.active, {color: "#000"})}>link</a>
|
|
16
|
+
*
|
|
17
|
+
* @returns {{className: string, style: object}} - params for React component
|
|
18
|
+
*/
|
|
19
|
+
export default (...stylesOrClasses) => {
|
|
20
|
+
const classes = [];
|
|
21
|
+
let style;
|
|
22
|
+
for (const obj of stylesOrClasses) {
|
|
23
|
+
if (obj instanceof Object) {
|
|
24
|
+
Object.assign(style || (style = {}), obj);
|
|
25
|
+
} else if (obj === undefined || obj === false) {
|
|
26
|
+
// ignore
|
|
27
|
+
} else if (typeof obj === 'string') {
|
|
28
|
+
classes.push(obj);
|
|
29
|
+
} else {
|
|
30
|
+
throw new Error(`Unexpected value ${obj}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
className: classes.length > 1 ? classes.join(' ') : classes[0],
|
|
35
|
+
style,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const icons: {
|
|
2
|
+
load: string;
|
|
3
|
+
loading: string;
|
|
4
|
+
loaded: string;
|
|
5
|
+
error: string;
|
|
6
|
+
noicon: string;
|
|
7
|
+
offline: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const loadStates: {
|
|
10
|
+
initial: string;
|
|
11
|
+
loading: string;
|
|
12
|
+
loaded: string;
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const load = 'load';
|
|
2
|
+
const loading = 'loading';
|
|
3
|
+
const loaded = 'loaded';
|
|
4
|
+
const error = 'error';
|
|
5
|
+
const noicon = 'noicon';
|
|
6
|
+
const offline = 'offline';
|
|
7
|
+
|
|
8
|
+
export const icons = {
|
|
9
|
+
load,
|
|
10
|
+
loading,
|
|
11
|
+
loaded,
|
|
12
|
+
error,
|
|
13
|
+
noicon,
|
|
14
|
+
offline,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const initial = 'initial';
|
|
18
|
+
|
|
19
|
+
export const loadStates = {
|
|
20
|
+
initial,
|
|
21
|
+
loading,
|
|
22
|
+
loaded,
|
|
23
|
+
error,
|
|
24
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const ssr: boolean;
|
|
2
|
+
export declare const nativeConnection: boolean;
|
|
3
|
+
export declare const guessMaxImageWidth: (dimensions: any, w: any) => number;
|
|
4
|
+
export declare const bytesToSize: (bytes: any) => string;
|
|
5
|
+
export declare const supportsWebp: boolean;
|
|
6
|
+
export declare const selectSrc: ({ srcSet, maxImageWidth, supportsWebp }: {
|
|
7
|
+
maxImageWidth: any;
|
|
8
|
+
srcSet: any;
|
|
9
|
+
supportsWebp: any;
|
|
10
|
+
}) => any;
|
|
11
|
+
export declare const fallbackParams: ({ srcSet, getUrl }: {
|
|
12
|
+
getUrl: any;
|
|
13
|
+
srcSet: any;
|
|
14
|
+
}) => {
|
|
15
|
+
nsSrcSet?: undefined;
|
|
16
|
+
nsSrc?: undefined;
|
|
17
|
+
ssr?: undefined;
|
|
18
|
+
} | {
|
|
19
|
+
nsSrcSet: any;
|
|
20
|
+
nsSrc: any;
|
|
21
|
+
ssr: true;
|
|
22
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
export const ssr =
|
|
2
|
+
typeof window === 'undefined' || window.navigator.userAgent === 'ReactSnap';
|
|
3
|
+
|
|
4
|
+
export const nativeConnection = !ssr && !!window.navigator.connection;
|
|
5
|
+
|
|
6
|
+
// export const getScreenWidth = () => {
|
|
7
|
+
// if (ssr) return 0
|
|
8
|
+
// const devicePixelRatio = window.devicePixelRatio || 1
|
|
9
|
+
// const {screen} = window
|
|
10
|
+
// const {width} = screen
|
|
11
|
+
// // const angle = (screen.orientation && screen.orientation.angle) || 0
|
|
12
|
+
// // return Math.max(width, height)
|
|
13
|
+
// // const rotated = Math.floor(angle / 90) % 2 !== 0
|
|
14
|
+
// // return (rotated ? height : width) * devicePixelRatio
|
|
15
|
+
// return width * devicePixelRatio
|
|
16
|
+
// }
|
|
17
|
+
// export const screenWidth = getScreenWidth()
|
|
18
|
+
|
|
19
|
+
export const guessMaxImageWidth = (dimensions, w) => {
|
|
20
|
+
if (ssr) return 0;
|
|
21
|
+
|
|
22
|
+
// Default to window object but don't use window as a default
|
|
23
|
+
// parameter so that this can be used on the server as well
|
|
24
|
+
if (!w) {
|
|
25
|
+
w = window;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const imgWidth = dimensions.width;
|
|
29
|
+
|
|
30
|
+
const {screen} = w;
|
|
31
|
+
const sWidth = screen.width;
|
|
32
|
+
const sHeight = screen.height;
|
|
33
|
+
|
|
34
|
+
const {documentElement} = document;
|
|
35
|
+
const windowWidth = w.innerWidth || documentElement.clientWidth;
|
|
36
|
+
const windowHeight = w.innerHeight || documentElement.clientHeight;
|
|
37
|
+
const devicePixelRatio = w.devicePixelRatio || 1;
|
|
38
|
+
|
|
39
|
+
const windowResized = sWidth > windowWidth;
|
|
40
|
+
|
|
41
|
+
let result;
|
|
42
|
+
if (windowResized) {
|
|
43
|
+
const body = document.getElementsByTagName('body')[0];
|
|
44
|
+
const scrollWidth = windowWidth - imgWidth;
|
|
45
|
+
const isScroll =
|
|
46
|
+
body.clientHeight > windowHeight || body.clientHeight > sHeight;
|
|
47
|
+
if (isScroll && scrollWidth <= 15) {
|
|
48
|
+
result = sWidth - scrollWidth;
|
|
49
|
+
} else {
|
|
50
|
+
result = (imgWidth / windowWidth) * sWidth;
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
result = imgWidth;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return result * devicePixelRatio;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const bytesToSize = (bytes) => {
|
|
60
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
61
|
+
if (bytes === 0) return 'n/a';
|
|
62
|
+
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
|
|
63
|
+
if (i === 0) return `${bytes} ${sizes[i]}`;
|
|
64
|
+
return `${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// async function supportsWebp() {
|
|
68
|
+
// if (typeof createImageBitmap === 'undefined' || typeof fetch === 'undefined')
|
|
69
|
+
// return false
|
|
70
|
+
// return fetch(
|
|
71
|
+
// 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=',
|
|
72
|
+
// )
|
|
73
|
+
// .then(response => response.blob())
|
|
74
|
+
// .then(blob => createImageBitmap(blob).then(() => true, () => false))
|
|
75
|
+
// }
|
|
76
|
+
// let webp = undefined
|
|
77
|
+
// const webpPromise = supportsWebp()
|
|
78
|
+
// webpPromise.then(x => (webp = x))
|
|
79
|
+
// export default () => {
|
|
80
|
+
// if (webp === undefined) return webpPromise
|
|
81
|
+
// return {
|
|
82
|
+
// then: callback => callback(webp),
|
|
83
|
+
// }
|
|
84
|
+
// }
|
|
85
|
+
|
|
86
|
+
const detectWebpSupport = () => {
|
|
87
|
+
if (ssr) return false;
|
|
88
|
+
const elem = document.createElement('canvas');
|
|
89
|
+
if (elem.getContext?.('2d')) {
|
|
90
|
+
// was able or not to get WebP representation
|
|
91
|
+
return elem.toDataURL('image/webp').indexOf('data:image/webp') === 0;
|
|
92
|
+
} else {
|
|
93
|
+
// very old browser like IE 8, canvas not supported
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const supportsWebp = detectWebpSupport();
|
|
99
|
+
|
|
100
|
+
const isWebp = (x) => x.format === 'webp' || x.src?.match(/\.webp($|\?.*)/i);
|
|
101
|
+
|
|
102
|
+
// eslint-disable-next-line no-shadow
|
|
103
|
+
export const selectSrc = ({srcSet, maxImageWidth, supportsWebp}) => {
|
|
104
|
+
if (srcSet.length === 0) throw new Error('Need at least one item in srcSet');
|
|
105
|
+
let supportedFormat, width;
|
|
106
|
+
if (supportsWebp) {
|
|
107
|
+
supportedFormat = srcSet.filter(isWebp);
|
|
108
|
+
if (supportedFormat.length === 0) supportedFormat = srcSet;
|
|
109
|
+
} else {
|
|
110
|
+
supportedFormat = srcSet.filter((x) => !isWebp(x));
|
|
111
|
+
if (supportedFormat.length === 0)
|
|
112
|
+
throw new Error('Need at least one supported format item in srcSet');
|
|
113
|
+
}
|
|
114
|
+
let widths = supportedFormat.filter((x) => x.width >= maxImageWidth);
|
|
115
|
+
if (widths.length === 0) {
|
|
116
|
+
widths = supportedFormat;
|
|
117
|
+
width = Math.max.apply(
|
|
118
|
+
null,
|
|
119
|
+
widths.map((x) => x.width),
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
width = Math.min.apply(
|
|
123
|
+
null,
|
|
124
|
+
widths.map((x) => x.width),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
return supportedFormat.filter((x) => x.width === width)[0];
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const fallbackParams = ({srcSet, getUrl}) => {
|
|
131
|
+
if (!ssr) return {};
|
|
132
|
+
const notWebp = srcSet.filter((x) => !isWebp(x));
|
|
133
|
+
const first = notWebp[0];
|
|
134
|
+
return {
|
|
135
|
+
nsSrcSet: notWebp
|
|
136
|
+
.map((x) => `${getUrl ? getUrl(x) : x.src} ${x.width}w`)
|
|
137
|
+
.join(','),
|
|
138
|
+
nsSrc: getUrl ? getUrl(first) : first.src,
|
|
139
|
+
ssr,
|
|
140
|
+
};
|
|
141
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import DownloadIcon from './Icon/Download';
|
|
2
|
+
import OfflineIcon from './Icon/Offline';
|
|
3
|
+
import WarningIcon from './Icon/Warning';
|
|
4
|
+
import LoadingIcon from './Icon/Loading';
|
|
5
|
+
declare const load: string, loading: string, loaded: string, error: string, noicon: string, offline: string;
|
|
6
|
+
declare const _default: {
|
|
7
|
+
[load]: typeof DownloadIcon;
|
|
8
|
+
[loading]: typeof LoadingIcon;
|
|
9
|
+
[loaded]: null;
|
|
10
|
+
[error]: typeof WarningIcon;
|
|
11
|
+
[noicon]: null;
|
|
12
|
+
[offline]: typeof OfflineIcon;
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import DownloadIcon from './Icon/Download';
|
|
2
|
+
import OfflineIcon from './Icon/Offline';
|
|
3
|
+
import WarningIcon from './Icon/Warning';
|
|
4
|
+
import LoadingIcon from './Icon/Loading';
|
|
5
|
+
import {icons} from './constants';
|
|
6
|
+
|
|
7
|
+
const {load, loading, loaded, error, noicon, offline} = icons;
|
|
8
|
+
|
|
9
|
+
export default {
|
|
10
|
+
[load]: DownloadIcon,
|
|
11
|
+
[loading]: LoadingIcon,
|
|
12
|
+
[loaded]: null,
|
|
13
|
+
[error]: WarningIcon,
|
|
14
|
+
[noicon]: null,
|
|
15
|
+
[offline]: OfflineIcon,
|
|
16
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* returns new "promise" with cancel function combined
|
|
3
|
+
*
|
|
4
|
+
* @param {Promise} p1 - first "promise" with cancel
|
|
5
|
+
* @param {Promise} p2 - second "promise" with cancel
|
|
6
|
+
* @returns {Promise} - new "promise" with cancel
|
|
7
|
+
*/
|
|
8
|
+
export declare const combineCancel: (p1: Promise<any>, p2: Promise<any>) => Promise<any>;
|
|
9
|
+
export declare const timeout: (threshold: any) => Promise<any>;
|
|
10
|
+
export declare const imageLoader: (src: any) => Promise<any>;
|
|
11
|
+
export declare const xhrLoader: (url: any, options: any) => Promise<any>;
|