Skip to content

Commit f662288

Browse files
committed
chore: move components to folders
1 parent 902c095 commit f662288

File tree

13 files changed

+214
-213
lines changed

13 files changed

+214
-213
lines changed

src/components/Parallax/hooks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ParallaxController } from 'parallax-controller';
2+
import { useEffect } from 'react';
3+
4+
export function useVerifyController(controller: ParallaxController) {
5+
useEffect(() => {
6+
// Make sure the provided controller is an instance of the Parallax Controller
7+
const isInstance = controller instanceof ParallaxController;
8+
// Throw if neither context or global is available
9+
if (!controller && !isInstance) {
10+
throw new Error(
11+
"Must wrap your application's <Parallax /> components in a <ParallaxProvider />."
12+
);
13+
}
14+
}, [controller]);
15+
}

src/components/Parallax.test.tsx renamed to src/components/Parallax/index.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { PropsWithChildren } from 'react';
22
import { ParallaxController, ScrollAxis } from 'parallax-controller';
33
import { render } from '@testing-library/react';
4-
import { Parallax } from './Parallax';
5-
import { ParallaxProvider } from './ParallaxProvider';
4+
import { Parallax } from '.';
5+
import { ParallaxProvider } from '../ParallaxProvider';
66

7-
import { MockProvider } from '../testUtils/MockProvider';
8-
import expectRenderError from '../testUtils/expectRenderError';
7+
import { MockProvider } from '../../testUtils/MockProvider';
8+
import expectRenderError from '../../testUtils/expectRenderError';
99

1010
const consoleLog = global.console.log;
1111

src/components/Parallax/index.tsx

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import React, { PropsWithChildren, useEffect, useRef, useState } from 'react';
2+
import { CreateElementOptions, Element } from 'parallax-controller';
3+
import { useController } from '../../hooks/useController';
4+
import { removeUndefinedObjectKeys } from '../../utils/removeUndefinedObjectKeys';
5+
import { ParallaxProps } from './types';
6+
import { useVerifyController } from './hooks';
7+
8+
export function Parallax(props: PropsWithChildren<ParallaxProps>) {
9+
const controller = useController();
10+
const refInner = useRef<HTMLElement>();
11+
const refOuter = useRef<HTMLElement>();
12+
13+
useVerifyController(controller);
14+
15+
function _getElementOptions(): CreateElementOptions {
16+
const useSpeedProp = typeof props.speed !== 'undefined';
17+
const isHorizontal = controller.scrollAxis == 'horizontal';
18+
const isVertical = controller.scrollAxis == 'vertical';
19+
20+
let translateX = props.translateX;
21+
let translateY = props.translateY;
22+
23+
if (useSpeedProp && isHorizontal) {
24+
translateX = [
25+
`${(props.speed || 0) * 10}px`,
26+
`${(props.speed || 0) * -10}px`,
27+
];
28+
}
29+
30+
if (useSpeedProp && isVertical) {
31+
translateY = [
32+
`${(props.speed || 0) * 10}px`,
33+
`${(props.speed || 0) * -10}px`,
34+
];
35+
}
36+
37+
return {
38+
elInner: refInner.current,
39+
elOuter: refOuter.current,
40+
props: removeUndefinedObjectKeys({
41+
disabled: props.disabled,
42+
translateX,
43+
translateY,
44+
rotate: props.rotate,
45+
rotateX: props.rotateX,
46+
rotateY: props.rotateY,
47+
rotateZ: props.rotateZ,
48+
scale: props.scale,
49+
scaleX: props.scaleX,
50+
scaleY: props.scaleY,
51+
scaleZ: props.scaleZ,
52+
}),
53+
};
54+
}
55+
56+
const [element, setElement] = useState<Element>();
57+
58+
// create element
59+
useEffect(() => {
60+
const newElement = controller.createElement(_getElementOptions());
61+
setElement(newElement);
62+
63+
return () => controller.removeElementById(newElement.id);
64+
}, []);
65+
66+
// update element
67+
useEffect(() => {
68+
if (element) {
69+
if (props.disabled) {
70+
controller.resetElementStyles(element);
71+
} else {
72+
controller.updateElementPropsById(
73+
element.id,
74+
_getElementOptions().props
75+
);
76+
}
77+
}
78+
}, [
79+
props.disabled,
80+
props.translateX,
81+
props.translateY,
82+
props.rotate,
83+
props.rotateX,
84+
props.rotateY,
85+
props.rotateZ,
86+
props.scale,
87+
props.scaleX,
88+
props.scaleY,
89+
props.scaleZ,
90+
props.speed,
91+
]);
92+
93+
const Outer = props.tagOuter;
94+
const Inner = props.tagInner;
95+
96+
const rootClass =
97+
'parallax-outer' + (props.className ? ` ${props.className}` : '');
98+
99+
return (
100+
<Outer className={rootClass} ref={refOuter} style={props.styleOuter}>
101+
<Inner className="parallax-inner" ref={refInner} style={props.styleInner}>
102+
{props.children}
103+
</Inner>
104+
</Outer>
105+
);
106+
}
107+
108+
Parallax.defaultProps = {
109+
disabled: false,
110+
tagInner: 'div',
111+
tagOuter: 'div',
112+
};

src/components/Parallax.tsx renamed to src/components/Parallax/types.ts

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
import React, { PropsWithChildren, useEffect, useRef, useState } from 'react';
2-
import {
3-
CreateElementOptions,
4-
ParallaxController,
5-
Element,
6-
} from 'parallax-controller';
7-
import { useController } from '../hooks/useController';
8-
import { removeUndefinedObjectKeys } from '../utils/removeUndefinedObjectKeys';
9-
101
export interface ParallaxProps {
112
/**
123
* A number to slowdown `n < 0` or speed up `n > 0` the scroll speed of an element
@@ -154,122 +145,3 @@ export interface ParallaxProps {
154145
*/
155146
tagOuter?: any;
156147
}
157-
158-
function useVerifyController(controller: ParallaxController) {
159-
useEffect(() => {
160-
// Make sure the provided controller is an instance of the Parallax Controller
161-
const isInstance = controller instanceof ParallaxController;
162-
// Throw if neither context or global is available
163-
if (!controller && !isInstance) {
164-
throw new Error(
165-
"Must wrap your application's <Parallax /> components in a <ParallaxProvider />."
166-
);
167-
}
168-
}, [controller]);
169-
}
170-
171-
export function Parallax(props: PropsWithChildren<ParallaxProps>) {
172-
const controller = useController();
173-
const refInner = useRef<HTMLElement>();
174-
const refOuter = useRef<HTMLElement>();
175-
176-
useVerifyController(controller);
177-
178-
function _getElementOptions(): CreateElementOptions {
179-
const useSpeedProp = typeof props.speed !== 'undefined';
180-
const isHorizontal = controller.scrollAxis == 'horizontal';
181-
const isVertical = controller.scrollAxis == 'vertical';
182-
183-
let translateX = props.translateX;
184-
let translateY = props.translateY;
185-
186-
if (useSpeedProp && isHorizontal) {
187-
translateX = [
188-
`${(props.speed || 0) * 10}px`,
189-
`${(props.speed || 0) * -10}px`,
190-
];
191-
}
192-
193-
if (useSpeedProp && isVertical) {
194-
translateY = [
195-
`${(props.speed || 0) * 10}px`,
196-
`${(props.speed || 0) * -10}px`,
197-
];
198-
}
199-
200-
return {
201-
elInner: refInner.current,
202-
elOuter: refOuter.current,
203-
props: removeUndefinedObjectKeys({
204-
disabled: props.disabled,
205-
translateX,
206-
translateY,
207-
rotate: props.rotate,
208-
rotateX: props.rotateX,
209-
rotateY: props.rotateY,
210-
rotateZ: props.rotateZ,
211-
scale: props.scale,
212-
scaleX: props.scaleX,
213-
scaleY: props.scaleY,
214-
scaleZ: props.scaleZ,
215-
}),
216-
};
217-
}
218-
219-
const [element, setElement] = useState<Element>();
220-
221-
// create element
222-
useEffect(() => {
223-
const newElement = controller.createElement(_getElementOptions());
224-
setElement(newElement);
225-
226-
return () => controller.removeElementById(newElement.id);
227-
}, []);
228-
229-
// update element
230-
useEffect(() => {
231-
if (element) {
232-
if (props.disabled) {
233-
controller.resetElementStyles(element);
234-
} else {
235-
controller.updateElementPropsById(
236-
element.id,
237-
_getElementOptions().props
238-
);
239-
}
240-
}
241-
}, [
242-
props.disabled,
243-
props.translateX,
244-
props.translateY,
245-
props.rotate,
246-
props.rotateX,
247-
props.rotateY,
248-
props.rotateZ,
249-
props.scale,
250-
props.scaleX,
251-
props.scaleY,
252-
props.scaleZ,
253-
props.speed,
254-
]);
255-
256-
const Outer = props.tagOuter;
257-
const Inner = props.tagInner;
258-
259-
const rootClass =
260-
'parallax-outer' + (props.className ? ` ${props.className}` : '');
261-
262-
return (
263-
<Outer className={rootClass} ref={refOuter} style={props.styleOuter}>
264-
<Inner className="parallax-inner" ref={refInner} style={props.styleInner}>
265-
{props.children}
266-
</Inner>
267-
</Outer>
268-
);
269-
}
270-
271-
Parallax.defaultProps = {
272-
disabled: false,
273-
tagInner: 'div',
274-
tagOuter: 'div',
275-
};

src/components/ParallaxBanner.test.tsx renamed to src/components/ParallaxBanner/index.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
// @ts-ignore
44
import renderer from 'react-test-renderer';
5-
import createNodeMock from '../testUtils/createNodeMock';
6-
import { ParallaxBanner } from './ParallaxBanner';
7-
import { ParallaxProvider } from './ParallaxProvider';
5+
import createNodeMock from '../../testUtils/createNodeMock';
6+
import { ParallaxBanner } from '.';
7+
import { ParallaxProvider } from '../ParallaxProvider';
88

99
describe('Expect the <ParallaxBanner> component', () => {
1010
afterEach(() => {});

src/components/ParallaxBanner.tsx renamed to src/components/ParallaxBanner/index.tsx

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { PropsWithChildren } from 'react';
2-
import { Parallax } from './Parallax';
2+
import { Parallax } from '../Parallax';
3+
import { ParallaxBannerProps } from './types';
34

45
const containerStyle = {
56
position: 'relative',
@@ -16,50 +17,6 @@ const absoluteStyle = {
1617
left: 0,
1718
};
1819

19-
export interface BannerLayer {
20-
/**
21-
* A value from `-1` to `1` that represents the vertical offset to be applied to the current
22-
* layer, `0.1` would equal a `10%` offset on the top and bottom.
23-
*/
24-
amount: number;
25-
/**
26-
* Custom layer children provided as a React element, for example `<Video />`
27-
*/
28-
children?: any;
29-
/**
30-
* Indicate if the layer should be expanded with negative top/bottom margins so the edges will
31-
* never be visible.
32-
*/
33-
expanded?: boolean;
34-
/**
35-
* Image source that will be applied as a CSS background image on the layer.
36-
*/
37-
image?: string;
38-
/*
39-
* Props to apply to the layer element.
40-
*/
41-
props?: any;
42-
}
43-
44-
export interface ParallaxBannerProps {
45-
/**
46-
* Optionally pass additional class names to be added to the outermost parallax banner element.
47-
*/
48-
className?: string;
49-
/**
50-
* Determines if the internal parallax layers will have offsets applied.
51-
*/
52-
disabled?: boolean;
53-
/**
54-
* A required Array of Objects with layer properties: `[{ amount: 0.1, image: 'foo.jpg' }]`.
55-
*/
56-
layers: BannerLayer[];
57-
/**
58-
* Optionally pass a style object to be added to the outermost parallax banner element.
59-
*/
60-
style?: any;
61-
}
62-
6320
export const ParallaxBanner = ({
6421
children,
6522
className,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export interface BannerLayer {
2+
/**
3+
* A value from `-1` to `1` that represents the vertical offset to be applied to the current
4+
* layer, `0.1` would equal a `10%` offset on the top and bottom.
5+
*/
6+
amount: number;
7+
/**
8+
* Custom layer children provided as a React element, for example `<Video />`
9+
*/
10+
children?: any;
11+
/**
12+
* Indicate if the layer should be expanded with negative top/bottom margins so the edges will
13+
* never be visible.
14+
*/
15+
expanded?: boolean;
16+
/**
17+
* Image source that will be applied as a CSS background image on the layer.
18+
*/
19+
image?: string;
20+
/*
21+
* Props to apply to the layer element.
22+
*/
23+
props?: any;
24+
}
25+
26+
export interface ParallaxBannerProps {
27+
/**
28+
* Optionally pass additional class names to be added to the outermost parallax banner element.
29+
*/
30+
className?: string;
31+
/**
32+
* Determines if the internal parallax layers will have offsets applied.
33+
*/
34+
disabled?: boolean;
35+
/**
36+
* A required Array of Objects with layer properties: `[{ amount: 0.1, image: 'foo.jpg' }]`.
37+
*/
38+
layers: BannerLayer[];
39+
/**
40+
* Optionally pass a style object to be added to the outermost parallax banner element.
41+
*/
42+
style?: any;
43+
}

0 commit comments

Comments
 (0)