Skip to content

Commit f6819f0

Browse files
committed
rename x/y internal values to translateX/translateY
1 parent 69ca2bb commit f6819f0

13 files changed

+218
-142
lines changed

src/classes/Bounds.test.ts

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ describe.each([
1313
originTotalDistX: 1700,
1414
},
1515
{
16-
y0: { value: 0, unit: 'px' },
17-
y1: { value: 0, unit: 'px' },
18-
x1: { value: 0, unit: 'px' },
19-
x0: { value: 0, unit: 'px' },
16+
translateY: [
17+
{ value: 0, unit: 'px' },
18+
{ value: 0, unit: 'px' },
19+
],
20+
translateX: [
21+
{ value: 0, unit: 'px' },
22+
{ value: 0, unit: 'px' },
23+
],
2024
},
2125
{ width: 1000, height: 100 },
2226
{
@@ -40,10 +44,14 @@ describe.each([
4044
originTotalDistX: 700,
4145
},
4246
{
43-
y0: { value: -10, unit: '%' },
44-
y1: { value: 10, unit: '%' },
45-
x1: { value: -10, unit: '%' },
46-
x0: { value: 10, unit: '%' },
47+
translateY: [
48+
{ value: -10, unit: '%' },
49+
{ value: 10, unit: '%' },
50+
],
51+
translateX: [
52+
{ value: 10, unit: '%' },
53+
{ value: -10, unit: '%' },
54+
],
4755
},
4856
{ width: 500, height: 500 },
4957
{
@@ -69,10 +77,14 @@ describe.each([
6977
{
7078
xUnit: '%',
7179
yUnit: '%',
72-
y0: { value: 50, unit: '%' },
73-
y1: { value: -50, unit: '%' },
74-
x0: { value: 0, unit: '%' },
75-
x1: { value: 0, unit: '%' },
80+
translateY: [
81+
{ value: 50, unit: '%' },
82+
{ value: -50, unit: '%' },
83+
],
84+
translateX: [
85+
{ value: 0, unit: '%' },
86+
{ value: 0, unit: '%' },
87+
],
7688
},
7789
{ width: 805, height: 675 },
7890
{
@@ -98,10 +110,14 @@ describe.each([
98110
{
99111
xUnit: '%',
100112
yUnit: '%',
101-
y0: { value: 50, unit: '%' },
102-
y1: { value: -50, unit: '%' },
103-
x0: { value: 0, unit: '%' },
104-
x1: { value: 0, unit: '%' },
113+
translateY: [
114+
{ value: 50, unit: '%' },
115+
{ value: -50, unit: '%' },
116+
],
117+
translateX: [
118+
{ value: 0, unit: '%' },
119+
{ value: 0, unit: '%' },
120+
],
105121
},
106122
{ width: 1024, height: 675 },
107123
{
@@ -127,10 +143,14 @@ describe.each([
127143
{
128144
xUnit: '%',
129145
yUnit: '%',
130-
y0: { value: 0, unit: '%' },
131-
y1: { value: 0, unit: '%' },
132-
x0: { value: -50, unit: '%' },
133-
x1: { value: 50, unit: '%' },
146+
translateY: [
147+
{ value: 0, unit: '%' },
148+
{ value: 0, unit: '%' },
149+
],
150+
translateX: [
151+
{ value: -50, unit: '%' },
152+
{ value: 50, unit: '%' },
153+
],
134154
},
135155
{ width: 1024, height: 675 },
136156
{
@@ -156,10 +176,14 @@ describe.each([
156176
{
157177
xUnit: '%',
158178
yUnit: '%',
159-
y0: { value: 50, unit: '%' },
160-
y1: { value: -50, unit: '%' },
161-
x0: { value: 50, unit: '%' },
162-
x1: { value: -50, unit: '%' },
179+
translateY: [
180+
{ value: 50, unit: '%' },
181+
{ value: -50, unit: '%' },
182+
],
183+
translateX: [
184+
{ value: 50, unit: '%' },
185+
{ value: -50, unit: '%' },
186+
],
163187
},
164188
{ width: 1024, height: 813 },
165189
{
@@ -185,10 +209,14 @@ describe.each([
185209
{
186210
xUnit: '%',
187211
yUnit: '%',
188-
y0: { value: -50, unit: '%' },
189-
y1: { value: 50, unit: '%' },
190-
x0: { value: -50, unit: '%' },
191-
x1: { value: 50, unit: '%' },
212+
translateY: [
213+
{ value: -50, unit: '%' },
214+
{ value: 50, unit: '%' },
215+
],
216+
translateX: [
217+
{ value: -50, unit: '%' },
218+
{ value: 50, unit: '%' },
219+
],
192220
},
193221
{ width: 1024, height: 813 },
194222
{
@@ -214,10 +242,14 @@ describe.each([
214242
{
215243
xUnit: '%',
216244
yUnit: '%',
217-
y0: { value: 50, unit: '%' },
218-
y1: { value: -50, unit: '%' },
219-
x0: { value: 50, unit: '%' },
220-
x1: { value: -50, unit: '%' },
245+
translateY: [
246+
{ value: 50, unit: '%' },
247+
{ value: -50, unit: '%' },
248+
],
249+
translateX: [
250+
{ value: 50, unit: '%' },
251+
{ value: -50, unit: '%' },
252+
],
221253
},
222254
{ width: 1024, height: 813 },
223255
{
@@ -243,10 +275,14 @@ describe.each([
243275
{
244276
xUnit: '%',
245277
yUnit: 'px',
246-
y0: { value: 85, unit: 'px' },
247-
y1: { value: -85, unit: 'px' },
248-
x0: { value: 0, unit: '%' },
249-
x1: { value: 0, unit: '%' },
278+
translateY: [
279+
{ value: 85, unit: 'px' },
280+
{ value: -85, unit: 'px' },
281+
],
282+
translateX: [
283+
{ value: 0, unit: '%' },
284+
{ value: 0, unit: '%' },
285+
],
250286
},
251287
{ width: 979, height: 643 },
252288
{
@@ -272,10 +308,14 @@ describe.each([
272308
{
273309
xUnit: '%',
274310
yUnit: 'px',
275-
y0: { value: -200, unit: 'px' },
276-
y1: { value: 125, unit: 'px' },
277-
x0: { value: 0, unit: '%' },
278-
x1: { value: 0, unit: '%' },
311+
translateY: [
312+
{ value: -200, unit: 'px' },
313+
{ value: 125, unit: 'px' },
314+
],
315+
translateX: [
316+
{ value: 0, unit: '%' },
317+
{ value: 0, unit: '%' },
318+
],
279319
},
280320
{ width: 891, height: 643 },
281321
{

src/classes/Bounds.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export class Bounds {
1111
right: number;
1212

1313
constructor(rect: Rect, offsets: ParallaxStartEndOffsets, view: View) {
14-
const { y0, y1, x1, x0 } = offsets;
14+
const {
15+
translateY: [y0, y1],
16+
translateX: [x0, x1],
17+
} = offsets;
1518

1619
// Y offsets
1720
const yPercent = y1.unit === '%' && y0.unit === '%';

src/classes/Element.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const DEFAULT_OPTIONS = {
88
elInner: document.createElement('div'),
99
elOuter: document.createElement('div'),
1010
scrollAxis: ScrollAxis.vertical,
11-
props: { x0: 0, x1: 0, y0: 0, y1: 0 },
11+
props: { translateX: [0, 0], translateY: [0, 0] },
1212
};
1313

1414
describe('Expect the Element class', () => {
@@ -19,7 +19,11 @@ describe('Expect the Element class', () => {
1919

2020
it('to update props and return the instance', () => {
2121
const element = new Element(DEFAULT_OPTIONS);
22-
const updates = { disabled: true, x0: 100, x1: 100, y0: 0, y1: 0 };
22+
const updates = {
23+
disabled: true,
24+
translateX: [100, 100],
25+
translateY: [0, 0],
26+
};
2327
const instance = element.updateProps(updates);
2428
expect(instance.props).toMatchObject(updates);
2529
expect(instance).toBeInstanceOf(Element);

src/classes/ParallaxController.test.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ const OPTIONS = {
1212
elOuter: document.createElement('div'),
1313
props: {
1414
disabled: false,
15-
x1: 0,
16-
x0: 0,
17-
y1: 0,
18-
y0: 0,
15+
translateX: [0, 0],
16+
translateY: [0, 0],
1917
},
2018
};
2119

@@ -66,19 +64,21 @@ describe('Expect the ParallaxController', () => {
6664
percent: 100,
6765
updatePosition: expect.any(Function),
6866
offsets: {
69-
x1: { unit: '%', value: 0 },
70-
x0: { unit: '%', value: 0 },
67+
translateY: [
68+
{ unit: '%', value: 0 },
69+
{ unit: '%', value: 0 },
70+
],
71+
translateX: [
72+
{ unit: '%', value: 0 },
73+
{ unit: '%', value: 0 },
74+
],
7175
xUnit: '%',
72-
y1: { unit: '%', value: 0 },
73-
y0: { unit: '%', value: 0 },
7476
yUnit: '%',
7577
},
7678
props: {
7779
disabled: false,
78-
x1: 0,
79-
x0: 0,
80-
y1: 0,
81-
y0: 0,
80+
translateX: [0, 0],
81+
translateY: [0, 0],
8282
},
8383
};
8484
expect(element).toMatchObject(expectedElement);
@@ -116,13 +116,11 @@ describe('Expect the ParallaxController', () => {
116116
elOuter: document.createElement('div'),
117117
props: {
118118
disabled: false,
119-
x1: '100px',
120-
x0: '-10%',
121-
y1: '50px',
122-
y0: 100, // defaults to %
119+
translateX: ['-10%', '100px'],
120+
translateY: [100, '50px'],
123121
},
124122
};
125-
123+
// @ts-expect-error
126124
expect(() => controller.createElement(incorrectOffsets)).toThrowError(
127125
'Must provide matching units for the min and max offset values of each axis.'
128126
);

src/classes/ParallaxController.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ export type ParallaxControllerOptions = {
2828

2929
export type ParallaxElementProperties = {
3030
disabled?: boolean;
31-
x0: string | number;
32-
x1: string | number;
33-
y0: string | number;
34-
y1: string | number;
31+
translateX: string[] | number[];
32+
translateY: string[] | number[];
3533
};
3634

3735
export type CreateElementOptions = {

src/components/Parallax.test.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('Expect the <Parallax> component', () => {
7474
expect(controller.createElement).toBeCalledWith({
7575
elInner: expect.any(HTMLElement),
7676
elOuter: expect.any(HTMLElement),
77-
props: { disabled: false, x0: 0, x1: 0, y0: -100, y1: 100 },
77+
props: { disabled: false, translateX: [0, 0], translateY: [-100, 100] },
7878
});
7979
});
8080

@@ -116,10 +116,8 @@ describe('Expect the <Parallax> component', () => {
116116

117117
expect(controller.updateElementPropsById).toBeCalledWith(element.id, {
118118
disabled: false,
119-
x0: 100,
120-
x1: -100,
121-
y0: -100,
122-
y1: 100,
119+
translateX: [100, -100],
120+
translateY: [-100, 100],
123121
});
124122

125123
const newProps = { disabled: false, x: [-40, -60], y: [10, 80] };
@@ -130,10 +128,8 @@ describe('Expect the <Parallax> component', () => {
130128

131129
expect(controller.updateElementPropsById).toBeCalledWith(element.id, {
132130
disabled: false,
133-
x0: -40,
134-
x1: -60,
135-
y0: 10,
136-
y1: 80,
131+
translateX: [-40, -60],
132+
translateY: [10, 80],
137133
});
138134

139135
// only update with valid props

src/components/Parallax.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,29 @@ import { useController } from '../hooks/useController';
88

99
export interface ParallaxProps {
1010
/**
11-
* Offsets on x-axis in % or px. If no unit is passed percent is assumed. Percent is based on
11+
* Start and end translation on x-axis in % or px. If no unit is passed percent is assumed. Percent is based on
1212
* the elements width.
13+
*
14+
* Example:
15+
*
16+
* x={[-100, 100]}
17+
*
18+
* First value is the starting translation
19+
* Second value is the ending translation
1320
*/
14-
x?: Array<string | number>;
21+
x?: string[] | number[];
1522
/**
16-
* Offsets on y-axis in % or px. If no unit is passed percent is assumed. Percent is based on
17-
* the elements width.
23+
* Start and end translation on y-axis in % or px. If no unit is passed percent is assumed. Percent is based on
24+
* the elements height.
25+
*
26+
* Example:
27+
*
28+
* y={[-100, 100]}
29+
*
30+
* First value is the starting translation
31+
* Second value is the ending translation
1832
*/
19-
y?: Array<string | number>;
33+
y?: string[] | number[];
2034
/**
2135
* Optionally pass additional class names to be added to the outermost parallax element.
2236
*/
@@ -56,7 +70,7 @@ function useVerifyController(controller: ParallaxController) {
5670
}, [controller]);
5771
}
5872

59-
function Parallax(props: PropsWithChildren<ParallaxProps>) {
73+
export function Parallax(props: PropsWithChildren<ParallaxProps>) {
6074
const controller = useController();
6175
const refInner = useRef<HTMLElement>();
6276
const refOuter = useRef<HTMLElement>();
@@ -71,13 +85,9 @@ function Parallax(props: PropsWithChildren<ParallaxProps>) {
7185
disabled: props.disabled,
7286
// Defaults set in Parallax.defaultProps
7387
// @ts-expect-error
74-
x0: props.x[0],
75-
// @ts-expect-error
76-
x1: props.x[1],
88+
translateX: props.x,
7789
// @ts-expect-error
78-
y0: props.y[0],
79-
// @ts-expect-error
80-
y1: props.y[1],
90+
translateY: props.y,
8191
},
8292
};
8393
}
@@ -128,5 +138,3 @@ Parallax.defaultProps = {
128138
x: [0, 0],
129139
y: [0, 0],
130140
};
131-
132-
export { Parallax };

0 commit comments

Comments
 (0)