Skip to content

Commit 0585fe4

Browse files
committed
feat: add methods for disable/enable all parallax elements #11
1 parent 26d7352 commit 0585fe4

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/classes/ParallaxController.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ describe('Expect the ParallaxController', () => {
128128
controller.destroy();
129129
});
130130

131+
it('to disable all elements when calling disableAllElements()', () => {
132+
const controller = ParallaxController.init({
133+
scrollAxis: ScrollAxis.vertical,
134+
});
135+
const elements = Array.from({ length: 3 }, () =>
136+
controller.createElement(OPTIONS)
137+
);
138+
elements.forEach(element => {
139+
expect(element.props.disabled).toBe(false);
140+
});
141+
controller.disableAllElements();
142+
elements.forEach(element => {
143+
expect(element.props.disabled).toBe(true);
144+
});
145+
});
146+
147+
it('to enable all elements when calling enableAllElements()', () => {
148+
const controller = ParallaxController.init({
149+
scrollAxis: ScrollAxis.vertical,
150+
});
151+
const elements = Array.from({ length: 3 }, () =>
152+
controller.createElement({ ...OPTIONS, props: { disabled: true } })
153+
);
154+
elements.forEach(element => {
155+
expect(element.props.disabled).toBe(true);
156+
});
157+
controller.enableAllElements();
158+
elements.forEach(element => {
159+
expect(element.props.disabled).toBe(false);
160+
});
161+
});
162+
131163
it('to remove listeners when destroyed', () => {
132164
window.removeEventListener = jest.fn();
133165
const controller = ParallaxController.init({

src/classes/ParallaxController.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,12 @@ export class ParallaxController {
233233
const scrollHeight = this.viewEl.scrollHeight;
234234
// @ts-expect-error
235235
const scrollWidth = this.viewEl.scrollWidth;
236-
return this.view.setSize({ width, height, scrollHeight, scrollWidth });
236+
return this.view.setSize({
237+
width,
238+
height,
239+
scrollHeight,
240+
scrollWidth,
241+
});
237242
}
238243

239244
const html = document.documentElement;
@@ -277,7 +282,10 @@ export class ParallaxController {
277282
* Creates and returns new parallax element with provided options to be managed by the controller.
278283
*/
279284
createElement(options: CreateElementOptions): Element {
280-
const newElement = new Element({ ...options, scrollAxis: this.scrollAxis });
285+
const newElement = new Element({
286+
...options,
287+
scrollAxis: this.scrollAxis,
288+
});
281289
newElement.setCachedAttributes(this.view, this.scroll);
282290
this.elements = this.elements
283291
? [...this.elements, newElement]
@@ -357,6 +365,30 @@ export class ParallaxController {
357365
this._updateAllElements({ updateCache: true });
358366
}
359367

368+
/**
369+
* Disable all parallax elements
370+
*/
371+
disableAllElements() {
372+
if (this.elements) {
373+
this.elements = this.elements.map(el => {
374+
return el.updateProps({ disabled: true });
375+
});
376+
}
377+
this.update();
378+
}
379+
380+
/**
381+
* Enable all parallax elements
382+
*/
383+
enableAllElements() {
384+
if (this.elements) {
385+
this.elements = this.elements.map(el => {
386+
return el.updateProps({ disabled: false });
387+
});
388+
}
389+
this.update();
390+
}
391+
360392
/**
361393
* Removes all listeners and resets all styles on managed elements.
362394
*/

0 commit comments

Comments
 (0)