Skip to content

Commit cd1e479

Browse files
chore: allow parent component to forward ref
1 parent 20fef39 commit cd1e479

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/design-system/dialog/dialog-root.component.tsx

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ export interface DialogRootProps {
2626
// TODO refactor: pass z-indices config on app level: https://vanilla-extract.style/documentation/packages/dynamic/
2727
zIndex?: number;
2828
}
29-
30-
export const Root = ({
29+
/*
30+
export const Root = React.forwardRef(({
3131
open,
3232
setOpen,
3333
onCloseAutoFocusRef,
3434
children,
3535
portalContainer,
3636
zIndex,
37-
}: Readonly<DialogRootProps>): JSX.Element => (
37+
}: Readonly<DialogRootProps>, ref)): JSX.Element => (
3838
<AlertDialog.Root open={open}>
3939
<AlertDialog.Portal container={portalContainer}>
4040
<AlertDialog.Overlay asChild>
@@ -62,3 +62,45 @@ export const Root = ({
6262
</AlertDialog.Portal>
6363
</AlertDialog.Root>
6464
);
65+
*/
66+
67+
export const Root = React.forwardRef((
68+
props: Readonly<DialogRootProps>, forwardReference?: any
69+
) => {
70+
const {
71+
open,
72+
setOpen,
73+
onCloseAutoFocusRef,
74+
children,
75+
portalContainer,
76+
zIndex,
77+
} = props;
78+
return (
79+
<AlertDialog.Root open={open}>
80+
<AlertDialog.Portal container={portalContainer}>
81+
<AlertDialog.Overlay asChild>
82+
<Backdrop zIndex={zIndex}/>
83+
</AlertDialog.Overlay>
84+
<AlertDialog.Content
85+
ref={forwardReference}
86+
asChild
87+
onEscapeKeyDown={(): void => {
88+
setOpen(false);
89+
}}
90+
onCloseAutoFocus={
91+
onCloseAutoFocusRef &&
92+
((): void => {
93+
onCloseAutoFocusRef.current?.focus();
94+
})
95+
}
96+
>
97+
<Content
98+
className={cx.dialogContent}
99+
style={{ zIndex: zIndex === undefined ? undefined : zIndex + 1 }}
100+
>
101+
{children}
102+
</Content>
103+
</AlertDialog.Content>
104+
</AlertDialog.Portal>
105+
</AlertDialog.Root>
106+
)});

0 commit comments

Comments
 (0)