Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit d113a48

Browse files
authored
chore(ts-js): hooks convert (#1027)
1 parent 177bab0 commit d113a48

File tree

14 files changed

+97
-83
lines changed

14 files changed

+97
-83
lines changed

src/hooks/index.js renamed to src/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export { default as useOutsideClick } from './useOutsideClick'
88
export { default as useLangPress } from './useLangPress'
99
export { default as useTrans } from './useTrans'
1010
export { default as useDevice } from './useDevice'
11-
export { default as useHover } from './useHover'
1211

1312
export { default as useNetwork } from 'react-use/lib/useNetwork'
1413
export { default as useCopyToClipboard } from 'react-use/lib/useCopyToClipboard'

src/hooks/useCustomScroll.js renamed to src/hooks/useCustomScroll.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import { useEffect, useState } from 'react'
1+
import { useEffect, useState, RefObject } from 'react'
22
import OverlayScrollbars from 'overlayscrollbars'
33

4+
type TOption = {
5+
className?: string
6+
themeCategory?: string
7+
scrollbars?: {
8+
autoHide?: 'scroll' | 'never' // string
9+
}
10+
callbacks?: {
11+
onScroll?: () => void
12+
onScrollStart?: () => void
13+
onScrollStop?: () => void
14+
}
15+
// more callbacks see overlayscrollbars docs
16+
}
17+
418
/**
519
* options detail see:
620
* https://kingsora.github.io/OverlayScrollbars/#!documentation/options
721
*
822
* @returns
923
*/
10-
const useCustomScroll = (ref, option = {}) => {
24+
const useCustomScroll = (
25+
ref: RefObject<HTMLElement>,
26+
option: TOption = {},
27+
): any => {
1128
const [scrollInstance, setScrollInstance] = useState(null)
1229

1330
useEffect(() => {

src/hooks/useDevice.js renamed to src/hooks/useDevice.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { useEffect, useState } from 'react'
22
import { isMobile as detectMobile } from '@/utils'
33

4-
const useDevice = () => {
4+
type TDevice = {
5+
isMobile: boolean
6+
}
7+
8+
const useDevice = (): TDevice => {
59
const [isMobile, setIsMobile] = useState(false)
610

711
useEffect(() => {

src/hooks/useHover.js

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

src/hooks/useLangPress.js renamed to src/hooks/useLangPress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { useState, useEffect, useCallback } from 'react'
2121
* @param {number} [ms=500] hold m-sec
2222
* @returns
2323
*/
24-
const useLongPress = (callback = () => {}, ms = 500) => {
24+
const useLongPress = (callback: () => void, ms = 500) => {
2525
const [startLongPress, setStartLongPress] = useState(false)
2626

2727
useEffect(() => {

src/hooks/useOutsideClick.js renamed to src/hooks/useOutsideClick.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { useEffect } from 'react'
1+
import { useEffect, RefObject } from 'react'
22

3-
const useOutsideClick = (ref, callback) => {
4-
const handleClick = (e) => {
3+
const useOutsideClick = (
4+
ref: RefObject<HTMLElement>,
5+
callback?: (e) => void,
6+
): void => {
7+
const handleClick = (e): void => {
58
if (ref.current && !ref.current.contains(e.target)) {
6-
callback(e)
9+
callback?.(e)
710
}
811
}
912

src/hooks/usePlatform.js renamed to src/hooks/usePlatform.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useEffect } from 'react'
22
import { merge } from 'ramda'
33

4+
import type { TPlatform } from '@/spec'
45
import { Global } from '@/utils'
56

67
const initPlatform = {
@@ -14,12 +15,13 @@ const initPlatform = {
1415
}
1516

1617
// see https://stackoverflow.com/questions/49328382/browser-detection-in-reactjs/49328524
17-
const usePlatform = (/* { breakpoint } */) => {
18+
const usePlatform = (/* { breakpoint } */): TPlatform => {
1819
const [platform, setPlatform] = useState(initPlatform)
1920

2021
/* eslint-disable */
2122
useEffect(() => {
2223
// Firefox 1.0+
24+
// @ts-ignore
2325
const isFirefox = typeof InstallTrigger !== 'undefined'
2426

2527
// Safari 3.0+ "[object HTMLElementConstructor]"
@@ -29,10 +31,12 @@ const usePlatform = (/* { breakpoint } */) => {
2931
return p.toString() === '[object SafariRemoteNotification]'
3032
})(
3133
!Global.safari ||
34+
// @ts-ignore
3235
(typeof safari !== 'undefined' && safari.pushNotification),
3336
)
3437

3538
// Internet Explorer 6-11
39+
// @ts-ignore
3640
const isIE = /*@cc_on!@*/ false || !!document.documentMode
3741

3842
// Edge 20+
@@ -58,8 +62,6 @@ const usePlatform = (/* { breakpoint } */) => {
5862
isMobile,
5963
}),
6064
)
61-
62-
return () => {}
6365
}, [])
6466

6567
return platform

src/hooks/useResize.js renamed to src/hooks/useResize.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { useState, useEffect, useCallback } from 'react'
22

3+
type TSize = {
4+
width?: number
5+
height?: number
6+
}
7+
38
/**
49
* hooks for detect window size
510
* see: https://usehooks.com/useWindowSize/ for details
611
*
712
* @returns
813
*/
9-
const useWindowSize = (cb) => {
14+
const useWindowSize = (cb: (size: TSize) => void): TSize => {
1015
const isClient = typeof window === 'object'
1116

1217
const getSize = useCallback(() => {
@@ -20,12 +25,12 @@ const useWindowSize = (cb) => {
2025

2126
useEffect(() => {
2227
if (!isClient) {
23-
return false
28+
return
2429
}
2530

2631
const handleResize = () => {
2732
setWindowSize(getSize())
28-
if (cb) cb(getSize())
33+
cb?.(getSize())
2934
}
3035

3136
window.addEventListener('resize', handleResize)

src/hooks/useScript.js renamed to src/hooks/useScript.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { useState, useEffect } from 'react'
22

33
const cachedScripts = []
44

5-
const useScript = (src) => {
5+
type TLoadState = boolean
6+
7+
const useScript = (src: string): TLoadState[] => {
68
// Keeping track of script loaded and error state
79
const [state, setState] = useState({
810
loaded: false,

src/hooks/useScroll.js renamed to src/hooks/useScroll.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,46 @@ import { merge } from 'ramda'
33

44
import { debounce } from '@/utils'
55

6+
type TScrollState = {
7+
direction: string
8+
scrollPos: number
9+
}
10+
611
const initState = {
712
direction: 'up', // 'down'
813
scrollPos: 0,
914
}
1015

1116
// detect the scroll direction
1217
// see https://codepen.io/lehollandaisvolant/pen/ryrrGx?editors=1010
13-
const useScroll = (cb) => {
18+
const useScroll = (cb: () => void): TScrollState => {
1419
const [scroll, setScroll] = useState(initState)
1520

1621
/* eslint-disable */
1722
useEffect(() => {
1823
// adding scroll event
1924
let scrollPos = scroll.scrollPos
20-
const handleScroll = debounce(function () {
21-
// detects new state and compares it with the new one
22-
let direction =
23-
document.body.getBoundingClientRect().top > scrollPos ? 'up' : 'down'
24-
25-
// saves the new position for iteration.
26-
scrollPos = document.body.getBoundingClientRect().top
27-
28-
if (cb) cb()
29-
30-
setScroll(
31-
merge(initState, {
32-
direction,
33-
scrollPos,
34-
}),
35-
)
36-
}, 50)
25+
const handleScroll = debounce(
26+
() => {
27+
// detects new state and compares it with the new one
28+
let direction =
29+
document.body.getBoundingClientRect().top > scrollPos ? 'up' : 'down'
30+
31+
// saves the new position for iteration.
32+
scrollPos = document.body.getBoundingClientRect().top
33+
34+
cb?.()
35+
36+
setScroll(
37+
merge(initState, {
38+
direction,
39+
scrollPos,
40+
}),
41+
)
42+
},
43+
50,
44+
true,
45+
)
3746

3847
window.addEventListener('scroll', handleScroll)
3948

0 commit comments

Comments
 (0)