Skip to content

Commit 6bcd85e

Browse files
committed
WIP
1 parent 0829177 commit 6bcd85e

File tree

5 files changed

+402
-269
lines changed

5 files changed

+402
-269
lines changed

src/useFocusManager.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useCallback, useRef } from 'react'
2-
3-
import useMounted from './useMounted'
42
import useEventCallback from './useEventCallback'
3+
import useMounted from './useMounted'
54

65
export interface FocusManagerOptions {
76
/**
@@ -26,13 +25,17 @@ export interface FocusManagerOptions {
2625
isDisabled: () => boolean
2726
}
2827

28+
export interface FocusController {
29+
onBlur: (event: any) => void
30+
onFocus: (event: any) => void
31+
}
2932
/**
3033
* useFocusManager provides a way to track and manage focus as it moves around
3134
* a container element. An `onChange` is fired when focus enters or leaves the
3235
* element, but not when it moves around inside the element, similar to
3336
* `pointerenter` and `pointerleave` DOM events.
3437
*
35-
* ```ts
38+
* ```tsx
3639
* const [focused, setFocusState] = useState(false)
3740
*
3841
* const { onBlur, onFocus } = useFocusManager({
@@ -49,10 +52,10 @@ export interface FocusManagerOptions {
4952
* </div>
5053
* ```
5154
*
52-
* @param opts Options
53-
* @returns FocusController a set of paired focus and blur event handlers
5455
*/
55-
export default function useFocusManager(opts: FocusManagerOptions) {
56+
export default function useFocusManager(
57+
opts: FocusManagerOptions,
58+
): FocusController {
5659
const isMounted = useMounted()
5760

5861
const lastFocused = useRef<boolean | undefined>()

src/useInterval.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import useCommittedRef from './useCommittedRef'
44
/**
55
* Creates a `setInterval` that is properly cleaned up when a component unmounted
66
*
7+
* ```tsx
8+
* function Timer() {
9+
* const [timer, setTimer] = useState(0)
10+
* useInterval(() => setTimer(i => i + 1), 1000)
11+
*
12+
* return <span>{timer} seconds past</span>
13+
* }
14+
* ```
15+
*
716
* @param fn an function run on each interval
817
* @param ms The milliseconds duration of the interval
918
*/
@@ -12,22 +21,53 @@ function useInterval(fn: () => void, ms: number): void
1221
/**
1322
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
1423
*
24+
* ```tsx
25+
* const [paused, setPaused] = useState(false)
26+
* const [timer, setTimer] = useState(0)
27+
*
28+
* useInterval(() => setTimer(i => i + 1), 1000, paused)
29+
*
30+
* return (
31+
* <span>
32+
* {timer} seconds past
33+
*
34+
* <button onClick={() => setPaused(p => !p)}>{paused ? 'Play' : 'Pause' }</button>
35+
* </span>
36+
* )
37+
* ```
38+
*
1539
* @param fn an function run on each interval
1640
* @param ms The milliseconds duration of the interval
1741
* @param paused Whether or not the interval is currently running
1842
*/
1943
function useInterval(fn: () => void, ms: number, paused: boolean): void
2044

2145
/**
22-
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
46+
* Creates a pausable `setInterval` that _fires_ immediately and is
47+
* properly cleaned up when a component unmounted
48+
*
49+
* ```tsx
50+
* const [timer, setTimer] = useState(-1)
51+
* useInterval(() => setTimer(i => i + 1), 1000, false, true)
52+
*
53+
* // will update to 0 on the first effect
54+
* return <span>{timer} seconds past</span>
55+
* ```
2356
*
2457
* @param fn an function run on each interval
2558
* @param ms The milliseconds duration of the interval
2659
* @param paused Whether or not the interval is currently running
2760
* @param runImmediately Whether to run the function immediately on mount or unpause
2861
* rather than waiting for the first interval to elapse
62+
*
63+
2964
*/
30-
function useInterval(fn: () => void, ms: number, paused: boolean, runImmediately: boolean): void
65+
function useInterval(
66+
fn: () => void,
67+
ms: number,
68+
paused: boolean,
69+
runImmediately: boolean,
70+
): void
3171

3272
function useInterval(
3373
fn: () => void,

www/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"build": "gatsby build"
88
},
99
"dependencies": {
10-
"@docpocalypse/gatsby-theme": "^0.8.12",
11-
"gatsby": "^2.18.11",
10+
"@docpocalypse/gatsby-theme": "^0.9.4",
11+
"gatsby": "^2.19.18",
1212
"react": "^16.12.0",
1313
"react-dom": "^16.12.0"
1414
}

0 commit comments

Comments
 (0)