Skip to content

Commit 5aac151

Browse files
committed
enable strict ts
1 parent 29068ad commit 5aac151

21 files changed

+127
-68
lines changed

src/classes/Bounds.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ describe.each([
287287
right: 887.8125,
288288
},
289289
],
290-
])('Bounds()', (rect, offsets, view, expected) => {
290+
])('Bounds()', (rect: any, offsets: any, view: any, expected) => {
291291
test(`returns expected bounds based on rect, offsets, and view`, () => {
292292
expect(new Bounds(rect, offsets, view)).toEqual(
293293
expect.objectContaining(expected)

src/classes/Bounds.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { ParallaxStartEndOffsets } from '../types';
2+
import { Rect } from './Rect';
3+
import { View } from './View';
4+
15
export class Bounds {
26
totalDistY: number;
37
totalDistX: number;
@@ -6,7 +10,7 @@ export class Bounds {
610
left: number;
711
right: number;
812

9-
constructor(rect, offsets, view) {
13+
constructor(rect: Rect, offsets: ParallaxStartEndOffsets, view: View) {
1014
const { y0, y1, x1, x0 } = offsets;
1115

1216
// Y offsets

src/classes/Element.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,31 @@ import { isElementInView } from '../helpers/isElementInView';
77
import { percentMoved } from '../helpers/percentMoved';
88
import { setParallaxStyles } from '../helpers/elementStyles';
99
import { createId } from '../utils/createId';
10+
import {
11+
CreateElementOptions,
12+
ParallaxElementProperties,
13+
} from './ParallaxController';
14+
import { View } from './View';
15+
import { Scroll } from './Scroll';
16+
17+
type ElementConstructorOptions = CreateElementOptions & {
18+
scrollAxis: 'vertical' | 'horizontal';
19+
};
1020

1121
export class Element {
12-
elInner: HTMLElement;
13-
elOuter: HTMLElement;
14-
// TODO
15-
props: any;
22+
elInner?: HTMLElement;
23+
elOuter?: HTMLElement;
24+
props: ParallaxElementProperties;
1625
scrollAxis: 'vertical' | 'horizontal';
1726
id: number;
1827
offsets: ParallaxStartEndOffsets;
19-
isInView: boolean;
28+
isInView: boolean | null;
2029
percent: number;
30+
rect?: Rect;
31+
bounds?: Bounds;
32+
updatePosition: (view: View, scroll: Scroll) => Element;
2133

22-
updatePosition: (view: any, scroll: any) => void;
23-
24-
rect: Rect;
25-
bounds: Bounds;
26-
27-
constructor(options) {
34+
constructor(options: ElementConstructorOptions) {
2835
this.elInner = options.elInner;
2936
this.elOuter = options.elOuter;
3037
this.props = options.props;
@@ -40,19 +47,23 @@ export class Element {
4047
: this._updatePositionHorizontal;
4148
}
4249

43-
updateProps(nextProps) {
50+
updateProps(nextProps: ParallaxElementProperties) {
4451
this.props = { ...this.props, ...nextProps };
4552
this.offsets = getOffsets(nextProps);
4653
return this;
4754
}
4855

49-
setCachedAttributes(view, scroll) {
56+
setCachedAttributes(view: View, scroll: Scroll): Element {
57+
if (!this.elOuter) return this;
58+
5059
this.rect = new Rect(this.elOuter, view, scroll);
5160
this.bounds = new Bounds(this.rect, this.offsets, view);
5261
return this;
5362
}
5463

55-
_updatePositionHorizontal(view, scroll) {
64+
_updatePositionHorizontal(view: View, scroll: Scroll): Element {
65+
if (!this.bounds || !this.rect || !this.elInner) return this;
66+
5667
this.isInView = isElementInView(
5768
this.bounds.left,
5869
this.bounds.right,
@@ -74,7 +85,9 @@ export class Element {
7485
return this;
7586
}
7687

77-
_updatePositionVertical(view, scroll) {
88+
_updatePositionVertical(view: View, scroll: Scroll): Element {
89+
if (!this.bounds || !this.rect || !this.elInner) return this;
90+
7891
this.isInView = isElementInView(
7992
this.bounds.top,
8093
this.bounds.bottom,

src/classes/ParallaxController.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Element } from './Element';
55
import { VERTICAL } from '../constants';
66
import { testForPassiveScroll } from '../utils/testForPassiveScroll';
77

8-
type ViewElement = HTMLElement | Window;
8+
export type ViewElement = HTMLElement | Window;
99

1010
/**
1111
* -------------------------------------------------------
@@ -26,13 +26,19 @@ export type ParallaxControllerOptions = {
2626
};
2727

2828
export type ParallaxElementProperties = {
29-
disabled: boolean;
29+
disabled?: boolean;
3030
x0: string | number;
3131
x1: string | number;
3232
y0: string | number;
3333
y1: string | number;
3434
};
3535

36+
export type CreateElementOptions = {
37+
elInner?: HTMLElement;
38+
elOuter?: HTMLElement;
39+
props: ParallaxElementProperties;
40+
};
41+
3642
export class ParallaxController {
3743
elements: Element[];
3844
scrollAxis: 'vertical' | 'horizontal';
@@ -68,7 +74,7 @@ export class ParallaxController {
6874
this.elements = [];
6975

7076
this._hasScrollContainer = !!scrollContainer;
71-
this.viewEl = scrollContainer || window;
77+
this.viewEl = scrollContainer ?? window;
7278

7379
// Scroll and View
7480
const x = this._hasScrollContainer
@@ -81,7 +87,11 @@ export class ParallaxController {
8187
: window.pageYOffset;
8288

8389
this.scroll = new Scroll(x, y);
84-
this.view = new View({ width: 0, height: 0, scrollContainer });
90+
this.view = new View({
91+
width: 0,
92+
height: 0,
93+
scrollContainer: this._hasScrollContainer ? scrollContainer : undefined,
94+
});
8595

8696
// Ticking
8797
this._ticking = false;
@@ -112,7 +122,8 @@ export class ParallaxController {
112122
'update',
113123
'updateScrollContainer',
114124
'destroy',
115-
].forEach((method) => {
125+
].forEach((method: string) => {
126+
// @ts-expect-error
116127
this[method] = this[method].bind(this);
117128
});
118129
}
@@ -189,7 +200,7 @@ export class ParallaxController {
189200
* Determines if the element is in view based on the cached
190201
* attributes, if so set the elements parallax styles.
191202
*/
192-
private _updateElementPosition(element) {
203+
private _updateElementPosition(element: Element) {
193204
if (element.props.disabled) return;
194205
element.updatePosition(this.view, this.scroll);
195206
}
@@ -233,11 +244,7 @@ export class ParallaxController {
233244
* @param {object} options
234245
* @return {object} element
235246
*/
236-
createElement(options: {
237-
elInner: HTMLElement;
238-
elOuter: HTMLElement;
239-
props: ParallaxElementProperties;
240-
}): Element {
247+
createElement(options: CreateElementOptions): Element {
241248
const newElement = new Element({ ...options, scrollAxis: this.scrollAxis });
242249
newElement.setCachedAttributes(this.view, this.scroll);
243250
this.elements = this.elements
@@ -310,6 +317,7 @@ export class ParallaxController {
310317
if (this.elements) {
311318
this.elements.forEach((element) => resetStyles(element));
312319
}
320+
// @ts-expect-error
313321
this.elements = undefined;
314322
}
315323
}

src/classes/Scroll.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export class Scroll {
33
y: number;
44

55
constructor(x: number, y: number) {
6-
this.setScroll(x, y);
6+
this.x = x;
7+
this.y = y;
78
}
89

910
setScroll(x: number, y: number) {

src/classes/View.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class View {
2-
scrollContainer: HTMLElement;
2+
scrollContainer: HTMLElement | undefined;
33
width: number;
44
height: number;
55

@@ -10,10 +10,11 @@ export class View {
1010
}: {
1111
width: number;
1212
height: number;
13-
scrollContainer: HTMLElement;
13+
scrollContainer?: HTMLElement;
1414
}) {
1515
this.scrollContainer = scrollContainer;
16-
this.setSize(width, height);
16+
this.width = width;
17+
this.height = height;
1718
}
1819

1920
setSize(width: number, height: number) {

src/components/Parallax.test.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { PropsWithChildren } from 'react';
22
import { render } from '@testing-library/react';
33
import { Parallax } from './Parallax';
44
import { ParallaxProvider } from './ParallaxProvider';
@@ -10,14 +10,16 @@ import expectRenderError from '../testUtils/expectRenderError';
1010
const consoleLog = global.console.log;
1111

1212
describe('Expect the <Parallax> component', () => {
13-
const preventError = (e) => e.preventDefault();
13+
const preventError = (e: ErrorEvent) => e.preventDefault();
1414

1515
beforeEach(() => {
1616
window.addEventListener('error', preventError);
1717
});
1818

1919
afterEach(() => {
2020
global.console.log = consoleLog;
21+
// NOTE: this can probably be removed now
22+
// @ts-ignore
2123
global.ParallaxController = undefined;
2224

2325
window.removeEventListener('error', preventError);
@@ -96,7 +98,7 @@ describe('Expect the <Parallax> component', () => {
9698
const controller = ParallaxController.init({ scrollAxis: VERTICAL });
9799
controller.updateElementPropsById = jest.fn();
98100

99-
function Wrapper(props) {
101+
function Wrapper(props: PropsWithChildren<{}>) {
100102
return (
101103
<MockProvider controllerMock={controller}>
102104
{props.children}
@@ -152,7 +154,7 @@ describe('Expect the <Parallax> component', () => {
152154
const controller = ParallaxController.init({ scrollAxis: VERTICAL });
153155
controller.resetElementStyles = jest.fn();
154156

155-
function Wrapper(props) {
157+
function Wrapper(props: PropsWithChildren<{}>) {
156158
return (
157159
<MockProvider controllerMock={controller}>
158160
{props.children}

src/components/Parallax.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import React, { PropsWithChildren, useEffect, useRef, useState } from 'react';
2-
import { ParallaxController } from '../classes/ParallaxController';
2+
import { Element } from '../classes/Element';
3+
import {
4+
CreateElementOptions,
5+
ParallaxController,
6+
} from '../classes/ParallaxController';
37
import { useController } from '../hooks/useController';
48

59
export interface ParallaxProps {
@@ -39,7 +43,7 @@ export interface ParallaxProps {
3943
tagOuter?: any;
4044
}
4145

42-
function useVerifyController(controller) {
46+
function useVerifyController(controller: ParallaxController) {
4347
useEffect(() => {
4448
// Make sure the provided controller is an instance of the Parallax Controller
4549
const isInstance = controller instanceof ParallaxController;
@@ -54,45 +58,46 @@ function useVerifyController(controller) {
5458

5559
function Parallax(props: PropsWithChildren<ParallaxProps>) {
5660
const controller = useController();
57-
const refInner = useRef();
58-
const refOuter = useRef();
61+
const refInner = useRef<HTMLElement>();
62+
const refOuter = useRef<HTMLElement>();
5963

6064
useVerifyController(controller);
6165

62-
function _getElementOptions() {
66+
function _getElementOptions(): CreateElementOptions {
6367
return {
6468
elInner: refInner.current,
6569
elOuter: refOuter.current,
6670
props: {
6771
disabled: props.disabled,
72+
// Defaults set in Parallax.defaultProps
73+
// @ts-expect-error
6874
x0: props.x[0],
75+
// @ts-expect-error
6976
x1: props.x[1],
77+
// @ts-expect-error
7078
y0: props.y[0],
79+
// @ts-expect-error
7180
y1: props.y[1],
7281
},
7382
};
7483
}
7584

76-
const [element, setElement] = useState(null);
85+
const [element, setElement] = useState<Element>();
7786

7887
// create element
7988
useEffect(() => {
80-
// @ts-ignore
8189
const newElement = controller.createElement(_getElementOptions());
8290
setElement(newElement);
8391

84-
// @ts-ignore
8592
return () => controller.removeElementById(newElement.id);
8693
}, []);
8794

8895
// update element
8996
useEffect(() => {
9097
if (element) {
9198
if (props.disabled) {
92-
// @ts-ignore
9399
controller.resetElementStyles(element);
94100
} else {
95-
// @ts-ignore
96101
controller.updateElementPropsById(
97102
element.id,
98103
_getElementOptions().props

src/components/ParallaxBanner.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3+
// @ts-ignore
34
import renderer from 'react-test-renderer';
45
import createNodeMock from '../testUtils/createNodeMock';
56
import { ParallaxBanner } from './ParallaxBanner';

src/components/ParallaxBanner.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface BannerLayer {
3636
*/
3737
image?: string;
3838
/*
39-
* Props to apply to the layer element.
39+
* Props to apply to the layer element.
4040
*/
4141
props?: any;
4242
}

0 commit comments

Comments
 (0)