Skip to content

Commit 21b0e34

Browse files
committed
♻️ Change linter from flow to typescript
1 parent 4446363 commit 21b0e34

10 files changed

Lines changed: 182 additions & 246 deletions

File tree

.eslintignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/node_modules
2-
./example/metro.config.js
3-
./example/babel.config.js
2+
**/example/metro.config.js
3+
**/example/babel.config.js
4+
**/lib

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extends": ["bambi/native"],
33
"rules": {
44
"prettier/prettier": "error",
5-
"flowtype/type-id-match": [0, ""],
5+
"flowtype/type-id-match": [0, ""]
66
}
77
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ example/web-build/
1616

1717
# macOS
1818
example/.DS_Store
19+
20+
lib

example/babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function(api) {
77
'module-resolver',
88
{
99
alias: {
10-
'react-native-image-header-scroll-view': '..',
10+
'react-native-image-header-scroll-view': '../src/index.ts',
1111
},
1212
},
1313
],

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"name": "react-native-image-header-scroll-view",
33
"version": "0.10.3",
44
"description": "ScrollView with an image in header which become a navbar",
5-
"main": "src/index.js",
5+
"main": "lib/index.js",
66
"scripts": {
77
"lint": "eslint .",
88
"test": "npm run lint && flow",
9-
"prettify": "prettier --write src/**.js"
9+
"prettify": "prettier --write src/**.js",
10+
"build": "tsc"
1011
},
1112
"files": [
1213
"README.md",
@@ -34,13 +35,16 @@
3435
},
3536
"homepage": "https://github.com/bamlab/react-native-image-header-scroll-view#readme",
3637
"devDependencies": {
38+
"@types/react": "^16.9.56",
39+
"@types/react-native": "^0.63.35",
3740
"babel-preset-react-native": "4.0.0",
3841
"eslint": "^4.14.0",
3942
"eslint-config-bambi": "^1.2.0",
4043
"flow-bin": "^0.61.0",
4144
"prettier": "^1.10.2",
4245
"react": "16.2.0",
43-
"react-native": "0.52.2"
46+
"react-native": "0.52.2",
47+
"typescript": "^4.0.5"
4448
},
4549
"peerDependencies": {
4650
"react": ">=16.8.1",

src/TriggeringView.js

Lines changed: 0 additions & 161 deletions
This file was deleted.

src/TriggeringView.tsx

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import React, { FunctionComponent, MutableRefObject, useEffect, useRef, useState } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { View, Animated, ViewProps } from 'react-native';
4+
5+
interface Props extends ViewProps {
6+
onBeginHidden: Function;
7+
onHide: Function;
8+
onBeginDisplayed: Function;
9+
onDisplay: Function;
10+
onTouchTop: Function;
11+
onTouchBottom: Function;
12+
bottomOffset?: number;
13+
topOffset?: number;
14+
}
15+
16+
type Context = {
17+
scrollPageY?: number;
18+
scrollY: Animated.Value;
19+
};
20+
21+
const TriggeringView: FunctionComponent<Props> = ({
22+
topOffset = 0,
23+
bottomOffset = 0,
24+
onDisplay,
25+
onBeginDisplayed,
26+
onHide,
27+
onBeginHidden,
28+
onTouchBottom,
29+
onTouchTop,
30+
onLayout,
31+
children,
32+
...viewProps
33+
}) => {
34+
const [initialPageY, setInitialPageY] = useState(0);
35+
const ref = useRef<MutableRefObject<View>>(null).current;
36+
const [touched, setTouched] = useState(false);
37+
const [hidden, setHidden] = useState(false);
38+
const [context, setContext] = useState<Context>({
39+
scrollPageY: 0,
40+
scrollY: new Animated.Value(0),
41+
});
42+
43+
const [height, setHeight] = useState(0);
44+
useEffect(() => {
45+
if (!context.scrollY) {
46+
return;
47+
}
48+
const listenerId = context.scrollY.addListener(onScroll);
49+
50+
return () => {
51+
context.scrollY.removeListener(listenerId);
52+
};
53+
}, []);
54+
55+
const handleOnLayout = (e: any) => {
56+
if (onLayout) {
57+
onLayout(e);
58+
}
59+
if (!ref) {
60+
return;
61+
}
62+
const layout = e.nativeEvent.layout;
63+
setHeight(layout.height);
64+
65+
ref.current.measure((_x, _y, _width, _height, _ageX, pageY) => {
66+
setInitialPageY(pageY);
67+
});
68+
};
69+
70+
const onScroll = (event: any) => {
71+
if (!context.scrollPageY) {
72+
return;
73+
}
74+
const pageY = initialPageY - event.value;
75+
triggerEvents(context.scrollPageY, pageY, pageY + height);
76+
};
77+
78+
const triggerEvents = (value: number, top: number, bottom: number) => {
79+
if (!touched && value >= top + topOffset) {
80+
setTouched(true);
81+
onBeginHidden();
82+
onTouchTop(true);
83+
} else if (touched && value < top + topOffset) {
84+
setTouched(false);
85+
86+
onDisplay();
87+
onTouchTop(false);
88+
}
89+
90+
if (!hidden && value >= bottom + bottomOffset) {
91+
setHidden(true);
92+
onHide();
93+
onTouchBottom(true);
94+
} else if (hidden && value < bottom + bottomOffset) {
95+
setHidden(false);
96+
onBeginDisplayed();
97+
onTouchBottom(false);
98+
}
99+
};
100+
101+
return (
102+
<View
103+
ref={ref}
104+
collapsable={false}
105+
{...viewProps}
106+
onLayout={handleOnLayout}
107+
style={{ backgroundColor: 'red' }}
108+
>
109+
{children}
110+
</View>
111+
);
112+
};
113+
114+
TriggeringView.contextTypes = {
115+
scrollY: PropTypes.instanceOf(Animated.Value),
116+
scrollPageY: PropTypes.number,
117+
};
118+
119+
export default TriggeringView;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// @flow
2+
// @ts-ignore
23
export { default } from './ImageHeaderScrollView';
34
export { default as TriggeringView } from './TriggeringView';

tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
5+
"jsx": "react",
6+
// "outFile": "./", /* Concatenate and emit output to single file. */
7+
"outDir": "./lib" /* Redirect output structure to the directory. */,
8+
"strict": true /* Enable all strict type-checking options. */,
9+
"baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
10+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
11+
// "allowJs": true,
12+
/* Advanced Options */
13+
"skipLibCheck": true /* Skip type checking of declaration files. */,
14+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
15+
},
16+
"exclude": ["node_modules", "example", "lib"]
17+
}

0 commit comments

Comments
 (0)