Skip to content

Commit f3ac2d1

Browse files
committed
Migrate useSyncedRef to TypeScript
1 parent d2af27f commit f3ac2d1

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

src/hooks/use-synced-ref.js renamed to src/hooks/use-synced-ref.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
1+
import type { Ref, RefObject } from 'preact';
12
import { useRef } from 'preact/hooks';
23

3-
/**
4-
* @template T
5-
* @typedef {import('preact').RefObject<T>} RefObject
6-
*/
7-
8-
/**
9-
* @template T
10-
* @typedef {import('preact').Ref<T>} Ref
11-
*/
12-
134
/**
145
* Object ref which synchronizes its value to another ref.
15-
*
16-
* @template T
17-
* @implements {RefObject<T>}
186
*/
19-
class SyncedRef {
7+
class SyncedRef<T> implements RefObject<T> {
8+
private _target?: Ref<T>;
9+
private _value: T | null;
10+
2011
/**
21-
* @param {Ref<T>} [target] - Initial target for this ref to synchronize to.
12+
* @param [target] - Initial target for this ref to synchronize to.
2213
* This is not called/set until the {@link current} property of the
2314
* SyncedRef is set. This makes the target behave close to how it would
2415
* if used in place of the SyncedRef.
2516
*/
26-
constructor(target) {
27-
/** @type {Ref<T>|undefined} */
17+
constructor(target?: Ref<T>) {
2818
this._target = target;
29-
30-
/** @type {T|null} */
3119
this._value = null;
3220
}
3321

34-
get current() {
22+
get current(): T | null {
3523
return this._value;
3624
}
3725

38-
/** @param {T|null} value */
39-
set current(value) {
26+
set current(value: null) {
4027
this._value = value;
4128
this._updateTarget();
4229
}
@@ -45,8 +32,7 @@ class SyncedRef {
4532
return this._target;
4633
}
4734

48-
/** @param {Ref<T>|undefined} target */
49-
set target(target) {
35+
set target(target: Ref<T> | undefined) {
5036
if (target === this._target) {
5137
return;
5238
}
@@ -88,12 +74,8 @@ class SyncedRef {
8874
*
8975
* return <input ref={ref}>...</input>;
9076
* }
91-
*
92-
* @template T
93-
* @param {import('preact').Ref<T>} [targetRef]
94-
* @return {import('preact').RefObject<T>}
9577
*/
96-
export function useSyncedRef(targetRef) {
78+
export function useSyncedRef<T>(targetRef?: Ref<T>): RefObject<T> {
9779
const container = useRef(new SyncedRef(targetRef));
9880
container.current.target = targetRef;
9981
return container.current;

0 commit comments

Comments
 (0)