@micromag/element-image 0.3.541 → 0.3.547

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.
Files changed (2) hide show
  1. package/package.json +4 -5
  2. package/lib/index.js +0 -199
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micromag/element-image",
3
- "version": "0.3.541",
3
+ "version": "0.3.547",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [
@@ -30,11 +30,10 @@
30
30
  }
31
31
  ],
32
32
  "license": "ISC",
33
- "main": "lib/index.js",
33
+ "type": "module",
34
34
  "module": "es/index.js",
35
35
  "exports": {
36
36
  ".": {
37
- "require": "./lib/index.js",
38
37
  "import": "./es/index.js"
39
38
  },
40
39
  "./assets/css/styles": "./assets/css/styles.css",
@@ -61,7 +60,7 @@
61
60
  "dependencies": {
62
61
  "@babel/runtime": "^7.13.10",
63
62
  "@folklore/size": "^0.1.20",
64
- "@micromag/core": "^0.3.541",
63
+ "@micromag/core": "^0.3.547",
65
64
  "classnames": "^2.2.6",
66
65
  "prop-types": "^15.7.2",
67
66
  "react-intl": "^6.6.4",
@@ -71,5 +70,5 @@
71
70
  "access": "public",
72
71
  "registry": "https://registry.npmjs.org/"
73
72
  },
74
- "gitHead": "6c04a7e327b5fbc096785c11320b3fbd3c5751c8"
73
+ "gitHead": "3c2f5904ce61fcfa61f673c38c2a5ec56e9e2b07"
75
74
  }
package/lib/index.js DELETED
@@ -1,199 +0,0 @@
1
- 'use strict';
2
-
3
- var _defineProperty = require('@babel/runtime/helpers/defineProperty');
4
- var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
5
- var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
6
- var size = require('@folklore/size');
7
- var classNames = require('classnames');
8
- var PropTypes = require('prop-types');
9
- var React = require('react');
10
- var core = require('@micromag/core');
11
- var utils = require('@micromag/core/utils');
12
-
13
- var styles = {"container":"micromag-element-image-container","showEmpty":"micromag-element-image-showEmpty","img":"micromag-element-image-img"};
14
-
15
- var propTypes = {
16
- media: core.PropTypes.imageMedia,
17
- alt: PropTypes.string,
18
- width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
19
- height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
20
- resolution: PropTypes.number,
21
- objectFit: core.PropTypes.objectFit,
22
- containerStyle: core.PropTypes.containerStyle,
23
- imageStyle: core.PropTypes.containerStyle,
24
- className: PropTypes.string,
25
- imageClassName: PropTypes.string,
26
- onLoaded: PropTypes.func,
27
- loadingMode: PropTypes.string,
28
- shouldLoad: PropTypes.bool
29
- };
30
- var defaultProps = {
31
- media: null,
32
- alt: null,
33
- width: null,
34
- height: null,
35
- resolution: 1,
36
- objectFit: null,
37
- containerStyle: {},
38
- //
39
- imageStyle: {},
40
- className: null,
41
- imageClassName: null,
42
- onLoaded: null,
43
- loadingMode: 'lazy',
44
- shouldLoad: true
45
- };
46
- var Image = function Image(_ref) {
47
- var media = _ref.media,
48
- alt = _ref.alt,
49
- width = _ref.width,
50
- height = _ref.height,
51
- resolution = _ref.resolution,
52
- objectFit = _ref.objectFit,
53
- containerStyle = _ref.containerStyle,
54
- imageStyle = _ref.imageStyle,
55
- className = _ref.className,
56
- imageClassName = _ref.imageClassName,
57
- onLoaded = _ref.onLoaded,
58
- loadingMode = _ref.loadingMode,
59
- shouldLoad = _ref.shouldLoad;
60
- var _ref2 = media || {},
61
- _ref2$metadata = _ref2.metadata,
62
- metadata = _ref2$metadata === void 0 ? null : _ref2$metadata;
63
- var _ref3 = metadata || {},
64
- _ref3$width = _ref3.width,
65
- mediaWidth = _ref3$width === void 0 ? 0 : _ref3$width,
66
- _ref3$height = _ref3.height,
67
- mediaHeight = _ref3$height === void 0 ? 0 : _ref3$height,
68
- _ref3$description = _ref3.description,
69
- description = _ref3$description === void 0 ? 'image' : _ref3$description;
70
- var mediaRatio = mediaWidth / mediaHeight;
71
- var _useState = React.useState({
72
- width: mediaWidth,
73
- height: mediaHeight
74
- }),
75
- _useState2 = _slicedToArray(_useState, 2),
76
- _useState2$ = _useState2[0],
77
- _useState2$$width = _useState2$.width,
78
- realWidth = _useState2$$width === void 0 ? 0 : _useState2$$width,
79
- _useState2$$height = _useState2$.height,
80
- realHeight = _useState2$$height === void 0 ? 0 : _useState2$$height,
81
- setRealSize = _useState2[1];
82
- var wasLoadedRef = React.useRef(shouldLoad);
83
- if (shouldLoad && !wasLoadedRef.current) {
84
- wasLoadedRef.current = shouldLoad;
85
- }
86
- var finalShouldLoad = wasLoadedRef.current;
87
- var onImageLoaded = React.useCallback(function (e) {
88
- var _e$target = e.target,
89
- _e$target$naturalWidt = _e$target.naturalWidth,
90
- naturalWidth = _e$target$naturalWidt === void 0 ? 0 : _e$target$naturalWidt,
91
- _e$target$naturalHeig = _e$target.naturalHeight,
92
- naturalHeight = _e$target$naturalHeig === void 0 ? 0 : _e$target$naturalHeig;
93
- if (naturalWidth !== realWidth || naturalHeight !== realHeight) {
94
- setRealSize({
95
- width: naturalWidth || 0,
96
- height: naturalHeight || 0
97
- });
98
- }
99
- if (onLoaded !== null) {
100
- onLoaded(e);
101
- }
102
- }, [onLoaded]);
103
- var withFit = objectFit !== null;
104
- var mediaHasSize = realWidth > 0 && realHeight > 0;
105
- var finalContainerStyle;
106
- var finalImageStyle;
107
- if (withFit) {
108
- var imageTop = 0;
109
- var imageLeft = 0;
110
- var imageWidth = width;
111
- var imageHeight = height;
112
- var imageObjectFit = null;
113
- var imageObjectPosition = null;
114
- var _ref4 = objectFit || {},
115
- _ref4$fit = _ref4.fit,
116
- fit = _ref4$fit === void 0 ? null : _ref4$fit,
117
- _ref4$horizontalPosit = _ref4.horizontalPosition,
118
- horizontalPosition = _ref4$horizontalPosit === void 0 ? 'center' : _ref4$horizontalPosit,
119
- _ref4$verticalPositio = _ref4.verticalPosition,
120
- verticalPosition = _ref4$verticalPositio === void 0 ? 'center' : _ref4$verticalPositio;
121
- if (mediaHasSize) {
122
- var _getSizeWithinBounds = size.getSizeWithinBounds(realWidth, realHeight, width, height, {
123
- cover: fit === 'cover'
124
- }),
125
- resizedImageWidth = _getSizeWithinBounds.width,
126
- resizedImageHeight = _getSizeWithinBounds.height;
127
- imageWidth = resizedImageWidth;
128
- imageHeight = resizedImageHeight;
129
- if (horizontalPosition === 'center') {
130
- imageLeft = -(resizedImageWidth - width) / 2;
131
- } else if (horizontalPosition === 'right') {
132
- imageLeft = -(resizedImageWidth - width);
133
- }
134
- if (verticalPosition === 'center') {
135
- imageTop = -(resizedImageHeight - height) / 2;
136
- } else if (verticalPosition === 'bottom') {
137
- imageTop = -(resizedImageHeight - height);
138
- }
139
- } else {
140
- imageObjectFit = fit;
141
- imageObjectPosition = "".concat(horizontalPosition, " ").concat(verticalPosition);
142
- }
143
- finalContainerStyle = {
144
- width: width,
145
- height: height
146
- };
147
- finalImageStyle = {
148
- position: 'absolute',
149
- width: imageWidth,
150
- height: imageHeight,
151
- top: imageTop,
152
- left: imageLeft,
153
- objectFit: imageObjectFit,
154
- objectPosition: imageObjectPosition
155
- };
156
- } else {
157
- var validWidth = width !== null && typeof width === 'number';
158
- var validHeight = height !== null && typeof height === 'number';
159
- var ratioWidth = mediaRatio && validHeight ? height * mediaRatio : null;
160
- var ratioHeight = mediaRatio && validWidth ? width / mediaRatio : null;
161
- var _finalWidth = width !== null ? width : ratioWidth;
162
- var _finalHeight = height !== null ? height : ratioHeight;
163
- if (_finalWidth === null && _finalHeight === null) {
164
- _finalWidth = realWidth > 0 ? mediaWidth : null;
165
- _finalHeight = realHeight > 0 ? mediaHeight : null;
166
- }
167
- finalImageStyle = {
168
- width: _finalWidth,
169
- height: _finalHeight
170
- };
171
- finalContainerStyle = finalImageStyle;
172
- }
173
- finalContainerStyle = _objectSpread(_objectSpread({}, finalContainerStyle), containerStyle);
174
- finalImageStyle = _objectSpread(_objectSpread({}, finalImageStyle), imageStyle);
175
- var _finalImageStyle = finalImageStyle,
176
- _finalImageStyle$widt = _finalImageStyle.width,
177
- finalWidth = _finalImageStyle$widt === void 0 ? null : _finalImageStyle$widt,
178
- _finalImageStyle$heig = _finalImageStyle.height,
179
- finalHeight = _finalImageStyle$heig === void 0 ? null : _finalImageStyle$heig;
180
- var finalUrl = utils.getOptimalImageUrl(media, finalWidth, finalHeight, {
181
- resolution: resolution
182
- });
183
- return /*#__PURE__*/React.createElement("div", {
184
- className: classNames([styles.container, _defineProperty({}, className, className !== null)]),
185
- style: finalContainerStyle
186
- }, finalUrl !== null && finalShouldLoad ? /*#__PURE__*/React.createElement("img", {
187
- src: finalUrl,
188
- alt: alt || description,
189
- className: classNames([styles.img, _defineProperty({}, imageClassName, imageClassName !== null)]),
190
- style: finalImageStyle,
191
- onLoad: onImageLoaded,
192
- loading: loadingMode
193
- }) : null);
194
- };
195
- Image.propTypes = propTypes;
196
- Image.defaultProps = defaultProps;
197
- var Image$1 = Image;
198
-
199
- module.exports = Image$1;