Skip to content

Commit 0badd14

Browse files
authored
Merge pull request #797 from Patternslib/pat-scroll-click
pat-scroll: Do handle click events also when trigger is set to "auto".
2 parents fd68f4e + 48bcf26 commit 0badd14

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- pat forward: Add `delay` option for delaying the click action forwarding for a given number of milliseconds.
6464
- pat forward: Add `self` as possible value for the `selector` option to trigger the event on itself.
6565
- pat-scroll: Implement `selector:bottom` attribute value to scroll to the bottom of the scroll container.
66+
- pat-scroll: Do handle click events also when trigger is set to `auto`.
6667

6768
### Technical
6869

src/pat/scroll/documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The available options are:
4949

5050
| Field | Default | Options | Description |
5151
| ----------- | ----------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
52-
| `trigger` | `click` | `click`, `auto` | `auto` means that the scrolling will happen as soon as the page loads. `click` means that the configured element needs to be clicked first. |
52+
| `trigger` | `click` | `click`, `auto` | `auto` means that the scrolling will happen as soon as the page loads, additional to `click` events. `click` means that the configured element needs to be clicked first. |
5353
| `direction` | `top` | `top`, `left` | The direction in which the scrolling happens. |
5454
| `selector` | `top`, `bottom`, CSS selector | A CSS or jQuery selector string or 'top'. | A selector for the element which will be scrolled by a number of pixels equal to `offset`. By default it will be the element on which the pattern is declared. Ignored unless `offset` is specified. |
5555
| `offset` | | A number | `offset` can only be used with scrollable elements. (An element is "scrollable" if it has scrollbars, i.e. when the CSS property `overflow` is either `auto` or `scroll`.) The element scrolled by `offset` can be specified with the `selector` option. If `selector` is not present, the element on which `pat-scroll` is declared will be scrolled. |

src/pat/scroll/scroll.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ export default Base.extend({
2828
ImagesLoaded = ImagesLoaded.default;
2929
// Only calculate the offset when all images are loaded
3030
ImagesLoaded($("body"), () => this.smoothScroll());
31-
} else if (this.options.trigger == "click") {
32-
this.$el.click(this.onClick.bind(this));
3331
}
32+
this.el.addEventListener("click", this.onClick.bind(this));
3433
this.$el.on("pat-update", this.onPatternsUpdate.bind(this));
3534
this.markBasedOnFragment();
3635
this.on("hashchange", this.clearIfHidden.bind(this));

src/pat/scroll/scroll.test.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,46 @@ describe("pat-scroll", function () {
1111
jest.restoreAllMocks();
1212
});
1313

14-
describe("If the trigger is set to 'auto", function () {
15-
it("will automatically scroll to an anchor if the trigger is set to 'auto'", async function (done) {
14+
describe("pat-scroll ...", function () {
15+
it("will automatically scroll to an anchor if the trigger is set to 'auto' and will also handle 'click' events", async function (done) {
1616
$("#lab").html(
1717
[
1818
'<a href="#p1" class="pat-scroll" data-pat-scroll="trigger: auto">p1</a>',
1919
'<p id="p1"></p>',
2020
].join("\n")
2121
);
22+
const el = document.querySelector(".pat-scroll");
2223
const spy_animate = jest.spyOn($.fn, "animate");
23-
pattern.init($(".pat-scroll"));
24+
25+
pattern.init(el);
2426
await utils.timeout(10); // wait some ticks for async to settle.
25-
expect(spy_animate).toHaveBeenCalled();
27+
28+
expect(spy_animate).toHaveBeenCalledTimes(1);
29+
30+
el.click();
31+
await utils.timeout(1); // wait a tick for async to settle.
32+
33+
expect(spy_animate).toHaveBeenCalledTimes(2);
34+
2635
done();
2736
});
28-
});
2937

30-
describe("If the trigger is set to 'click'", function () {
31-
it("will scroll to an anchor on click", async function (done) {
38+
it("will scroll to an anchor on click if the trigger is set to 'click'", async function (done) {
3239
$("#lab").html(
3340
[
3441
'<a href="#p1" class="pat-scroll" data-pat-scroll="trigger: click">p1</a>',
3542
'<p id="p1"></p>',
3643
].join("\n")
3744
);
38-
const $el = $(".pat-scroll");
45+
const el = document.querySelector(".pat-scroll");
3946
const spy_animate = spyOn($.fn, "animate");
40-
pattern.init($el);
47+
48+
pattern.init(el);
4149
await utils.timeout(1); // wait a tick for async to settle.
42-
$el.click();
50+
51+
el.click();
4352
await utils.timeout(1); // wait a tick for async to settle.
44-
// wait for scrolling via click to be done.
53+
4554
expect(spy_animate).toHaveBeenCalled();
4655
done();
4756
});

0 commit comments

Comments
 (0)