embla-a11y 1.1.1 → 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.
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project are listed here. The project follows [Semantic Versioning](https://semver.org).
|
|
4
4
|
|
|
5
|
+
## 1.1.2 (2026-10-22)
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Focus on a partly visible slide moves the carousel just far enough to show that slide in full, instead of jumping to its snap point. The jump pulled the next slide in, so Tab walked through every slide of a carousel with peeking slides. Now Tab reaches the slides on screen and then the buttons, as in other layouts. After such a move the carousel can rest between two snap points until the next button press, drag or key.
|
|
10
|
+
|
|
5
11
|
## 1.1.1 (2026-10-22)
|
|
6
12
|
|
|
7
13
|
### Fixed
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { EmblaCarouselType } from 'embla-carousel';
|
|
2
2
|
import type { Feature } from './Feature.js';
|
|
3
3
|
/**
|
|
4
|
-
* Moves the carousel
|
|
4
|
+
* Moves the carousel just far enough to show the slide that got focus in full, and never to the slide's snap,
|
|
5
|
+
* so the slide after it does not peek in and Tab stays with the slides on screen.
|
|
5
6
|
* It follows keyboard, screen reader and script focus, but not mouse or touch presses or focus the browser restores.
|
|
6
7
|
* It also undoes the browser's own scrolling of the viewport, and moves the carousel to what the browser scrolled to
|
|
7
8
|
* without focus, as find in page or a link to a slide do.
|
|
@@ -17,13 +18,17 @@ export declare class FollowFocus implements Feature {
|
|
|
17
18
|
private readonly onWindowFocus;
|
|
18
19
|
private windowWasLeft;
|
|
19
20
|
private pressed;
|
|
21
|
+
private wantedStart;
|
|
20
22
|
private restoreTimeout;
|
|
21
23
|
constructor(emblaApi: EmblaCarouselType);
|
|
22
24
|
destroy(): void;
|
|
23
25
|
private follow;
|
|
24
|
-
private resetNativeScroll;
|
|
25
26
|
private goToSlideOf;
|
|
27
|
+
private resetNativeScroll;
|
|
28
|
+
private boxOf;
|
|
29
|
+
private startOf;
|
|
26
30
|
private moveBy;
|
|
27
31
|
private overflow;
|
|
32
|
+
private edges;
|
|
28
33
|
}
|
|
29
34
|
//# sourceMappingURL=FollowFocus.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FollowFocus.d.ts","sourceRoot":"","sources":["../../src/features/FollowFocus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE3C
|
|
1
|
+
{"version":3,"file":"FollowFocus.d.ts","sourceRoot":"","sources":["../../src/features/FollowFocus.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE3C;;;;;;GAMG;AACH,qBAAa,WAAY,YAAW,OAAO;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;IAC5C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyD;IACnF,OAAO,CAAC,QAAQ,CAAC,aAAa,CAE7B;IACD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAEzB;IACD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAU5B;IACD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAM9B;IACD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAE5B;IACD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAK7B;IACD,OAAO,CAAC,aAAa,CAAQ;IAC7B,OAAO,CAAC,OAAO,CAAoB;IACnC,OAAO,CAAC,WAAW,CAAsB;IACzC,OAAO,CAAC,cAAc,CAA2C;IAEjE,YAAmB,QAAQ,EAAE,iBAAiB,EAS7C;IAEM,OAAO,IAAI,IAAI,CASrB;IAED,OAAO,CAAC,MAAM;IAuBd,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,iBAAiB;IAMzB,OAAO,CAAC,KAAK;IASb,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,MAAM;IAYd,OAAO,CAAC,QAAQ;IAShB,OAAO,CAAC,KAAK;CAKd"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Moves the carousel
|
|
2
|
+
* Moves the carousel just far enough to show the slide that got focus in full, and never to the slide's snap,
|
|
3
|
+
* so the slide after it does not peek in and Tab stays with the slides on screen.
|
|
3
4
|
* It follows keyboard, screen reader and script focus, but not mouse or touch presses or focus the browser restores.
|
|
4
5
|
* It also undoes the browser's own scrolling of the viewport, and moves the carousel to what the browser scrolled to
|
|
5
6
|
* without focus, as find in page or a link to a slide do.
|
|
@@ -16,7 +17,15 @@ export class FollowFocus {
|
|
|
16
17
|
onSlideFocus = () => {
|
|
17
18
|
this.resetNativeScroll();
|
|
18
19
|
const focused = document.activeElement;
|
|
19
|
-
|
|
20
|
+
if (!(focused instanceof Element))
|
|
21
|
+
return true;
|
|
22
|
+
const box = this.boxOf(focused);
|
|
23
|
+
const clipped = this.overflow(box);
|
|
24
|
+
if (clipped === 0)
|
|
25
|
+
return false;
|
|
26
|
+
// Embla jumps to the snap next; follow() corrects it to this.
|
|
27
|
+
this.wantedStart = this.startOf(box) - clipped;
|
|
28
|
+
return true;
|
|
20
29
|
};
|
|
21
30
|
onNativeScroll = () => {
|
|
22
31
|
const root = this.emblaApi.rootNode();
|
|
@@ -36,6 +45,7 @@ export class FollowFocus {
|
|
|
36
45
|
};
|
|
37
46
|
windowWasLeft = false;
|
|
38
47
|
pressed = null;
|
|
48
|
+
wantedStart = null;
|
|
39
49
|
restoreTimeout;
|
|
40
50
|
constructor(emblaApi) {
|
|
41
51
|
this.emblaApi = emblaApi;
|
|
@@ -59,48 +69,77 @@ export class FollowFocus {
|
|
|
59
69
|
}
|
|
60
70
|
follow(target) {
|
|
61
71
|
this.resetNativeScroll();
|
|
72
|
+
const wantedStart = this.wantedStart;
|
|
62
73
|
const restoredByBrowser = this.windowWasLeft;
|
|
63
74
|
const pressed = this.pressed;
|
|
75
|
+
this.wantedStart = null;
|
|
64
76
|
this.windowWasLeft = false;
|
|
65
77
|
this.pressed = null;
|
|
66
78
|
if (restoredByBrowser || !(target instanceof Element))
|
|
67
79
|
return;
|
|
68
80
|
if (pressed !== null && target.contains(pressed))
|
|
69
81
|
return;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
82
|
+
const box = this.boxOf(target);
|
|
83
|
+
const start = wantedStart ?? this.startOf(box) - this.overflow(box);
|
|
84
|
+
const distance = start - this.startOf(box);
|
|
85
|
+
if (distance !== 0)
|
|
86
|
+
this.moveBy(distance);
|
|
87
|
+
// Under Fade only the snap shows the slide.
|
|
88
|
+
if (this.overflow(box) !== 0)
|
|
89
|
+
this.goToSlideOf(target);
|
|
90
|
+
// A wide button can stick out of its slide.
|
|
73
91
|
const clipped = this.overflow(target);
|
|
74
92
|
if (clipped !== 0)
|
|
75
93
|
this.moveBy(-clipped);
|
|
76
94
|
}
|
|
77
|
-
resetNativeScroll() {
|
|
78
|
-
const root = this.emblaApi.rootNode();
|
|
79
|
-
root.scrollLeft = 0;
|
|
80
|
-
root.scrollTop = 0;
|
|
81
|
-
}
|
|
82
95
|
goToSlideOf(target) {
|
|
83
96
|
const slideIndex = this.emblaApi.slideNodes().findIndex((slide) => slide.contains(target));
|
|
84
97
|
const snapIndex = this.emblaApi.internalEngine().scrollSnapList.snapBySlide[slideIndex];
|
|
85
98
|
if (snapIndex !== undefined)
|
|
86
99
|
this.emblaApi.goTo(snapIndex, true);
|
|
87
100
|
}
|
|
101
|
+
resetNativeScroll() {
|
|
102
|
+
const root = this.emblaApi.rootNode();
|
|
103
|
+
root.scrollLeft = 0;
|
|
104
|
+
root.scrollTop = 0;
|
|
105
|
+
}
|
|
106
|
+
boxOf(target) {
|
|
107
|
+
const slide = this.emblaApi.slideNodes().find((candidate) => candidate.contains(target));
|
|
108
|
+
if (slide === undefined)
|
|
109
|
+
return target;
|
|
110
|
+
const [start, end] = this.edges();
|
|
111
|
+
const inner = slide.getBoundingClientRect();
|
|
112
|
+
const outer = this.emblaApi.rootNode().getBoundingClientRect();
|
|
113
|
+
return inner[end] - inner[start] <= outer[end] - outer[start] ? slide : target;
|
|
114
|
+
}
|
|
115
|
+
startOf(element) {
|
|
116
|
+
return element.getBoundingClientRect()[this.edges()[0]];
|
|
117
|
+
}
|
|
88
118
|
moveBy(distance) {
|
|
89
119
|
const engine = this.emblaApi.internalEngine();
|
|
120
|
+
const current = engine.target.get();
|
|
121
|
+
const wanted = current + engine.axis.direction(distance);
|
|
122
|
+
// Past the limit, an instant move never settles.
|
|
123
|
+
const reachable = engine.options.loop
|
|
124
|
+
? wanted
|
|
125
|
+
: Math.min(Math.max(wanted, engine.limit.min), engine.limit.max);
|
|
90
126
|
engine.scrollBody.useBaseFriction().useDuration(0);
|
|
91
|
-
engine.scrollTo.distance(
|
|
127
|
+
engine.scrollTo.distance(reachable - current, false);
|
|
92
128
|
}
|
|
93
129
|
overflow(element) {
|
|
94
130
|
const inner = element.getBoundingClientRect();
|
|
95
131
|
const outer = this.emblaApi.rootNode().getBoundingClientRect();
|
|
96
|
-
const [start, end] = this.
|
|
97
|
-
? ['left', 'right']
|
|
98
|
-
: ['top', 'bottom'];
|
|
132
|
+
const [start, end] = this.edges();
|
|
99
133
|
if (inner[start] < outer[start])
|
|
100
134
|
return inner[start] - outer[start];
|
|
101
135
|
if (inner[end] > outer[end])
|
|
102
136
|
return inner[end] - outer[end];
|
|
103
137
|
return 0;
|
|
104
138
|
}
|
|
139
|
+
edges() {
|
|
140
|
+
return this.emblaApi.internalEngine().axis.scroll === 'x'
|
|
141
|
+
? ['left', 'right']
|
|
142
|
+
: ['top', 'bottom'];
|
|
143
|
+
}
|
|
105
144
|
}
|
|
106
145
|
//# sourceMappingURL=FollowFocus.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FollowFocus.js","sourceRoot":"","sources":["../../src/features/FollowFocus.ts"],"names":[],"mappings":"AAGA
|
|
1
|
+
{"version":3,"file":"FollowFocus.js","sourceRoot":"","sources":["../../src/features/FollowFocus.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,OAAO,WAAW;IACL,QAAQ,CAAmB;IAC3B,SAAS,GAAG,CAAC,KAAiB,EAAQ,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;IAClE,aAAa,GAAG,CAAC,KAAmB,EAAQ,EAAE;QAC7D,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,MAAM,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA;IACnE,CAAC,CAAA;IACgB,SAAS,GAAG,GAAS,EAAE;QACtC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;IACrB,CAAC,CAAA;IACgB,YAAY,GAAG,GAAY,EAAE;QAC5C,IAAI,CAAC,iBAAiB,EAAE,CAAA;QACxB,MAAM,OAAO,GAAG,QAAQ,CAAC,aAAa,CAAA;QACtC,IAAI,CAAC,CAAC,OAAO,YAAY,OAAO,CAAC;YAAE,OAAO,IAAI,CAAA;QAC9C,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAC/B,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;QAClC,IAAI,OAAO,KAAK,CAAC;YAAE,OAAO,KAAK,CAAA;QAC/B,8DAA8D;QAC9D,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,CAAA;QAC9C,OAAO,IAAI,CAAA;IACb,CAAC,CAAA;IACgB,cAAc,GAAG,GAAS,EAAE;QAC3C,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC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Q,CAAW,CAAA;IAClC,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@ import type { EmblaCarouselType } from 'embla-carousel'
|
|
|
2
2
|
import type { Feature } from './Feature.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Moves the carousel
|
|
5
|
+
* Moves the carousel just far enough to show the slide that got focus in full, and never to the slide's snap,
|
|
6
|
+
* so the slide after it does not peek in and Tab stays with the slides on screen.
|
|
6
7
|
* It follows keyboard, screen reader and script focus, but not mouse or touch presses or focus the browser restores.
|
|
7
8
|
* It also undoes the browser's own scrolling of the viewport, and moves the carousel to what the browser scrolled to
|
|
8
9
|
* without focus, as find in page or a link to a slide do.
|
|
@@ -19,7 +20,13 @@ export class FollowFocus implements Feature {
|
|
|
19
20
|
private readonly onSlideFocus = (): boolean => {
|
|
20
21
|
this.resetNativeScroll()
|
|
21
22
|
const focused = document.activeElement
|
|
22
|
-
|
|
23
|
+
if (!(focused instanceof Element)) return true
|
|
24
|
+
const box = this.boxOf(focused)
|
|
25
|
+
const clipped = this.overflow(box)
|
|
26
|
+
if (clipped === 0) return false
|
|
27
|
+
// Embla jumps to the snap next; follow() corrects it to this.
|
|
28
|
+
this.wantedStart = this.startOf(box) - clipped
|
|
29
|
+
return true
|
|
23
30
|
}
|
|
24
31
|
private readonly onNativeScroll = (): void => {
|
|
25
32
|
const root = this.emblaApi.rootNode()
|
|
@@ -39,6 +46,7 @@ export class FollowFocus implements Feature {
|
|
|
39
46
|
}
|
|
40
47
|
private windowWasLeft = false
|
|
41
48
|
private pressed: Node | null = null
|
|
49
|
+
private wantedStart: number | null = null
|
|
42
50
|
private restoreTimeout: ReturnType<typeof setTimeout> | undefined
|
|
43
51
|
|
|
44
52
|
public constructor(emblaApi: EmblaCarouselType) {
|
|
@@ -66,46 +74,75 @@ export class FollowFocus implements Feature {
|
|
|
66
74
|
private follow(target: EventTarget | null): void {
|
|
67
75
|
this.resetNativeScroll()
|
|
68
76
|
|
|
77
|
+
const wantedStart = this.wantedStart
|
|
69
78
|
const restoredByBrowser = this.windowWasLeft
|
|
70
79
|
const pressed = this.pressed
|
|
80
|
+
this.wantedStart = null
|
|
71
81
|
this.windowWasLeft = false
|
|
72
82
|
this.pressed = null
|
|
73
83
|
if (restoredByBrowser || !(target instanceof Element)) return
|
|
74
84
|
if (pressed !== null && target.contains(pressed)) return
|
|
75
|
-
if (this.overflow(target) === 0) return
|
|
76
|
-
this.goToSlideOf(target)
|
|
77
85
|
|
|
86
|
+
const box = this.boxOf(target)
|
|
87
|
+
const start = wantedStart ?? this.startOf(box) - this.overflow(box)
|
|
88
|
+
const distance = start - this.startOf(box)
|
|
89
|
+
if (distance !== 0) this.moveBy(distance)
|
|
90
|
+
// Under Fade only the snap shows the slide.
|
|
91
|
+
if (this.overflow(box) !== 0) this.goToSlideOf(target)
|
|
92
|
+
// A wide button can stick out of its slide.
|
|
78
93
|
const clipped = this.overflow(target)
|
|
79
94
|
if (clipped !== 0) this.moveBy(-clipped)
|
|
80
95
|
}
|
|
81
96
|
|
|
97
|
+
private goToSlideOf(target: Element): void {
|
|
98
|
+
const slideIndex = this.emblaApi.slideNodes().findIndex((slide) => slide.contains(target))
|
|
99
|
+
const snapIndex = this.emblaApi.internalEngine().scrollSnapList.snapBySlide[slideIndex]
|
|
100
|
+
if (snapIndex !== undefined) this.emblaApi.goTo(snapIndex, true)
|
|
101
|
+
}
|
|
102
|
+
|
|
82
103
|
private resetNativeScroll(): void {
|
|
83
104
|
const root = this.emblaApi.rootNode()
|
|
84
105
|
root.scrollLeft = 0
|
|
85
106
|
root.scrollTop = 0
|
|
86
107
|
}
|
|
87
108
|
|
|
88
|
-
private
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
109
|
+
private boxOf(target: Element): Element {
|
|
110
|
+
const slide = this.emblaApi.slideNodes().find((candidate) => candidate.contains(target))
|
|
111
|
+
if (slide === undefined) return target
|
|
112
|
+
const [start, end] = this.edges()
|
|
113
|
+
const inner = slide.getBoundingClientRect()
|
|
114
|
+
const outer = this.emblaApi.rootNode().getBoundingClientRect()
|
|
115
|
+
return inner[end] - inner[start] <= outer[end] - outer[start] ? slide : target
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private startOf(element: Element): number {
|
|
119
|
+
return element.getBoundingClientRect()[this.edges()[0]]
|
|
92
120
|
}
|
|
93
121
|
|
|
94
122
|
private moveBy(distance: number): void {
|
|
95
123
|
const engine = this.emblaApi.internalEngine()
|
|
124
|
+
const current = engine.target.get()
|
|
125
|
+
const wanted = current + engine.axis.direction(distance)
|
|
126
|
+
// Past the limit, an instant move never settles.
|
|
127
|
+
const reachable = engine.options.loop
|
|
128
|
+
? wanted
|
|
129
|
+
: Math.min(Math.max(wanted, engine.limit.min), engine.limit.max)
|
|
96
130
|
engine.scrollBody.useBaseFriction().useDuration(0)
|
|
97
|
-
engine.scrollTo.distance(
|
|
131
|
+
engine.scrollTo.distance(reachable - current, false)
|
|
98
132
|
}
|
|
99
133
|
|
|
100
134
|
private overflow(element: Element): number {
|
|
101
135
|
const inner = element.getBoundingClientRect()
|
|
102
136
|
const outer = this.emblaApi.rootNode().getBoundingClientRect()
|
|
103
|
-
const [start, end] =
|
|
104
|
-
this.emblaApi.internalEngine().axis.scroll === 'x'
|
|
105
|
-
? (['left', 'right'] as const)
|
|
106
|
-
: (['top', 'bottom'] as const)
|
|
137
|
+
const [start, end] = this.edges()
|
|
107
138
|
if (inner[start] < outer[start]) return inner[start] - outer[start]
|
|
108
139
|
if (inner[end] > outer[end]) return inner[end] - outer[end]
|
|
109
140
|
return 0
|
|
110
141
|
}
|
|
142
|
+
|
|
143
|
+
private edges(): readonly ['left', 'right'] | readonly ['top', 'bottom'] {
|
|
144
|
+
return this.emblaApi.internalEngine().axis.scroll === 'x'
|
|
145
|
+
? (['left', 'right'] as const)
|
|
146
|
+
: (['top', 'bottom'] as const)
|
|
147
|
+
}
|
|
111
148
|
}
|