@meduza/ui-kit-2 1.4.2 → 1.4.3

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.
@@ -8,6 +8,7 @@ var dayjs = require('dayjs');
8
8
  require('dayjs/locale/ru');
9
9
  var relativeTime = require('dayjs/plugin/relativeTime');
10
10
  var reactRouterDom = require('react-router-dom');
11
+ var useEmblaCarousel = require('embla-carousel-react');
11
12
 
12
13
  const makeClassName = list => {
13
14
  return list.filter(([, active]) => active).map(([className]) => className).join(' ');
@@ -2672,7 +2673,7 @@ const ListViewSwitcher = ({
2672
2673
  className: styles$3.list
2673
2674
  })));
2674
2675
 
2675
- var styles$2 = {"root":"RelatedCarousel-module_root__G-Eub","track":"RelatedCarousel-module_track__UVQ2n","slide":"RelatedCarousel-module_slide__MI9tF","dots":"RelatedCarousel-module_dots__mOSIC","dotsTrack":"RelatedCarousel-module_dotsTrack__V7bnX","dotCell":"RelatedCarousel-module_dotCell__kl64s","dot":"RelatedCarousel-module_dot__Jo52r","isSmall":"RelatedCarousel-module_isSmall__ZP5hd","isTiny":"RelatedCarousel-module_isTiny__mjhtD","isActive":"RelatedCarousel-module_isActive__Nz2aj"};
2676
+ var styles$2 = {"root":"RelatedCarousel-module_root__G-Eub","viewport":"RelatedCarousel-module_viewport__HCzJi","track":"RelatedCarousel-module_track__UVQ2n","slide":"RelatedCarousel-module_slide__MI9tF","dots":"RelatedCarousel-module_dots__mOSIC","dotsTrack":"RelatedCarousel-module_dotsTrack__V7bnX","dotCell":"RelatedCarousel-module_dotCell__kl64s","dot":"RelatedCarousel-module_dot__Jo52r","isSmall":"RelatedCarousel-module_isSmall__ZP5hd","isTiny":"RelatedCarousel-module_isTiny__mjhtD","isActive":"RelatedCarousel-module_isActive__Nz2aj"};
2676
2677
 
2677
2678
  const VISIBLE_DOTS = 6;
2678
2679
  const EDGE_DOTS = 2;
@@ -2707,52 +2708,39 @@ const getDotScale = (index, start, total) => {
2707
2708
  }
2708
2709
  return 'full';
2709
2710
  };
2710
- const getSlideStep = track => {
2711
- if (!track) {
2712
- return 0;
2713
- }
2714
- const [first, second] = Array.from(track.children);
2715
- if (!first) {
2716
- return 0;
2717
- }
2718
- return second ? second.offsetLeft - first.offsetLeft : first.offsetWidth;
2719
- };
2720
2711
  const RelatedCarousel = ({
2721
2712
  children,
2722
2713
  ariaLabel
2723
2714
  }) => {
2724
- const trackRef = React.useRef(null);
2725
- const frameRef = React.useRef(0);
2715
+ const [viewportRef, embla] = useEmblaCarousel({
2716
+ loop: true,
2717
+ align: 'start',
2718
+ containScroll: false
2719
+ });
2726
2720
  const [activeIndex, setActiveIndex] = React.useState(0);
2727
2721
  const [dotsStart, setDotsStart] = React.useState(0);
2728
2722
  const slides = React.Children.toArray(children);
2729
- const lastIndex = slides.length - 1;
2730
2723
  const total = slides.length;
2724
+ const handleSelect = React.useCallback(() => {
2725
+ if (!embla) {
2726
+ return;
2727
+ }
2728
+ const index = embla.selectedScrollSnap();
2729
+ setActiveIndex(index);
2730
+ setDotsStart(start => getDotsStart(index, start, total));
2731
+ }, [embla, total]);
2731
2732
  React.useEffect(() => {
2732
- const track = trackRef.current;
2733
- if (!track) {
2733
+ if (!embla) {
2734
2734
  return undefined;
2735
2735
  }
2736
- const handleScroll = () => {
2737
- cancelAnimationFrame(frameRef.current);
2738
- frameRef.current = requestAnimationFrame(() => {
2739
- const step = getSlideStep(track);
2740
- if (!step) {
2741
- return;
2742
- }
2743
- const index = Math.max(0, Math.min(lastIndex, Math.round(track.scrollLeft / step)));
2744
- setActiveIndex(index);
2745
- setDotsStart(start => getDotsStart(index, start, lastIndex + 1));
2746
- });
2747
- };
2748
- track.addEventListener('scroll', handleScroll, {
2749
- passive: true
2750
- });
2736
+ handleSelect();
2737
+ embla.on('select', handleSelect);
2738
+ embla.on('reInit', handleSelect);
2751
2739
  return () => {
2752
- cancelAnimationFrame(frameRef.current);
2753
- track.removeEventListener('scroll', handleScroll);
2740
+ embla.off('select', handleSelect);
2741
+ embla.off('reInit', handleSelect);
2754
2742
  };
2755
- }, [lastIndex]);
2743
+ }, [embla, handleSelect]);
2756
2744
  const dotsStyle = {
2757
2745
  '--dotsVisible': Math.min(total, VISIBLE_DOTS),
2758
2746
  '--dotsStart': dotsStart
@@ -2762,13 +2750,15 @@ const RelatedCarousel = ({
2762
2750
  className: makeClassName([[styles$2.root, true]]),
2763
2751
  role: "group",
2764
2752
  "aria-label": ariaLabel
2765
- }, /*#__PURE__*/React.createElement("ul", {
2766
- className: styles$2.track,
2767
- ref: trackRef
2768
- }, slides.map((slide, index) => (/*#__PURE__*/React.createElement("li", {
2753
+ }, /*#__PURE__*/React.createElement("div", {
2754
+ className: styles$2.viewport,
2755
+ ref: viewportRef
2756
+ }, /*#__PURE__*/React.createElement("div", {
2757
+ className: styles$2.track
2758
+ }, slides.map((slide, index) => (/*#__PURE__*/React.createElement("div", {
2769
2759
  className: styles$2.slide,
2770
2760
  key: index
2771
- }, slide)))), /*#__PURE__*/React.createElement("div", {
2761
+ }, slide))))), /*#__PURE__*/React.createElement("div", {
2772
2762
  "aria-hidden": "true",
2773
2763
  className: styles$2.dots,
2774
2764
  style: dotsStyle