1
+ // eslint-disable-next-line eslint-comments/disable-enable-pair
2
+ /* eslint-disable react-hooks/exhaustive-deps */
3
+
1
4
/* @jsx createElement */
2
5
3
6
/**
4
7
* External dependencies
5
8
*/
6
- import { h as createElement } from 'preact' ;
9
+ import { h as createElement , type RefObject } from 'preact' ;
7
10
import { useContext , useMemo , useRef } from 'preact/hooks' ;
8
- import { deepSignal , peek } from 'deepsignal' ;
11
+ import { deepSignal , peek , type DeepSignal } from 'deepsignal' ;
9
12
10
13
/**
11
14
* Internal dependencies
@@ -23,8 +26,8 @@ const contextObjectToProxy = new WeakMap();
23
26
const contextProxyToObject = new WeakMap ( ) ;
24
27
const contextObjectToFallback = new WeakMap ( ) ;
25
28
26
- const isPlainObject = ( item ) =>
27
- item && typeof item === 'object' && item . constructor === Object ;
29
+ const isPlainObject = ( item : unknown ) : boolean =>
30
+ Boolean ( item && typeof item === 'object' && item . constructor === Object ) ;
28
31
29
32
const descriptor = Reflect . getOwnPropertyDescriptor ;
30
33
@@ -37,17 +40,17 @@ const descriptor = Reflect.getOwnPropertyDescriptor;
37
40
* By default, all plain objects inside the context are wrapped, unless it is
38
41
* listed in the `ignore` option.
39
42
*
40
- * @param { Object } current Current context.
41
- * @param { Object } inherited Inherited context, used as fallback.
43
+ * @param current Current context.
44
+ * @param inherited Inherited context, used as fallback.
42
45
*
43
- * @return { Object } The wrapped context object.
46
+ * @return The wrapped context object.
44
47
*/
45
- const proxifyContext = ( current , inherited = { } ) => {
48
+ const proxifyContext = ( current : object , inherited : object = { } ) : object => {
46
49
// Update the fallback object reference when it changes.
47
50
contextObjectToFallback . set ( current , inherited ) ;
48
51
if ( ! contextObjectToProxy . has ( current ) ) {
49
52
const proxy = new Proxy ( current , {
50
- get : ( target , k ) => {
53
+ get : ( target : DeepSignal < any > , k ) => {
51
54
const fallback = contextObjectToFallback . get ( current ) ;
52
55
// Always subscribe to prop changes in the current context.
53
56
const currentProp = target [ k ] ;
@@ -127,10 +130,13 @@ const proxifyContext = ( current, inherited = {} ) => {
127
130
/**
128
131
* Recursively update values within a deepSignal object.
129
132
*
130
- * @param { Object } target A deepSignal instance.
131
- * @param { Object } source Object with properties to update in `target`
133
+ * @param target A deepSignal instance.
134
+ * @param source Object with properties to update in `target`.
132
135
*/
133
- const updateSignals = ( target , source ) => {
136
+ const updateSignals = (
137
+ target : DeepSignal < any > ,
138
+ source : DeepSignal < any >
139
+ ) => {
134
140
for ( const k in source ) {
135
141
if (
136
142
isPlainObject ( peek ( target , k ) ) &&
@@ -146,23 +152,23 @@ const updateSignals = ( target, source ) => {
146
152
/**
147
153
* Recursively clone the passed object.
148
154
*
149
- * @param { Object } source Source object.
150
- * @return { Object } Cloned object.
155
+ * @param source Source object.
156
+ * @return Cloned object.
151
157
*/
152
- const deepClone = ( source ) => {
158
+ function deepClone < T > ( source : T ) : T {
153
159
if ( isPlainObject ( source ) ) {
154
160
return Object . fromEntries (
155
- Object . entries ( source ) . map ( ( [ key , value ] ) => [
161
+ Object . entries ( source as object ) . map ( ( [ key , value ] ) => [
156
162
key ,
157
163
deepClone ( value ) ,
158
164
] )
159
- ) ;
165
+ ) as T ;
160
166
}
161
167
if ( Array . isArray ( source ) ) {
162
- return source . map ( ( i ) => deepClone ( i ) ) ;
168
+ return source . map ( ( i ) => deepClone ( i ) ) as T ;
163
169
}
164
170
return source ;
165
- } ;
171
+ }
166
172
167
173
const newRule =
168
174
/ (?: ( [ \u0080 - \uFFFF \w - % @ ] + ) * : ? * ( [ ^ { ; ] + ?) ; | ( [ ^ ; } { ] * ?) * { ) | ( } \s * ) / g;
@@ -176,10 +182,12 @@ const empty = ' ';
176
182
* Made by Cristian Bote (@cristianbote) for Goober.
177
183
* https://unpkg.com/browse/[email protected] /src/core/astish.js
178
184
*
179
- * @param { string } val CSS string.
180
- * @return { Object } CSS object.
185
+ * @param val CSS string.
186
+ * @return CSS object.
181
187
*/
182
- const cssStringToObject = ( val ) => {
188
+ const cssStringToObject = (
189
+ val : string
190
+ ) : Record < string , string | number > => {
183
191
const tree = [ { } ] ;
184
192
let block , left ;
185
193
@@ -203,10 +211,9 @@ const cssStringToObject = ( val ) => {
203
211
* Creates a directive that adds an event listener to the global window or
204
212
* document object.
205
213
*
206
- * @param {string } type 'window' or 'document'
207
- * @return {void }
214
+ * @param type 'window' or 'document'
208
215
*/
209
- const getGlobalEventDirective = ( type ) => {
216
+ const getGlobalEventDirective = ( type : 'window' | 'document' ) => {
210
217
return ( { directives, evaluate } ) => {
211
218
directives [ `on-${ type } ` ]
212
219
. filter ( ( { suffix } ) => suffix !== 'default' )
@@ -217,7 +224,7 @@ const getGlobalEventDirective = ( type ) => {
217
224
const globalVar = type === 'window' ? window : document ;
218
225
globalVar . addEventListener ( eventName , cb ) ;
219
226
return ( ) => globalVar . removeEventListener ( eventName , cb ) ;
220
- } , [ ] ) ;
227
+ } ) ;
221
228
} ) ;
222
229
} ;
223
230
} ;
@@ -333,9 +340,13 @@ export default () => {
333
340
* need deps because it only needs to do it the first time.
334
341
*/
335
342
if ( ! result ) {
336
- element . ref . current . classList . remove ( className ) ;
343
+ (
344
+ element . ref as RefObject < HTMLElement >
345
+ ) . current ! . classList . remove ( className ) ;
337
346
} else {
338
- element . ref . current . classList . add ( className ) ;
347
+ (
348
+ element . ref as RefObject < HTMLElement >
349
+ ) . current ! . classList . add ( className ) ;
339
350
}
340
351
} ) ;
341
352
} ) ;
@@ -368,9 +379,13 @@ export default () => {
368
379
* because it only needs to do it the first time.
369
380
*/
370
381
if ( ! result ) {
371
- element . ref . current . style . removeProperty ( styleProp ) ;
382
+ (
383
+ element . ref as RefObject < HTMLElement >
384
+ ) . current ! . style . removeProperty ( styleProp ) ;
372
385
} else {
373
- element . ref . current . style [ styleProp ] = result ;
386
+ (
387
+ element . ref as RefObject < HTMLElement >
388
+ ) . current ! . style [ styleProp ] = result ;
374
389
}
375
390
} ) ;
376
391
} ) ;
@@ -390,7 +405,8 @@ export default () => {
390
405
* first time. After that, Preact will handle the changes.
391
406
*/
392
407
useInit ( ( ) => {
393
- const el = element . ref . current ;
408
+ const el = ( element . ref as RefObject < HTMLElement > )
409
+ . current ! ;
394
410
395
411
/*
396
412
* We set the value directly to the corresponding HTMLElement instance
@@ -462,6 +478,8 @@ export default () => {
462
478
type : Type ,
463
479
props : { innerHTML, ...rest } ,
464
480
} ,
481
+ } : {
482
+ element : any ;
465
483
} ) => {
466
484
// Preserve the initial inner HTML.
467
485
const cached = useMemo ( ( ) => innerHTML , [ ] ) ;
@@ -477,6 +495,11 @@ export default () => {
477
495
// data-wp-text
478
496
directive ( 'text' , ( { directives : { text } , element, evaluate } ) => {
479
497
const entry = text . find ( ( { suffix } ) => suffix === 'default' ) ;
498
+ if ( ! entry ) {
499
+ element . props . children = null ;
500
+ return ;
501
+ }
502
+
480
503
try {
481
504
const result = evaluate ( entry ) ;
482
505
element . props . children =
0 commit comments