-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slider): use resize observer for slider reset
- Loading branch information
Showing
5 changed files
with
29 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import type BoxSlider from './box-slider' | ||
import BoxSlider from './box-slider' | ||
|
||
let boxSliders: BoxSlider[] = [] | ||
let resizeDebounceTimer: number | ||
let resizeObserver: ResizeObserver | ||
|
||
if (typeof window !== 'undefined') { | ||
window.addEventListener('resize', () => { | ||
window.clearTimeout(resizeDebounceTimer) | ||
resizeDebounceTimer = window.setTimeout(() => boxSliders.forEach((b) => b.reset()), 200) | ||
if (typeof ResizeObserver !== 'undefined') { | ||
resizeObserver = new ResizeObserver((sliderElements) => { | ||
sliderElements.forEach((sliderEl) => boxSliders.find((bs) => bs.el === sliderEl.target)?.reset()) | ||
}) | ||
} | ||
|
||
export const responder = { | ||
add(boxSlider: BoxSlider): void { | ||
boxSliders.push(boxSlider) | ||
resizeObserver?.observe(boxSlider.el) | ||
}, | ||
remove(boxSlider: BoxSlider): void { | ||
resizeObserver?.unobserve(boxSlider.el) | ||
boxSliders = boxSliders.filter((b) => b !== boxSlider) | ||
}, | ||
} | ||
|
||
export default responder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters