Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create controller only once #235

Merged
merged 1 commit into from
Nov 21, 2023
Merged
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
13 changes: 7 additions & 6 deletions src/components/ParallaxProvider/ParallaxProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React, { PropsWithChildren, useEffect, useRef } from 'react';

import { ParallaxContext } from '../../context/ParallaxContext';
import { ScrollAxis } from 'parallax-controller';
import { ParallaxController, ScrollAxis } from 'parallax-controller';
import { ParallaxProviderProps } from './types';
import { createController } from './helpers';

export function ParallaxProvider(
props: PropsWithChildren<ParallaxProviderProps>
) {
const controller = useRef(
createController({
const controller = useRef<null | ParallaxController>(null);

if (!controller.current) {
controller.current = createController({
scrollAxis: props.scrollAxis || ScrollAxis.vertical,
scrollContainer: props.scrollContainer,
disabled: props.isDisabled,
})
);
});
}

// update scroll container
useEffect(() => {
Expand All @@ -37,7 +39,6 @@ export function ParallaxProvider(
useEffect(() => {
return () => {
controller?.current && controller?.current.destroy();
controller.current = null;
};
}, []);

Expand Down
16 changes: 14 additions & 2 deletions src/components/ParallaxProvider/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ParallaxController } from 'parallax-controller';
import { render } from '@testing-library/react';
import { ParallaxProvider } from '.';
import { useParallaxController } from '../../hooks/useParallaxController';
import * as helpers from './helpers';

describe('A <ParallaxProvider>', () => {
it('to render children', () => {
Expand Down Expand Up @@ -48,6 +49,15 @@ describe('A <ParallaxProvider>', () => {
expect(parallaxController).toBeInstanceOf(ParallaxController);
});

it('calls to createController only once', () => {
jest.spyOn(helpers, 'createController');
const { rerender } = render(<ParallaxProvider />);
rerender(<ParallaxProvider />);
rerender(<ParallaxProvider />);
rerender(<ParallaxProvider />);
expect(helpers.createController).toHaveBeenCalledTimes(1);
});

it('to disable parallax elements and re-enable', () => {
let parallaxController: ParallaxController | null = null;

Expand Down Expand Up @@ -100,8 +110,10 @@ describe('A <ParallaxProvider>', () => {
);

screen.unmount();
// @ts-expect-error
expect(parallaxController?.destroy).toBeCalled();

expect(
(parallaxController as unknown as ParallaxController)?.destroy
).toBeCalled();
});

it('to update the scroll container when receiving a new container el', () => {
Expand Down
Loading