@micromag/element-background 0.2.401 → 0.2.403

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/es/index.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
2
2
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
3
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
4
- import React, { useMemo } from 'react';
4
+ import React, { useRef, useMemo } from 'react';
5
5
  import PropTypes from 'prop-types';
6
6
  import classNames from 'classnames';
7
7
  import { PropTypes as PropTypes$1 } from '@micromag/core';
8
8
  import { getStyleFromColor, getOptimalImageUrl, getLayersFromBackground } from '@micromag/core/utils';
9
- import Video from '@micromag/element-video';
10
9
  import { getSizeWithinBounds } from '@folklore/size';
10
+ import { useIntersectionObserver } from '@micromag/core/hooks';
11
+ import Video from '@micromag/element-video';
11
12
 
12
13
  var styles = {"container":"micromag-element-background-container","layer":"micromag-element-background-layer","layers":"micromag-element-background-layers","video":"micromag-element-background-video","videoContainer":"micromag-element-background-videoContainer","right":"micromag-element-background-right","background":"micromag-element-background-background","bottom":"micromag-element-background-bottom"};
13
14
 
@@ -22,7 +23,8 @@ var propTypes$1 = {
22
23
  media: PropTypes.oneOfType([PropTypes$1.imageMedia, PropTypes$1.videoMedia]),
23
24
  className: PropTypes.string,
24
25
  playing: PropTypes.bool,
25
- children: PropTypes.node
26
+ children: PropTypes.node,
27
+ loadingMode: PropTypes.string
26
28
  };
27
29
  var defaultProps$1 = {
28
30
  width: null,
@@ -35,7 +37,8 @@ var defaultProps$1 = {
35
37
  media: null,
36
38
  className: null,
37
39
  playing: false,
38
- children: null
40
+ children: null,
41
+ loadingMode: 'lazy'
39
42
  };
40
43
 
41
44
  var Background = function Background(_ref) {
@@ -49,7 +52,8 @@ var Background = function Background(_ref) {
49
52
  media = _ref.media,
50
53
  className = _ref.className,
51
54
  playing = _ref.playing,
52
- children = _ref.children;
55
+ children = _ref.children,
56
+ loadingMode = _ref.loadingMode;
53
57
 
54
58
  var _ref2 = media || {},
55
59
  _ref2$type = _ref2.type,
@@ -65,7 +69,23 @@ var Background = function Background(_ref) {
65
69
  mediaHeight = _ref3$height === void 0 ? 0 : _ref3$height;
66
70
 
67
71
  var isVideo = mediaType === 'video';
68
- var isImage = mediaType === 'image'; // color
72
+ var isImage = mediaType === 'image'; // Lazy load
73
+
74
+ var _useIntersectionObser = useIntersectionObserver({
75
+ disabled: loadingMode !== 'lazy',
76
+ rootMargin: '-10px'
77
+ }),
78
+ intersectingRef = _useIntersectionObser.ref,
79
+ isIntersecting = _useIntersectionObser.entry.isIntersecting;
80
+
81
+ var shouldLoadRef = useRef(isIntersecting || loadingMode !== 'lazy');
82
+
83
+ if (isIntersecting && !shouldLoadRef.current) {
84
+ shouldLoadRef.current = isIntersecting;
85
+ }
86
+
87
+ var shouldLoad = shouldLoadRef.current;
88
+ console.log(shouldLoad, isIntersecting); // color
69
89
 
70
90
  var containerStyle = _objectSpread({
71
91
  width: width,
@@ -77,7 +97,7 @@ var Background = function Background(_ref) {
77
97
  var finalUrl = getOptimalImageUrl(isVideo ? {
78
98
  url: mediaThumbnailUrl
79
99
  } : media, width, height);
80
- containerStyle.backgroundImage = finalUrl !== null ? "url(\"".concat(finalUrl, "\")") : null;
100
+ containerStyle.backgroundImage = finalUrl !== null && shouldLoad ? "url(\"".concat(finalUrl, "\")") : null;
81
101
  containerStyle.backgroundRepeat = repeat ? 'repeat' : 'no-repeat';
82
102
  containerStyle.backgroundPosition = [horizontalAlign, verticalAlign].join(' ');
83
103
 
@@ -121,9 +141,11 @@ var Background = function Background(_ref) {
121
141
  media: media,
122
142
  autoPlay: playing,
123
143
  initialMuted: true,
124
- loop: true
144
+ loop: true,
145
+ preload: shouldLoad ? 'auto' : 'metadata'
125
146
  })) : null, /*#__PURE__*/React.createElement("div", {
126
- className: styles.content
147
+ className: styles.content,
148
+ ref: intersectingRef
127
149
  }, children));
128
150
  };
129
151
 
package/lib/index.js CHANGED
@@ -10,8 +10,9 @@ var PropTypes = require('prop-types');
10
10
  var classNames = require('classnames');
11
11
  var core = require('@micromag/core');
12
12
  var utils = require('@micromag/core/utils');
13
- var Video = require('@micromag/element-video');
14
13
  var size = require('@folklore/size');
14
+ var hooks = require('@micromag/core/hooks');
15
+ var Video = require('@micromag/element-video');
15
16
 
16
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
17
18
 
@@ -36,7 +37,8 @@ var propTypes$1 = {
36
37
  media: PropTypes__default["default"].oneOfType([core.PropTypes.imageMedia, core.PropTypes.videoMedia]),
37
38
  className: PropTypes__default["default"].string,
38
39
  playing: PropTypes__default["default"].bool,
39
- children: PropTypes__default["default"].node
40
+ children: PropTypes__default["default"].node,
41
+ loadingMode: PropTypes__default["default"].string
40
42
  };
41
43
  var defaultProps$1 = {
42
44
  width: null,
@@ -49,7 +51,8 @@ var defaultProps$1 = {
49
51
  media: null,
50
52
  className: null,
51
53
  playing: false,
52
- children: null
54
+ children: null,
55
+ loadingMode: 'lazy'
53
56
  };
54
57
 
55
58
  var Background = function Background(_ref) {
@@ -63,7 +66,8 @@ var Background = function Background(_ref) {
63
66
  media = _ref.media,
64
67
  className = _ref.className,
65
68
  playing = _ref.playing,
66
- children = _ref.children;
69
+ children = _ref.children,
70
+ loadingMode = _ref.loadingMode;
67
71
 
68
72
  var _ref2 = media || {},
69
73
  _ref2$type = _ref2.type,
@@ -79,7 +83,23 @@ var Background = function Background(_ref) {
79
83
  mediaHeight = _ref3$height === void 0 ? 0 : _ref3$height;
80
84
 
81
85
  var isVideo = mediaType === 'video';
82
- var isImage = mediaType === 'image'; // color
86
+ var isImage = mediaType === 'image'; // Lazy load
87
+
88
+ var _useIntersectionObser = hooks.useIntersectionObserver({
89
+ disabled: loadingMode !== 'lazy',
90
+ rootMargin: '-10px'
91
+ }),
92
+ intersectingRef = _useIntersectionObser.ref,
93
+ isIntersecting = _useIntersectionObser.entry.isIntersecting;
94
+
95
+ var shouldLoadRef = React.useRef(isIntersecting || loadingMode !== 'lazy');
96
+
97
+ if (isIntersecting && !shouldLoadRef.current) {
98
+ shouldLoadRef.current = isIntersecting;
99
+ }
100
+
101
+ var shouldLoad = shouldLoadRef.current;
102
+ console.log(shouldLoad, isIntersecting); // color
83
103
 
84
104
  var containerStyle = _objectSpread__default["default"]({
85
105
  width: width,
@@ -91,7 +111,7 @@ var Background = function Background(_ref) {
91
111
  var finalUrl = utils.getOptimalImageUrl(isVideo ? {
92
112
  url: mediaThumbnailUrl
93
113
  } : media, width, height);
94
- containerStyle.backgroundImage = finalUrl !== null ? "url(\"".concat(finalUrl, "\")") : null;
114
+ containerStyle.backgroundImage = finalUrl !== null && shouldLoad ? "url(\"".concat(finalUrl, "\")") : null;
95
115
  containerStyle.backgroundRepeat = repeat ? 'repeat' : 'no-repeat';
96
116
  containerStyle.backgroundPosition = [horizontalAlign, verticalAlign].join(' ');
97
117
 
@@ -135,9 +155,11 @@ var Background = function Background(_ref) {
135
155
  media: media,
136
156
  autoPlay: playing,
137
157
  initialMuted: true,
138
- loop: true
158
+ loop: true,
159
+ preload: shouldLoad ? 'auto' : 'metadata'
139
160
  })) : null, /*#__PURE__*/React__default["default"].createElement("div", {
140
- className: styles.content
161
+ className: styles.content,
162
+ ref: intersectingRef
141
163
  }, children));
142
164
  };
143
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/element-background",
3
- "version": "0.2.401",
3
+ "version": "0.2.403",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "javascript"
@@ -48,8 +48,8 @@
48
48
  "dependencies": {
49
49
  "@babel/runtime": "^7.13.10",
50
50
  "@folklore/size": "^0.1.15",
51
- "@micromag/core": "^0.2.401",
52
- "@micromag/element-video": "^0.2.401",
51
+ "@micromag/core": "^0.2.403",
52
+ "@micromag/element-video": "^0.2.403",
53
53
  "classnames": "^2.2.6",
54
54
  "lodash": "^4.17.21",
55
55
  "prop-types": "^15.7.2",
@@ -59,5 +59,5 @@
59
59
  "publishConfig": {
60
60
  "access": "public"
61
61
  },
62
- "gitHead": "fd03ec6d9decc9e18d6088a36e47c7b1e0e72b4c"
62
+ "gitHead": "7a6604651c990286e594350ef730d40772937195"
63
63
  }