react-datocms 4.0.3 → 4.0.5

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/useQuerySubscription/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjC,OAAO,EACL,gBAAgB,GAKjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,2BAA2B,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAyB9F,MAAM,UAAU,oBAAoB,CAGlC,OAA0D;IAClD,IAAA,OAAO,GAA4B,OAAO,QAAnC,EAAE,WAAW,GAAe,OAAO,YAAtB,EAAK,KAAK,UAAK,OAAO,EAA5C,0BAAkC,CAAF,CAAa;IAE7C,IAAA,KAAoB,QAAQ,CAA0B,IAAI,CAAC,EAA1D,KAAK,QAAA,EAAE,QAAQ,QAA2C,CAAC;IAC5D,IAAA,KAAkB,QAAQ,CAAqB,IAAI,CAAC,EAAnD,IAAI,QAAA,EAAE,OAAO,QAAsC,CAAC;IACrD,IAAA,KAAsB,QAAQ,CAClC,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,QAAQ,CAClC,EAFM,MAAM,QAAA,EAAE,SAAS,QAEvB,CAAC;IAEF,IAAM,uBAAuB,GAAG,KAG/B,CAAC;IAEF,oBAAoB,CAAC;QACnB,IAAI,OAAO,KAAK,KAAK,EAAE;YACrB,SAAS,CAAC,QAAQ,CAAC,CAAC;YAEpB,OAAO;gBACL,yCAAyC;YAC3C,CAAC,CAAC;SACH;QAED,IAAI,WAAiC,CAAC;QAEtC,SAAe,SAAS;;;;gCACR,qBAAM,gBAAgB,uBAC/B,uBAAuB,KAC1B,cAAc,EAAE,UAAC,MAAM;oCACrB,SAA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CAAC,EAAnD,IAAI,QAAA,EAAE,OAAO,QAAsC,CAAC;IACrD,IAAA,KAAsB,QAAQ,CAClC,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,QAAQ,CAClC,EAFM,MAAM,QAAA,EAAE,SAAS,QAEvB,CAAC;IAEF,IAAM,uBAAuB,GAAG,KAG/B,CAAC;IAEF,oBAAoB,CAAC;QACnB,IAAI,OAAO,KAAK,KAAK,EAAE;YACrB,SAAS,CAAC,QAAQ,CAAC,CAAC;YAEpB,OAAO;gBACL,yCAAyC;YAC3C,CAAC,CAAC;SACH;QAED,IAAI,WAAiC,CAAC;QAEtC,SAAe,SAAS;;;;gCACR,qBAAM,gBAAgB,uBAC/B,uBAAuB,KAC1B,cAAc,EAAE,UAAC,MAAM;oCACrB,SAAS,CAAC,MAAM,CAAC,CAAC;gCACpB,CAAC,EACD,QAAQ,EAAE,UAAC,UAAU;oCACnB,QAAQ,CAAC,IAAI,CAAC,CAAC;oCACf,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;gCACpC,CAAC,EACD,cAAc,EAAE,UAAC,SAAS;oCACxB,OAAO,CAAC,IAAI,CAAC,CAAC;oCACd,QAAQ,CAAC,SAAS,CAAC,CAAC;gCACtB,CAAC,IACD,EAAA;;4BAbF,WAAW,GAAG,SAaZ,CAAC;;;;;SACJ;QAED,SAAS,EAAE,CAAC;QAEZ,OAAO;YACL,IAAI,WAAW,EAAE;gBACf,WAAW,EAAE,CAAC;aACf;QACH,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,uBAAuB,CAAC,CAAC,CAAC;IAE9B,OAAO,EAAE,KAAK,OAAA,EAAE,MAAM,QAAA,EAAE,IAAI,EAAE,IAAI,IAAI,WAAW,EAAE,CAAC;AACtD,CAAC"}
@@ -31,6 +31,8 @@ export declare type ImagePropTypes = {
31
31
  className?: string;
32
32
  /** Additional CSS class for the image inside the `<picture />` tag */
33
33
  pictureClassName?: string;
34
+ /** Additional CSS class for the placeholder image */
35
+ placeholderClassName?: string;
34
36
  /** Duration (in ms) of the fade-in transition effect upoad image loading */
35
37
  fadeInDuration?: number;
36
38
  /** @deprecated Use the intersectionThreshold prop */
@@ -45,6 +47,8 @@ export declare type ImagePropTypes = {
45
47
  style?: React.CSSProperties;
46
48
  /** Additional CSS rules to add to the image inside the `<picture />` tag */
47
49
  pictureStyle?: React.CSSProperties;
50
+ /** Additional CSS rules to add to the placeholder image */
51
+ placeholderStyle?: React.CSSProperties;
48
52
  /**
49
53
  * The layout behavior of the image as the viewport changes size
50
54
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-datocms",
3
- "version": "4.0.3",
3
+ "version": "4.0.5",
4
4
  "types": "dist/types/index.d.ts",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -35,11 +35,13 @@
35
35
  "react": "^17.0.2",
36
36
  "react-dom": "^17.0.2",
37
37
  "rimraf": "^3.0.2",
38
+ "rome": "^10.0.1",
38
39
  "ts-jest": "^26.5.5",
39
40
  "typescript": "^4.5.5"
40
41
  },
41
42
  "scripts": {
42
43
  "build": "rimraf dist && tsc && tsc --project ./tsconfig.esnext.json",
44
+ "format": "rome format --write src",
43
45
  "watch": "rimraf dist && tsc --watch",
44
46
  "prepare": "npm run test && npm run build",
45
47
  "test": "jest --coverage",
@@ -1,4 +1,11 @@
1
- import React, { useState, forwardRef, useCallback, CSSProperties } from 'react';
1
+ import React, {
2
+ useState,
3
+ forwardRef,
4
+ useCallback,
5
+ CSSProperties,
6
+ useRef,
7
+ useEffect,
8
+ } from 'react';
2
9
  import { useInView } from 'react-intersection-observer';
3
10
  import { encode } from 'universal-base64';
4
11
 
@@ -42,6 +49,8 @@ export type ImagePropTypes = {
42
49
  className?: string;
43
50
  /** Additional CSS class for the image inside the `<picture />` tag */
44
51
  pictureClassName?: string;
52
+ /** Additional CSS class for the placeholder image */
53
+ placeholderClassName?: string;
45
54
  /** Duration (in ms) of the fade-in transition effect upoad image loading */
46
55
  fadeInDuration?: number;
47
56
  /** @deprecated Use the intersectionThreshold prop */
@@ -56,6 +65,8 @@ export type ImagePropTypes = {
56
65
  style?: React.CSSProperties;
57
66
  /** Additional CSS rules to add to the image inside the `<picture />` tag */
58
67
  pictureStyle?: React.CSSProperties;
68
+ /** Additional CSS rules to add to the placeholder image */
69
+ placeholderStyle?: React.CSSProperties;
59
70
  /**
60
71
  * The layout behavior of the image as the viewport changes size
61
72
  *
@@ -140,7 +151,7 @@ const buildSrcSet = (
140
151
  width: number | undefined,
141
152
  candidateMultipliers: number[],
142
153
  ) => {
143
- if (!src || !width) {
154
+ if (!(src && width)) {
144
155
  return undefined;
145
156
  }
146
157
 
@@ -199,6 +210,8 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
199
210
  priority = false,
200
211
  sizes,
201
212
  srcSetCandidates = [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4],
213
+ placeholderClassName,
214
+ placeholderStyle,
202
215
  },
203
216
  ref,
204
217
  ) => {
@@ -206,11 +219,24 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
206
219
 
207
220
  const [loaded, setLoaded] = useState(false);
208
221
 
222
+ const imageRef = useRef<HTMLImageElement>(null);
223
+
209
224
  const handleLoad = () => {
210
225
  onLoad?.();
211
226
  setLoaded(true);
212
227
  };
213
228
 
229
+ // https://stackoverflow.com/q/39777833/266535
230
+ useEffect(() => {
231
+ if (!imageRef.current) {
232
+ return;
233
+ }
234
+
235
+ if (imageRef.current.complete && imageRef.current.naturalWidth) {
236
+ handleLoad();
237
+ }
238
+ }, []);
239
+
214
240
  const [viewRef, inView] = useInView({
215
241
  threshold: intersectionThreshold || intersectionTreshold || 0,
216
242
  rootMargin: intersectionMargin || '0px 0px 0px 0px',
@@ -221,7 +247,9 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
221
247
  const callbackRef = useCallback(
222
248
  (_ref: HTMLDivElement) => {
223
249
  viewRef(_ref);
224
- if (ref) (ref as React.MutableRefObject<HTMLDivElement>).current = _ref;
250
+ if (ref) {
251
+ (ref as React.MutableRefObject<HTMLDivElement>).current = _ref;
252
+ }
225
253
  },
226
254
  [viewRef],
227
255
  );
@@ -272,6 +300,7 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
272
300
  aria-hidden="true"
273
301
  alt=""
274
302
  src={data.base64 ?? undefined}
303
+ className={placeholderClassName}
275
304
  style={{
276
305
  backgroundColor: data.bgColor ?? undefined,
277
306
  objectFit,
@@ -287,6 +316,7 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
287
316
  top: '-5%',
288
317
  width: '110%',
289
318
  height: '110%',
319
+ ...placeholderStyle,
290
320
  }}
291
321
  />
292
322
  ) : null;
@@ -334,6 +364,7 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
334
364
  {regularSource}
335
365
  {data.src && (
336
366
  <img
367
+ ref={imageRef}
337
368
  src={data.src}
338
369
  alt={data.alt ?? ''}
339
370
  title={data.title ?? undefined}
@@ -362,7 +393,12 @@ export const Image = forwardRef<HTMLDivElement, ImagePropTypes>(
362
393
  alt={data.alt ?? ''}
363
394
  title={data.title ?? undefined}
364
395
  className={pictureClassName}
365
- style={{ ...absolutePositioning, ...pictureStyle }}
396
+ style={{
397
+ ...absolutePositioning,
398
+ objectFit,
399
+ objectPosition,
400
+ ...pictureStyle,
401
+ }}
366
402
  loading={lazyLoad ? 'lazy' : undefined}
367
403
  fetchpriority={priority ? 'high' : undefined}
368
404
  />