Skip to content

Commit

Permalink
Bug Fix: Prevent scrolling when the active slide gets focus.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaotoshiFujita committed Oct 17, 2021
1 parent de01967 commit ab94fff
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dist/js/splide-renderer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions dist/js/splide.cjs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Splide.js
* Version : 3.1.6
* Version : 3.1.7
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
Expand Down Expand Up @@ -200,6 +200,10 @@ function display(elm, display2) {
style(elm, "display", display2);
}

function focus(elm) {
elm["setActive"] && elm["setActive"]() || elm.focus({ preventScroll: true });
}

function getAttribute(elm, attr) {
return elm.getAttribute(attr);
}
Expand Down Expand Up @@ -1854,12 +1858,12 @@ function Drag(Splide2, Components2, options) {
function coordOf(e, orthogonal) {
return (isTouchEvent(e) ? e.touches[0] : e)[`page${resolve(orthogonal ? "Y" : "X")}`];
}
function isTouchEvent(e) {
return typeof TouchEvent !== "undefined" && e instanceof TouchEvent;
}
function timeOf(e) {
return e.timeStamp;
}
function isTouchEvent(e) {
return typeof TouchEvent !== "undefined" && e instanceof TouchEvent;
}
function constrain(diff) {
return diff / (hasExceeded && Splide2.is(SLIDE) ? FRICTION : 1);
}
Expand Down Expand Up @@ -2056,7 +2060,7 @@ function Pagination(Splide2, Components2, options) {
function onClick(page) {
Controller.go(`>${page}`, true, () => {
const Slide = Slides.getAt(Controller.toIndex(page));
Slide && Slide.slide.focus();
Slide && focus(Slide.slide);
});
}
function getAt(index) {
Expand Down
14 changes: 9 additions & 5 deletions dist/js/splide.esm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Splide.js
* Version : 3.1.6
* Version : 3.1.7
* License : MIT
* Copyright: 2021 Naotoshi Fujita
*/
Expand Down Expand Up @@ -196,6 +196,10 @@ function display(elm, display2) {
style(elm, "display", display2);
}

function focus(elm) {
elm["setActive"] && elm["setActive"]() || elm.focus({ preventScroll: true });
}

function getAttribute(elm, attr) {
return elm.getAttribute(attr);
}
Expand Down Expand Up @@ -1850,12 +1854,12 @@ function Drag(Splide2, Components2, options) {
function coordOf(e, orthogonal) {
return (isTouchEvent(e) ? e.touches[0] : e)[`page${resolve(orthogonal ? "Y" : "X")}`];
}
function isTouchEvent(e) {
return typeof TouchEvent !== "undefined" && e instanceof TouchEvent;
}
function timeOf(e) {
return e.timeStamp;
}
function isTouchEvent(e) {
return typeof TouchEvent !== "undefined" && e instanceof TouchEvent;
}
function constrain(diff) {
return diff / (hasExceeded && Splide2.is(SLIDE) ? FRICTION : 1);
}
Expand Down Expand Up @@ -2052,7 +2056,7 @@ function Pagination(Splide2, Components2, options) {
function onClick(page) {
Controller.go(`>${page}`, true, () => {
const Slide = Slides.getAt(Controller.toIndex(page));
Slide && Slide.slide.focus();
Slide && focus(Slide.slide);
});
}
function getAt(index) {
Expand Down
18 changes: 12 additions & 6 deletions dist/js/splide.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/splide.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/js/splide.min.js

Large diffs are not rendered by default.

Binary file modified dist/js/splide.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/types/components/Controller/Controller.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/types/components/Layout/Layout.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/types/components/Pagination/Pagination.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/types/utils/dom/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { child } from './child/child';
export { children } from './children/children';
export { create } from './create/create';
export { display } from './display/display';
export { focus } from './focus/focus';
export { getAttribute } from './getAttribute/getAttribute';
export { hasClass } from './hasClass/hasClass';
export { matches } from './matches/matches';
Expand Down
2 changes: 1 addition & 1 deletion dist/types/utils/dom/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splidejs/splide",
"version": "3.1.6",
"version": "3.1.7",
"description": "Splide is a lightweight, flexible and accessible slider/carousel. No dependencies, no Lighthouse errors.",
"author": "Naotoshi Fujita",
"license": "MIT",
Expand Down
15 changes: 13 additions & 2 deletions src/js/components/Pagination/Pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import {
import { EventInterface } from '../../constructors';
import { Splide } from '../../core/Splide/Splide';
import { BaseComponent, Components, Options } from '../../types';
import { addClass, ceil, create, empty, format, remove, removeAttribute, removeClass, setAttribute } from '../../utils';
import {
addClass,
ceil,
create,
empty,
focus,
format,
remove,
removeAttribute,
removeClass,
setAttribute,
} from '../../utils';


/**
Expand Down Expand Up @@ -142,7 +153,7 @@ export function Pagination( Splide: Splide, Components: Components, options: Opt
function onClick( page: number ): void {
Controller.go( `>${ page }`, true, () => {
const Slide = Slides.getAt( Controller.toIndex( page ) );
Slide && Slide.slide.focus();
Slide && focus( Slide.slide );
} );
}

Expand Down
17 changes: 17 additions & 0 deletions src/js/utils/dom/focus/focus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { focus } from './focus';


describe( 'focus', () => {
test( 'can make an element focused if it is focusable.', () => {
const div = document.createElement( 'div' );

div.tabIndex = 0;
document.body.appendChild( div );

expect( document.activeElement ).not.toBe( div );

focus( div );

expect( document.activeElement ).toBe( div );
} );
} );
8 changes: 8 additions & 0 deletions src/js/utils/dom/focus/focus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Focuses the provided element without scrolling the ascendant element.
*
* @param elm - An element to focus.
*/
export function focus( elm: HTMLElement ): void {
elm[ 'setActive' ] && elm[ 'setActive' ]() || elm.focus( { preventScroll: true } )
}
1 change: 1 addition & 0 deletions src/js/utils/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { child } from './child/child';
export { children } from './children/children';
export { create } from './create/create';
export { display } from './display/display';
export { focus } from './focus/focus';
export { getAttribute } from './getAttribute/getAttribute';
export { hasClass } from './hasClass/hasClass';
export { matches } from './matches/matches';
Expand Down

0 comments on commit ab94fff

Please sign in to comment.