This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
export default function useThrottle(value, interval = 500) { | |
const [throttledValue, setThrottledValue] = React.useState(value); | |
const lastUpdated = React.useRef(null); | |
React.useEffect(() => { | |
const now = Date.now(); | |
if (lastUpdated.current && now >= lastUpdated.current + interval) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
React.useEffectEvent = React.experimental_useEffectEvent; | |
export default function useKeyPress(key, cb, options = {}) { | |
const { event = 'keydown', target = window ?? null, eventOptions } = options; | |
const onListen = React.useEffectEvent(cb); | |
React.useEffect(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
React.useEffectEvent = React.experimental_useEffectEvent; | |
export default function usePageLeave(cb) { | |
const onLeave = React.useEffectEvent((event) => { | |
const to = event.relatedTarget || event.toElement; | |
if (!to || to.nodeName === "HTML") { | |
cb(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
export function useBattery() { | |
const [state, setState] = React.useState({ | |
supported: true, | |
loading: true, | |
level: null, | |
charging: null, | |
chargingTime: null, | |
dischargingTime: null, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
export default function useOrientation() { | |
const [orientation, setOrientation] = React.useState({ | |
angle: 0, | |
type: 'UNKNOWN', | |
}); | |
React.useLayoutEffect(() => { | |
const handleChange = () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
React.useEffectEvent = React.experimental_useEffectEvent; | |
export function useLogger(name, ...rest) { | |
const initialRenderRef = React.useRef(true); | |
const handleLog = React.useEffectEvent((event) => { | |
console.log(`${name} ${event}:`, rest); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
export function useWindowScroll() { | |
const [state, setState] = React.useState({ | |
x: null, | |
y: null, | |
}); | |
const scrollTo = React.useCallback((...args) => { | |
if (typeof args[0] === 'object') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
React.useEffectEvent = React.experimental_useEffectEvent; | |
export function useClickAway(callback) { | |
const ref = React.useRef(null); | |
const onEventHandler = React.useEffectEvent((e) => { | |
const element = ref.current; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
export function useMouse() { | |
const [state, setState] = React.useState({ | |
x: 0, | |
y: 0, | |
elementX: 0, | |
elementY: 0, | |
elementPositionX: 0, | |
elementPositionY: 0, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
export default function useCounter(startingValue = 0, options = {}) { | |
const { min, max } = options; | |
if (typeof min === 'number' && startingValue < min) { | |
throw new Error( | |
`Your starting value of ${startingValue} is less than your min of ${min}.` | |
); | |
} |