@twistedandy/better-carousel 1.1.0 → 1.1.2

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.
@@ -2,6 +2,7 @@
2
2
  * Motion: the easing, the duration of a goToSnap() motion, and the motion engine.
3
3
  */
4
4
 
5
+ import { abs } from './geometry.js';
5
6
  import { steps } from './loop.js';
6
7
 
7
8
  // The easing of a goToSnap() motion
@@ -30,10 +31,10 @@ export function bezier([x1, y1, x2, y2]) {
30
31
  for (let i = 0; i < 6; i++) {
31
32
  const error = axis(x1, x2, t) - x,
32
33
  slope = 3 * (1 + 3 * x1 - 3 * x2) * t * t + 2 * (3 * x2 - 6 * x1) * t + 3 * x1;
33
- if (Math.abs(error) < 1e-5) {
34
+ if (abs(error) < 1e-5) {
34
35
  return axis(y1, y2, t);
35
36
  }
36
- if (Math.abs(slope) < 1e-6) {
37
+ if (abs(slope) < 1e-6) {
37
38
  break;
38
39
  }
39
40
  t -= error / slope;
@@ -4,8 +4,7 @@
4
4
  * Each feature takes the context of a carousel (see carousel.js). It returns nothing if its option is off, or an object with remove().
5
5
  */
6
6
 
7
- import { find } from './carousel.js';
8
- import { create, optionElement, setAttribute, setClass } from './markup.js';
7
+ import { bind, create, createButton, optionElement, setAttribute, setClass, setStyle } from './markup.js';
9
8
 
10
9
  /**
11
10
  * Make the arrows and the dots of a carousel. They update on the carousel events.
@@ -40,32 +39,25 @@ export function nav(context) {
40
39
  }
41
40
 
42
41
  if (arrows) {
43
- [['prev', 'goToPrev'], ['next', 'goToNext']].forEach(([key, method]) => {
44
- const button = create('button', cls.button + ' ' + cls[key + 'Button']);
45
- button.type = 'button';
46
- button.setAttribute('aria-label', labels[key]);
47
- button.setAttribute('aria-controls', context.container.id);
48
- button.addEventListener('click', () => api[method]());
49
- buttons.push(button);
50
- });
42
+ buttons.push(createButton(cls.button + ' ' + cls.prevButton, context.container, labels.prev, () => api.goToPrev()), createButton(cls.button + ' ' + cls.nextButton, context.container, labels.next, () => api.goToNext()));
51
43
  }
52
44
 
53
45
  function update() {
54
46
  const fit = api.isFit(),
55
- count = fit ? 0 : api.snapProgress().length,
47
+ count = fit ? 0 : context.geo().logical.length,
56
48
  selected = api.selectedSnap();
57
49
 
58
- buttons.forEach((button, index) => {
50
+ buttons.forEach((item, index) => {
59
51
  if (fit) {
60
- button.remove();
52
+ item.remove();
61
53
  } else {
62
- if (!button.isConnected) {
54
+ if (!item.isConnected) {
63
55
  // Before the dots, for the tab order
64
- list?.parentNode === arrows ? list.before(button) : arrows.append(button);
56
+ list?.parentNode === arrows ? list.before(item) : arrows.append(item);
65
57
  }
66
58
  const disabled = !(index ? api.canGoToNext() : api.canGoToPrev());
67
- if (button.disabled !== disabled) {
68
- button.disabled = disabled;
59
+ if (item.disabled !== disabled) {
60
+ item.disabled = disabled;
69
61
  }
70
62
  }
71
63
  });
@@ -85,16 +77,12 @@ export function nav(context) {
85
77
  }
86
78
 
87
79
  while (dots.length < count) {
88
- const dot = create('button', cls.dot),
89
- index = dots.length;
90
- dot.type = 'button';
91
- dot.setAttribute('aria-label', labels.dot.replace('%d', index + 1));
92
- dot.setAttribute('aria-controls', context.container.id);
80
+ const index = dots.length,
81
+ dot = createButton(cls.dot, context.container, labels.dot.replace('%d', index + 1), () => api.goToSnap(index));
93
82
  // The window of the dots hides the dots out of the zoom: keep them out of the tab order
94
83
  if (windowed) {
95
84
  dot.tabIndex = -1;
96
85
  }
97
- dot.addEventListener('click', () => api.goToSnap(index));
98
86
  dots.push(dot);
99
87
  track.append(dot);
100
88
  }
@@ -130,23 +118,16 @@ export function nav(context) {
130
118
 
131
119
  // Center the group of up to 5 dots in the window of the dots, and move the marker to the selected dot. The CSS moves the track and the marker with "translate".
132
120
  if (windowed) {
133
- variable('--better-shift', 2 - (Math.max(0, selected - 2) + Math.min(count - 1, selected + 2)) / 2);
121
+ setStyle(list, '--better-shift', 2 - (Math.max(0, selected - 2) + Math.min(count - 1, selected + 2)) / 2);
134
122
  }
135
- variable('--better-dot', selected);
123
+ setStyle(list, '--better-dot', selected);
136
124
  }
137
125
 
138
- // Set a CSS variable on the window of the dots, if its value changes
139
- function variable(name, value) {
140
- if (list.style.getPropertyValue(name) !== String(value)) {
141
- list.style.setProperty(name, value);
142
- }
143
- }
144
-
145
- ['init', 'reinit', 'resize', 'select', 'slideschanged'].forEach((name) => context.on(name, update));
126
+ context.on('init reinit resize select slideschanged', update);
146
127
 
147
128
  return {
148
129
  remove() {
149
- buttons.forEach((button) => button.remove());
130
+ buttons.forEach((item) => item.remove());
150
131
  list?.remove();
151
132
  }
152
133
  };
@@ -176,11 +157,11 @@ export function sync(context) {
176
157
  api.goToSlide(index, instant === true);
177
158
  }
178
159
 
160
+ // A click on a clone comes to its slide. See onClick() in carousel.js.
179
161
  function click(event) {
180
- // The slide or the clone that gets the click, and then its original slide
181
- const index = context.nodes().findIndex((node) => node.contains(event.target));
162
+ const index = api.slideNodes().findIndex((node) => node.contains(event.target));
182
163
  if (other && index > -1) {
183
- other.goToSlide(index % api.slideNodes().length);
164
+ other.goToSlide(index);
184
165
  }
185
166
  }
186
167
 
@@ -189,21 +170,22 @@ export function sync(context) {
189
170
  if (other) {
190
171
  return;
191
172
  }
192
- other = target.goToSnap ? target : find(typeof target === 'string' ? optionElement(target, context.root) : target);
173
+ other = target.goToSnap ? target : context.find(typeof target === 'string' ? optionElement(target, context.root) : target);
193
174
  if (other) {
194
175
  other.on('select', show);
195
176
  show(true);
196
177
  }
197
178
  }
198
179
 
199
- context.on('init', connect);
200
- context.on('reinit', connect);
201
- container.addEventListener('click', click);
180
+ context.on('init reinit', connect);
181
+ const listen = bind(container, [['click', click]]);
182
+
183
+ listen(true);
202
184
 
203
185
  return {
204
186
  remove() {
205
187
  other?.off('select', show);
206
- container.removeEventListener('click', click);
188
+ listen();
207
189
  }
208
190
  };
209
191
  }
@@ -29,6 +29,11 @@ $better-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'
29
29
  z-index: 1;
30
30
  }
31
31
 
32
+ // The width comes from the parent, not from the slides. Without it, a carousel in a flex item or a grid column grows to the width of all slides.
33
+ .#{$better}_horizontal .#{$better}_viewport {
34
+ contain: inline-size;
35
+ }
36
+
32
37
  .#{$better}_horizontal .#{$better}_container {
33
38
  overflow: visible;
34
39
  --gap-grid-y: 0px;
@@ -163,10 +163,10 @@ export interface Options {
163
163
  startSnap?: number;
164
164
  /** `true`: start when the carousel comes within one screen of the view. A number: the distance in px. @default false @see {@link https://twistedandy.github.io/better-carousel/#lazy | example} */
165
165
  lazy?: boolean | number;
166
- /** Drag with the mouse, the pen and the finger. @default true */
166
+ /** Drag with the mouse, the pen and the finger. A press on a form field does not start a drag. To keep the drag off another element, such as a map in a slide, stop the propagation of its `pointerdown` event. @default true */
167
167
  drag?: boolean;
168
- /** The mouse wheel moves a vertical carousel. `'both'`: also a horizontal carousel. `false`: the wheel does not move the carousel. @default true @see {@link https://twistedandy.github.io/better-carousel/#vertical | example} */
169
- wheel?: boolean | 'both';
168
+ /** The mouse wheel moves a vertical carousel. `'axis'`: also a horizontal carousel, but only with a horizontal wheel motion, such as a trackpad swipe. A vertical wheel scrolls the page. `'both'`: also a horizontal carousel, with all wheel motions. `false`: the wheel does not move the carousel. @default true @see {@link https://twistedandy.github.io/better-carousel/#vertical | example} */
169
+ wheel?: boolean | 'axis' | 'both';
170
170
  /** The arrow keys move the carousel while the keyboard focus is in it: left and right in a horizontal carousel, up and down in a vertical carousel. A key in a form field does not move the carousel. @default true */
171
171
  keys?: boolean;
172
172
  /** Measure again when a size changes. @default true @see {@link https://twistedandy.github.io/better-carousel/#options-usage | example} */
@@ -193,7 +193,7 @@ export interface Options {
193
193
  dots?: boolean | Element | string;
194
194
  /** The style of the dots. `'zoom'`: a window of 5 dots that slides, with smaller dots at the ends. `'all'`: all dots with the same size. @default 'zoom' */
195
195
  dotsStyle?: 'zoom' | 'all';
196
- /** The texts for screen readers: `carousel`, `slide` (`'%d of %n'`), `prev`, `next`, `dot` (`'Go to slide %d'`), `play`, `pause`. `%d` is the slide number, and `%n` is the number of slides. @see {@link https://twistedandy.github.io/better-carousel/#accessibility | example} */
196
+ /** The texts for screen readers: `carousel`, `slide` (`'%d of %n'`), `prev`, `next`, `dot` (`'Go to slide %d'`), `play`, `pause`. `%d` is the slide number, and `%n` is the number of slides. An empty text adds no name. @see {@link https://twistedandy.github.io/better-carousel/#accessibility | example} */
197
197
  labels?: Labels;
198
198
  /** A class prefix such as `'embla'`, or an object with the names to change. The Classes table gives all names. @default null @see {@link https://twistedandy.github.io/better-carousel/#embla | example} */
199
199
  classes?: string | ClassNames | null;