Skip to content

Commit

Permalink
Ensure hostelement is connected to dom for confirmation dialogs (#4012)
Browse files Browse the repository at this point in the history
* ensure hostelement is connected to DOM for confirmation dialogs

* remove duplicate enum

* changeset
  • Loading branch information
keithamus authored Dec 4, 2023
1 parent 968d4ef commit 5fd025f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-ladybugs-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Ensure ConfirmationDialog adds host element to the DOM
7 changes: 5 additions & 2 deletions src/ConfirmationDialog/ConfirmationDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ConfirmationDialogProps {
* Required. This callback is invoked when a gesture to close the dialog
* is performed. The first argument indicates the gesture.
*/
onClose: (gesture: 'confirm' | 'cancel' | 'close-button' | 'cancel' | 'escape') => void
onClose: (gesture: 'confirm' | 'close-button' | 'cancel' | 'escape') => void

/**
* Required. The title of the ConfirmationDialog. This is usually a brief
Expand Down Expand Up @@ -145,11 +145,14 @@ export const ConfirmationDialog: React.FC<React.PropsWithChildren<ConfirmationDi
)
}

let hostElement: Element | null = null
export type ConfirmOptions = Omit<ConfirmationDialogProps, 'onClose'> & {content: React.ReactNode}
async function confirm(themeProps: ThemeProviderProps, options: ConfirmOptions): Promise<boolean> {
const {content, ...confirmationDialogProps} = options
return new Promise(resolve => {
const root = createRoot(document.createElement('div'))
hostElement = document.createElement('div')
if (!hostElement.isConnected) document.body.append(hostElement)
const root = createRoot(hostElement)
const onClose: ConfirmationDialogProps['onClose'] = gesture => {
root.unmount()
if (gesture === 'confirm') {
Expand Down

0 comments on commit 5fd025f

Please sign in to comment.