@evermade/overflow-slider 4.1.0 → 4.2.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.
@@ -1,6 +1,7 @@
1
1
  import { Slider, DeepPartial } from '../../core/types';
2
2
 
3
3
  export type DotsOptions = {
4
+ type: 'view' | 'slide';
4
5
  texts: {
5
6
  dotDescription: string;
6
7
  },
@@ -23,6 +24,7 @@ const DEFAULT_CLASS_NAMES = {
23
24
  export default function DotsPlugin( args?: DeepPartial<DotsOptions> ) {
24
25
  return ( slider: Slider ) => {
25
26
  const options = <DotsOptions>{
27
+ type: args?.type ?? 'slide',
26
28
  texts: {
27
29
  ...DEFAULT_TEXTS,
28
30
  ...args?.texts || []
@@ -43,22 +45,24 @@ export default function DotsPlugin( args?: DeepPartial<DotsOptions> ) {
43
45
  dots.setAttribute( 'data-has-content', slider.details.hasOverflow.toString() );
44
46
  dots.innerHTML = '';
45
47
 
48
+ console.log('buildDots');
49
+
46
50
  const dotsList = document.createElement( 'ul' );
47
51
 
48
- const pages = slider.details.slideCount;
49
- const currentItem = slider.activeSlideIdx;
52
+ const count = options.type === 'view' ? slider.details.amountOfPages : slider.details.slideCount;
53
+ const currentIndex = options.type === 'view' ? slider.details.currentPage : slider.activeSlideIdx;
50
54
 
51
- if ( pages <= 1 ) {
55
+ if ( count <= 1 ) {
52
56
  return;
53
57
  }
54
58
 
55
- for ( let i = 0; i < pages; i++ ) {
59
+ for ( let i = 0; i < count; i++ ) {
56
60
  const dotListItem = document.createElement( 'li' );
57
61
  const dot = document.createElement( 'button' );
58
62
  dot.setAttribute( 'type', 'button' );
59
63
  dot.setAttribute( 'class', options.classNames.dotsItem );
60
- dot.setAttribute( 'aria-label', options.texts.dotDescription.replace( '%d', ( i + 1 ).toString() ).replace( '%d', pages.toString() ) );
61
- dot.setAttribute( 'aria-pressed', ( i === currentItem ).toString() );
64
+ dot.setAttribute( 'aria-label', options.texts.dotDescription.replace( '%d', ( i + 1 ).toString() ).replace( '%d', count.toString() ) );
65
+ dot.setAttribute( 'aria-pressed', ( i === currentIndex ).toString() );
62
66
  dot.setAttribute( 'data-item', ( i + 1 ).toString() );
63
67
  dotListItem.appendChild( dot );
64
68
  dotsList.appendChild( dotListItem );
@@ -71,23 +75,23 @@ export default function DotsPlugin( args?: DeepPartial<DotsOptions> ) {
71
75
  }
72
76
  const currentItem = parseInt( currentItemItem.getAttribute( 'data-item' ) ?? '1' );
73
77
  if ( e.key === 'ArrowLeft' ) {
74
- const previousPage = currentItem - 1;
75
- if ( previousPage > 0 ) {
76
- const matchingDot = dots.querySelector( `[data-item="${previousPage}"]` );
78
+ const previousIndex = currentItem - 1;
79
+ if ( previousIndex > 0 ) {
80
+ const matchingDot = dots.querySelector( `[data-item="${previousIndex}"]` );
77
81
  if ( matchingDot ) {
78
82
  ( <HTMLElement>matchingDot ).focus();
79
83
  }
80
- activateDot( previousPage );
84
+ activateDot( previousIndex );
81
85
  }
82
86
  }
83
87
  if ( e.key === 'ArrowRight' ) {
84
- const nextPage = currentItem + 1;
85
- if ( nextPage <= pages ) {
86
- const matchingDot = dots.querySelector( `[data-item="${nextPage}"]` );
88
+ const nextIndex = currentItem + 1;
89
+ if ( nextIndex <= count ) {
90
+ const matchingDot = dots.querySelector( `[data-item="${nextIndex}"]` );
87
91
  if ( matchingDot ) {
88
92
  ( <HTMLElement>matchingDot ).focus();
89
93
  }
90
- activateDot( nextPage );
94
+ activateDot( nextIndex );
91
95
  }
92
96
  }
93
97
  } );
@@ -103,9 +107,27 @@ export default function DotsPlugin( args?: DeepPartial<DotsOptions> ) {
103
107
  }
104
108
  }
105
109
  };
110
+ const activateDot = ( index: number ) => {
111
+ console.log('activateDot', index, 'slider.details', slider.details);
112
+
113
+ if ( options.type === 'view' ) {
114
+ const count = slider.details.amountOfPages;
115
+ let targetPosition = slider.details.containerWidth * ( index - 1 );
116
+
117
+ // For the last page, scroll to the maximum scroll position to ensure it activates
118
+ if ( index === count ) {
119
+ const maxScroll = slider.details.scrollableAreaWidth - slider.details.containerWidth;
120
+ targetPosition = maxScroll;
121
+ }
106
122
 
107
- const activateDot = ( item: number ) => {
108
- slider.moveToSlide( item - 1 );
123
+ const scrollLeft = slider.options.rtl ? -targetPosition : targetPosition;
124
+ slider.container.scrollTo({
125
+ left: scrollLeft,
126
+ behavior: slider.options.scrollBehavior as ScrollBehavior
127
+ });
128
+ } else {
129
+ slider.moveToSlide( index - 1 );
130
+ }
109
131
  };
110
132
 
111
133
  buildDots();
@@ -119,5 +141,6 @@ export default function DotsPlugin( args?: DeepPartial<DotsOptions> ) {
119
141
  slider.on( 'scrollEnd', buildDots );
120
142
  slider.on( 'contentsChanged', buildDots );
121
143
  slider.on( 'containerSizeChanged', buildDots );
144
+ slider.on( 'detailsChanged', buildDots );
122
145
  };
123
146
  };
@@ -49,7 +49,6 @@
49
49
  position: absolute;
50
50
  }
51
51
  &[aria-pressed="true"],
52
- &:focus,
53
52
  &:hover {
54
53
  background: var(--overflow-slider-dot-active-color);
55
54
  }