@sheinx/base 3.1.14 → 3.1.16

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.
@@ -245,16 +245,6 @@ function Select(props0) {
245
245
  }),
246
246
  groupData = _useGroup.data,
247
247
  groupKey = _useGroup.groupKey;
248
-
249
- // const triggerOpen = usePersistFn(() => {
250
- // if (disabled === true) return;
251
- // if (open) {
252
- // closePop();
253
- // } else {
254
- // openPop();
255
- // }
256
- // });
257
-
258
248
  var focusAndOpen = function focusAndOpen() {
259
249
  if (!focused) {
260
250
  var _inputRef$current2;
@@ -661,7 +651,7 @@ function Select(props0) {
661
651
  children: [tipNode, renderResult(), /*#__PURE__*/_jsx(AbsoluteList, {
662
652
  adjust: true,
663
653
  focus: open,
664
- fixedWidth: autoAdapt ? 'min' : true,
654
+ fixedWidth: !props.columns && (autoAdapt ? 'min' : true),
665
655
  absolute: props.absolute,
666
656
  zIndex: props.zIndex,
667
657
  position: position,
@@ -1 +1 @@
1
- {"version":3,"file":"sticky.d.ts","sourceRoot":"","sources":["sticky.tsx"],"names":[],"mappings":";AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAsB5C,QAAA,MAAM,MAAM,UAAW,WAAW,gBAmPjC,CAAC;AAEF,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"sticky.d.ts","sourceRoot":"","sources":["sticky.tsx"],"names":[],"mappings":";AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAuB5C,QAAA,MAAM,MAAM,UAAW,WAAW,gBA2XjC,CAAC;AAEF,eAAe,MAAM,CAAC"}
@@ -12,13 +12,14 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
12
12
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
13
  import { usePersistFn, util, useResize, useRender } from '@sheinx/hooks';
14
14
  import { createPortal } from 'react-dom';
15
- import React, { useEffect, useRef, useState } from 'react';
15
+ import React, { useLayoutEffect, useRef, useState } from 'react';
16
16
  import { jsx as _jsx } from "react/jsx-runtime";
17
17
  import { Fragment as _Fragment } from "react/jsx-runtime";
18
18
  import { jsxs as _jsxs } from "react/jsx-runtime";
19
19
  var cssSupport = util.cssSupport;
20
20
  var supportSticky = cssSupport('position', 'sticky');
21
21
  var defaultZIndex = 900;
22
+ var events = ['scroll', 'pageshow', 'load', 'resize'];
22
23
 
23
24
  // const getFirstScrollParent = (el: HTMLElement) => {
24
25
  // let parent = el.parentNode as HTMLElement;
@@ -37,17 +38,19 @@ var defaultZIndex = 900;
37
38
  // };
38
39
 
39
40
  var Sticky = function Sticky(props) {
40
- var _props$style;
41
41
  var children = props.children,
42
42
  top = props.top,
43
43
  bottom = props.bottom;
44
- var css = props.css || props.target;
44
+ // 是否使用css sticky
45
+ var css = (props.css || props.target) && supportSticky;
46
+ var forceUpdate = useRender();
45
47
  var _useRef = useRef({
46
48
  target: null,
47
49
  div: null,
48
- observer: null,
50
+ position: '',
51
+ targetObserver: null,
49
52
  parentObserver: null,
50
- isTop: false
53
+ fixedObserver: null
51
54
  }),
52
55
  context = _useRef.current;
53
56
  var _useState = useState({}),
@@ -62,12 +65,7 @@ var Sticky = function Sticky(props) {
62
65
  _useState6 = _slicedToArray(_useState5, 2),
63
66
  parentVisible = _useState6[0],
64
67
  setParentVisible = _useState6[1];
65
- var forceRender = useRender();
66
68
  var elementRef = useRef(null);
67
- var elementSize = useResize({
68
- targetRef: elementRef,
69
- timer: 0
70
- });
71
69
  var getTarget = function getTarget() {
72
70
  var scrollContainer = props.scrollContainer;
73
71
  if (typeof scrollContainer === 'string') {
@@ -77,22 +75,19 @@ var Sticky = function Sticky(props) {
77
75
  if (scrollContainer && scrollContainer.nodeType === 1) {
78
76
  return scrollContainer;
79
77
  }
80
- return document.body;
78
+ return null;
81
79
  };
80
+
81
+ // window resize 时需要重新计算底部附着的位置
82
82
  var updateStyle = usePersistFn(function () {
83
- if (context.div && !context.isTop && show) {
84
- var shouldFixed = context.target === document.body;
83
+ if (context.div && context.position === 'bottom' && show) {
85
84
  var scrollRect = context.target.getBoundingClientRect();
86
85
  var targetRect = elementRef.current.getBoundingClientRect();
87
86
  var targetLeft = targetRect.left;
88
- var width = targetRect.width,
89
- height = targetRect.height;
90
87
  if (typeof bottom === 'number') {
91
88
  var outRootRect = context.target.getBoundingClientRect();
92
89
  var _style = {
93
- position: shouldFixed ? 'fixed' : 'absolute',
94
- width: "".concat(width, "px"),
95
- height: "".concat(height, "px"),
90
+ position: 'absolute',
96
91
  top: "".concat(scrollRect.bottom - (props.bottom || 0) - outRootRect.top, "px"),
97
92
  left: "".concat(targetLeft - outRootRect.left, "px"),
98
93
  transform: 'translateY(-100%)'
@@ -101,30 +96,22 @@ var Sticky = function Sticky(props) {
101
96
  }
102
97
  }
103
98
  });
104
- var handlePosition = usePersistFn(function (entries) {
99
+
100
+ // 有滚动容器时的定位
101
+ var handleTargetPosition = usePersistFn(function (entries) {
105
102
  var entry = entries[0];
106
103
  var scrollRect = entry.rootBounds;
107
104
  var targetRect = entry.boundingClientRect;
108
- var shouldFixed = context.target === document.body;
109
105
  if (!entry.isIntersecting) {
110
106
  var targetLeft = targetRect.left;
111
- var outRootRect = shouldFixed ? {
112
- top: 0,
113
- bottom: 0,
114
- left: 0,
115
- right: 0
116
- } : context.div.getBoundingClientRect();
117
- var width = targetRect.width,
118
- height = targetRect.height;
107
+ var outRootRect = context.div.getBoundingClientRect();
119
108
  if (scrollRect && targetRect.bottom < scrollRect.bottom) {
120
109
  // top in
121
- context.isTop = true;
110
+ context.position = 'top';
122
111
  if (typeof top === 'number') {
123
112
  setShow(true);
124
113
  var _style2 = {
125
- position: shouldFixed ? 'fixed' : 'absolute',
126
- width: "".concat(width, "px"),
127
- height: "".concat(height, "px"),
114
+ position: 'absolute',
128
115
  top: "".concat(scrollRect.top - outRootRect.top, "px"),
129
116
  left: "".concat(targetLeft - outRootRect.left, "px")
130
117
  };
@@ -132,16 +119,13 @@ var Sticky = function Sticky(props) {
132
119
  }
133
120
  } else if (scrollRect) {
134
121
  // bottom in
135
- context.isTop = false;
122
+ context.position = 'bottom';
136
123
  if (typeof bottom === 'number') {
137
124
  setShow(true);
138
- var _outRootRect = context.target.getBoundingClientRect();
139
125
  var _style3 = {
140
- position: shouldFixed ? 'fixed' : 'absolute',
141
- width: "".concat(width, "px"),
142
- height: "".concat(height, "px"),
143
- top: "".concat(scrollRect.bottom - _outRootRect.top, "px"),
144
- left: "".concat(targetLeft - _outRootRect.left, "px"),
126
+ position: 'absolute',
127
+ top: "".concat(scrollRect.bottom - outRootRect.top, "px"),
128
+ left: "".concat(targetLeft - outRootRect.left, "px"),
145
129
  transform: 'translateY(-100%)'
146
130
  };
147
131
  setStyle(_style3);
@@ -150,8 +134,11 @@ var Sticky = function Sticky(props) {
150
134
  }
151
135
  if (entry.isIntersecting) {
152
136
  setShow(false);
137
+ context.position = '';
153
138
  }
154
139
  });
140
+
141
+ // 父元素是否可见
155
142
  var handleParentVisible = usePersistFn(function (entries) {
156
143
  var entry = entries[0];
157
144
  if (entry.isIntersecting) {
@@ -160,35 +147,118 @@ var Sticky = function Sticky(props) {
160
147
  setParentVisible(false);
161
148
  }
162
149
  });
163
- var cleanEvents = function cleanEvents() {
164
- if (context.observer) {
165
- context.observer.disconnect();
166
- context.observer = null;
150
+ var setFixedStyle = usePersistFn(function (s, m) {
151
+ var l = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
152
+ if (s !== show) {
153
+ setShow(s);
167
154
  }
168
- if (context.parentObserver) {
169
- context.parentObserver.disconnect();
170
- context.parentObserver = null;
155
+ if (s && m) {
156
+ var newStyle = _defineProperty(_defineProperty({
157
+ position: 'fixed'
158
+ }, m, m === 'top' ? top : bottom), "left", l);
159
+ if (!util.shallowEqual(style, newStyle)) {
160
+ setStyle(newStyle);
161
+ }
162
+ }
163
+ });
164
+
165
+ // 无滚动容器的时候,body 滚动 resize 计算
166
+ var handleFixedPosition = usePersistFn(function () {
167
+ if (css || context.target) return;
168
+ var element = elementRef.current;
169
+ if (!parentVisible) {
170
+ setFixedStyle(false);
171
+ return;
172
+ }
173
+ if (!element) return;
174
+ var selfRect = element.getBoundingClientRect();
175
+ if (selfRect === null) return;
176
+ // If the element is hidden, the width and height will be 0
177
+ if (selfRect && selfRect.width === 0 && selfRect.height === 0) return;
178
+ var scrollRect = document.body.getBoundingClientRect();
179
+ var top = props.top,
180
+ bottom = props.bottom;
181
+ if (top !== undefined && Math.ceil(selfRect.top) <= top) {
182
+ setFixedStyle(true, 'top', selfRect.left);
183
+ return;
184
+ } else if (bottom !== undefined && Math.ceil(selfRect.bottom) + bottom > scrollRect.bottom) {
185
+ setFixedStyle(true, 'bottom', selfRect.left);
186
+ return;
187
+ } else {
188
+ setFixedStyle(false);
189
+ }
190
+ });
191
+
192
+ // 无滚动容器时内滚场景触发
193
+ var handleFixedInter = usePersistFn(function (entries) {
194
+ if (!parentVisible) {
195
+ setFixedStyle(false);
196
+ return;
197
+ }
198
+ var entry = entries[0];
199
+ var scrollRect = entry.rootBounds;
200
+ var targetRect = entry.boundingClientRect;
201
+ if (scrollRect && scrollRect.top === 0 && scrollRect.bottom === 0) {
202
+ return;
203
+ }
204
+ if (!entry.isIntersecting) {
205
+ var targetLeft = targetRect.left;
206
+ var outRootRect = document.body.getBoundingClientRect();
207
+ if (scrollRect && targetRect.bottom < scrollRect.bottom) {
208
+ // top in
209
+ if (typeof top === 'number') {
210
+ setFixedStyle(true, 'top', targetLeft - outRootRect.left);
211
+ }
212
+ } else if (scrollRect) {
213
+ // bottom in
214
+ if (typeof bottom === 'number') {
215
+ setFixedStyle(true, 'bottom', targetLeft - outRootRect.left);
216
+ }
217
+ }
218
+ }
219
+ if (entry.isIntersecting) {
220
+ setFixedStyle(false);
221
+ }
222
+ });
223
+ var elementSize = useResize({
224
+ targetRef: elementRef,
225
+ timer: 10,
226
+ cb: handleFixedPosition
227
+ });
228
+ var cancelFixedObserver = function cancelFixedObserver() {
229
+ if (context.fixedObserver) {
230
+ context.fixedObserver.disconnect();
231
+ context.fixedObserver = null;
232
+ }
233
+ };
234
+ var createFixedObserver = function createFixedObserver() {
235
+ cancelFixedObserver();
236
+ context.fixedObserver = new IntersectionObserver(handleFixedInter, {
237
+ root: null,
238
+ rootMargin: "-".concat(top || 0, "px 0px -").concat(bottom || 0, "px 0px"),
239
+ threshold: 1.0
240
+ });
241
+ };
242
+ var cancelObserver = function cancelObserver() {
243
+ if (context.targetObserver) {
244
+ context.targetObserver.disconnect();
245
+ context.targetObserver = null;
171
246
  }
172
- window.removeEventListener('resize', updateStyle);
173
247
  };
174
248
  var createObserver = function createObserver() {
175
- var target = getTarget();
176
- if (!context.div && target !== document.body) {
249
+ if (!context.div) {
177
250
  context.div = document.createElement('div');
178
251
  context.div.style.position = 'relative';
179
252
  }
180
- if (target && target !== context.target) {
181
- forceRender();
182
- context.target = target;
183
- cleanEvents();
253
+ if (context.target) {
184
254
  if (context.div) {
185
255
  // append div
186
- if (target === document.body) {
187
- document.body.appendChild(context.div);
256
+ if (context.target === document.body) {
257
+ document.body.insertBefore(context.div, document.body.firstChild);
188
258
  } else {
189
- target.parentNode.insertBefore(context.div, target);
259
+ context.target.parentNode.insertBefore(context.div, context.target);
190
260
  }
191
- var _style4 = window.getComputedStyle(target);
261
+ var _style4 = window.getComputedStyle(context.target);
192
262
  if (_style4.position === 'absolute' || _style4.position === 'fixed') {
193
263
  context.div.style.position = _style4.position;
194
264
  context.div.style.top = _style4.top;
@@ -197,59 +267,109 @@ var Sticky = function Sticky(props) {
197
267
  context.div.style.bottom = _style4.bottom;
198
268
  }
199
269
  }
270
+ cancelFixedObserver();
200
271
  if (window.IntersectionObserver) {
201
- var observer = new IntersectionObserver(handlePosition, {
202
- root: target,
272
+ var observer = new IntersectionObserver(handleTargetPosition, {
273
+ root: context.target,
203
274
  rootMargin: "-".concat(top || 0, "px 0px -").concat(bottom || 0, "px 0px"),
204
275
  threshold: 1.0
205
276
  });
206
- context.observer = observer;
207
- context.parentObserver = new IntersectionObserver(handleParentVisible, {
208
- root: target,
209
- rootMargin: "-".concat(top || 0, "px 0px -").concat(bottom || 0, "px 0px"),
210
- threshold: 0
211
- });
277
+ context.targetObserver = observer;
212
278
  }
213
279
  }
214
280
  };
281
+ var cancelParentObserver = function cancelParentObserver() {
282
+ if (context.parentObserver) {
283
+ context.parentObserver.disconnect();
284
+ context.parentObserver = null;
285
+ }
286
+ };
287
+ var createParentObserver = function createParentObserver() {
288
+ if (!props.parent) return;
289
+ cancelParentObserver();
290
+ context.parentObserver = new IntersectionObserver(handleParentVisible, {
291
+ root: context.target,
292
+ rootMargin: "-".concat(top || 0, "px 0px -").concat(bottom || 0, "px 0px"),
293
+ threshold: 0
294
+ });
295
+ context.parentObserver.observe(props.parent);
296
+ };
297
+
298
+ // 存在滚动容器时的定位
299
+ var createTargetEvents = function createTargetEvents() {
300
+ createObserver();
301
+ window.addEventListener('resize', updateStyle);
302
+ };
303
+ var cancelTargetEvents = function cancelTargetEvents() {
304
+ cancelObserver();
305
+ window.removeEventListener('resize', updateStyle);
306
+ if (context.div) {
307
+ context.div.remove();
308
+ context.div = null;
309
+ }
310
+ };
311
+ var createFixedEvents = function createFixedEvents() {
312
+ createFixedObserver();
313
+ events.forEach(function (event) {
314
+ window.addEventListener(event, handleFixedPosition);
315
+ });
316
+ };
317
+ var cancelFixedEvents = function cancelFixedEvents() {
318
+ cancelFixedObserver();
319
+ events.forEach(function (event) {
320
+ window.removeEventListener(event, handleFixedPosition);
321
+ });
322
+ };
215
323
  var handleElementRef = function handleElementRef(el) {
216
324
  if (el) {
217
325
  elementRef.current = el;
218
- if (el && context.observer) {
219
- context.observer.observe(el);
326
+ if (context.targetObserver) {
327
+ if (!el) {
328
+ context.targetObserver.disconnect();
329
+ } else {
330
+ context.targetObserver.observe(el);
331
+ }
332
+ }
333
+ if (context.fixedObserver) {
334
+ if (!el) {
335
+ context.fixedObserver.disconnect();
336
+ } else {
337
+ context.fixedObserver.observe(el);
338
+ }
220
339
  }
221
340
  }
222
341
  };
223
- useEffect(function () {
224
- if (!css) {
225
- createObserver();
226
- window.addEventListener('resize', updateStyle);
342
+ useLayoutEffect(function () {
343
+ if (css) return;
344
+ var target = getTarget();
345
+ if (context.target !== target) {
346
+ context.target = target;
347
+ forceUpdate();
227
348
  }
228
- return function () {
229
- cleanEvents();
230
- if (context.div) {
231
- context.div.remove();
232
- context.div = null;
233
- }
234
- };
235
- }, [css]);
236
- useEffect(function () {
237
- if (props.parent && context.parentObserver) {
349
+ if (context.target) {
350
+ createTargetEvents();
351
+ return cancelTargetEvents;
352
+ } else {
353
+ // fixed 布局
354
+ createFixedEvents();
355
+ return cancelFixedEvents;
356
+ }
357
+ }, [css, top, bottom]);
358
+ useLayoutEffect(function () {
359
+ if (props.parent && !css) {
360
+ // createParentObserver 用到context.target
361
+ createParentObserver();
238
362
  context.parentObserver.observe(props.parent);
239
363
  }
240
- return function () {
241
- if (context.parentObserver) {
242
- context.parentObserver.disconnect();
243
- }
244
- };
245
- }, [props.parent]);
364
+ return cancelParentObserver;
365
+ }, [props.parent, css, context.target, top, bottom]);
246
366
 
247
367
  // 纯css方法 直接使用css
248
368
  // js方法
249
369
  // 1. 不指定滚动容器 基于document.body 使用fixed + js计算
250
370
  // 2. 指定滚动容器 在滚动容器上方插入一个dom占位 基于该dom渲染和定位
251
371
  // 3. 使用 intersectionObserver 来判断是否需要sticky
252
- if (css && supportSticky) {
372
+ if (css) {
253
373
  return /*#__PURE__*/_jsx("div", {
254
374
  className: props.className,
255
375
  style: _objectSpread(_objectSpread({
@@ -273,12 +393,14 @@ var Sticky = function Sticky(props) {
273
393
  })
274
394
  }) : null;
275
395
  var isFixed = style.position === 'fixed';
396
+ var hideStyle = {
397
+ opacity: 0,
398
+ pointerEvents: 'none'
399
+ };
276
400
  return /*#__PURE__*/_jsxs(_Fragment, {
277
401
  children: [isFixed ? StickyEl : context.target && context.div && /*#__PURE__*/createPortal(StickyEl, context.div), /*#__PURE__*/_jsx("div", {
278
402
  className: props.className,
279
- style: _objectSpread(_objectSpread({}, props.style), {}, {
280
- opacity: show && parentVisible ? 0 : (_props$style = props.style) === null || _props$style === void 0 ? void 0 : _props$style.opacity
281
- }),
403
+ style: _objectSpread(_objectSpread({}, props.style), show && parentVisible ? hideStyle : {}),
282
404
  ref: handleElementRef,
283
405
  children: children
284
406
  })]
@@ -39,7 +39,7 @@ var Scroll = function Scroll(props) {
39
39
  position: 'sticky'
40
40
  }, isRtl ? 'right' : 'left', 0), "top", 0);
41
41
  var placeStyle = _defineProperty(_defineProperty(_defineProperty(_defineProperty({
42
- marginTop: Math.max(0, Math.floor(scrollHeight - height))
42
+ marginTop: height > 0 && scrollHeight > 0 ? Math.max(0, Math.floor(scrollHeight - height)) : 0
43
43
  }, "margin".concat(isRtl ? 'Left' : 'Right'), scrollWidth), "height", 0), "width", 0), "overflow", 'hidden');
44
44
  var handleScroll = usePersistFn(function (e) {
45
45
  var target = e.currentTarget;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sheinx/base",
3
- "version": "3.1.14",
3
+ "version": "3.1.16",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "MIT",
@@ -10,7 +10,7 @@
10
10
  "module": "./esm/index.js",
11
11
  "typings": "./cjs/index.d.ts",
12
12
  "dependencies": {
13
- "@sheinx/hooks": "3.1.14",
13
+ "@sheinx/hooks": "3.1.16",
14
14
  "immer": "^10.0.0",
15
15
  "classnames": "^2.0.0",
16
16
  "@shined/reactive": "^0.1.3-alpha.0"