@@ -43,9 +43,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
43
43
const Component = ( tagNameSuffix ? `${ tagName } -${ tagNameSuffix } ` : tagName ) as unknown as ComponentType <
44
44
CommonProps & { class ?: string ; ref ?: Ref < RefType > }
45
45
> ;
46
-
47
46
const [ isDefined , setIsDefined ] = useState ( definedWebComponents . has ( Component ) ) ;
48
-
49
47
// regular props (no booleans, no slots and no events)
50
48
const regularProps = regularProperties . reduce ( ( acc , name ) => {
51
49
if ( Object . prototype . hasOwnProperty . call ( rest , name ) && isPrimitiveAttribute ( rest [ name ] ) ) {
@@ -152,6 +150,9 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
152
150
return events ;
153
151
} , { } ) ;
154
152
153
+ // In React 19 events aren't correctly attached after hydration
154
+ const [ attachEvents , setAttachEvents ] = useState ( ! webComponentsSupported || ! Object . keys ( eventHandlers ) . length ) ; // apply workaround only for React19 and if event props are defined
155
+
155
156
// non web component related props, just pass them
156
157
const nonWebComponentRelatedProps = Object . entries ( rest )
157
158
. filter ( ( [ key ] ) => ! regularProperties . includes ( key ) )
@@ -188,7 +189,11 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
188
189
} ) ;
189
190
} , [ Component , ...propsToApply ] ) ;
190
191
191
- if ( ( waitForDefine && ! isDefined ) || typeof window === 'undefined' ) {
192
+ useEffect ( ( ) => {
193
+ setAttachEvents ( true ) ;
194
+ } , [ ] ) ;
195
+
196
+ if ( waitForDefine && ! isDefined ) {
192
197
return null ;
193
198
}
194
199
@@ -203,7 +208,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
203
208
ref = { componentRef }
204
209
{ ...booleanProps }
205
210
{ ...restRegularProps }
206
- { ...eventHandlers }
211
+ { ...( attachEvents ? eventHandlers : { } ) }
207
212
{ ...nonWebComponentRelatedProps }
208
213
overflow-mode = { overflowMode ?? ( showOverflowInPopover ? 'Popover' : 'InPlace' ) }
209
214
// @ts -expect-error: text is available
@@ -222,7 +227,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
222
227
ref = { componentRef }
223
228
{ ...booleanProps }
224
229
{ ...regularProps }
225
- { ...eventHandlers }
230
+ { ...( attachEvents ? eventHandlers : { } ) }
226
231
{ ...nonWebComponentRelatedProps }
227
232
class = { className }
228
233
suppressHydrationWarning
0 commit comments