Skip to content

Commit b210d0b

Browse files
committed
oops, need to listen to container not window for scroll
1 parent ec8086a commit b210d0b

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { WrapGridArguments } from './types';
2-
export declare const wrapGrid: (container: HTMLElement, { duration, stagger, easing }?: WrapGridArguments) => {
2+
export declare const wrapGrid: (container: HTMLElement, { duration, stagger, easing, onStart, onEnd, watchScroll, }?: WrapGridArguments) => {
33
unwrapGrid: () => void;
44
forceGridAnimation: () => void;
55
};

dist/main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ export interface WrapGridArguments {
4242
duration?: number;
4343
stagger?: number;
4444
easing?: keyof PopmotionEasing;
45+
onStart?: (animatedChildren: HTMLElement[]) => void;
46+
onEnd?: (animatedChildren: HTMLElement[]) => void;
47+
watchScroll?: boolean;
4548
}

src/index.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,22 @@ export const wrapGrid = (
141141
};
142142
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
143143

144-
const createMutationListener = (
145-
eventType: string,
146-
throttleDuration: number,
147-
) => {
148-
const throttledListener = throttle(() => {
149-
const bodyElement = document.querySelector('body');
150-
const containerIsNoLongerInPage =
151-
bodyElement && !bodyElement.contains(container);
152-
if (!container || containerIsNoLongerInPage) {
153-
window.removeEventListener(eventType, throttledListener);
154-
}
155-
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
156-
}, throttleDuration);
157-
return throttledListener;
158-
};
159-
160-
const resizeListener = createMutationListener('resize', 250);
161-
window.addEventListener('resize', resizeListener);
144+
const throttledResizeListener = throttle(() => {
145+
const bodyElement = document.querySelector('body');
146+
const containerIsNoLongerInPage =
147+
bodyElement && !bodyElement.contains(container);
148+
if (!container || containerIsNoLongerInPage) {
149+
window.removeEventListener('resize', throttledResizeListener);
150+
}
151+
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
152+
}, 250);
153+
window.addEventListener('resize', throttledResizeListener);
162154

163-
const scrollListener = createMutationListener('scroll', 20);
155+
const throttledScrollListener = throttle(() => {
156+
recordPositions(container.children as HTMLCollectionOf<HTMLElement>);
157+
}, 20);
164158
if (watchScroll) {
165-
window.addEventListener('scroll', scrollListener);
159+
container.addEventListener('scroll', throttledScrollListener);
166160
}
167161

168162
const mutationCallback = (
@@ -335,9 +329,9 @@ export const wrapGrid = (
335329
attributeFilter: ['class'],
336330
});
337331
const unwrapGrid = () => {
338-
window.removeEventListener('resize', resizeListener);
332+
window.removeEventListener('resize', throttledResizeListener);
339333
if (watchScroll) {
340-
window.removeEventListener('scroll', scrollListener);
334+
container.removeEventListener('scroll', throttledScrollListener);
341335
}
342336
observer.disconnect();
343337
};

0 commit comments

Comments
 (0)