11import React , { forwardRef , useRef , type HTMLAttributes } from 'react'
22import { IconButton } from '../Button'
33import useDialog from '../hooks/useDialog'
4- import type { SxProp } from '../sx'
54import type { ComponentProps } from '../utils/types'
65import { useRefObjectAsForwardedRef } from '../hooks/useRefObjectAsForwardedRef'
76import { XIcon } from '@primer/octicons-react'
87import { clsx } from 'clsx'
98import classes from './Dialog.module.css'
10- import { BoxWithFallback } from '../internal/components/BoxWithFallback'
119
1210// Dialog v1
1311const noop = ( ) => null
1412
1513type StyledDialogBaseProps = {
1614 narrow ?: boolean
1715 wide ?: boolean
18- } & SxProp
16+ as ?: React . ElementType
17+ }
1918
20- export type DialogHeaderProps = React . PropsWithChildren < HTMLAttributes < HTMLDivElement > > & SxProp
19+ export type DialogHeaderProps = React . PropsWithChildren < HTMLAttributes < HTMLDivElement > > & {
20+ as ?: React . ElementType
21+ }
2122
22- function DialogHeader ( { children, className, ...rest } : DialogHeaderProps ) {
23+ function DialogHeader ( { children, className, as : Component = 'div' , ...rest } : DialogHeaderProps ) {
2324 if ( React . Children . toArray ( children ) . every ( ch => typeof ch === 'string' ) ) {
2425 children = < span className = { classes . HeaderChild } > { children } </ span >
2526 }
2627
2728 return (
28- < BoxWithFallback as = "div" { ...rest } className = { clsx ( classes . Header , className ) } >
29+ < Component { ...rest } className = { clsx ( classes . Header , className ) } >
2930 { children }
30- </ BoxWithFallback >
31+ </ Component >
3132 )
3233}
3334
@@ -36,11 +37,15 @@ type InternalDialogProps = {
3637 onDismiss ?: ( ) => void
3738 initialFocusRef ?: React . RefObject < HTMLElement >
3839 returnFocusRef ?: React . RefObject < HTMLElement >
40+ as ?: React . ElementType
3941} & StyledDialogBaseProps &
4042 HTMLAttributes < HTMLDivElement >
4143
4244const Dialog = forwardRef < HTMLDivElement , InternalDialogProps > (
43- ( { children, onDismiss = noop , isOpen, initialFocusRef, returnFocusRef, className, ...props } , forwardedRef ) => {
45+ (
46+ { children, onDismiss = noop , isOpen, initialFocusRef, returnFocusRef, className, as : Component = 'div' , ...props } ,
47+ forwardedRef ,
48+ ) => {
4449 const overlayRef = useRef ( null )
4550 const modalRef = useRef < HTMLDivElement > ( null )
4651 useRefObjectAsForwardedRef ( forwardedRef , modalRef )
@@ -65,9 +70,8 @@ const Dialog = forwardRef<HTMLDivElement, InternalDialogProps>(
6570
6671 return isOpen ? (
6772 < >
68- < BoxWithFallback as = "span" className = { classes . Overlay } ref = { overlayRef } />
69- < BoxWithFallback
70- as = "div"
73+ < span className = { classes . Overlay } ref = { overlayRef } />
74+ < Component
7175 tabIndex = { - 1 }
7276 ref = { modalRef }
7377 role = "dialog"
@@ -86,7 +90,7 @@ const Dialog = forwardRef<HTMLDivElement, InternalDialogProps>(
8690 className = { classes . CloseIcon }
8791 />
8892 { children }
89- </ BoxWithFallback >
93+ </ Component >
9094 </ >
9195 ) : null
9296 } ,
0 commit comments