Skip to content

Commit 9c99a2e

Browse files
committed
fix(pat-collapsible): Allow to explicitly clear the scroll selector.
In case of nested pat-collapsible elements, child elements inherit the options from parent elements, including the scroll-selector. If the child element should do no scrolling, it needs to explicitly be reset. This can be done by adding th following options to the collapsible data attribute: data-pat-collapsible="scroll-selector: none"
1 parent 1272ac5 commit 9c99a2e

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/pat/collapsible/collapsible.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,27 @@ class Pattern extends BasePattern {
106106
$(document).on("click", this.options.openTrigger, this.open.bind(this));
107107
}
108108

109-
// scroll debouncer for later use.
110-
this.debounce_scroll = utils.debounce(
111-
this._scroll.bind(this),
112-
10,
113-
debounce_scroll_timer
114-
);
115-
116109
// pat-scroll support
117-
if (this.options.scroll?.selector) {
110+
if (this.options.scroll?.selector && this.options.scroll.selector !== "none") {
118111
const Scroll = (await import("../scroll/scroll")).default;
119112
this.scroll = new Scroll(this.el, {
120113
trigger: "manual",
121114
selector: this.options.scroll.selector,
122115
offset: this.options.scroll?.offset,
123116
});
124117
await events.await_pattern_init(this.scroll);
118+
119+
// scroll debouncer for later use.
120+
this.debounce_scroll = utils.debounce(
121+
this.scroll.scrollTo.bind(this.scroll),
122+
10,
123+
debounce_scroll_timer
124+
);
125125
}
126126

127127
return $el;
128128
}
129129

130-
async _scroll() {
131-
const scroll_selector = this.options.scroll?.selector;
132-
if (!scroll_selector) {
133-
return;
134-
}
135-
await this.scroll.scrollTo();
136-
}
137-
138130
open() {
139131
if (!this.$el.hasClass("open")) {
140132
this.toggle();
@@ -196,7 +188,7 @@ class Pattern extends BasePattern {
196188
if (new_state === "open") {
197189
this.$el.trigger("patterns-collapsible-open");
198190
this._transit(this.$el, "closed", "open");
199-
this.debounce_scroll();
191+
this.debounce_scroll?.(); // debounce scroll, if available.
200192
} else {
201193
this.$el.trigger("patterns-collapsible-close");
202194
this._transit(this.$el, "open", "closed");

src/pat/collapsible/collapsible.test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,12 @@ describe("pat-collapsible", function () {
147147
`;
148148
const collapsible = document.querySelector(".pat-collapsible");
149149
const instance = new Pattern(collapsible, { transition: "none" });
150-
const spy_scroll = jest.spyOn(instance, "_scroll");
151150
await events.await_pattern_init(instance);
152151

153152
instance.toggle();
154153
await utils.timeout(10);
155154

156-
expect(spy_scroll).toHaveBeenCalledTimes(1);
155+
expect(this.spy_scrollTo).toHaveBeenCalledTimes(1);
157156
});
158157

159158
it("8.2 - does not scroll when being closed.", async function () {
@@ -164,13 +163,12 @@ describe("pat-collapsible", function () {
164163
`;
165164
const collapsible = document.querySelector(".pat-collapsible");
166165
const instance = new Pattern(collapsible, { transition: "none" });
167-
const spy_scroll = jest.spyOn(instance, "_scroll");
168166
await events.await_pattern_init(instance);
169167

170168
instance.toggle();
171169
await utils.timeout(10);
172170

173-
expect(spy_scroll).not.toHaveBeenCalled();
171+
expect(this.spy_scrollTo).not.toHaveBeenCalled();
174172
});
175173

176174
it("8.3 - only scrolls once even if multiple collapsible are opened at once.", async function () {
@@ -238,6 +236,29 @@ describe("pat-collapsible", function () {
238236
const arg_1 = this.spy_scrollTo.mock.calls[0][0];
239237
expect(arg_1.top).toBe(40); // the offset is substracted from the scroll position, so a negative offset is added to the scroll position and stops AFTER the target position.
240238
});
239+
240+
it("8.6 - disables scrolling if a parent pat-collapsible has enabled it.", async function () {
241+
document.body.innerHTML = `
242+
<div id="id1" class="pat-collapsible closed" data-pat-collapsible="scroll-selector: self; transition: none">
243+
<p>Collapsible content</p>
244+
<div id="id2" class="pat-collapsible closed" data-pat-collapsible="scroll-selector: none">
245+
<p>Collapsible content</p>
246+
</div>
247+
</div>
248+
`;
249+
const collapsible_1 = document.querySelector("#id1");
250+
const instance_1 = new Pattern(collapsible_1);
251+
await events.await_pattern_init(instance_1);
252+
253+
const collapsible_2 = document.querySelector("#id2");
254+
const instance_2 = new Pattern(collapsible_2);
255+
await events.await_pattern_init(instance_2);
256+
257+
instance_2.toggle();
258+
await utils.timeout(10);
259+
260+
expect(this.spy_scrollTo).not.toHaveBeenCalled();
261+
});
241262
});
242263

243264
it("9 - triggers the pat-update event.", async function () {

src/pat/collapsible/documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,5 @@ attribute. The available options are:
169169
| `transition` | `slide` | Transition effect when opening or closing a collapsinble. Must be one of `none`, `css`, `fade`, `slide` or `slide-horizontal`. |
170170
| `effect-duration` | `fast` | Duration of transition. This is ignored if the transition is `none` or `css`. |
171171
| `effect-easing` | `swing` | Easing to use for the open/close animation. This must be a known jQuery easing method. jQuery includes `swing` and `linear`, but more can be included via jQuery UI. |
172-
| `scroll-selector` | | CSS selector or `self`. Defines which element will be scrolled into view. `self` if it is the collapsible element itself. |
172+
| `scroll-selector` | | CSS selector, `self` or `none`. Defines which element will be scrolled into view. `self` if it is the collapsible element itself. `none` to disable scrolling if a scrolling selector is inherited from a parent pat-collapsible element. |
173173
| `scroll-offset` | | `offset` in pixels to stop scrolling before the target position defines by `scroll-selector`. Can also be a negative number. |

0 commit comments

Comments
 (0)