@@ -23,12 +23,17 @@ import {
2323
2424import { getCloudinaryConfig } from "../../lib/cloudinary" ;
2525
26- // Stable ID hook supporting React < 18 fallback
27- const useStableId = ( ) => {
28- if ( typeof ( React as any ) . useId === 'function' ) {
29- return ( React as any ) . useId ( ) . replace ( / : / g, '' ) ;
30- }
31- return React . useRef ( `id-${ Date . now ( ) } -${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 9 ) } ` ) . current ;
26+ // Stable ID hook with React 18+ useId and React < 18 fallback
27+ const useUploadWidgetId = ( ) : string => {
28+ const reactId = ( React as { useId ?: ( ) => string } ) . useId ?.( ) ?? null ;
29+
30+ // Preserve the original random ID behavior for React < 18
31+ const fallbackId = React . useRef ( Math . floor ( Math . random ( ) * 100 ) ) . current ;
32+
33+ // Remove colons from React useId() output (e.g., ":r1:" -> "r1") to avoid issues with CSS selectors and HTML IDs
34+ const sanitizedId = reactId ? reactId . replace ( / : / g, '' ) : fallbackId ;
35+
36+ return `cloudinary-uploadwidget-${ sanitizedId } ` ;
3237} ;
3338
3439const CldUploadWidget = ( {
@@ -42,7 +47,7 @@ const CldUploadWidget = ({
4247 uploadPreset,
4348 ...props
4449} : CldUploadWidgetProps ) => {
45- const uniqueId = useStableId ( ) ;
50+ const uploadWidgetId = useUploadWidgetId ( ) ;
4651 const cloudinary : CldUploadWidgetCloudinaryInstance = useRef ( ) ;
4752 const widget : CldUploadWidgetWidgetInstance = useRef ( ) ;
4853
@@ -248,7 +253,7 @@ const CldUploadWidget = ({
248253 ...instanceMethods ,
249254 } ) }
250255 < Script
251- id = { `cloudinary-uploadwidget- ${ uniqueId } ` }
256+ id = { uploadWidgetId }
252257 src = "https://upload-widget.cloudinary.com/global/all.js"
253258 onLoad = { handleOnLoad }
254259 onError = { ( e ) => console . error ( `Failed to load Cloudinary Upload Widget: ${ e . message } ` ) }
0 commit comments