@nutui/nutui-react 2.5.2 → 2.6.1

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 (42) hide show
  1. package/CHANGELOG.md +29 -5
  2. package/dist/esm/CellGroup.js +1 -1
  3. package/dist/esm/Form/style/style.css +1 -1
  4. package/dist/esm/Image.js +1 -1
  5. package/dist/esm/ImagePreview/style/style.css +1 -1
  6. package/dist/esm/ImagePreview.js +16 -20
  7. package/dist/esm/Indicator.js +25 -1
  8. package/dist/esm/SideNavBar.js +1 -1
  9. package/dist/esm/SubSideNavBar.js +1 -1
  10. package/dist/esm/Swiper/style/index.js +0 -1
  11. package/dist/esm/Swiper/style/style.css +1 -1
  12. package/dist/esm/Swiper.js +273 -306
  13. package/dist/esm/SwiperItem.js +6 -18
  14. package/dist/esm/context4.js +16 -2
  15. package/dist/esm/use-touch.js +13 -3
  16. package/dist/locales/base.js +1 -1
  17. package/dist/locales/en-US.js +1 -1
  18. package/dist/locales/id-ID.js +1 -1
  19. package/dist/locales/tr-TR.js +1 -1
  20. package/dist/locales/zh-CN.js +1 -1
  21. package/dist/locales/zh-TW.js +1 -1
  22. package/dist/locales/zh-UG.js +1 -1
  23. package/dist/nutui.react.es.js +405 -397
  24. package/dist/nutui.react.umd.js +405 -397
  25. package/dist/packages/form/form.scss +0 -3
  26. package/dist/packages/swiper/swiper.scss +30 -16
  27. package/dist/style.css +1 -1
  28. package/dist/style.mjs +1 -1
  29. package/dist/types/packages/input/input.taro.d.ts +1 -1
  30. package/dist/types/packages/swiper/effects/default.d.ts +11 -0
  31. package/dist/types/packages/swiper/effects/focus.d.ts +21 -0
  32. package/dist/types/packages/swiper/index.d.ts +4 -2
  33. package/dist/types/packages/swiper/swiper.d.ts +24 -43
  34. package/dist/types/packages/swiper/types.d.ts +6 -0
  35. package/dist/types/packages/swiperitem/swiperitem.d.ts +8 -4
  36. package/dist/types/utils/use-lock-fn.d.ts +2 -0
  37. package/dist/types/utils/use-ref-state.d.ts +3 -0
  38. package/dist/types/utils/use-touch.d.ts +3 -1
  39. package/package.json +1 -1
  40. package/dist/esm/context5.js +0 -19
  41. package/dist/esm/indicator2.js +0 -28
  42. package/dist/types/packages/picker/doc.en.d.ts +0 -145
@@ -2,7 +2,7 @@
2
2
  typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("react-dom")) : typeof define === "function" && define.amd ? define(["exports", "react", "react-dom"], factory) : (global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self, factory((global2.nutui = global2.nutui || {}, global2.nutui.react = {}), global2.React, global2.ReactDOM));
3
3
  })(this, function(exports2, React, ReactDOM) {
4
4
  "use strict";/*!
5
- * @nutui/nutui-react v2.5.2 Wed Apr 03 2024 18:19:41 GMT+0800 (China Standard Time)
5
+ * @nutui/nutui-react v2.6.1 Fri Apr 19 2024 14:49:31 GMT+0800 (China Standard Time)
6
6
  * (c) 2023 @jdf2e.
7
7
  * Released under the MIT License.
8
8
  */
@@ -1248,7 +1248,7 @@ Check the top-level render call using <` + t2 + ">.");
1248
1248
  };
1249
1249
  const classPrefix$r = "nut-cell-group";
1250
1250
  const CellGroup = (props) => {
1251
- const { children, className, style, title, description, divider, ...rest } = {
1251
+ const { children, className, title, description, divider, ...rest } = {
1252
1252
  ...defaultProps$1t,
1253
1253
  ...props
1254
1254
  };
@@ -2635,7 +2635,8 @@ Check the top-level render call using <` + t2 + ">.");
2635
2635
  alt,
2636
2636
  loading: "lazy",
2637
2637
  onLoad: handleLoad,
2638
- onError: handleError
2638
+ onError: handleError,
2639
+ draggable: props.draggable
2639
2640
  }
2640
2641
  ) : /* @__PURE__ */ React.createElement(
2641
2642
  "img",
@@ -2646,7 +2647,8 @@ Check the top-level render call using <` + t2 + ">.");
2646
2647
  src,
2647
2648
  alt,
2648
2649
  onLoad: handleLoad,
2649
- onError: handleError
2650
+ onError: handleError,
2651
+ draggable: props.draggable
2650
2652
  }
2651
2653
  ),
2652
2654
  renderLoading(),
@@ -4399,16 +4401,20 @@ Check the top-level render call using <` + t2 + ">.");
4399
4401
  const offsetX = React.useRef(0);
4400
4402
  const offsetY = React.useRef(0);
4401
4403
  const direction = React.useRef("");
4402
- const touchTime = React.useRef(null);
4404
+ const last = React.useRef(false);
4405
+ const velocity = React.useRef(0);
4406
+ const touchTime = React.useRef(Date.now());
4403
4407
  const isVertical = () => direction.current === "vertical";
4404
4408
  const isHorizontal = () => direction.current === "horizontal";
4405
4409
  const reset = () => {
4406
- touchTime.current = null;
4410
+ touchTime.current = Date.now();
4407
4411
  deltaX.current = 0;
4408
4412
  deltaY.current = 0;
4409
4413
  offsetX.current = 0;
4410
4414
  offsetY.current = 0;
4415
+ delta.current = 0;
4411
4416
  direction.current = "";
4417
+ last.current = false;
4412
4418
  };
4413
4419
  const start2 = (event) => {
4414
4420
  reset();
@@ -4427,7 +4433,12 @@ Check the top-level render call using <` + t2 + ">.");
4427
4433
  direction.current = getDirection(offsetX.current, offsetY.current);
4428
4434
  }
4429
4435
  };
4436
+ const end = (event) => {
4437
+ last.current = true;
4438
+ velocity.current = Math.sqrt(deltaX.current ** 2 + deltaY.current ** 2) / (Date.now() - touchTime.current);
4439
+ };
4430
4440
  return {
4441
+ end,
4431
4442
  move,
4432
4443
  start: start2,
4433
4444
  reset,
@@ -4441,7 +4452,8 @@ Check the top-level render call using <` + t2 + ">.");
4441
4452
  offsetY,
4442
4453
  direction,
4443
4454
  isVertical,
4444
- isHorizontal
4455
+ isHorizontal,
4456
+ last
4445
4457
  };
4446
4458
  }
4447
4459
  const canUseDom = !!(typeof window !== "undefined" && typeof document !== "undefined" && window.document && window.document.createElement);
@@ -4646,7 +4658,7 @@ Check the top-level render call using <` + t2 + ">.");
4646
4658
  };
4647
4659
  Overlay.defaultProps = defaultOverlayProps;
4648
4660
  Overlay.displayName = "NutOverlay";
4649
- const DataContext$2 = React.createContext({});
4661
+ const DataContext$1 = React.createContext({});
4650
4662
  const defaultProps$1q = {
4651
4663
  ...ComponentDefaults,
4652
4664
  span: "24",
@@ -4660,7 +4672,7 @@ Check the top-level render call using <` + t2 + ">.");
4660
4672
  };
4661
4673
  const [colName, setColName] = React.useState("");
4662
4674
  const [colStyle, setColStyle] = React.useState({});
4663
- const { gutter } = React.useContext(DataContext$2);
4675
+ const { gutter } = React.useContext(DataContext$1);
4664
4676
  const classs = () => {
4665
4677
  const prefixCls2 = "nut-col";
4666
4678
  return `${prefixCls2} ${prefixCls2}-${span} ${gutter ? `${prefixCls2}-gutter` : ""} ${prefixCls2}-offset-${offset}`;
@@ -4911,7 +4923,7 @@ Check the top-level render call using <` + t2 + ">.");
4911
4923
  const parentRow = {
4912
4924
  gutter
4913
4925
  };
4914
- return /* @__PURE__ */ React.createElement(DataContext$2.Provider, { value: parentRow }, React.createElement(
4926
+ return /* @__PURE__ */ React.createElement(DataContext$1.Provider, { value: parentRow }, React.createElement(
4915
4927
  "div",
4916
4928
  {
4917
4929
  className: classNames(getClasses(), className),
@@ -22804,7 +22816,6 @@ Please add \`${key}Action\` when creating your handler.`);
22804
22816
  };
22805
22817
  Video.defaultProps = defaultProps$j;
22806
22818
  Video.displayName = "NutVideo";
22807
- const DataContext$1 = React.createContext({});
22808
22819
  const defaultProps$i = {
22809
22820
  total: 3,
22810
22821
  current: 0,
@@ -22838,413 +22849,410 @@ Please add \`${key}Action\` when creating your handler.`);
22838
22849
  };
22839
22850
  Indicator.defaultProps = defaultProps$i;
22840
22851
  Indicator.displayName = "NutIndicator";
22852
+ const getRefValue = (ref) => {
22853
+ return ref.current;
22854
+ };
22855
+ const useRefState = (param) => {
22856
+ const ref = React.useRef(param);
22857
+ const [, setState] = React.useState(param);
22858
+ const updateState = (p) => {
22859
+ ref.current = p;
22860
+ setState(p);
22861
+ };
22862
+ return [ref, updateState];
22863
+ };
22864
+ const getPerSlidePosition$1 = (index, position, loop2, count) => {
22865
+ const currentPosition = index * 100 + position;
22866
+ if (loop2) {
22867
+ const cycle = count * 100;
22868
+ const shift = cycle / 2;
22869
+ const nextPosition = (currentPosition + shift) % cycle;
22870
+ const shiftedPosition = (nextPosition < 0 ? nextPosition + cycle : nextPosition) - shift;
22871
+ return `${shiftedPosition}%`;
22872
+ }
22873
+ return `${currentPosition}%`;
22874
+ };
22875
+ const defaultEffect = (args) => {
22876
+ return React.Children.map(args.children, (child, index) => {
22877
+ const { isVertical, getSpringsAxis, loop: loop2, count } = args;
22878
+ return /* @__PURE__ */ React.createElement(
22879
+ animated.div,
22880
+ {
22881
+ className: "nut-swiper-slide",
22882
+ style: {
22883
+ [isVertical ? "y" : "x"]: getSpringsAxis().to((position) => {
22884
+ return getPerSlidePosition$1(index, position, loop2, count);
22885
+ }),
22886
+ [isVertical ? "top" : "left"]: `-${index * 100}%`
22887
+ }
22888
+ },
22889
+ child
22890
+ );
22891
+ });
22892
+ };
22893
+ const getPerSlidePosition = (index, position, loop2, count) => {
22894
+ const currentPosition = index * 100 + position;
22895
+ if (loop2) {
22896
+ const cycle = count * 100;
22897
+ const shift = cycle / 2;
22898
+ const nextPosition = (currentPosition + shift) % cycle;
22899
+ const shiftedPosition = (nextPosition < 0 ? nextPosition + cycle : nextPosition) - shift;
22900
+ return `${shiftedPosition}%`;
22901
+ }
22902
+ return `${currentPosition}%`;
22903
+ };
22904
+ const focusEffect = (args) => {
22905
+ return React.Children.map(args.children, (child, index) => {
22906
+ const {
22907
+ isVertical,
22908
+ springs,
22909
+ transforms,
22910
+ loop: loop2,
22911
+ count,
22912
+ swiperDirection,
22913
+ dragging,
22914
+ current,
22915
+ effect
22916
+ } = args;
22917
+ return /* @__PURE__ */ React.createElement(
22918
+ animated.div,
22919
+ {
22920
+ className: "nut-swiper-slide",
22921
+ style: {
22922
+ [isVertical ? "y" : "x"]: springs[isVertical ? "y" : "x"].to(
22923
+ (position) => {
22924
+ return getPerSlidePosition(index, position, loop2, count);
22925
+ }
22926
+ ),
22927
+ [isVertical ? "top" : "left"]: `-${index * 100}%`,
22928
+ scale: springs.s.to((ss) => {
22929
+ const scales = getRefValue(transforms);
22930
+ if (!scales)
22931
+ return 1;
22932
+ const scale = scales[index];
22933
+ const currentRefValue = getRefValue(current);
22934
+ if (dragging === false)
22935
+ ss = 0;
22936
+ const ps = ss * scale;
22937
+ if (index === currentRefValue) {
22938
+ return Math.max(scale - ps, effect.scale);
22939
+ }
22940
+ if (index === currentRefValue + swiperDirection.current) {
22941
+ return Math.min(scale + ps, 1);
22942
+ }
22943
+ return scale;
22944
+ })
22945
+ }
22946
+ },
22947
+ child
22948
+ );
22949
+ });
22950
+ };
22951
+ const useList = (effect, count, current) => {
22952
+ const [transforms, setTransforms] = useRefState([]);
22953
+ React.useEffect(() => {
22954
+ setTransforms(
22955
+ Array.from({ length: count }).fill(1).map(
22956
+ (scale, index) => index !== getRefValue(current) ? scale * (effect ? effect.scale : 1) : scale
22957
+ )
22958
+ );
22959
+ }, [count]);
22960
+ return [transforms, setTransforms];
22961
+ };
22962
+ const updateTransform = (transforms, setTransforms, effect, page) => {
22963
+ setTransforms(
22964
+ getRefValue(transforms).map(
22965
+ (s, index) => (
22966
+ // eslint-disable-next-line no-nested-ternary
22967
+ page === index ? 1 : effect ? effect.scale : 1
22968
+ )
22969
+ )
22970
+ );
22971
+ };
22841
22972
  const defaultProps$h = {
22842
- duration: 500,
22843
- defaultValue: 0,
22844
- autoPlay: 0,
22845
22973
  direction: "horizontal",
22846
22974
  indicator: false,
22847
22975
  loop: false,
22976
+ duration: 3e3,
22977
+ autoPlay: false,
22978
+ defaultValue: 0,
22848
22979
  touchable: true,
22849
- preventDefault: true,
22850
- stopPropagation: true,
22851
- center: false,
22852
- className: ""
22980
+ effect: void 0
22853
22981
  };
22854
- const Swiper = React.forwardRef((props, ref) => {
22855
- const rtl = useRtl();
22856
- const mergedProps = { ...defaultProps$h, ...props };
22857
- const {
22858
- children,
22859
- direction,
22860
- className,
22861
- onChange,
22862
- defaultValue,
22863
- indicator,
22864
- touchable,
22865
- preventDefault: preventDefault2,
22866
- stopPropagation,
22867
- autoPlay,
22868
- center,
22869
- ...rest
22870
- } = mergedProps;
22871
- const container = React.useRef(null);
22872
- const innerRef = React.useRef(null);
22873
- const swiperRef = React.useRef({
22874
- moving: false,
22875
- autoplayTimer: null,
22876
- width: 0,
22877
- height: 0,
22878
- offset: 0,
22879
- size: 0
22880
- });
22881
- const touch = useTouch();
22882
- const isVertical = direction === "vertical";
22883
- const [rect, setRect] = React.useState(null);
22884
- let [active, setActive] = React.useState(0);
22885
- const [width, setWidth] = React.useState(0);
22886
- const [height, setHeight] = React.useState(0);
22887
- const [offset, setOffset] = React.useState(0);
22888
- const [childOffset, setChildOffset] = React.useState([]);
22889
- const [ready, setReady] = React.useState(false);
22890
- let size = isVertical ? height : width;
22891
- const { swiperItems, swiperItemCount } = React.useMemo(() => {
22892
- let count = 0;
22893
- const swiperItems2 = React.Children.map(props.children, (child) => {
22894
- if (!React.isValidElement(child))
22895
- return null;
22896
- count++;
22897
- return child;
22898
- });
22899
- return {
22900
- swiperItems: swiperItems2,
22901
- swiperItemCount: count
22902
- };
22903
- }, [props.children]);
22904
- let trackSize = swiperItemCount * Number(size);
22905
- const parent = {
22906
- propSwiper: mergedProps,
22907
- size
22908
- };
22909
- const minOffset = (() => {
22910
- if (rect) {
22911
- const base = isVertical ? rect.height : rect.width;
22912
- return base - Number(size) * swiperItemCount;
22913
- }
22914
- return 0;
22915
- })();
22916
- const stopAutoPlay = () => {
22917
- clearTimeout(swiperRef.current.autoplayTimer);
22918
- swiperRef.current.autoplayTimer = null;
22919
- };
22920
- const startPlay = () => {
22921
- if (Number(mergedProps.autoPlay) <= 0 || swiperItemCount <= 1)
22922
- return;
22923
- stopAutoPlay();
22924
- swiperRef.current.autoplayTimer = setTimeout(() => {
22925
- next();
22926
- startPlay();
22927
- }, Number(mergedProps.autoPlay));
22928
- };
22929
- const resetPosition = () => {
22930
- swiperRef.current.moving = true;
22931
- if (active <= -1) {
22932
- move({ pace: swiperItemCount });
22933
- }
22934
- if (active >= swiperItemCount) {
22935
- move({ pace: -swiperItemCount });
22936
- }
22937
- };
22938
- const prev = () => {
22939
- resetPosition();
22940
- touch.reset();
22941
- requestAniFrame$1(() => {
22942
- requestAniFrame$1(() => {
22943
- swiperRef.current.moving = false;
22944
- move({
22945
- pace: -1,
22946
- isEmit: true
22947
- });
22948
- });
22949
- });
22950
- };
22951
- const next = () => {
22952
- resetPosition();
22953
- touch.reset();
22954
- requestAniFrame$1(() => {
22955
- requestAniFrame$1(() => {
22956
- swiperRef.current.moving = false;
22957
- move({
22958
- pace: 1,
22959
- isEmit: true
22960
- });
22961
- });
22962
- });
22963
- };
22964
- const to2 = (index) => {
22965
- resetPosition();
22966
- touch.reset();
22967
- requestAniFrame$1(() => {
22968
- requestAniFrame$1(() => {
22969
- swiperRef.current.moving = false;
22970
- let targetIndex;
22971
- if (props.loop && swiperItemCount === index) {
22972
- targetIndex = active === 0 ? 0 : index;
22973
- } else {
22974
- targetIndex = index % swiperItemCount;
22975
- }
22976
- move({
22977
- pace: targetIndex - active,
22978
- isEmit: true
22979
- });
22982
+ const Swiper = React.forwardRef(
22983
+ (props, ref) => {
22984
+ const classPrefix2 = "nut-swiper";
22985
+ const {
22986
+ children,
22987
+ direction,
22988
+ indicator,
22989
+ loop: loop2,
22990
+ effect,
22991
+ autoPlay,
22992
+ touchable,
22993
+ defaultValue,
22994
+ duration,
22995
+ style,
22996
+ className
22997
+ } = { ...defaultProps$h, ...props };
22998
+ const isVertical = direction === "vertical";
22999
+ const count = React.useMemo(() => {
23000
+ let c6 = 0;
23001
+ React.Children.map(children, (child, index) => {
23002
+ c6 += 1;
22980
23003
  });
22981
- });
22982
- };
22983
- const resize = () => {
22984
- init(active);
22985
- };
22986
- const move = ({
22987
- pace = 0,
22988
- offset: offset2 = 0,
22989
- isEmit = false,
22990
- movingStatus = false
22991
- }) => {
22992
- var _a;
22993
- if (swiperItemCount <= 1)
22994
- return;
22995
- const targetActive = getActive(pace);
22996
- const targetOffset = getOffset(targetActive, offset2);
22997
- if (props.loop) {
22998
- if (Array.isArray(children) && children[0] && targetOffset !== minOffset) {
22999
- const rightBound = targetOffset < minOffset;
23000
- childOffset[0] = rightBound ? trackSize : 0;
23004
+ return c6;
23005
+ }, [children]);
23006
+ const getSlideSize = () => {
23007
+ if (props.slideSize)
23008
+ return props.slideSize;
23009
+ if (stageRef.current) {
23010
+ if (isVertical)
23011
+ return stageRef.current.offsetHeight;
23012
+ return stageRef.current.offsetWidth;
23001
23013
  }
23002
- if (Array.isArray(children) && children[swiperItemCount - 1] && targetOffset !== 0) {
23003
- const leftBound = targetOffset > 0;
23004
- childOffset[swiperItemCount - 1] = leftBound ? -trackSize : 0;
23005
- }
23006
- setChildOffset(childOffset);
23007
- }
23008
- if (isEmit && active !== targetActive) {
23009
- (_a = props.onChange) == null ? void 0 : _a.call(props, (targetActive + swiperItemCount) % swiperItemCount);
23010
- }
23011
- active = targetActive;
23012
- setActive(targetActive);
23013
- setOffset(targetOffset);
23014
- getStyle(targetOffset);
23015
- };
23016
- React.useImperativeHandle(ref, () => ({
23017
- to: to2,
23018
- next,
23019
- prev,
23020
- resize
23021
- }));
23022
- const getActive = (pace) => {
23023
- if (pace) {
23024
- const _active = active + pace;
23025
- if (props.loop) {
23026
- return range2(_active, -1, swiperItemCount);
23014
+ return 0;
23015
+ };
23016
+ const getSwiperSize = () => {
23017
+ if (swiperRef.current) {
23018
+ if (isVertical)
23019
+ return swiperRef.current.offsetHeight;
23020
+ return swiperRef.current.offsetWidth;
23027
23021
  }
23028
- return range2(_active, 0, swiperItemCount - 1);
23029
- }
23030
- return active;
23031
- };
23032
- const getOffset = (active2, offset2 = 0) => {
23033
- let currentPosition = active2 * Number(size);
23034
- if (!props.loop) {
23035
- currentPosition = Math.min(currentPosition, -minOffset);
23036
- }
23037
- let targetOffset = offset2 - currentPosition;
23038
- if (!props.loop) {
23039
- targetOffset = range2(targetOffset, minOffset, 0);
23040
- }
23041
- return targetOffset;
23042
- };
23043
- const range2 = (num, min, max) => {
23044
- return Math.min(Math.max(num, min), max);
23045
- };
23046
- const classPrefix2 = "nut-swiper";
23047
- const contentClass = classNames({
23048
- [`${classPrefix2}-inner`]: true,
23049
- [`${classPrefix2}-vertical`]: isVertical
23050
- });
23051
- const getStyle = (moveOffset = offset) => {
23052
- const target = innerRef.current;
23053
- if (!target)
23054
- return;
23055
- let _offset = 0;
23056
- if (!center) {
23057
- _offset = moveOffset;
23058
- } else {
23059
- const _size = isVertical ? height : width;
23060
- const val = isVertical ? rect.height - _size : rect.width - _size;
23061
- _offset = moveOffset + (active === swiperItemCount - 1 && !props.loop ? -val / 2 : val / 2);
23062
- }
23063
- target.style.transitionDuration = `${swiperRef.current.moving ? 0 : props.duration}ms`;
23064
- target.style[isVertical ? "height" : "width"] = `${Number(size) * swiperItemCount}px`;
23065
- target.style[isVertical ? "width" : "height"] = `${isVertical ? width : height}px`;
23066
- target.style.transform = `translate3D${!isVertical ? `(${rtl ? -_offset : _offset}px,0,0)` : `(0,${_offset}px,0)`}`;
23067
- };
23068
- const onTouchStart = (e2) => {
23069
- if (!props.touchable)
23070
- return;
23071
- touch.start(e2);
23072
- stopAutoPlay();
23073
- resetPosition();
23074
- };
23075
- const onTouchMove = (e2) => {
23076
- if (props.preventDefault)
23077
- e2.preventDefault();
23078
- if (props.stopPropagation)
23079
- e2.stopPropagation();
23080
- if (props.touchable && swiperRef.current.moving) {
23081
- touch.move(e2);
23082
- if (touch.direction.current === props.direction) {
23083
- move({
23084
- offset: touch.delta.current
23085
- });
23022
+ return 0;
23023
+ };
23024
+ const bound2 = (v, min, max) => {
23025
+ if (min !== void 0) {
23026
+ v = Math.max(v, min);
23086
23027
  }
23087
- }
23088
- };
23089
- const onTouchEnd = (e2) => {
23090
- if (!props.touchable || !swiperRef.current.moving)
23091
- return;
23092
- const speed = touch.delta.current / (Date.now() - touch.touchTime.current);
23093
- const isShouldMove = Math.abs(speed) > 0.3 || Math.abs(touch.delta.current) > Number((size / 2).toFixed(2));
23094
- swiperRef.current.moving = false;
23095
- if (isShouldMove && touch.direction.current === props.direction) {
23096
- let pace = 0;
23097
- const offset2 = touch.isVertical() ? touch.offsetY : touch.offsetX;
23098
- if (props.loop) {
23099
- if (offset2.current > 0) {
23100
- pace = touch.delta.current > 0 ? -1 : 1;
23101
- }
23102
- } else {
23103
- pace = -Math[touch.delta.current > 0 ? "ceil" : "floor"](
23104
- touch.delta.current / size
23105
- );
23028
+ if (max !== void 0) {
23029
+ v = Math.min(v, max);
23106
23030
  }
23107
- move({
23108
- pace,
23109
- isEmit: true
23031
+ return v;
23032
+ };
23033
+ const timeoutRef = React.useRef(null);
23034
+ const [dragging, setDragging] = React.useState(false);
23035
+ const [current, setCurrent] = useRefState(defaultValue);
23036
+ const stageRef = React.useRef(null);
23037
+ const swiperRef = React.useRef(null);
23038
+ const [springs, api] = useSpring(() => ({
23039
+ x: !isVertical ? current.current * 100 * -1 : 0,
23040
+ y: isVertical ? current.current * 100 * -1 : 0,
23041
+ s: 0,
23042
+ reset: () => {
23043
+ },
23044
+ config: { tension: 200, friction: 30 }
23045
+ }));
23046
+ React.useEffect(() => {
23047
+ api.start({
23048
+ [isVertical ? "y" : "x"]: boundIndex(current.current) * -1 * 100,
23049
+ immediate: true
23110
23050
  });
23111
- } else if (touch.delta.current) {
23112
- move({ pace: 0 });
23113
- } else {
23114
- getStyle();
23115
- }
23116
- startPlay();
23117
- };
23118
- const getItemStyle = (index) => {
23119
- const style = {};
23120
- if (size) {
23121
- style[direction === "horizontal" ? "width" : "height"] = `${size}px`;
23122
- }
23123
- const offset2 = childOffset[index];
23124
- if (offset2) {
23125
- style.transform = `translate3D${direction === "horizontal" ? `(${rtl ? -offset2 : offset2}px,0,0)` : `(0,${offset2}px,0)`}`;
23126
- }
23127
- return style;
23128
- };
23129
- const init = (index) => {
23130
- const rect2 = getRect(container == null ? void 0 : container.current);
23131
- const currentIndex = Math.max(
23132
- Math.min(swiperItemCount - 1, index || Number(mergedProps.defaultValue)),
23133
- 0
23134
- );
23135
- const width2 = Number(mergedProps.width) || rect2.width;
23136
- const height2 = Number(mergedProps.height) || rect2.height;
23137
- size = isVertical ? height2 : width2;
23138
- trackSize = swiperItemCount * Number(size);
23139
- const targetOffset = getOffset(currentIndex);
23140
- swiperRef.current.moving = true;
23141
- if (ready) {
23142
- swiperRef.current.moving = false;
23143
- }
23144
- setRect(rect2);
23145
- setActive(currentIndex);
23146
- setWidth(width2);
23147
- setHeight(height2);
23148
- setOffset(targetOffset);
23149
- setReady(true);
23150
- };
23151
- React.useEffect(() => {
23152
- if (ready) {
23153
- getStyle();
23154
- }
23155
- }, [isVertical, width, height, offset, ready]);
23156
- React.useEffect(() => {
23157
- if (ready) {
23158
- stopAutoPlay();
23159
- startPlay();
23160
- }
23161
- return () => setReady(false);
23162
- }, [ready]);
23163
- React.useEffect(() => {
23164
- const events = [
23165
- { name: "touchstart", e: onTouchStart },
23166
- { name: "touchmove", e: onTouchMove },
23167
- { name: "touchend", e: onTouchEnd }
23168
- ];
23169
- events.forEach((item) => {
23051
+ }, [swiperRef.current]);
23052
+ const swiperDirection = React.useRef(1);
23053
+ const [transforms, setTransforms] = useList(effect, count, current);
23054
+ const runTimeSwiper = () => {
23055
+ const durationNumber = typeof duration === "string" ? parseInt(duration) : duration;
23056
+ const d = typeof autoPlay === "number" ? autoPlay : durationNumber;
23057
+ timeoutRef.current = window.setTimeout(() => {
23058
+ next();
23059
+ runTimeSwiper();
23060
+ }, d);
23061
+ };
23062
+ React.useEffect(() => {
23063
+ if (!autoPlay || dragging)
23064
+ return;
23065
+ runTimeSwiper();
23066
+ return () => {
23067
+ if (timeoutRef.current)
23068
+ window.clearTimeout(timeoutRef.current);
23069
+ };
23070
+ }, [autoPlay, duration, dragging, count]);
23071
+ function boundIndex(current2) {
23072
+ const min = 0;
23073
+ const max = count - 1;
23074
+ if (current2 === max && !loop2 && props.slideSize) {
23075
+ const slideSize = props.slideSize;
23076
+ const swiperSize = getSwiperSize();
23077
+ const ratio = (swiperSize - slideSize) / slideSize;
23078
+ return bound2(current2, min, max - ratio);
23079
+ }
23080
+ return current2;
23081
+ }
23082
+ const to2 = (index, immediate = false) => {
23170
23083
  var _a;
23171
- (_a = container.current) == null ? void 0 : _a.addEventListener(item.name, item.e, false);
23172
- });
23173
- return () => {
23174
- events.forEach((item) => {
23175
- var _a;
23176
- (_a = container.current) == null ? void 0 : _a.removeEventListener(item.name, item.e, false);
23084
+ let targetIndex = bound2(index, 0, count - 1);
23085
+ if (loop2) {
23086
+ const cycleIndex = index % count;
23087
+ targetIndex = cycleIndex < 0 ? cycleIndex + count : cycleIndex;
23088
+ }
23089
+ setCurrent(targetIndex);
23090
+ (_a = props.onChange) == null ? void 0 : _a.call(props, targetIndex);
23091
+ if (effect) {
23092
+ updateTransform(transforms, setTransforms, effect, targetIndex);
23093
+ }
23094
+ api.start({
23095
+ // 这里需要统一成百分比
23096
+ [isVertical ? "y" : "x"]: (loop2 ? -index : boundIndex(targetIndex) * -1) * 100,
23097
+ s: 0,
23098
+ immediate
23177
23099
  });
23178
23100
  };
23179
- });
23180
- React.useEffect(() => init(), [props.defaultValue]);
23181
- React.useEffect(() => {
23182
- return () => stopAutoPlay();
23183
- }, []);
23184
- const renderIndicator = () => {
23185
- if (React.isValidElement(indicator))
23186
- return indicator;
23187
- if (!indicator)
23188
- return null;
23189
- return /* @__PURE__ */ React.createElement(
23190
- "div",
23191
- {
23192
- className: classNames({
23193
- [`${classPrefix2}-indicator`]: true,
23194
- [`${classPrefix2}-indicator-vertical`]: isVertical
23195
- })
23101
+ const getSpringsAxis = () => {
23102
+ return springs[isVertical ? "y" : "x"];
23103
+ };
23104
+ const next = () => {
23105
+ to2(Math.round(-getSpringsAxis().get() / 100) + 1);
23106
+ };
23107
+ const prev = () => {
23108
+ to2(Math.round(-getSpringsAxis().get() / 100) - 1);
23109
+ };
23110
+ React.useImperativeHandle(ref, () => ({
23111
+ to: to2,
23112
+ next,
23113
+ prev
23114
+ }));
23115
+ const bind = useDrag(
23116
+ (state) => {
23117
+ const axis = Number(isVertical);
23118
+ const slideSize = getSlideSize();
23119
+ const offset = state.offset[axis];
23120
+ setDragging(!!state.dragging);
23121
+ const distance = state.distance[axis];
23122
+ swiperDirection.current = state.direction[axis];
23123
+ if (state.last) {
23124
+ const swipeDirection = state.direction[axis];
23125
+ const velocity = state.velocity[axis];
23126
+ const minIndex = Math.floor(offset / slideSize);
23127
+ const maxIndex = minIndex + 1;
23128
+ const index = Math.round(
23129
+ (offset + velocity * 2e3 * swipeDirection) / slideSize
23130
+ );
23131
+ to2(bound2(index, minIndex, maxIndex));
23132
+ } else {
23133
+ api.start({
23134
+ [isVertical ? "y" : "x"]: -(offset / slideSize * 100),
23135
+ s: distance / slideSize,
23136
+ immediate: true
23137
+ });
23138
+ }
23196
23139
  },
23197
- /* @__PURE__ */ React.createElement(
23198
- Indicator,
23199
- {
23200
- current: (active + swiperItemCount) % swiperItemCount,
23201
- total: swiperItems == null ? void 0 : swiperItems.length,
23202
- direction
23140
+ {
23141
+ enabled: touchable,
23142
+ transform: ([x, y]) => [-x, -y],
23143
+ from: () => {
23144
+ const slideSize = getSlideSize();
23145
+ const x = springs.x.get() / 100 * slideSize;
23146
+ const y = springs.y.get() / 100 * slideSize;
23147
+ return [-x, -y];
23148
+ },
23149
+ triggerAllEvents: true,
23150
+ bounds: () => {
23151
+ if (loop2)
23152
+ return {};
23153
+ const slideSize = getSlideSize();
23154
+ getSwiperSize();
23155
+ if (isVertical) {
23156
+ return { top: 0, bottom: (count - 1) * slideSize };
23157
+ }
23158
+ return { left: 0, right: (count - 1) * slideSize };
23159
+ },
23160
+ preventDefault: true,
23161
+ rubberband: true,
23162
+ axis: isVertical ? "y" : "x",
23163
+ pointer: {
23164
+ touch: true
23203
23165
  }
23204
- )
23166
+ }
23205
23167
  );
23206
- };
23207
- return /* @__PURE__ */ React.createElement(DataContext$1.Provider, { value: parent }, /* @__PURE__ */ React.createElement(
23208
- "div",
23209
- {
23210
- className: classNames(classPrefix2, className),
23211
- ref: container,
23212
- ...rest
23213
- },
23214
- /* @__PURE__ */ React.createElement("div", { className: contentClass, ref: innerRef }, React.Children.map(swiperItems, (child, index) => /* @__PURE__ */ React.createElement(
23168
+ const renderIndicator = () => {
23169
+ if (React.isValidElement(indicator))
23170
+ return indicator;
23171
+ if (!indicator)
23172
+ return null;
23173
+ return /* @__PURE__ */ React.createElement(
23174
+ "div",
23175
+ {
23176
+ className: classNames({
23177
+ [`${classPrefix2}-indicator`]: true,
23178
+ [`${classPrefix2}-indicator-vertical`]: isVertical,
23179
+ [`${classPrefix2}-indicator-horizontal`]: !isVertical
23180
+ })
23181
+ },
23182
+ /* @__PURE__ */ React.createElement(
23183
+ Indicator,
23184
+ {
23185
+ current: getRefValue(current),
23186
+ total: count,
23187
+ direction
23188
+ }
23189
+ )
23190
+ );
23191
+ };
23192
+ const renderEffect = () => {
23193
+ if (!effect)
23194
+ return defaultEffect({
23195
+ children,
23196
+ getSpringsAxis,
23197
+ loop: loop2,
23198
+ count,
23199
+ isVertical
23200
+ });
23201
+ if (effect && effect.name === "focus") {
23202
+ return focusEffect({
23203
+ children,
23204
+ springs,
23205
+ loop: loop2,
23206
+ count,
23207
+ isVertical,
23208
+ effect,
23209
+ current,
23210
+ swiperDirection,
23211
+ dragging,
23212
+ transforms
23213
+ });
23214
+ }
23215
+ };
23216
+ const renderSlides = () => {
23217
+ return /* @__PURE__ */ React.createElement(
23218
+ "div",
23219
+ {
23220
+ ref: stageRef,
23221
+ className: classNames("nut-swiper-inner", {
23222
+ "nut-swiper-inner-vertical": isVertical,
23223
+ "nut-swiper-inner-horizontal": !isVertical
23224
+ }),
23225
+ style: {
23226
+ ...props.slideSize ? { [isVertical ? "height" : "width"]: `${props.slideSize}px` } : {}
23227
+ }
23228
+ },
23229
+ renderEffect()
23230
+ );
23231
+ };
23232
+ return /* @__PURE__ */ React.createElement(
23215
23233
  "div",
23216
23234
  {
23217
- className: `${classPrefix2}-item-wrapper`,
23218
- style: getItemStyle(index),
23219
- key: index
23235
+ className: classNames("nut-swiper", className),
23236
+ style,
23237
+ ref: swiperRef,
23238
+ ...bind()
23220
23239
  },
23221
- child
23222
- ))),
23223
- renderIndicator()
23224
- ));
23225
- });
23240
+ renderSlides(),
23241
+ renderIndicator()
23242
+ );
23243
+ }
23244
+ );
23226
23245
  Swiper.defaultProps = defaultProps$h;
23227
23246
  Swiper.displayName = "NutSwiper";
23228
23247
  const defaultProps$g = {
23229
- direction: "horizontal"
23248
+ onClick: (e2) => void 0
23230
23249
  };
23231
- const SwiperItem = React.forwardRef((props, ref) => {
23250
+ const SwiperItem = (props) => {
23232
23251
  const classPrefix2 = "nut-swiper-item";
23233
- const _props = { ...defaultProps$g, ...props };
23234
- const { className, style, children, direction, size } = _props;
23235
- const parent = React.useContext(DataContext$1);
23252
+ const { className, style, children } = { ...defaultProps$g, ...props };
23236
23253
  const classes = classNames(classPrefix2, className);
23237
- const getStyle = () => {
23238
- const style2 = {};
23239
- const _direction = (parent == null ? void 0 : parent.propSwiper.direction) || direction;
23240
- const _size = (parent == null ? void 0 : parent.size) || size;
23241
- if (_size) {
23242
- style2[_direction === "horizontal" ? "width" : "height"] = `${_size}px`;
23243
- }
23244
- return style2;
23245
- };
23246
- return /* @__PURE__ */ React.createElement("div", { className: classes, style: { ...style, ...getStyle() } }, children);
23247
- });
23254
+ return /* @__PURE__ */ React.createElement("div", { className: classes, onClick: props.onClick, style }, children);
23255
+ };
23248
23256
  SwiperItem.defaultProps = defaultProps$g;
23249
23257
  SwiperItem.displayName = "NutSwiperItem";
23250
23258
  const InnerSwiper = Swiper;
@@ -23420,6 +23428,7 @@ Please add \`${key}Action\` when creating your handler.`);
23420
23428
  onCloseInner();
23421
23429
  }
23422
23430
  };
23431
+ const duration = typeof autoPlay === "string" ? parseInt(autoPlay) : autoPlay;
23423
23432
  return /* @__PURE__ */ React.createElement(
23424
23433
  Popup,
23425
23434
  {
@@ -23437,15 +23446,14 @@ Please add \`${key}Action\` when creating your handler.`);
23437
23446
  onClick: closeOnImg,
23438
23447
  onTouchStart
23439
23448
  },
23440
- /* @__PURE__ */ React.createElement(
23449
+ showPop ? /* @__PURE__ */ React.createElement(
23441
23450
  InnerSwiper,
23442
23451
  {
23443
- autoPlay,
23452
+ autoPlay: !!duration,
23453
+ duration,
23444
23454
  className: `${classPrefix2}-swiper`,
23445
23455
  loop: true,
23446
- preventDefault: false,
23447
23456
  style: {
23448
- display: showPop ? "block" : "none",
23449
23457
  "--nutui-indicator-color": indicatorColor
23450
23458
  },
23451
23459
  direction: "horizontal",
@@ -23457,10 +23465,10 @@ Please add \`${key}Action\` when creating your handler.`);
23457
23465
  return /* @__PURE__ */ React.createElement(SwiperItem, { key: index }, /* @__PURE__ */ React.createElement(Video, { source: item.source, options: item.options }));
23458
23466
  }) : []).concat(
23459
23467
  images && images.length > 0 ? images.map((item, index) => {
23460
- return /* @__PURE__ */ React.createElement(SwiperItem, { key: index }, /* @__PURE__ */ React.createElement(Image$1, { src: item.src }));
23468
+ return /* @__PURE__ */ React.createElement(SwiperItem, { key: index }, /* @__PURE__ */ React.createElement(Image$1, { src: item.src, draggable: false }));
23461
23469
  }) : []
23462
23470
  )
23463
- )
23471
+ ) : null
23464
23472
  ),
23465
23473
  /* @__PURE__ */ React.createElement("div", { className: `${classPrefix2}-index` }, active, "/", (images ? images.length : 0) + (videos ? videos.length : 0)),
23466
23474
  closeIcon !== false ? /* @__PURE__ */ React.createElement(