@@ -43,9 +43,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
4343 const Component = ( tagNameSuffix ? `${ tagName } -${ tagNameSuffix } ` : tagName ) as unknown as ComponentType <
4444 CommonProps & { class ?: string ; ref ?: Ref < RefType > }
4545 > ;
46-
4746 const [ isDefined , setIsDefined ] = useState ( definedWebComponents . has ( Component ) ) ;
48-
4947 // regular props (no booleans, no slots and no events)
5048 const regularProps = regularProperties . reduce ( ( acc , name ) => {
5149 if ( Object . prototype . hasOwnProperty . call ( rest , name ) && isPrimitiveAttribute ( rest [ name ] ) ) {
@@ -152,6 +150,9 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
152150 return events ;
153151 } , { } ) ;
154152
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+
155156 // non web component related props, just pass them
156157 const nonWebComponentRelatedProps = Object . entries ( rest )
157158 . filter ( ( [ key ] ) => ! regularProperties . includes ( key ) )
@@ -188,7 +189,11 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
188189 } ) ;
189190 } , [ Component , ...propsToApply ] ) ;
190191
191- if ( ( waitForDefine && ! isDefined ) || typeof window === 'undefined' ) {
192+ useEffect ( ( ) => {
193+ setAttachEvents ( true ) ;
194+ } , [ ] ) ;
195+
196+ if ( waitForDefine && ! isDefined ) {
192197 return null ;
193198 }
194199
@@ -203,7 +208,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
203208 ref = { componentRef }
204209 { ...booleanProps }
205210 { ...restRegularProps }
206- { ...eventHandlers }
211+ { ...( attachEvents ? eventHandlers : { } ) }
207212 { ...nonWebComponentRelatedProps }
208213 overflow-mode = { overflowMode ?? ( showOverflowInPopover ? 'Popover' : 'InPlace' ) }
209214 // @ts -expect-error: text is available
@@ -222,7 +227,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
222227 ref = { componentRef }
223228 { ...booleanProps }
224229 { ...regularProps }
225- { ...eventHandlers }
230+ { ...( attachEvents ? eventHandlers : { } ) }
226231 { ...nonWebComponentRelatedProps }
227232 class = { className }
228233 suppressHydrationWarning
0 commit comments