@sheinx/hooks 3.6.4 → 3.6.5-beta.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/cjs/components/use-image/use-image.d.ts +2 -0
- package/cjs/components/use-image/use-image.d.ts.map +1 -1
- package/cjs/components/use-image/use-image.js +40 -2
- package/esm/components/use-image/use-image.d.ts +2 -0
- package/esm/components/use-image/use-image.d.ts.map +1 -1
- package/esm/components/use-image/use-image.js +40 -2
- package/package.json +1 -1
@@ -14,6 +14,8 @@ declare const useImage: (props?: BaseImageProps) => {
|
|
14
14
|
onClick: (event: React.MouseEvent<HTMLImageElement | HTMLDivElement>) => void;
|
15
15
|
};
|
16
16
|
getImageProps: <TOther_1 extends ObjectType = ObjectType>(externalProps?: TOther_1) => {
|
17
|
+
style: React.CSSProperties;
|
18
|
+
} & {
|
17
19
|
[x: string]: (event: any) => void;
|
18
20
|
} & TOther_1 & {
|
19
21
|
alt: string | undefined;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"use-image.d.ts","sourceRoot":"","sources":["use-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AACzC,OAAO,EAAe,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAWlD,QAAA,MAAM,QAAQ,WAAW,cAAc;;;;;;;;;;
|
1
|
+
{"version":3,"file":"use-image.d.ts","sourceRoot":"","sources":["use-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AACzC,OAAO,EAAe,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAWlD,QAAA,MAAM,QAAQ,WAAW,cAAc;;;;;;;;;;yBAiG3B,gBAAgB,CAAC,gBAAgB,GAAG,cAAc,CAAC;;;;;;;;;;;;;CAoF9D,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
@@ -43,6 +43,10 @@ var useImage = function useImage() {
|
|
43
43
|
_React$useState2 = _slicedToArray(_React$useState, 2),
|
44
44
|
status = _React$useState2[0],
|
45
45
|
setStatus = _React$useState2[1];
|
46
|
+
var _React$useState3 = _react.default.useState({}),
|
47
|
+
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
48
|
+
imgCoverStyle = _React$useState4[0],
|
49
|
+
setImgCoverStyle = _React$useState4[1];
|
46
50
|
var elementRef = _react.default.useRef(null);
|
47
51
|
var getUrl = function getUrl(url) {
|
48
52
|
var auto = 'autoSSL' in props ? autoSSL : config.autoSSL;
|
@@ -77,6 +81,35 @@ var useImage = function useImage() {
|
|
77
81
|
};
|
78
82
|
img.src = getUrl(alt);
|
79
83
|
};
|
84
|
+
var handleCoverStyle = function handleCoverStyle(img) {
|
85
|
+
var container = elementRef === null || elementRef === void 0 ? void 0 : elementRef.current;
|
86
|
+
if (!container) return;
|
87
|
+
|
88
|
+
// 根据容器的宽高获取宽高比
|
89
|
+
var containerWidth = container.clientWidth;
|
90
|
+
var containerHeight = container.clientHeight;
|
91
|
+
var containerRatio = containerWidth / containerHeight;
|
92
|
+
|
93
|
+
// 根据图片的自然尺寸获取宽高比
|
94
|
+
var imageWidth = img.naturalWidth;
|
95
|
+
var imageHeight = img.naturalHeight;
|
96
|
+
var imageRatio = imageWidth / imageHeight;
|
97
|
+
|
98
|
+
// 判断宽高比,决定图片填充策略
|
99
|
+
if (imageRatio > containerRatio) {
|
100
|
+
// 图片更宽,宽度占满容器,高度自适应
|
101
|
+
setImgCoverStyle({
|
102
|
+
height: '100%',
|
103
|
+
width: 'auto'
|
104
|
+
});
|
105
|
+
} else {
|
106
|
+
// 图片更高,填满容器高度
|
107
|
+
setImgCoverStyle({
|
108
|
+
width: '100%',
|
109
|
+
height: 'auto'
|
110
|
+
});
|
111
|
+
}
|
112
|
+
};
|
80
113
|
var markToRender = function markToRender() {
|
81
114
|
if (!src) {
|
82
115
|
handleAlt();
|
@@ -84,7 +117,10 @@ var useImage = function useImage() {
|
|
84
117
|
}
|
85
118
|
var img = new window.Image();
|
86
119
|
img.onload = function () {
|
87
|
-
|
120
|
+
setStatus(SRC);
|
121
|
+
if (props.fit === 'fill') {
|
122
|
+
handleCoverStyle(img);
|
123
|
+
}
|
88
124
|
};
|
89
125
|
img.onerror = function (e) {
|
90
126
|
return handleError(SRC, e);
|
@@ -113,7 +149,9 @@ var useImage = function useImage() {
|
|
113
149
|
alt: alt,
|
114
150
|
draggable: !noImgDrag
|
115
151
|
});
|
116
|
-
return _objectSpread({
|
152
|
+
return _objectSpread({
|
153
|
+
style: imgCoverStyle
|
154
|
+
}, mergedEventHandlers);
|
117
155
|
};
|
118
156
|
var getImageDivProps = function getImageDivProps() {
|
119
157
|
var externalProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
@@ -14,6 +14,8 @@ declare const useImage: (props?: BaseImageProps) => {
|
|
14
14
|
onClick: (event: React.MouseEvent<HTMLImageElement | HTMLDivElement>) => void;
|
15
15
|
};
|
16
16
|
getImageProps: <TOther_1 extends ObjectType = ObjectType>(externalProps?: TOther_1) => {
|
17
|
+
style: React.CSSProperties;
|
18
|
+
} & {
|
17
19
|
[x: string]: (event: any) => void;
|
18
20
|
} & TOther_1 & {
|
19
21
|
alt: string | undefined;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"use-image.d.ts","sourceRoot":"","sources":["use-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AACzC,OAAO,EAAe,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAWlD,QAAA,MAAM,QAAQ,WAAW,cAAc;;;;;;;;;;
|
1
|
+
{"version":3,"file":"use-image.d.ts","sourceRoot":"","sources":["use-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoB,MAAM,OAAO,CAAC;AACzC,OAAO,EAAe,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAWlD,QAAA,MAAM,QAAQ,WAAW,cAAc;;;;;;;;;;yBAiG3B,gBAAgB,CAAC,gBAAgB,GAAG,cAAc,CAAC;;;;;;;;;;;;;CAoF9D,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
@@ -36,6 +36,10 @@ var useImage = function useImage() {
|
|
36
36
|
_React$useState2 = _slicedToArray(_React$useState, 2),
|
37
37
|
status = _React$useState2[0],
|
38
38
|
setStatus = _React$useState2[1];
|
39
|
+
var _React$useState3 = React.useState({}),
|
40
|
+
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
41
|
+
imgCoverStyle = _React$useState4[0],
|
42
|
+
setImgCoverStyle = _React$useState4[1];
|
39
43
|
var elementRef = React.useRef(null);
|
40
44
|
var getUrl = function getUrl(url) {
|
41
45
|
var auto = 'autoSSL' in props ? autoSSL : config.autoSSL;
|
@@ -70,6 +74,35 @@ var useImage = function useImage() {
|
|
70
74
|
};
|
71
75
|
img.src = getUrl(alt);
|
72
76
|
};
|
77
|
+
var handleCoverStyle = function handleCoverStyle(img) {
|
78
|
+
var container = elementRef === null || elementRef === void 0 ? void 0 : elementRef.current;
|
79
|
+
if (!container) return;
|
80
|
+
|
81
|
+
// 根据容器的宽高获取宽高比
|
82
|
+
var containerWidth = container.clientWidth;
|
83
|
+
var containerHeight = container.clientHeight;
|
84
|
+
var containerRatio = containerWidth / containerHeight;
|
85
|
+
|
86
|
+
// 根据图片的自然尺寸获取宽高比
|
87
|
+
var imageWidth = img.naturalWidth;
|
88
|
+
var imageHeight = img.naturalHeight;
|
89
|
+
var imageRatio = imageWidth / imageHeight;
|
90
|
+
|
91
|
+
// 判断宽高比,决定图片填充策略
|
92
|
+
if (imageRatio > containerRatio) {
|
93
|
+
// 图片更宽,宽度占满容器,高度自适应
|
94
|
+
setImgCoverStyle({
|
95
|
+
height: '100%',
|
96
|
+
width: 'auto'
|
97
|
+
});
|
98
|
+
} else {
|
99
|
+
// 图片更高,填满容器高度
|
100
|
+
setImgCoverStyle({
|
101
|
+
width: '100%',
|
102
|
+
height: 'auto'
|
103
|
+
});
|
104
|
+
}
|
105
|
+
};
|
73
106
|
var markToRender = function markToRender() {
|
74
107
|
if (!src) {
|
75
108
|
handleAlt();
|
@@ -77,7 +110,10 @@ var useImage = function useImage() {
|
|
77
110
|
}
|
78
111
|
var img = new window.Image();
|
79
112
|
img.onload = function () {
|
80
|
-
|
113
|
+
setStatus(SRC);
|
114
|
+
if (props.fit === 'fill') {
|
115
|
+
handleCoverStyle(img);
|
116
|
+
}
|
81
117
|
};
|
82
118
|
img.onerror = function (e) {
|
83
119
|
return handleError(SRC, e);
|
@@ -106,7 +142,9 @@ var useImage = function useImage() {
|
|
106
142
|
alt: alt,
|
107
143
|
draggable: !noImgDrag
|
108
144
|
});
|
109
|
-
return _objectSpread({
|
145
|
+
return _objectSpread({
|
146
|
+
style: imgCoverStyle
|
147
|
+
}, mergedEventHandlers);
|
110
148
|
};
|
111
149
|
var getImageDivProps = function getImageDivProps() {
|
112
150
|
var externalProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|