1
+ import type { Ref , RefObject } from 'preact' ;
1
2
import { useRef } from 'preact/hooks' ;
2
3
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
-
13
4
/**
14
5
* Object ref which synchronizes its value to another ref.
15
- *
16
- * @template T
17
- * @implements {RefObject<T>}
18
6
*/
19
- class SyncedRef {
7
+ class SyncedRef < T > implements RefObject < T > {
8
+ private _target ?: Ref < T > ;
9
+ private _value : T | null ;
10
+
20
11
/**
21
- * @param { Ref<T> } [target] - Initial target for this ref to synchronize to.
12
+ * @param [target] - Initial target for this ref to synchronize to.
22
13
* This is not called/set until the {@link current} property of the
23
14
* SyncedRef is set. This makes the target behave close to how it would
24
15
* if used in place of the SyncedRef.
25
16
*/
26
- constructor ( target ) {
27
- /** @type {Ref<T>|undefined } */
17
+ constructor ( target ?: Ref < T > ) {
28
18
this . _target = target ;
29
-
30
- /** @type {T|null } */
31
19
this . _value = null ;
32
20
}
33
21
34
- get current ( ) {
22
+ get current ( ) : T | null {
35
23
return this . _value ;
36
24
}
37
25
38
- /** @param {T|null } value */
39
- set current ( value ) {
26
+ set current ( value : null ) {
40
27
this . _value = value ;
41
28
this . _updateTarget ( ) ;
42
29
}
@@ -45,8 +32,7 @@ class SyncedRef {
45
32
return this . _target ;
46
33
}
47
34
48
- /** @param {Ref<T>|undefined } target */
49
- set target ( target ) {
35
+ set target ( target : Ref < T > | undefined ) {
50
36
if ( target === this . _target ) {
51
37
return ;
52
38
}
@@ -88,12 +74,8 @@ class SyncedRef {
88
74
*
89
75
* return <input ref={ref}>...</input>;
90
76
* }
91
- *
92
- * @template T
93
- * @param {import('preact').Ref<T> } [targetRef]
94
- * @return {import('preact').RefObject<T> }
95
77
*/
96
- export function useSyncedRef ( targetRef ) {
78
+ export function useSyncedRef < T > ( targetRef ?: Ref < T > ) : RefObject < T > {
97
79
const container = useRef ( new SyncedRef ( targetRef ) ) ;
98
80
container . current . target = targetRef ;
99
81
return container . current ;
0 commit comments