Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Dom/scrollLocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export default class ScrollLocker {
// If same container effect, return
if (
locks.some(
({ options }) => options?.container === this.options?.container,
({ options }) =>
(options?.container || document.body) ===
(this.options?.container || document.body),
)
) {
locks = [...locks, { target: this.lockTarget, options: this.options }];
Expand All @@ -87,7 +89,9 @@ export default class ScrollLocker {

if (
locks.filter(
({ options }) => options?.container === this.options?.container,
({ options }) =>
(options?.container || document.body) ===
(this.options?.container || document.body),
).length === 0
) {
cacheStyle.set(
Expand Down Expand Up @@ -124,7 +128,9 @@ export default class ScrollLocker {
if (
!findLock ||
locks.some(
({ options }) => options?.container === findLock.options?.container,
({ options }) =>
(options?.container || document.body) ===
(findLock.options?.container || document.body),
)
) {
return;
Expand Down
22 changes: 22 additions & 0 deletions tests/scrollLocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,26 @@ describe('ScrollLocker', () => {

domSpy.mockRestore();
});
it.each([false, true])(
'shares the default body lock with an explicit body container (explicit first: %s)',
explicitFirst => {
const defaultLocker = new ScrollLocker();
const explicitLocker = new ScrollLocker({ container: document.body });
const first = explicitFirst ? explicitLocker : defaultLocker;
const second = explicitFirst ? defaultLocker : explicitLocker;
document.body.style.overflow = 'auto';

first.lock();
second.lock();
first.unLock();

expect(document.body.style.overflow).toBe('hidden');
expect(document.body.classList.contains(effectClassname)).toBe(true);

second.unLock();
expect(document.body.style.overflow).toBe('auto');
expect(document.body.classList.contains(effectClassname)).toBe(false);
document.body.removeAttribute('style');
},
);
});