@@ -181,27 +181,47 @@ addModalTranslations({
181181
182182export { addModalTranslations } ;
183183
184- function createOpenModalButtonProps ( id : string ) {
184+ function createOpenModalButtonProps ( params : { modalId : string ; isOpenedByDefault : boolean } ) {
185+ const { modalId, isOpenedByDefault } = params ;
186+
185187 return {
188+ //For RSC we don't want to pass an empty function.
186189 "onClick" : undefined as any as ( ) => void ,
187190 "nativeButtonProps" : {
188- "aria-controls" : id ,
189- "data-fr-opened" : false
191+ "aria-controls" : modalId ,
192+ "data-fr-opened" : isOpenedByDefault
190193 }
191194 } ;
192195}
193196
194197let counter = 0 ;
195198
196199/** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-modal> */
197- export function createModal < Name extends string > (
198- name : Name
199- ) : Record < `${Uncapitalize < Name > } ModalButtonProps`, ReturnType < typeof createOpenModalButtonProps > > &
200+ export function createModal < Name extends string > ( params : {
201+ name : Name ;
202+ isOpenedByDefault : boolean ;
203+ } ) : Record < `${Uncapitalize < Name > } ModalButtonProps`, ReturnType < typeof createOpenModalButtonProps > > &
200204 Record < `${Capitalize < Name > } Modal`, ( props : ModalProps ) => JSX . Element > {
205+ const { name, isOpenedByDefault } = params ;
206+
201207 const modalId = `${ uncapitalize ( name ) } -modal-${ counter ++ } ` ;
202208
209+ const openModalButtonProps = createOpenModalButtonProps ( {
210+ modalId,
211+ isOpenedByDefault
212+ } ) ;
213+
203214 function InternalModal ( props : ModalProps ) {
204- return < Modal { ...props } id = { modalId } /> ;
215+ return (
216+ < >
217+ { isOpenedByDefault && (
218+ < Button { ...openModalButtonProps } className = { fr . cx ( "fr-hidden" ) } >
219+ { " " }
220+ </ Button >
221+ ) }
222+ < Modal { ...props } id = { modalId } />
223+ </ >
224+ ) ;
205225 }
206226
207227 InternalModal . displayName = `${ capitalize ( name ) } Modal` ;
@@ -210,6 +230,6 @@ export function createModal<Name extends string>(
210230
211231 return {
212232 [ InternalModal . displayName ] : InternalModal ,
213- [ `${ uncapitalize ( name ) } ModalButtonProps` ] : createOpenModalButtonProps ( modalId )
233+ [ `${ uncapitalize ( name ) } ModalButtonProps` ] : openModalButtonProps
214234 } as any ;
215235}
0 commit comments