swiper-slide.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
  2. import React, { useRef, useState, forwardRef } from 'react';
  3. import { uniqueClasses } from './utils.js';
  4. import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js';
  5. import { SwiperSlideContext } from './context.js';
  6. const SwiperSlide = /*#__PURE__*/forwardRef(function (_temp, externalRef) {
  7. let {
  8. tag: Tag = 'div',
  9. children,
  10. className = '',
  11. swiper,
  12. zoom,
  13. virtualIndex,
  14. ...rest
  15. } = _temp === void 0 ? {} : _temp;
  16. const slideElRef = useRef(null);
  17. const [slideClasses, setSlideClasses] = useState('swiper-slide');
  18. function updateClasses(_s, el, classNames) {
  19. if (el === slideElRef.current) {
  20. setSlideClasses(classNames);
  21. }
  22. }
  23. useIsomorphicLayoutEffect(() => {
  24. if (externalRef) {
  25. externalRef.current = slideElRef.current;
  26. }
  27. if (!slideElRef.current || !swiper) {
  28. return;
  29. }
  30. if (swiper.destroyed) {
  31. if (slideClasses !== 'swiper-slide') {
  32. setSlideClasses('swiper-slide');
  33. }
  34. return;
  35. }
  36. swiper.on('_slideClass', updateClasses); // eslint-disable-next-line
  37. return () => {
  38. if (!swiper) return;
  39. swiper.off('_slideClass', updateClasses);
  40. };
  41. });
  42. useIsomorphicLayoutEffect(() => {
  43. if (swiper && slideElRef.current) {
  44. setSlideClasses(swiper.getSlideClasses(slideElRef.current));
  45. }
  46. }, [swiper]);
  47. const slideData = {
  48. isActive: slideClasses.indexOf('swiper-slide-active') >= 0 || slideClasses.indexOf('swiper-slide-duplicate-active') >= 0,
  49. isVisible: slideClasses.indexOf('swiper-slide-visible') >= 0,
  50. isDuplicate: slideClasses.indexOf('swiper-slide-duplicate') >= 0,
  51. isPrev: slideClasses.indexOf('swiper-slide-prev') >= 0 || slideClasses.indexOf('swiper-slide-duplicate-prev') >= 0,
  52. isNext: slideClasses.indexOf('swiper-slide-next') >= 0 || slideClasses.indexOf('swiper-slide-duplicate-next') >= 0
  53. };
  54. const renderChildren = () => {
  55. return typeof children === 'function' ? children(slideData) : children;
  56. };
  57. return /*#__PURE__*/React.createElement(Tag, _extends({
  58. ref: slideElRef,
  59. className: uniqueClasses(`${slideClasses}${className ? ` ${className}` : ''}`),
  60. "data-swiper-slide-index": virtualIndex
  61. }, rest), /*#__PURE__*/React.createElement(SwiperSlideContext.Provider, {
  62. value: slideData
  63. }, zoom ? /*#__PURE__*/React.createElement("div", {
  64. className: "swiper-zoom-container",
  65. "data-swiper-zoom": typeof zoom === 'number' ? zoom : undefined
  66. }, renderChildren()) : renderChildren()));
  67. });
  68. SwiperSlide.displayName = 'SwiperSlide';
  69. export { SwiperSlide };