File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,6 +35,10 @@ function isBorderBoxSize(value: unknown): value is BorderBoxSize {
3535 )
3636}
3737
38+ function isElement ( value : unknown ) : value is HTMLElement {
39+ return typeof Element !== 'undefined' && value instanceof Element
40+ }
41+
3842function useSize ( target : ElementTarget < HTMLElement > ) : ( ) => Size | undefined {
3943 const size = createSignal < Size | undefined > ( undefined )
4044
@@ -46,7 +50,7 @@ function useSize(target: ElementTarget<HTMLElement>): () => Size | undefined {
4650 cleanup ?.( )
4751 cleanup = undefined
4852
49- if ( ! element ) {
53+ if ( ! element || ! isElement ( element ) ) {
5054 size ( undefined )
5155 return
5256 }
Original file line number Diff line number Diff line change @@ -107,4 +107,31 @@ describe('@fictjs/use-size', () => {
107107
108108 expect ( container . querySelector ( 'output' ) ?. textContent ) . toBe ( '180x72' )
109109 } )
110+
111+ it ( 'ignores temporary ref targets that are not elements' , async ( ) => {
112+ const observe = vi . fn ( )
113+ vi . stubGlobal ( 'ResizeObserver' , class {
114+ observe = observe
115+ unobserve ( ) : void { }
116+ disconnect ( ) : void { }
117+ } )
118+
119+ function Test ( ) {
120+ const target = {
121+ current : document . createComment ( 'placeholder' ) as unknown as HTMLDivElement ,
122+ }
123+ const size = useSize ( target )
124+
125+ return < output > { size ( ) ? 'ready' : 'none' } </ output >
126+ }
127+
128+ const container = document . createElement ( 'div' )
129+ document . body . appendChild ( container )
130+
131+ render ( ( ) => < Test /> , container )
132+ await flushMicrotasks ( )
133+
134+ expect ( container . querySelector ( 'output' ) ?. textContent ) . toBe ( 'none' )
135+ expect ( observe ) . not . toHaveBeenCalled ( )
136+ } )
110137} )
You can’t perform that action at this time.
0 commit comments