@ember-joinery/carousel 0.0.0-stage → 1.1.0
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.
- package/CHANGELOG.md +175 -0
- package/LICENSE.md +9 -0
- package/NOTICE +532 -0
- package/README.md +41 -2
- package/addon-main.cjs +4 -0
- package/declarations/-private/direction.d.ts +7 -0
- package/declarations/-private/direction.d.ts.map +1 -0
- package/declarations/-private/waiter.d.ts +12 -0
- package/declarations/-private/waiter.d.ts.map +1 -0
- package/declarations/components/carousel/content.d.ts +28 -0
- package/declarations/components/carousel/content.d.ts.map +1 -0
- package/declarations/components/carousel/index.d.ts +104 -0
- package/declarations/components/carousel/index.d.ts.map +1 -0
- package/declarations/components/carousel/item.d.ts +33 -0
- package/declarations/components/carousel/item.d.ts.map +1 -0
- package/declarations/components/carousel/state.d.ts +58 -0
- package/declarations/components/carousel/state.d.ts.map +1 -0
- package/declarations/components/carousel/step.d.ts +59 -0
- package/declarations/components/carousel/step.d.ts.map +1 -0
- package/declarations/components/carousel.d.ts +3 -0
- package/declarations/embla.d.ts +49 -0
- package/declarations/embla.d.ts.map +1 -0
- package/declarations/index.d.ts +5 -0
- package/declarations/index.d.ts.map +1 -0
- package/declarations/messages.d.ts +30 -0
- package/declarations/messages.d.ts.map +1 -0
- package/declarations/test-support/carousel.d.ts +84 -0
- package/declarations/test-support/carousel.d.ts.map +1 -0
- package/declarations/test-support/index.d.ts +2 -0
- package/declarations/test-support/index.d.ts.map +1 -0
- package/declarations/test-support.d.ts +2 -0
- package/dist/components/carousel.js +383 -0
- package/dist/components/carousel.js.map +1 -0
- package/dist/embla-C9buOK_5.js +186 -0
- package/dist/embla-C9buOK_5.js.map +1 -0
- package/dist/embla.js +7 -0
- package/dist/embla.js.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/messages.js +32 -0
- package/dist/messages.js.map +1 -0
- package/dist/test-support.js +215 -0
- package/dist/test-support.js.map +1 -0
- package/package.json +116 -3
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import Component from '@glimmer/component';
|
|
2
|
+
import { get, hash } from '@ember/helper';
|
|
3
|
+
import { on } from '@ember/modifier';
|
|
4
|
+
import { isDestroying, associateDestroyableChild } from '@ember/destroyable';
|
|
5
|
+
import { As } from '@ember-joinery/primitives/as';
|
|
6
|
+
import { E as EmblaModifier, d as defer } from '../embla-C9buOK_5.js';
|
|
7
|
+
import { precompileTemplate } from '@ember/template-compilation';
|
|
8
|
+
import { setComponentTemplate } from '@ember/component';
|
|
9
|
+
import templateOnly from '@ember/component/template-only';
|
|
10
|
+
import { part } from '@ember-joinery/primitives/part';
|
|
11
|
+
import { translate, t } from '@ember-joinery/primitives/i18n';
|
|
12
|
+
import { messages } from '../messages.js';
|
|
13
|
+
import { tracked } from '@glimmer/tracking';
|
|
14
|
+
import { getDirection } from '@ember-joinery/primitives/direction';
|
|
15
|
+
import { idFor } from '@ember-joinery/primitives/id';
|
|
16
|
+
import { g, i } from 'decorator-transforms/runtime';
|
|
17
|
+
import { ChevronLeft, ChevronRight } from 'lucide';
|
|
18
|
+
import { VisuallyHidden } from '@ember-joinery/primitives/visually-hidden';
|
|
19
|
+
import Button, { buttonAttributes, buttonClasses } from '@ember-joinery/ember/components/button';
|
|
20
|
+
import Icon from '@ember-joinery/ember/components/icon';
|
|
21
|
+
import { setOwner } from '@ember/owner';
|
|
22
|
+
import { modifier } from 'ember-modifier';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The slides, yielded as `c.Content`. As upstream's, it is two elements: Embla's viewport,
|
|
26
|
+
* which clips (`data-slot="carousel-content"`), and inside it the row (or, vertically, the
|
|
27
|
+
* column) of slides Embla moves. Your attributes and `class` go on the row, as upstream's
|
|
28
|
+
* `className` does, so `-ms-2` (with `ps-2` on each slide) changes the spacing, and a
|
|
29
|
+
* vertical carousel's height goes there too.
|
|
30
|
+
*/
|
|
31
|
+
const CarouselContent = setComponentTemplate(precompileTemplate("<div id={{@carousel.contentId}} class=\"jn-carousel-content\" data-slot=\"carousel-content\" data-test-jn-part=\"content\" {{embla options=@carousel.options plugins=@carousel.plugins onInit=@carousel.onInit}}>\n <As @as={{@as}} @tag=\"div\" class=\"jn-carousel-container\" data-orientation={{@carousel.orientation}} data-test-jn-part=\"container\" ...attributes>{{yield}}</As>\n</div>", {
|
|
32
|
+
strictMode: true,
|
|
33
|
+
scope: () => ({
|
|
34
|
+
embla: EmblaModifier,
|
|
35
|
+
As
|
|
36
|
+
})
|
|
37
|
+
}), templateOnly());
|
|
38
|
+
|
|
39
|
+
function itemAttributes(orientation, context) {
|
|
40
|
+
return {
|
|
41
|
+
role: 'group',
|
|
42
|
+
'aria-roledescription': translate(context, messages.slide),
|
|
43
|
+
'data-slot': 'carousel-item',
|
|
44
|
+
'data-orientation': orientation,
|
|
45
|
+
'data-test-jn-part': 'item'
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** A slide as a modifier, for an element of your own (`<section {{c.item}}>`). */
|
|
49
|
+
function itemModifier(carousel) {
|
|
50
|
+
return part(() => ({
|
|
51
|
+
class: 'jn-carousel-item',
|
|
52
|
+
attributes: itemAttributes(carousel.orientation, carousel)
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A slide, yielded as `c.Item`: as wide as the carousel by default (`basis-full`); give it a
|
|
57
|
+
* `basis-*` class to show several at once.
|
|
58
|
+
*/
|
|
59
|
+
const CarouselItem = setComponentTemplate(precompileTemplate("{{#let (itemAttributes @carousel.orientation @carousel) as |a|}}\n <As @as={{@as}} @tag=\"div\" role={{get a \"role\"}} aria-roledescription={{get a \"aria-roledescription\"}} class=\"jn-carousel-item\" data-slot={{get a \"data-slot\"}} data-orientation={{get a \"data-orientation\"}} data-test-jn-part={{get a \"data-test-jn-part\"}} ...attributes>{{yield}}</As>\n{{/let}}", {
|
|
60
|
+
strictMode: true,
|
|
61
|
+
scope: () => ({
|
|
62
|
+
itemAttributes,
|
|
63
|
+
As,
|
|
64
|
+
get
|
|
65
|
+
})
|
|
66
|
+
}), templateOnly());
|
|
67
|
+
|
|
68
|
+
/** The labels this carousel gave its slides, so a label of the caller's own is left alone. */
|
|
69
|
+
const labels = new WeakMap();
|
|
70
|
+
|
|
71
|
+
/** Keys that type into a field rather than move the carousel. */
|
|
72
|
+
function isEditable(target) {
|
|
73
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
74
|
+
return target.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A carousel's state: Embla's API once it exists, whether it can scroll either way, and the
|
|
79
|
+
* actions its parts share. `<Carousel>` owns one and hands it to each part, as upstream's
|
|
80
|
+
* context does.
|
|
81
|
+
*/
|
|
82
|
+
class CarouselState {
|
|
83
|
+
static {
|
|
84
|
+
g(this.prototype, "api", [tracked]);
|
|
85
|
+
}
|
|
86
|
+
#api = (i(this, "api"), void 0);
|
|
87
|
+
/** Embla's API, once the carousel's content has rendered. */
|
|
88
|
+
static {
|
|
89
|
+
g(this.prototype, "canScrollPrev", [tracked], function () {
|
|
90
|
+
return false;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
#canScrollPrev = (i(this, "canScrollPrev"), void 0);
|
|
94
|
+
/** Whether there is a slide (or snap) before the current one. */
|
|
95
|
+
static {
|
|
96
|
+
g(this.prototype, "canScrollNext", [tracked], function () {
|
|
97
|
+
return false;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
#canScrollNext = (i(this, "canScrollNext"), void 0);
|
|
101
|
+
/** Whether there is a slide (or snap) after the current one. */
|
|
102
|
+
#args;
|
|
103
|
+
#scheduled = false;
|
|
104
|
+
constructor(args) {
|
|
105
|
+
this.#args = args;
|
|
106
|
+
}
|
|
107
|
+
get orientation() {
|
|
108
|
+
return this.#args.orientation() ?? 'horizontal';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Embla's options: the caller's `@opts`, with the axis the orientation gives, as upstream's. */
|
|
112
|
+
get options() {
|
|
113
|
+
return {
|
|
114
|
+
...this.#args.opts(),
|
|
115
|
+
axis: this.orientation === 'horizontal' ? 'x' : 'y'
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
get plugins() {
|
|
119
|
+
return this.#args.plugins() ?? [];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The id of the content, the element holding the slides, which the buttons control. */
|
|
123
|
+
get contentId() {
|
|
124
|
+
return idFor(this, 'content');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Embla made the carousel: keep its state current, and hand its API to `@setApi`. */
|
|
128
|
+
onInit = api => {
|
|
129
|
+
if (isDestroying(this)) return;
|
|
130
|
+
this.api = api;
|
|
131
|
+
api.on('select', this.#changed).on('reInit', this.#changed).on('slidesChanged', this.#changed);
|
|
132
|
+
api.on('destroy', () => defer(() => {
|
|
133
|
+
if (this.api !== api || isDestroying(this)) return;
|
|
134
|
+
this.api = undefined;
|
|
135
|
+
this.canScrollPrev = this.canScrollNext = false;
|
|
136
|
+
}));
|
|
137
|
+
this.#update();
|
|
138
|
+
this.#args.setApi()?.(api);
|
|
139
|
+
};
|
|
140
|
+
scrollPrev = () => {
|
|
141
|
+
this.api?.scrollPrev();
|
|
142
|
+
};
|
|
143
|
+
scrollNext = () => {
|
|
144
|
+
this.api?.scrollNext();
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Scrolls to a snap by index (a slide's, unless several show at once). It takes nothing
|
|
149
|
+
* else, so `{{on "click" (fn c.scrollTo 2)}}` works; to jump there without the animation,
|
|
150
|
+
* use the API: `c.api.scrollTo(2, true)`.
|
|
151
|
+
*/
|
|
152
|
+
scrollTo = index => {
|
|
153
|
+
this.api?.scrollTo(index);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The arrow keys, anywhere inside the carousel (upstream listens in the capture phase too):
|
|
158
|
+
* the left arrow scrolls back and the right arrow forward, the other way round right to left;
|
|
159
|
+
* a vertical carousel takes the up and down arrows as well. Keys typed into a field are left
|
|
160
|
+
* to it.
|
|
161
|
+
*/
|
|
162
|
+
onKeyDown = event => {
|
|
163
|
+
if (isEditable(event.target)) return;
|
|
164
|
+
const rtl = getDirection(event.currentTarget) === 'rtl';
|
|
165
|
+
const vertical = this.orientation === 'vertical';
|
|
166
|
+
let step = 0;
|
|
167
|
+
if (event.key === 'ArrowLeft') step = rtl ? 1 : -1;else if (event.key === 'ArrowRight') step = rtl ? -1 : 1;else if (vertical && event.key === 'ArrowUp') step = -1;else if (vertical && event.key === 'ArrowDown') step = 1;
|
|
168
|
+
if (!step) return;
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
if (step < 0) this.scrollPrev();else this.scrollNext();
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* An Embla event: the selection or the slides changed. It can come while the carousel
|
|
175
|
+
* reinitializes during a render, so the update waits for that render to finish.
|
|
176
|
+
*/
|
|
177
|
+
#changed = () => {
|
|
178
|
+
if (this.#scheduled) return;
|
|
179
|
+
this.#scheduled = true;
|
|
180
|
+
defer(() => {
|
|
181
|
+
this.#scheduled = false;
|
|
182
|
+
this.#update();
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
#update() {
|
|
186
|
+
const api = this.api;
|
|
187
|
+
if (!api || isDestroying(this)) return;
|
|
188
|
+
this.canScrollPrev = api.canScrollPrev();
|
|
189
|
+
this.canScrollNext = api.canScrollNext();
|
|
190
|
+
this.labelSlides();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Names each slide by its place (`2 of 5`), as the APG's carousel pattern asks, unless it has
|
|
195
|
+
* a name of the caller's own (`aria-label` or `aria-labelledby`). It reads the translator's
|
|
196
|
+
* tracked locale, so the carousel's modifier runs it again when the language changes.
|
|
197
|
+
*/
|
|
198
|
+
labelSlides() {
|
|
199
|
+
const api = this.api;
|
|
200
|
+
if (!api) return;
|
|
201
|
+
const slideLabel = this.#args.slideLabel() ?? ((index, count) => translate(this, messages.slideLabel, {
|
|
202
|
+
index,
|
|
203
|
+
count
|
|
204
|
+
}));
|
|
205
|
+
const slides = api.slideNodes();
|
|
206
|
+
slides.forEach((slide, index) => {
|
|
207
|
+
if (slide.getAttribute('data-slot') !== 'carousel-item') return;
|
|
208
|
+
if (slide.hasAttribute('aria-labelledby')) return;
|
|
209
|
+
const current = slide.getAttribute('aria-label');
|
|
210
|
+
if (current !== null && current !== labels.get(slide)) return;
|
|
211
|
+
const label = slideLabel(index + 1, slides.length);
|
|
212
|
+
labels.set(slide, label);
|
|
213
|
+
if (current !== label) slide.setAttribute('aria-label', label);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** A step button's look: what both `<c.Previous>` and the `c.previous` modifier take. */
|
|
219
|
+
/**
|
|
220
|
+
* The classes a step button renders: a Button's (`outline`, `icon-sm` by default), then
|
|
221
|
+
* upstream's `cn-carousel-previous` or `cn-carousel-next`, renamed `jn-`.
|
|
222
|
+
*/
|
|
223
|
+
function stepClasses(step, options = {}) {
|
|
224
|
+
const {
|
|
225
|
+
variant = 'outline',
|
|
226
|
+
size = 'icon-sm'
|
|
227
|
+
} = options;
|
|
228
|
+
return `${buttonClasses({
|
|
229
|
+
variant,
|
|
230
|
+
size
|
|
231
|
+
})} jn-carousel-${step}`;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The attributes a step button renders: a Button's, with upstream's `data-slot`, the
|
|
235
|
+
* orientation its rules key on, the content it controls, and `disabled` when there is nowhere
|
|
236
|
+
* to go.
|
|
237
|
+
*/
|
|
238
|
+
function stepAttributes(step, options, state) {
|
|
239
|
+
const {
|
|
240
|
+
variant = 'outline',
|
|
241
|
+
size = 'icon-sm',
|
|
242
|
+
name
|
|
243
|
+
} = options;
|
|
244
|
+
return {
|
|
245
|
+
...buttonAttributes({
|
|
246
|
+
variant,
|
|
247
|
+
size,
|
|
248
|
+
name
|
|
249
|
+
}),
|
|
250
|
+
'data-slot': `carousel-${step}`,
|
|
251
|
+
'data-orientation': state.orientation,
|
|
252
|
+
'data-test-jn-part': step,
|
|
253
|
+
'aria-controls': state.contentId,
|
|
254
|
+
disabled: !state.canScroll
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/** A step button's state, read from the carousel's. */
|
|
258
|
+
function stateOf(carousel, step) {
|
|
259
|
+
return {
|
|
260
|
+
orientation: carousel.orientation,
|
|
261
|
+
contentId: carousel.contentId,
|
|
262
|
+
canScroll: step === 'previous' ? carousel.canScrollPrev : carousel.canScrollNext
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* A step button as a modifier, for a `<button>` (or button component) of your own, with the
|
|
267
|
+
* same classes and attributes as `<c.Previous>` or `<c.Next>`, and a click that steps.
|
|
268
|
+
*/
|
|
269
|
+
function stepModifier(carousel, step) {
|
|
270
|
+
return part(options => ({
|
|
271
|
+
class: stepClasses(step, options),
|
|
272
|
+
attributes: stepAttributes(step, options, stateOf(carousel, step)),
|
|
273
|
+
on: {
|
|
274
|
+
click: step === 'previous' ? carousel.scrollPrev : carousel.scrollNext
|
|
275
|
+
}
|
|
276
|
+
}));
|
|
277
|
+
}
|
|
278
|
+
const attributesOf = (carousel, step, variant, size) => stepAttributes(step, {
|
|
279
|
+
variant,
|
|
280
|
+
size
|
|
281
|
+
}, stateOf(carousel, step));
|
|
282
|
+
/**
|
|
283
|
+
* The previous slide's button, yielded as `c.Previous`: an outline icon Button, outside the
|
|
284
|
+
* slides at their start (above them, vertically), disabled when there is no slide before.
|
|
285
|
+
*/
|
|
286
|
+
const CarouselPrevious = setComponentTemplate(precompileTemplate("{{#let (attributesOf @carousel \"previous\" @variant @size) as |a|}}\n <Button @variant={{if @variant @variant \"outline\"}} @size={{if @size @size \"icon-sm\"}} @name={{@name}} type=\"button\" class=\"jn-carousel-previous\" data-slot={{get a \"data-slot\"}} data-orientation={{get a \"data-orientation\"}} data-test-jn-part={{get a \"data-test-jn-part\"}} aria-controls={{get a \"aria-controls\"}} disabled={{get a \"disabled\"}} ...attributes {{on \"click\" @carousel.scrollPrev}}>\n <Icon @icon={{if @icon @icon ChevronLeft}} class=\"jn-rtl-flip\" />\n <VisuallyHidden>{{if @label @label (t messages.previous)}}</VisuallyHidden>\n </Button>\n{{/let}}", {
|
|
287
|
+
strictMode: true,
|
|
288
|
+
scope: () => ({
|
|
289
|
+
attributesOf,
|
|
290
|
+
Button,
|
|
291
|
+
get,
|
|
292
|
+
on,
|
|
293
|
+
Icon,
|
|
294
|
+
ChevronLeft,
|
|
295
|
+
VisuallyHidden,
|
|
296
|
+
t,
|
|
297
|
+
messages
|
|
298
|
+
})
|
|
299
|
+
}), templateOnly());
|
|
300
|
+
/**
|
|
301
|
+
* The next slide's button, yielded as `c.Next`: an outline icon Button, outside the slides at
|
|
302
|
+
* their end (below them, vertically), disabled when there is no slide after.
|
|
303
|
+
*/
|
|
304
|
+
const CarouselNext = setComponentTemplate(precompileTemplate("{{#let (attributesOf @carousel \"next\" @variant @size) as |a|}}\n <Button @variant={{if @variant @variant \"outline\"}} @size={{if @size @size \"icon-sm\"}} @name={{@name}} type=\"button\" class=\"jn-carousel-next\" data-slot={{get a \"data-slot\"}} data-orientation={{get a \"data-orientation\"}} data-test-jn-part={{get a \"data-test-jn-part\"}} aria-controls={{get a \"aria-controls\"}} disabled={{get a \"disabled\"}} ...attributes {{on \"click\" @carousel.scrollNext}}>\n <Icon @icon={{if @icon @icon ChevronRight}} class=\"jn-rtl-flip\" />\n <VisuallyHidden>{{if @label @label (t messages.next)}}</VisuallyHidden>\n </Button>\n{{/let}}", {
|
|
305
|
+
strictMode: true,
|
|
306
|
+
scope: () => ({
|
|
307
|
+
attributesOf,
|
|
308
|
+
Button,
|
|
309
|
+
get,
|
|
310
|
+
on,
|
|
311
|
+
Icon,
|
|
312
|
+
ChevronRight,
|
|
313
|
+
VisuallyHidden,
|
|
314
|
+
t,
|
|
315
|
+
messages
|
|
316
|
+
})
|
|
317
|
+
}), templateOnly());
|
|
318
|
+
|
|
319
|
+
const labelSlides = modifier((_, [state]) => state.labelSlides());
|
|
320
|
+
|
|
321
|
+
/** Embla's API, as `@setApi` hands it over and `c.api` yields it: upstream's `CarouselApi`. */
|
|
322
|
+
/** Embla's options, as `@opts` takes them: upstream's `CarouselOptions`. */
|
|
323
|
+
/** An Embla plugin, as `@plugins` takes them: upstream's `CarouselPlugin`. */
|
|
324
|
+
/** What `<Carousel>` yields: its parts, their modifiers, and its state, as upstream's `useCarousel()`. */
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* A carousel with motion and swipe built using Embla: shadcn's Carousel, on the
|
|
328
|
+
* framework-agnostic `embla-carousel`.
|
|
329
|
+
*
|
|
330
|
+
* <Carousel class="w-full max-w-xs" as |c|>
|
|
331
|
+
* <c.Content>
|
|
332
|
+
* <c.Item>…</c.Item>
|
|
333
|
+
* <c.Item>…</c.Item>
|
|
334
|
+
* </c.Content>
|
|
335
|
+
* <c.Previous />
|
|
336
|
+
* <c.Next />
|
|
337
|
+
* </Carousel>
|
|
338
|
+
*
|
|
339
|
+
* It is a region described as a carousel, its slides are groups described as slides and named
|
|
340
|
+
* by their place (`2 of 5`), and the arrow keys anywhere inside it move between them (flipped
|
|
341
|
+
* right to left). Give it an `aria-label` to name it.
|
|
342
|
+
*/
|
|
343
|
+
class Carousel extends Component {
|
|
344
|
+
state;
|
|
345
|
+
item;
|
|
346
|
+
previous;
|
|
347
|
+
next;
|
|
348
|
+
constructor(owner, args) {
|
|
349
|
+
super(owner, args);
|
|
350
|
+
this.state = associateDestroyableChild(this, new CarouselState({
|
|
351
|
+
orientation: () => this.args.orientation,
|
|
352
|
+
opts: () => this.args.opts,
|
|
353
|
+
plugins: () => this.args.plugins,
|
|
354
|
+
setApi: () => this.args.setApi,
|
|
355
|
+
slideLabel: () => this.args.slideLabel
|
|
356
|
+
}));
|
|
357
|
+
// The state names the slides itself, in the words of the app's translator.
|
|
358
|
+
setOwner(this.state, owner);
|
|
359
|
+
this.item = itemModifier(this.state);
|
|
360
|
+
this.previous = stepModifier(this.state, 'previous');
|
|
361
|
+
this.next = stepModifier(this.state, 'next');
|
|
362
|
+
}
|
|
363
|
+
static {
|
|
364
|
+
setComponentTemplate(precompileTemplate("<As @as={{@as}} @tag=\"div\" role=\"region\" aria-roledescription={{t messages.carousel}} class=\"jn-carousel\" data-slot=\"carousel\" data-orientation={{this.state.orientation}} data-test-jn=\"carousel\" data-test-jn-name={{@name}} ...attributes {{on \"keydown\" this.state.onKeyDown capture=true}} {{labelSlides this.state}}>\n {{yield (hash Content=(component CarouselContent carousel=this.state) Item=(component CarouselItem carousel=this.state) Previous=(component CarouselPrevious carousel=this.state) Next=(component CarouselNext carousel=this.state) item=this.item previous=this.previous next=this.next api=this.state.api orientation=this.state.orientation canScrollPrev=this.state.canScrollPrev canScrollNext=this.state.canScrollNext scrollPrev=this.state.scrollPrev scrollNext=this.state.scrollNext scrollTo=this.state.scrollTo)}}\n</As>", {
|
|
365
|
+
strictMode: true,
|
|
366
|
+
scope: () => ({
|
|
367
|
+
As,
|
|
368
|
+
t,
|
|
369
|
+
messages,
|
|
370
|
+
on,
|
|
371
|
+
labelSlides,
|
|
372
|
+
hash,
|
|
373
|
+
CarouselContent,
|
|
374
|
+
CarouselItem,
|
|
375
|
+
CarouselPrevious,
|
|
376
|
+
CarouselNext
|
|
377
|
+
})
|
|
378
|
+
}), this);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export { Carousel as default };
|
|
383
|
+
//# sourceMappingURL=carousel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"carousel.js","sources":["../../src/components/carousel/content.gts","../../src/components/carousel/item.gts","../../src/components/carousel/state.ts","../../src/components/carousel/step.gts","../../src/components/carousel/index.gts"],"sourcesContent":["import type { TOC } from '@ember/component/template-only'\nimport { As, type TagName } from '@ember-joinery/primitives/as'\nimport { embla } from '../../embla.ts'\nimport type { CarouselState } from './state.ts'\n\nexport interface CarouselContentSignature {\n Args: {\n /**\n * The tag of the element around the slides, instead of `div`. Not a list: its slides are\n * groups, which a list can't hold.\n */\n as?: TagName\n /** @internal Bound by `<Carousel>`. */\n carousel: CarouselState\n }\n Blocks: { default: [] }\n /** The element around the slides, which Embla moves: attributes and `class` go here. */\n Element: HTMLElement\n}\n\n/**\n * The slides, yielded as `c.Content`. As upstream's, it is two elements: Embla's viewport,\n * which clips (`data-slot=\"carousel-content\"`), and inside it the row (or, vertically, the\n * column) of slides Embla moves. Your attributes and `class` go on the row, as upstream's\n * `className` does, so `-ms-2` (with `ps-2` on each slide) changes the spacing, and a\n * vertical carousel's height goes there too.\n */\nexport const CarouselContent: TOC<CarouselContentSignature> = <template>\n <div\n id={{@carousel.contentId}}\n class=\"jn-carousel-content\"\n data-slot=\"carousel-content\"\n data-test-jn-part=\"content\"\n {{embla options=@carousel.options plugins=@carousel.plugins onInit=@carousel.onInit}}\n >\n <As\n @as={{@as}}\n @tag=\"div\"\n class=\"jn-carousel-container\"\n data-orientation={{@carousel.orientation}}\n data-test-jn-part=\"container\"\n ...attributes\n >{{yield}}</As>\n </div>\n</template>\n","import type { TOC } from '@ember/component/template-only'\nimport { get } from '@ember/helper'\nimport { As, type TagName } from '@ember-joinery/primitives/as'\nimport { part, type PartModifier } from '@ember-joinery/primitives/part'\nimport type { CarouselOrientation, CarouselState } from './state.ts'\nimport { translate } from '@ember-joinery/primitives/i18n'\nimport { messages } from '../../messages.ts'\n\n/**\n * The attributes a slide renders: upstream's role, role description and `data-slot`, and the\n * orientation its rules key on. Its name (`2 of 5`) comes from the carousel once Embla has\n * counted the slides.\n */\nexport function itemAttributes(\n orientation: CarouselOrientation,\n context: object,\n): Record<string, string> {\n return {\n role: 'group',\n 'aria-roledescription': translate(context, messages.slide),\n 'data-slot': 'carousel-item',\n 'data-orientation': orientation,\n 'data-test-jn-part': 'item',\n }\n}\n\n/** A slide as a modifier, for an element of your own (`<section {{c.item}}>`). */\nexport function itemModifier(carousel: CarouselState): PartModifier<HTMLElement> {\n return part<HTMLElement>(() => ({\n class: 'jn-carousel-item',\n attributes: itemAttributes(carousel.orientation, carousel),\n }))\n}\n\nexport interface CarouselItemSignature {\n Args: {\n /**\n * The tag to render instead of `div`, one that may be a group (`section`): a\n * slide's role is `group`, so not `li`.\n */\n as?: TagName\n /** @internal Bound by `<Carousel>`. */\n carousel: CarouselState\n }\n Blocks: { default: [] }\n Element: HTMLElement\n}\n\n/**\n * A slide, yielded as `c.Item`: as wide as the carousel by default (`basis-full`); give it a\n * `basis-*` class to show several at once.\n */\nexport const CarouselItem: TOC<CarouselItemSignature> = <template>\n {{#let (itemAttributes @carousel.orientation @carousel) as |a|}}\n <As\n @as={{@as}}\n @tag=\"div\"\n role={{get a \"role\"}}\n aria-roledescription={{get a \"aria-roledescription\"}}\n class=\"jn-carousel-item\"\n data-slot={{get a \"data-slot\"}}\n data-orientation={{get a \"data-orientation\"}}\n data-test-jn-part={{get a \"data-test-jn-part\"}}\n ...attributes\n >{{yield}}</As>\n {{/let}}\n</template>\n","import { tracked } from '@glimmer/tracking'\nimport { isDestroying } from '@ember/destroyable'\nimport type { EmblaCarouselType, EmblaOptionsType, EmblaPluginType } from 'embla-carousel'\nimport { getDirection } from '@ember-joinery/primitives/direction'\nimport { idFor } from '@ember-joinery/primitives/id'\nimport { defer } from '../../-private/waiter.ts'\nimport { translate } from '@ember-joinery/primitives/i18n'\nimport { messages } from '../../messages.ts'\n\n/** Which way the slides run. */\nexport type CarouselOrientation = 'horizontal' | 'vertical'\n\n/** A slide's accessible name, from its position: `(3, 5)` gives `3 of 5` by default. */\nexport type CarouselSlideLabel = (index: number, count: number) => string\n\n/** What a carousel's state reads from its component's arguments, each time it needs one. */\nexport interface CarouselStateArgs {\n orientation: () => CarouselOrientation | undefined\n opts: () => EmblaOptionsType | undefined\n plugins: () => EmblaPluginType[] | undefined\n setApi: () => ((api: EmblaCarouselType) => void) | undefined\n slideLabel: () => CarouselSlideLabel | undefined\n}\n\n/** The labels this carousel gave its slides, so a label of the caller's own is left alone. */\nconst labels = new WeakMap<Element, string>()\n\n/** Keys that type into a field rather than move the carousel. */\nfunction isEditable(target: EventTarget | null): boolean {\n if (!(target instanceof HTMLElement)) return false\n return target.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName)\n}\n\n/**\n * A carousel's state: Embla's API once it exists, whether it can scroll either way, and the\n * actions its parts share. `<Carousel>` owns one and hands it to each part, as upstream's\n * context does.\n */\nexport class CarouselState {\n /** Embla's API, once the carousel's content has rendered. */\n @tracked api: EmblaCarouselType | undefined\n /** Whether there is a slide (or snap) before the current one. */\n @tracked canScrollPrev = false\n /** Whether there is a slide (or snap) after the current one. */\n @tracked canScrollNext = false\n\n readonly #args: CarouselStateArgs\n #scheduled = false\n\n constructor(args: CarouselStateArgs) {\n this.#args = args\n }\n\n get orientation(): CarouselOrientation {\n return this.#args.orientation() ?? 'horizontal'\n }\n\n /** Embla's options: the caller's `@opts`, with the axis the orientation gives, as upstream's. */\n get options(): EmblaOptionsType {\n return { ...this.#args.opts(), axis: this.orientation === 'horizontal' ? 'x' : 'y' }\n }\n\n get plugins(): EmblaPluginType[] {\n return this.#args.plugins() ?? []\n }\n\n /** The id of the content, the element holding the slides, which the buttons control. */\n get contentId(): string {\n return idFor(this, 'content')\n }\n\n /** Embla made the carousel: keep its state current, and hand its API to `@setApi`. */\n onInit = (api: EmblaCarouselType): void => {\n if (isDestroying(this)) return\n this.api = api\n api.on('select', this.#changed).on('reInit', this.#changed).on('slidesChanged', this.#changed)\n api.on('destroy', () =>\n defer(() => {\n if (this.api !== api || isDestroying(this)) return\n this.api = undefined\n this.canScrollPrev = this.canScrollNext = false\n }),\n )\n this.#update()\n this.#args.setApi()?.(api)\n }\n\n scrollPrev = (): void => {\n this.api?.scrollPrev()\n }\n\n scrollNext = (): void => {\n this.api?.scrollNext()\n }\n\n /**\n * Scrolls to a snap by index (a slide's, unless several show at once). It takes nothing\n * else, so `{{on \"click\" (fn c.scrollTo 2)}}` works; to jump there without the animation,\n * use the API: `c.api.scrollTo(2, true)`.\n */\n scrollTo = (index: number): void => {\n this.api?.scrollTo(index)\n }\n\n /**\n * The arrow keys, anywhere inside the carousel (upstream listens in the capture phase too):\n * the left arrow scrolls back and the right arrow forward, the other way round right to left;\n * a vertical carousel takes the up and down arrows as well. Keys typed into a field are left\n * to it.\n */\n onKeyDown = (event: KeyboardEvent): void => {\n if (isEditable(event.target)) return\n const rtl = getDirection(event.currentTarget as Element) === 'rtl'\n const vertical = this.orientation === 'vertical'\n let step = 0\n if (event.key === 'ArrowLeft') step = rtl ? 1 : -1\n else if (event.key === 'ArrowRight') step = rtl ? -1 : 1\n else if (vertical && event.key === 'ArrowUp') step = -1\n else if (vertical && event.key === 'ArrowDown') step = 1\n if (!step) return\n event.preventDefault()\n if (step < 0) this.scrollPrev()\n else this.scrollNext()\n }\n\n /**\n * An Embla event: the selection or the slides changed. It can come while the carousel\n * reinitializes during a render, so the update waits for that render to finish.\n */\n #changed = (): void => {\n if (this.#scheduled) return\n this.#scheduled = true\n defer(() => {\n this.#scheduled = false\n this.#update()\n })\n }\n\n #update(): void {\n const api = this.api\n if (!api || isDestroying(this)) return\n this.canScrollPrev = api.canScrollPrev()\n this.canScrollNext = api.canScrollNext()\n this.labelSlides()\n }\n\n /**\n * Names each slide by its place (`2 of 5`), as the APG's carousel pattern asks, unless it has\n * a name of the caller's own (`aria-label` or `aria-labelledby`). It reads the translator's\n * tracked locale, so the carousel's modifier runs it again when the language changes.\n */\n labelSlides(): void {\n const api = this.api\n if (!api) return\n const slideLabel =\n this.#args.slideLabel() ??\n ((index: number, count: number) => translate(this, messages.slideLabel, { index, count }))\n const slides = api.slideNodes()\n slides.forEach((slide, index) => {\n if (slide.getAttribute('data-slot') !== 'carousel-item') return\n if (slide.hasAttribute('aria-labelledby')) return\n const current = slide.getAttribute('aria-label')\n if (current !== null && current !== labels.get(slide)) return\n const label = slideLabel(index + 1, slides.length)\n labels.set(slide, label)\n if (current !== label) slide.setAttribute('aria-label', label)\n })\n }\n}\n","import type { TOC } from '@ember/component/template-only'\nimport { get } from '@ember/helper'\nimport { on } from '@ember/modifier'\nimport { ChevronLeft, ChevronRight } from 'lucide'\nimport { part, type PartModifier } from '@ember-joinery/primitives/part'\nimport { VisuallyHidden } from '@ember-joinery/primitives/visually-hidden'\nimport Button, {\n buttonAttributes,\n buttonClasses,\n type ButtonSize,\n type ButtonVariant,\n} from '@ember-joinery/ember/components/button'\nimport Icon, { type IconNode } from '@ember-joinery/ember/components/icon'\nimport type { CarouselOrientation, CarouselState } from './state.ts'\nimport { t } from '@ember-joinery/primitives/i18n'\nimport { messages } from '../../messages.ts'\n\n/** Which way a step button goes: to the previous slide, or the next. */\nexport type CarouselStep = 'previous' | 'next'\n\n/** A step button's look: what both `<c.Previous>` and the `c.previous` modifier take. */\nexport interface CarouselStepOptions {\n /** Default `outline`, as upstream's. */\n variant?: ButtonVariant\n /** Default `icon-sm`, as upstream's. */\n size?: ButtonSize\n /** The button's name, for `ButtonHarness` (`data-test-jn-name`). */\n name?: string\n}\n\n/**\n * The classes a step button renders: a Button's (`outline`, `icon-sm` by default), then\n * upstream's `cn-carousel-previous` or `cn-carousel-next`, renamed `jn-`.\n */\nexport function stepClasses(step: CarouselStep, options: CarouselStepOptions = {}): string {\n const { variant = 'outline', size = 'icon-sm' } = options\n return `${buttonClasses({ variant, size })} jn-carousel-${step}`\n}\n\n/**\n * The attributes a step button renders: a Button's, with upstream's `data-slot`, the\n * orientation its rules key on, the content it controls, and `disabled` when there is nowhere\n * to go.\n */\nexport function stepAttributes(\n step: CarouselStep,\n options: CarouselStepOptions,\n state: { orientation: CarouselOrientation; contentId: string; canScroll: boolean },\n): Record<string, string | boolean | undefined> {\n const { variant = 'outline', size = 'icon-sm', name } = options\n return {\n ...buttonAttributes({ variant, size, name }),\n 'data-slot': `carousel-${step}`,\n 'data-orientation': state.orientation,\n 'data-test-jn-part': step,\n 'aria-controls': state.contentId,\n disabled: !state.canScroll,\n }\n}\n\n/** A step button's state, read from the carousel's. */\nfunction stateOf(carousel: CarouselState, step: CarouselStep) {\n return {\n orientation: carousel.orientation,\n contentId: carousel.contentId,\n canScroll: step === 'previous' ? carousel.canScrollPrev : carousel.canScrollNext,\n }\n}\n\n/**\n * A step button as a modifier, for a `<button>` (or button component) of your own, with the\n * same classes and attributes as `<c.Previous>` or `<c.Next>`, and a click that steps.\n */\nexport function stepModifier(\n carousel: CarouselState,\n step: CarouselStep,\n): PartModifier<HTMLElement, CarouselStepOptions> {\n return part<HTMLElement, CarouselStepOptions>(options => ({\n class: stepClasses(step, options),\n attributes: stepAttributes(step, options, stateOf(carousel, step)),\n on: { click: step === 'previous' ? carousel.scrollPrev : carousel.scrollNext },\n }))\n}\n\nexport interface CarouselStepSignature {\n Args: CarouselStepOptions & {\n /** The arrow: one from `lucide`. Default lucide's `chevron-left` or `chevron-right`. */\n icon?: IconNode\n /** Its name for assistive technology, visually hidden. Default `Previous slide` or `Next slide`. */\n label?: string\n /** @internal Bound by `<Carousel>`. */\n carousel: CarouselState\n }\n /** A `<button>` (a Button, which could be a link; the carousel's never is). */\n Element: HTMLButtonElement | HTMLAnchorElement\n}\n\nconst attributesOf = (\n carousel: CarouselState,\n step: CarouselStep,\n variant: ButtonVariant | undefined,\n size: ButtonSize | undefined,\n) => stepAttributes(step, { variant, size }, stateOf(carousel, step))\n\n/**\n * The previous slide's button, yielded as `c.Previous`: an outline icon Button, outside the\n * slides at their start (above them, vertically), disabled when there is no slide before.\n */\nexport const CarouselPrevious: TOC<CarouselStepSignature> = <template>\n {{#let (attributesOf @carousel \"previous\" @variant @size) as |a|}}\n <Button\n @variant={{if @variant @variant \"outline\"}}\n @size={{if @size @size \"icon-sm\"}}\n @name={{@name}}\n type=\"button\"\n class=\"jn-carousel-previous\"\n data-slot={{get a \"data-slot\"}}\n data-orientation={{get a \"data-orientation\"}}\n data-test-jn-part={{get a \"data-test-jn-part\"}}\n aria-controls={{get a \"aria-controls\"}}\n disabled={{get a \"disabled\"}}\n ...attributes\n {{on \"click\" @carousel.scrollPrev}}\n >\n <Icon @icon={{if @icon @icon ChevronLeft}} class=\"jn-rtl-flip\" />\n <VisuallyHidden>{{if @label @label (t messages.previous)}}</VisuallyHidden>\n </Button>\n {{/let}}\n</template>\n\n/**\n * The next slide's button, yielded as `c.Next`: an outline icon Button, outside the slides at\n * their end (below them, vertically), disabled when there is no slide after.\n */\nexport const CarouselNext: TOC<CarouselStepSignature> = <template>\n {{#let (attributesOf @carousel \"next\" @variant @size) as |a|}}\n <Button\n @variant={{if @variant @variant \"outline\"}}\n @size={{if @size @size \"icon-sm\"}}\n @name={{@name}}\n type=\"button\"\n class=\"jn-carousel-next\"\n data-slot={{get a \"data-slot\"}}\n data-orientation={{get a \"data-orientation\"}}\n data-test-jn-part={{get a \"data-test-jn-part\"}}\n aria-controls={{get a \"aria-controls\"}}\n disabled={{get a \"disabled\"}}\n ...attributes\n {{on \"click\" @carousel.scrollNext}}\n >\n <Icon @icon={{if @icon @icon ChevronRight}} class=\"jn-rtl-flip\" />\n <VisuallyHidden>{{if @label @label (t messages.next)}}</VisuallyHidden>\n </Button>\n {{/let}}\n</template>\n","import Component from '@glimmer/component'\nimport { hash } from '@ember/helper'\nimport { on } from '@ember/modifier'\nimport { associateDestroyableChild } from '@ember/destroyable'\nimport type Owner from '@ember/owner'\nimport type { WithBoundArgs } from '@glint/template'\nimport type { EmblaCarouselType, EmblaOptionsType, EmblaPluginType } from 'embla-carousel'\nimport { As, type TagName } from '@ember-joinery/primitives/as'\nimport type { PartModifier } from '@ember-joinery/primitives/part'\nimport { CarouselContent } from './content.gts'\nimport { CarouselItem, itemModifier } from './item.gts'\nimport { CarouselState, type CarouselOrientation, type CarouselSlideLabel } from './state.ts'\nimport { CarouselNext, CarouselPrevious, stepModifier, type CarouselStepOptions } from './step.gts'\nimport { t } from '@ember-joinery/primitives/i18n'\nimport { setOwner } from '@ember/owner'\nimport { messages } from '../../messages.ts'\nimport { modifier } from 'ember-modifier'\n\n// Names the slides again whenever what names them changes: the translator's locale, `@slideLabel`.\nconst labelSlides = modifier((_: Element, [state]: [CarouselState]) => state.labelSlides())\n\nexport type { CarouselOrientation, CarouselSlideLabel, CarouselStepOptions }\n\n/** Embla's API, as `@setApi` hands it over and `c.api` yields it: upstream's `CarouselApi`. */\nexport type CarouselApi = EmblaCarouselType\n\n/** Embla's options, as `@opts` takes them: upstream's `CarouselOptions`. */\nexport type CarouselOptions = EmblaOptionsType\n\n/** An Embla plugin, as `@plugins` takes them: upstream's `CarouselPlugin`. */\nexport type CarouselPlugin = EmblaPluginType\n\n/** What `<Carousel>` yields: its parts, their modifiers, and its state, as upstream's `useCarousel()`. */\nexport interface CarouselYield {\n /** The slides: `<c.Content>…</c.Content>`, around the `c.Item`s. */\n Content: WithBoundArgs<typeof CarouselContent, 'carousel'>\n /** A slide: `<c.Item class=\"basis-1/3\">…</c.Item>`. */\n Item: WithBoundArgs<typeof CarouselItem, 'carousel'>\n /** The previous slide's button. */\n Previous: WithBoundArgs<typeof CarouselPrevious, 'carousel'>\n /** The next slide's button. */\n Next: WithBoundArgs<typeof CarouselNext, 'carousel'>\n /** A slide, as a modifier on your own element: `<section {{c.item}}>`. */\n item: PartModifier<HTMLElement>\n /**\n * The previous slide's button, as a modifier on a button of your own, with the same classes\n * and attributes as `c.Previous`: `<button type=\"button\" {{c.previous variant=\"ghost\"}}>`.\n */\n previous: PartModifier<HTMLElement, CarouselStepOptions>\n /** The next slide's button, as a modifier on a button of your own. */\n next: PartModifier<HTMLElement, CarouselStepOptions>\n /** Embla's API, once the content has rendered; `undefined` until then. */\n api: CarouselApi | undefined\n /** Which way the slides run. */\n orientation: CarouselOrientation\n /** Whether there is a slide before the current one. */\n canScrollPrev: boolean\n /** Whether there is a slide after the current one. */\n canScrollNext: boolean\n /** Scrolls to the previous slide. */\n scrollPrev: () => void\n /** Scrolls to the next slide. */\n scrollNext: () => void\n /** Scrolls to a slide (a snap) by index: `{{on \"click\" (fn c.scrollTo 2)}}`. */\n scrollTo: (index: number) => void\n}\n\nexport interface CarouselSignature {\n Args: {\n /** Which way the slides run, and so Embla's axis. Default `horizontal`. */\n orientation?: CarouselOrientation\n /**\n * Embla's options (`align`, `loop`, `startIndex`…; see embla-carousel.com/api/options).\n * Without a `direction`, the carousel takes the text direction from the nearest `dir`.\n */\n opts?: CarouselOptions\n /** Embla plugins, such as `Autoplay()` from `embla-carousel-autoplay`. */\n plugins?: CarouselPlugin[]\n /** Called with Embla's API once the carousel exists, to listen to its events or drive it. */\n setApi?: (api: CarouselApi) => void\n /**\n * Each slide's accessible name, from its place among them (from 1) and their count.\n * Default `2 of 5`. A slide with an `aria-label` or `aria-labelledby` of its own keeps it.\n */\n slideLabel?: CarouselSlideLabel\n /** The tag to render instead of `div`. */\n as?: TagName\n /** Its name, for `CarouselHarness` (`data-test-jn-name`). */\n name?: string\n }\n Blocks: { default: [CarouselYield] }\n Element: HTMLElement\n}\n\n/**\n * A carousel with motion and swipe built using Embla: shadcn's Carousel, on the\n * framework-agnostic `embla-carousel`.\n *\n * <Carousel class=\"w-full max-w-xs\" as |c|>\n * <c.Content>\n * <c.Item>…</c.Item>\n * <c.Item>…</c.Item>\n * </c.Content>\n * <c.Previous />\n * <c.Next />\n * </Carousel>\n *\n * It is a region described as a carousel, its slides are groups described as slides and named\n * by their place (`2 of 5`), and the arrow keys anywhere inside it move between them (flipped\n * right to left). Give it an `aria-label` to name it.\n */\nexport default class Carousel extends Component<CarouselSignature> {\n state: CarouselState\n\n item: PartModifier<HTMLElement>\n previous: PartModifier<HTMLElement, CarouselStepOptions>\n next: PartModifier<HTMLElement, CarouselStepOptions>\n\n constructor(owner: Owner, args: CarouselSignature['Args']) {\n super(owner, args)\n this.state = associateDestroyableChild(\n this,\n new CarouselState({\n orientation: () => this.args.orientation,\n opts: () => this.args.opts,\n plugins: () => this.args.plugins,\n setApi: () => this.args.setApi,\n slideLabel: () => this.args.slideLabel,\n }),\n )\n // The state names the slides itself, in the words of the app's translator.\n setOwner(this.state, owner)\n this.item = itemModifier(this.state)\n this.previous = stepModifier(this.state, 'previous')\n this.next = stepModifier(this.state, 'next')\n }\n\n <template>\n <As\n @as={{@as}}\n @tag=\"div\"\n role=\"region\"\n aria-roledescription={{t messages.carousel}}\n class=\"jn-carousel\"\n data-slot=\"carousel\"\n data-orientation={{this.state.orientation}}\n data-test-jn=\"carousel\"\n data-test-jn-name={{@name}}\n ...attributes\n {{on \"keydown\" this.state.onKeyDown capture=true}}\n {{labelSlides this.state}}\n >\n {{yield\n (hash\n Content=(component CarouselContent carousel=this.state)\n Item=(component CarouselItem carousel=this.state)\n Previous=(component CarouselPrevious carousel=this.state)\n Next=(component CarouselNext carousel=this.state)\n item=this.item\n previous=this.previous\n next=this.next\n api=this.state.api\n orientation=this.state.orientation\n canScrollPrev=this.state.canScrollPrev\n canScrollNext=this.state.canScrollNext\n scrollPrev=this.state.scrollPrev\n scrollNext=this.state.scrollNext\n scrollTo=this.state.scrollTo\n )\n }}\n </As>\n </template>\n}\n"],"names":["CarouselContent","setComponentTemplate","precompileTemplate","strictMode","scope","embla","As","templateOnly","itemAttributes","orientation","context","role","translate","messages","slide","itemModifier","carousel","part","class","attributes","CarouselItem","get","labels","WeakMap","isEditable","target","HTMLElement","isContentEditable","includes","tagName","CarouselState","g","prototype","tracked","i","constructor","args","options","opts","axis","plugins","contentId","idFor","onInit","api","isDestroying","on","defer","undefined","canScrollPrev","canScrollNext","setApi","scrollPrev","scrollNext","scrollTo","index","onKeyDown","event","rtl","getDirection","currentTarget","vertical","step","key","preventDefault","#changed","#update","labelSlides","slideLabel","count","slides","slideNodes","forEach","getAttribute","hasAttribute","current","label","length","set","setAttribute","stepClasses","variant","size","buttonClasses","stepAttributes","state","name","buttonAttributes","disabled","canScroll","stateOf","stepModifier","click","attributesOf","CarouselPrevious","Button","Icon","ChevronLeft","VisuallyHidden","t","CarouselNext","ChevronRight","modifier","_","Carousel","Component","item","previous","next","owner","associateDestroyableChild","setOwner","hash"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAoBA;;;;;;AAMC;AACM,MAAMA,eAAqB,GAAAC,oBAAA,CAA4BC,kBAAA,CAAA,mYAAA,EAiB9D;EAAAC,UAAA,EAAA,IAAA;AAAAC,EAAAA,KAAA,EAAAA,OAAA;WAAAC,aAAA;AAAAC,IAAAA;AAAA,GAAA;AAAU,CAAA,CAAA,EAAAC,YAAA,EAAA,CAAA;;AC/BH,SAASC,cAAAA,CACdC,WAAgC,EAChCC,OAAe,EACO;EACtB,OAAO;AACLC,IAAAA,IAAA,EAAM,OAAA;IACN,sBAAA,EAAwBC,SAAA,CAAUF,OAAA,EAASG,QAAA,CAASC,KAAK,CAAA;AACzD,IAAA,WAAA,EAAa,eAAA;AACb,IAAA,kBAAA,EAAoBL,WAAA;AACpB,IAAA,mBAAA,EAAqB;GACvB;AACF;AAEA;AACO,SAASM,aAAaC,QAAuB,EAAgB;EAClE,OAAOC,IAAA,CAAkB,OAAO;AAC9BC,IAAAA,KAAA,EAAO,kBAAA;AACPC,IAAAA,UAAA,EAAYX,cAAA,CAAeQ,QAAA,CAASP,WAAW,EAAEO,QAAA;AACnD,GAAC,CAAA,CAAA;AACH;AAgBA;;;AAGC;AACM,MAAMI,YAAkB,GAAAnB,oBAAA,CAAyBC,kBAAA,CAAA,wXAAA,EAcxD;EAAAC,UAAA,EAAA,IAAA;AAAAC,EAAAA,KAAA,EAAAA,OAAA;IAAAI,cAAA;IAAAF,EAAA;AAAAe,IAAAA;AAAA,GAAA;AAAU,CAAA,CAAA,EAAAd,YAAA,EAAA,CAAA;;AC1CV;AACA,MAAMe,MAAM,GAAG,IAAIC,OAAO,EAAmB;;AAE7C;AACA,SAASC,UAAUA,CAACC,MAA0B,EAAW;AACvD,EAAA,IAAI,EAAEA,M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iC,EAAA;QAAAqB,WAAA;QAAA6C,IAAA;QAAAhH,eAAA;QAAAoB,YAAA;QAAA0E,gBAAA;AAAAM,QAAAA;AAAA,OAAA;KAAU,CAAA,EAAV,IAAW,CAAA;AAAD;AACZ;;"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import Modifier from 'ember-modifier';
|
|
2
|
+
import { registerDestructor } from '@ember/destroyable';
|
|
3
|
+
import EmblaCarousel from 'embla-carousel';
|
|
4
|
+
import { areOptionsEqual, arePluginsEqual } from 'embla-carousel-reactive-utils';
|
|
5
|
+
import { getDirection } from '@ember-joinery/primitives/direction';
|
|
6
|
+
import { isUndefined } from '@sindresorhus/is';
|
|
7
|
+
import { register } from '@ember/test-waiters';
|
|
8
|
+
|
|
9
|
+
/** Callbacks to call when any element's `dir` changes. */
|
|
10
|
+
const listeners = new Set();
|
|
11
|
+
let observer;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Calls `callback` whenever a `dir` attribute changes anywhere in the document, as the guide's
|
|
15
|
+
* RTL switch or an app's language picker does, until the returned function is called. One
|
|
16
|
+
* observer serves every carousel, and it watches only while one is listening.
|
|
17
|
+
*/
|
|
18
|
+
function watchDirection(callback) {
|
|
19
|
+
if (isUndefined(globalThis.MutationObserver)) return () => {};
|
|
20
|
+
if (!listeners.size) {
|
|
21
|
+
observer ??= new MutationObserver(() => {
|
|
22
|
+
for (const listener of [...listeners]) listener();
|
|
23
|
+
});
|
|
24
|
+
observer.observe(document.documentElement, {
|
|
25
|
+
attributes: true,
|
|
26
|
+
attributeFilter: ['dir'],
|
|
27
|
+
subtree: true
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
listeners.add(callback);
|
|
31
|
+
return () => {
|
|
32
|
+
listeners.delete(callback);
|
|
33
|
+
if (!listeners.size) observer?.disconnect();
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Every carousel on the page, while it lives. */
|
|
38
|
+
const live = new Set();
|
|
39
|
+
|
|
40
|
+
/** Work put off past the current render, which `settled()` waits for. */
|
|
41
|
+
let deferred = 0;
|
|
42
|
+
|
|
43
|
+
/** How close (in pixels, and pixels a step) a carousel must be to its target to be at rest. */
|
|
44
|
+
const REST = 0.5;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether a carousel has come to rest where a user sees it: not being dragged, and within half
|
|
48
|
+
* a pixel of where it is going, barely moving. Embla's own `settle` comes a good while later,
|
|
49
|
+
* once its easing is within a thousandth of a pixel (and not always after a jump), so tests
|
|
50
|
+
* would wait for nothing anyone can see.
|
|
51
|
+
*/
|
|
52
|
+
function isAtRest(api) {
|
|
53
|
+
const engine = api.internalEngine();
|
|
54
|
+
if (engine.dragHandler.pointerDown()) return false;
|
|
55
|
+
const distance = Math.abs(engine.target.get() - engine.location.get());
|
|
56
|
+
return distance < REST && Math.abs(engine.scrollBody.velocity()) < REST;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One test waiter for every carousel: `settled()` (and so every `@ember/test-helpers` action)
|
|
61
|
+
* waits while one is scrolling to a slide or being dragged, and for the state a carousel puts
|
|
62
|
+
* off past rendering. So a test reads the slides where they come to rest. Outside tests it
|
|
63
|
+
* costs one entry in the waiters' map.
|
|
64
|
+
*/
|
|
65
|
+
const waiter = {
|
|
66
|
+
name: '@ember-joinery/carousel',
|
|
67
|
+
waitUntil: () => deferred === 0 && [...live].every(isAtRest),
|
|
68
|
+
debugInfo: () => [...(deferred ? [{
|
|
69
|
+
label: `${deferred} carousel update(s) put off past rendering`,
|
|
70
|
+
stack: undefined
|
|
71
|
+
}] : []), ...[...live].filter(api => !isAtRest(api)).map(() => ({
|
|
72
|
+
label: 'a carousel still moving',
|
|
73
|
+
stack: undefined
|
|
74
|
+
}))]
|
|
75
|
+
};
|
|
76
|
+
let registered = false;
|
|
77
|
+
|
|
78
|
+
/** Counts a carousel in, for the waiter, until `untrack`. */
|
|
79
|
+
function track(api) {
|
|
80
|
+
if (!registered) {
|
|
81
|
+
register(waiter);
|
|
82
|
+
registered = true;
|
|
83
|
+
}
|
|
84
|
+
live.add(api);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Counts a carousel out. */
|
|
88
|
+
function untrack(api) {
|
|
89
|
+
live.delete(api);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Runs `callback` once the current render is done (in a microtask), for work that sets tracked
|
|
94
|
+
* state: a modifier, or an Embla event fired while the carousel reinitializes during a render,
|
|
95
|
+
* mustn't set state the render has read. The waiter waits for it.
|
|
96
|
+
*/
|
|
97
|
+
function defer(callback) {
|
|
98
|
+
deferred += 1;
|
|
99
|
+
void Promise.resolve().then(callback).finally(() => {
|
|
100
|
+
deferred -= 1;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** What `{{embla}}` takes. */
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Makes an element an Embla carousel's viewport, the way `embla-carousel-react`'s
|
|
108
|
+
* `useEmblaCarousel` does: the element clips, its first child is the track Embla moves, and
|
|
109
|
+
* that child's children are the slides. It is the headless part of `<Carousel>`, for a
|
|
110
|
+
* carousel with markup of your own:
|
|
111
|
+
*
|
|
112
|
+
* <div class="overflow-hidden" {{embla options=(hash loop=true) onInit=this.setApi}}>
|
|
113
|
+
* <div class="flex">
|
|
114
|
+
* {{#each this.slides as |slide|}}<div class="min-w-0 shrink-0 basis-full">…</div>{{/each}}
|
|
115
|
+
* </div>
|
|
116
|
+
* </div>
|
|
117
|
+
*
|
|
118
|
+
* When `options` or `plugins` change to something different, the carousel reinitializes and
|
|
119
|
+
* keeps its place (`api.reInit`), as the React hook does; it goes when the element does. While
|
|
120
|
+
* it moves to a slide, or is dragged, `settled()` waits for it in tests.
|
|
121
|
+
*/
|
|
122
|
+
class EmblaModifier extends Modifier {
|
|
123
|
+
#api;
|
|
124
|
+
#element;
|
|
125
|
+
#options = {};
|
|
126
|
+
#plugins = [];
|
|
127
|
+
/** The options Embla has now: the caller's, with the direction filled in. */
|
|
128
|
+
#resolved = {};
|
|
129
|
+
#unwatch;
|
|
130
|
+
#destroyed = false;
|
|
131
|
+
constructor(owner, args) {
|
|
132
|
+
super(owner, args);
|
|
133
|
+
registerDestructor(this, () => {
|
|
134
|
+
this.#destroyed = true;
|
|
135
|
+
this.#unwatch?.();
|
|
136
|
+
const api = this.#api;
|
|
137
|
+
if (!api) return;
|
|
138
|
+
untrack(api);
|
|
139
|
+
api.destroy();
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
modify(element, _positional, named) {
|
|
143
|
+
const {
|
|
144
|
+
options = {},
|
|
145
|
+
plugins = [],
|
|
146
|
+
onInit
|
|
147
|
+
} = named;
|
|
148
|
+
this.#element = element;
|
|
149
|
+
this.#options = options;
|
|
150
|
+
if (!this.#api) {
|
|
151
|
+
this.#plugins = plugins;
|
|
152
|
+
this.#resolved = this.#resolve();
|
|
153
|
+
const api = EmblaCarousel(element, this.#resolved, plugins);
|
|
154
|
+
this.#api = api;
|
|
155
|
+
track(api);
|
|
156
|
+
this.#unwatch = watchDirection(() => this.#update(this.#plugins));
|
|
157
|
+
if (onInit) defer(() => !this.#destroyed && onInit(api));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.#update(plugins);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The caller's options, with the text direction at the element when they don't give one. */
|
|
164
|
+
#resolve() {
|
|
165
|
+
const options = this.#options;
|
|
166
|
+
if (options.direction) return options;
|
|
167
|
+
return {
|
|
168
|
+
...options,
|
|
169
|
+
direction: getDirection(this.#element)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Reinitializes the carousel if its options (its direction included) or plugins changed. */
|
|
174
|
+
#update(plugins) {
|
|
175
|
+
const api = this.#api;
|
|
176
|
+
if (!api || this.#destroyed) return;
|
|
177
|
+
const resolved = this.#resolve();
|
|
178
|
+
if (areOptionsEqual(this.#resolved, resolved) && arePluginsEqual(this.#plugins, plugins)) return;
|
|
179
|
+
this.#resolved = resolved;
|
|
180
|
+
this.#plugins = plugins;
|
|
181
|
+
api.reInit(resolved, plugins);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export { EmblaModifier as E, defer as d };
|
|
186
|
+
//# sourceMappingURL=embla-C9buOK_5.js.map
|