Skip to content

Commit 046cdfe

Browse files
committed
upgrade stuff and fix tests
1 parent 2570ab6 commit 046cdfe

29 files changed

+557
-488
lines changed

package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{
22
"name": "@restart/hooks",
33
"version": "0.5.1",
4-
"main": "lib/cjs/index.js",
5-
"types": "lib/cjs/index.d.ts",
6-
"module": "lib/esm/index.js",
74
"exports": {
85
".": {
96
"types": "./esm/index.d.ts",
@@ -18,7 +15,7 @@
1815
},
1916
"repository": {
2017
"type": "git",
21-
"url": "git+https://github.com/jquense/react-common-hooks.git"
18+
"url": "git+https://github.com/react-restart/hooks.git"
2219
},
2320
"author": {
2421
"name": "Jason Quense",
@@ -40,6 +37,7 @@
4037
"build:cjs:types": "tsc -p . --emitDeclarationOnly --declaration --outDir cjs --module commonjs --moduleResolution node",
4138
"deploy-docs": "yarn --cwd www build --prefix-paths && gh-pages -d www/public",
4239
"prepublishOnly": "yarn build",
40+
"typecheck": "tsc -p . --noEmit",
4341
"release": "rollout"
4442
},
4543
"jest": {
@@ -70,14 +68,14 @@
7068
"@4c/jest-preset": "^1.8.1",
7169
"@4c/rollout": "^4.0.2",
7270
"@4c/tsconfig": "^0.4.1",
73-
"@babel/cli": "^7.22.9",
74-
"@babel/core": "^7.22.9",
75-
"@babel/preset-typescript": "^7.22.5",
76-
"@testing-library/react": "^12.1.5",
77-
"@testing-library/react-hooks": "^7.0.0",
71+
"@babel/cli": "^7.26.4",
72+
"@babel/core": "^7.26.0",
73+
"@babel/preset-typescript": "^7.26.0",
74+
"@testing-library/dom": "^10.4.0",
75+
"@testing-library/react": "^16.1.0",
7876
"@types/jest": "^29.5.3",
7977
"@types/lodash": "^4.14.195",
80-
"@types/react": "^18.2.15",
78+
"@types/react": "^19.0.2",
8179
"babel-jest": "^29.6.1",
8280
"babel-plugin-transform-rename-import": "^2.3.0",
8381
"cherry-pick": "^0.5.0",
@@ -91,8 +89,8 @@
9189
"lint-staged": "^13.2.3",
9290
"mq-polyfill": "^1.1.8",
9391
"prettier": "^3.0.0",
94-
"react": "^16.13.0",
95-
"react-dom": "^16.13.0",
92+
"react": "^19.0.0",
93+
"react-dom": "^19.0.0",
9694
"rimraf": "^5.0.1",
9795
"typescript": "^5.1.6"
9896
},

src/useBreakpoint.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,18 @@ export function createBreakpointHook<TKey extends string>(
124124

125125
let query = useMemo(
126126
() =>
127-
Object.entries(breakpointMap).reduce(
128-
(query, [key, direction]: [TKey, BreakpointDirection]) => {
129-
if (direction === 'up' || direction === true) {
130-
query = and(query, getMinQuery(key))
131-
}
132-
if (direction === 'down' || direction === true) {
133-
query = and(query, getMaxQuery(key))
134-
}
135-
136-
return query
137-
},
138-
'',
139-
),
127+
Object.entries(breakpointMap).reduce((query, entry) => {
128+
const [key, direction] = entry as [TKey, BreakpointDirection]
129+
130+
if (direction === 'up' || direction === true) {
131+
query = and(query, getMinQuery(key))
132+
}
133+
if (direction === 'down' || direction === true) {
134+
query = and(query, getMaxQuery(key))
135+
}
136+
137+
return query
138+
}, ''),
140139
[JSON.stringify(breakpointMap)],
141140
)
142141

src/useCustomEffect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function useCustomEffect<TDeps extends DependencyList = DependencyList>(
6161
? { isEqual: isEqualOrOptions }
6262
: isEqualOrOptions
6363

64-
const dependenciesRef = useRef<TDeps>()
64+
const dependenciesRef = useRef<TDeps | null>(null)
6565
dependenciesRef.current = dependencies
6666

6767
const cleanupRef = useRef<CleanUp | null>(null)

src/useDebouncedCallback.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface UseDebouncedCallbackOptionsLeading
1414
leading: true
1515
}
1616

17+
const EMPTY: unique symbol = Symbol('EMPTY')
18+
1719
/**
1820
* Creates a debounced function that will invoke the input function after the
1921
* specified wait.
@@ -50,7 +52,7 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(
5052
): (...args: Parameters<TCallback>) => ReturnType<TCallback> | undefined {
5153
const lastCallTimeRef = useRef<number | null>(null)
5254
const lastInvokeTimeRef = useRef(0)
53-
const returnValueRef = useRef<ReturnType<TCallback>>()
55+
const returnValueRef = useRef<ReturnType<TCallback> | typeof EMPTY>(EMPTY)
5456

5557
const isTimerSetRef = useRef(false)
5658
const lastArgsRef = useRef<unknown[] | null>(null)
@@ -80,7 +82,9 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(
8082
timeout.set(timerExpired, wait)
8183

8284
if (!leading) {
83-
return returnValueRef.current
85+
return returnValueRef.current === EMPTY
86+
? undefined
87+
: returnValueRef.current
8488
}
8589

8690
return invokeFunc(time)
@@ -96,7 +100,9 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(
96100
}
97101

98102
lastArgsRef.current = null
99-
return returnValueRef.current
103+
return returnValueRef.current === EMPTY
104+
? undefined
105+
: returnValueRef.current
100106
}
101107

102108
function timerExpired() {
@@ -110,6 +116,8 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(
110116
const timeSinceLastInvoke = time - lastInvokeTimeRef.current
111117
const timeWaiting = wait - timeSinceLastCall
112118

119+
// console.log('g', Math.min(timeWaiting, maxWait - timeSinceLastInvoke))
120+
113121
// Restart the timer.
114122
timeout.set(
115123
timerExpired,
@@ -170,7 +178,9 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(
170178
timeout.set(timerExpired, wait)
171179
}
172180

173-
return returnValueRef.current
181+
return returnValueRef.current === EMPTY
182+
? undefined
183+
: returnValueRef.current
174184
}
175185
}, [handleCallback, wait, maxWait, leading, trailing])
176186
}

src/useEventListener.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ type EventHandler<T, K extends keyof DocumentEventMap> = (
1818
*/
1919
export default function useEventListener<
2020
T extends Element | Document | Window,
21-
K extends keyof DocumentEventMap
21+
K extends keyof DocumentEventMap,
2222
>(
2323
eventTarget: T | (() => T),
2424
event: K,
2525
listener: EventHandler<T, K>,
2626
capture: boolean | AddEventListenerOptions = false,
2727
) {
28-
const handler = useEventCallback(listener)
28+
const handler = useEventCallback(listener) as EventListener
2929

3030
useEffect(() => {
3131
const target =

src/useMergedRefs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ type Ref<T> = React.MutableRefObject<T> | CallbackRef<T>
66
const toFnRef = <T>(ref?: Ref<T> | null) =>
77
!ref || typeof ref === 'function'
88
? ref
9-
: (value: T) => {
10-
ref.current = value
9+
: (value: T | null) => {
10+
ref.current = value as T
1111
}
1212

1313
export function mergeRefs<T>(refA?: Ref<T> | null, refB?: Ref<T> | null) {

test/helpers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { renderHook as baseRenderHook } from '@testing-library/react-hooks'
1+
import { renderHook as baseRenderHook } from '@testing-library/react'
22

33
type ReactWrapper<P> = {
44
setProps(props: Partial<P>): void

test/useAnimationFrame.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { renderHook, act } from '@testing-library/react-hooks'
1+
import { renderHook, act } from '@testing-library/react'
22
import useAnimationFrame from '../src/useAnimationFrame'
33

44
describe('useAnimationFrame', () => {

test/useBreakpoint.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import useBreakpoint, {
33
createBreakpointHook,
44
} from '../src/useBreakpoint'
55

6-
import { renderHook } from '@testing-library/react-hooks'
6+
import { renderHook } from '@testing-library/react'
77

88
interface Props {
99
breakpoint: DefaultBreakpointMap

test/useCommittedRef.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect } from 'react'
2-
import { renderHook } from '@testing-library/react-hooks'
2+
import { renderHook } from '@testing-library/react'
33
import useCommittedRef from '../src/useCommittedRef'
44

55
describe('useCommittedRef', () => {

0 commit comments

Comments
 (0)