Skip to content

Commit 092127f

Browse files
committed
add speed prop, update tests
1 parent 3e37e68 commit 092127f

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed

src/components/Parallax.test.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,35 @@ describe('Expect the <Parallax> component', () => {
7676
expect(controller.createElement).toBeCalledWith({
7777
elInner: expect.any(HTMLElement),
7878
elOuter: expect.any(HTMLElement),
79-
props: { disabled: false, translateX: [0, 0], translateY: [-100, 100] },
79+
props: { disabled: false, translateY: [-100, 100] },
80+
});
81+
});
82+
83+
it('to handle rotate props', () => {
84+
const controller = ParallaxController.init({
85+
scrollAxis: ScrollAxis.vertical,
86+
});
87+
controller.createElement = jest.fn(controller.createElement);
88+
controller.updateElementPropsById = jest.fn(
89+
controller.updateElementPropsById
90+
);
91+
92+
function Wrapper(props: PropsWithChildren<{}>) {
93+
return (
94+
<MockProvider controllerMock={controller}>
95+
{props.children}
96+
</MockProvider>
97+
);
98+
}
99+
100+
render(<Parallax rotate={['0deg', '100deg']} />, {
101+
wrapper: Wrapper,
102+
});
103+
104+
expect(controller.createElement).toBeCalledWith({
105+
elInner: expect.any(HTMLElement),
106+
elOuter: expect.any(HTMLElement),
107+
props: { disabled: false, rotate: ['0deg', '100deg'] },
80108
});
81109
});
82110

src/components/Parallax.tsx

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ import {
55
Element,
66
} from 'parallax-controller';
77
import { useController } from '../hooks/useController';
8+
import { removeUndefinedObjectKeys } from '../utils/removeUndefinedObjectKeys';
89

910
export interface ParallaxProps {
11+
/**
12+
* A number to slowdown `n < 0` or speed up `n > 0` the scroll speed of an element
13+
*
14+
* Example:
15+
*
16+
* x={-1}
17+
*
18+
*/
19+
speed?: number;
1020
/**
1121
* Start and end translation on x-axis in % or px. If no unit is passed percent is assumed. Percent is based on the elements width.
1222
*
@@ -121,19 +131,39 @@ export function Parallax(props: PropsWithChildren<ParallaxProps>) {
121131
useVerifyController(controller);
122132

123133
function _getElementOptions(): CreateElementOptions {
134+
const useSpeedProp = typeof props.speed !== 'undefined';
135+
const isHorizontal = controller.scrollAxis == 'horizontal';
136+
const isVertical = controller.scrollAxis == 'vertical';
137+
138+
let translateX = props.x;
139+
let translateY = props.y;
140+
141+
if (useSpeedProp && isHorizontal) {
142+
translateX = [
143+
`${(props.speed || 0) * 10}px`,
144+
`${(props.speed || 0) * -10}px`,
145+
];
146+
}
147+
148+
if (useSpeedProp && isVertical) {
149+
translateY = [
150+
`${(props.speed || 0) * 10}px`,
151+
`${(props.speed || 0) * -10}px`,
152+
];
153+
}
154+
124155
return {
125156
elInner: refInner.current,
126157
elOuter: refOuter.current,
127-
props: {
158+
props: removeUndefinedObjectKeys({
128159
disabled: props.disabled,
129-
// Defaults set in Parallax.defaultProps
130-
translateX: props.x,
131-
translateY: props.y,
160+
translateX,
161+
translateY,
132162
rotate: props.rotate,
133163
rotateX: props.rotateX,
134164
rotateY: props.rotateY,
135165
rotateZ: props.rotateZ,
136-
},
166+
}),
137167
};
138168
}
139169

@@ -167,6 +197,7 @@ export function Parallax(props: PropsWithChildren<ParallaxProps>) {
167197
props.rotateX,
168198
props.rotateY,
169199
props.rotateZ,
200+
props.speed,
170201
]);
171202

172203
const Outer = props.tagOuter;

src/components/__snapshots__/Parallax.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`Expect the <Parallax> component to render correctly 1`] = `
88
>
99
<div
1010
class="parallax-inner"
11-
style="border: 2px solid blue; transform: translate3d(100%, 100%, 0);"
11+
style="border: 2px solid blue; transform: translateX(100%)translateY(100%);"
1212
>
1313
<div
1414
class="foo"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function removeUndefinedObjectKeys(obj: { [key: string]: any }) {
2+
Object.keys(obj).forEach((key) =>
3+
obj[key] === undefined ? delete obj[key] : {}
4+
);
5+
return obj;
6+
}

stories/Parallax/0_ParallaxExamples.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ WithRotationXYZ.args = {
6464
rotateZ: '0deg,360deg',
6565
};
6666

67+
export const WithTranslateYAndRotation = Template.bind({});
68+
WithTranslateYAndRotation.args = {
69+
y: '-100,100',
70+
rotate: '0deg,360deg',
71+
};
72+
6773
export default {
6874
title: '<Parallax> New Transforms',
6975
component: WithRotation,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { Parallax } from '../../src';
3+
import { Element } from '../Element/Element';
4+
import { Container } from '../Container';
5+
import styles from './Parallax.module.scss';
6+
7+
const Template = (args) => {
8+
const props = args;
9+
return (
10+
<Container scrollAxis="vertical" className={styles.elements}>
11+
<Parallax speed={props.speed - props.speed} className={styles.parallax}>
12+
<Element name={props.speed - props.speed} />
13+
</Parallax>
14+
<Parallax {...props} className={styles.parallax}>
15+
<Element name={props.speed} />
16+
</Parallax>
17+
<Parallax speed={props.speed + props.speed} className={styles.parallax}>
18+
<Element name={props.speed + props.speed} />
19+
</Parallax>
20+
</Container>
21+
);
22+
};
23+
24+
export const WithSpeed = Template.bind({});
25+
WithSpeed.args = {
26+
speed: -1,
27+
};
28+
29+
export default {
30+
title: '<Parallax> Speed',
31+
component: WithSpeed,
32+
};

0 commit comments

Comments
 (0)