Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/hooks/index.js → src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export { default as useOutsideClick } from './useOutsideClick'
export { default as useLangPress } from './useLangPress'
export { default as useTrans } from './useTrans'
export { default as useDevice } from './useDevice'
export { default as useHover } from './useHover'

export { default as useNetwork } from 'react-use/lib/useNetwork'
export { default as useCopyToClipboard } from 'react-use/lib/useCopyToClipboard'
Expand Down
21 changes: 19 additions & 2 deletions src/hooks/useCustomScroll.js → src/hooks/useCustomScroll.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { useEffect, useState } from 'react'
import { useEffect, useState, RefObject } from 'react'
import OverlayScrollbars from 'overlayscrollbars'

type TOption = {
className?: string
themeCategory?: string
scrollbars?: {
autoHide?: 'scroll' | 'never' // string
}
callbacks?: {
onScroll?: () => void
onScrollStart?: () => void
onScrollStop?: () => void
}
// more callbacks see overlayscrollbars docs
}

/**
* options detail see:
* https://kingsora.github.io/OverlayScrollbars/#!documentation/options
*
* @returns
*/
const useCustomScroll = (ref, option = {}) => {
const useCustomScroll = (
ref: RefObject<HTMLElement>,
option: TOption = {},
): any => {
const [scrollInstance, setScrollInstance] = useState(null)

useEffect(() => {
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/useDevice.js → src/hooks/useDevice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useEffect, useState } from 'react'
import { isMobile as detectMobile } from '@/utils'

const useDevice = () => {
type TDevice = {
isMobile: boolean
}

const useDevice = (): TDevice => {
const [isMobile, setIsMobile] = useState(false)

useEffect(() => {
Expand Down
45 changes: 0 additions & 45 deletions src/hooks/useHover.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/hooks/useLangPress.js → src/hooks/useLangPress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { useState, useEffect, useCallback } from 'react'
* @param {number} [ms=500] hold m-sec
* @returns
*/
const useLongPress = (callback = () => {}, ms = 500) => {
const useLongPress = (callback: () => void, ms = 500) => {
const [startLongPress, setStartLongPress] = useState(false)

useEffect(() => {
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useOutsideClick.js → src/hooks/useOutsideClick.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useEffect } from 'react'
import { useEffect, RefObject } from 'react'

const useOutsideClick = (ref, callback) => {
const handleClick = (e) => {
const useOutsideClick = (
ref: RefObject<HTMLElement>,
callback?: (e) => void,
): void => {
const handleClick = (e): void => {
if (ref.current && !ref.current.contains(e.target)) {
callback(e)
callback?.(e)
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/hooks/usePlatform.js → src/hooks/usePlatform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect } from 'react'
import { merge } from 'ramda'

import type { TPlatform } from '@/spec'
import { Global } from '@/utils'

const initPlatform = {
Expand All @@ -14,12 +15,13 @@ const initPlatform = {
}

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

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

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

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

// Edge 20+
Expand All @@ -58,8 +62,6 @@ const usePlatform = (/* { breakpoint } */) => {
isMobile,
}),
)

return () => {}
}, [])

return platform
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/useResize.js → src/hooks/useResize.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { useState, useEffect, useCallback } from 'react'

type TSize = {
width?: number
height?: number
}

/**
* hooks for detect window size
* see: https://usehooks.com/useWindowSize/ for details
*
* @returns
*/
const useWindowSize = (cb) => {
const useWindowSize = (cb: (size: TSize) => void): TSize => {
const isClient = typeof window === 'object'

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

useEffect(() => {
if (!isClient) {
return false
return
}

const handleResize = () => {
setWindowSize(getSize())
if (cb) cb(getSize())
cb?.(getSize())
}

window.addEventListener('resize', handleResize)
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useScript.js → src/hooks/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { useState, useEffect } from 'react'

const cachedScripts = []

const useScript = (src) => {
type TLoadState = boolean

const useScript = (src: string): TLoadState[] => {
// Keeping track of script loaded and error state
const [state, setState] = useState({
loaded: false,
Expand Down
45 changes: 27 additions & 18 deletions src/hooks/useScroll.js → src/hooks/useScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,46 @@ import { merge } from 'ramda'

import { debounce } from '@/utils'

type TScrollState = {
direction: string
scrollPos: number
}

const initState = {
direction: 'up', // 'down'
scrollPos: 0,
}

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

/* eslint-disable */
useEffect(() => {
// adding scroll event
let scrollPos = scroll.scrollPos
const handleScroll = debounce(function () {
// detects new state and compares it with the new one
let direction =
document.body.getBoundingClientRect().top > scrollPos ? 'up' : 'down'

// saves the new position for iteration.
scrollPos = document.body.getBoundingClientRect().top

if (cb) cb()

setScroll(
merge(initState, {
direction,
scrollPos,
}),
)
}, 50)
const handleScroll = debounce(
() => {
// detects new state and compares it with the new one
let direction =
document.body.getBoundingClientRect().top > scrollPos ? 'up' : 'down'

// saves the new position for iteration.
scrollPos = document.body.getBoundingClientRect().top

cb?.()

setScroll(
merge(initState, {
direction,
scrollPos,
}),
)
},
50,
true,
)

window.addEventListener('scroll', handleScroll)

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useShortcut.js → src/hooks/useShortcut.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { useEffect } from 'react'
import tinykeys from 'tinykeys'

const useShortcut = (combination, cb) => {
const useShortcut = (combination: string | string[], cb: () => void): void => {
useEffect(() => {
const handlers = {}
if (Array.isArray(combination)) {
for (let i = 0; i < combination.length; i += 1) {
handlers[combination[i]] = (event) => {
event.preventDefault()
return cb()
return cb?.()
}
}
} else {
handlers[combination] = (event) => {
event.preventDefault()
return cb()
return cb?.()
}
}

Expand Down
File renamed without changes.
10 changes: 9 additions & 1 deletion src/spec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ export type { TButton } from './comp'

export type { TTheme, TThemeMap, TThemeName } from './theme'
export type { TAccount, TUser, TMembership } from './account'
export type { TTestable, TActive, TSpace, TGAEvent, TSEO, TLink } from './utils'
export type {
TTestable,
TActive,
TSpace,
TGAEvent,
TSEO,
TLink,
TPlatform,
} from './utils'

export type TRoute = {
communityPath?: string
Expand Down
10 changes: 10 additions & 0 deletions src/spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ export type TSEO = {
export type TLink = {
href: string
}

export type TPlatform = {
isChrome: boolean
isFirefox: boolean
isSafari: boolean
isIE: boolean
isEdge: boolean
isMacOS: boolean
isMobile: boolean
}