-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow multiple dialogs to be open at once #894
- Loading branch information
Showing
17 changed files
with
206 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
browser/data-browser/src/components/Dialog/DialogContainer.tsx
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
browser/data-browser/src/components/Dialog/DialogGlobalContextProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useId, | ||
useMemo, | ||
useRef, | ||
useState, | ||
type FC, | ||
type PropsWithChildren, | ||
type RefObject, | ||
} from 'react'; | ||
import { styled } from 'styled-components'; | ||
|
||
interface DialogGlobalContext { | ||
openDialogs: string[]; | ||
setDialogOpen: (id: string, open: boolean) => void; | ||
portal: RefObject<HTMLDivElement>; | ||
} | ||
|
||
export const DialogContext = createContext<DialogGlobalContext>(null!); | ||
|
||
export const DialogGlobalContextProvider: FC<PropsWithChildren> = ({ | ||
children, | ||
}) => { | ||
const [openDialogs, setOpenDialogs] = useState<string[]>([]); | ||
const portalRef = useRef<HTMLDivElement>(null); | ||
|
||
const setDialogOpen = useCallback((id: string, open: boolean) => { | ||
if (open) { | ||
setOpenDialogs(prev => { | ||
if (prev.includes(id)) { | ||
return prev; | ||
} | ||
|
||
return [...prev, id]; | ||
}); | ||
} else { | ||
setOpenDialogs(prev => prev.filter(dialogId => dialogId !== id)); | ||
} | ||
}, []); | ||
|
||
const context = useMemo( | ||
() => ({ openDialogs, setDialogOpen, portal: portalRef }), | ||
[openDialogs, setDialogOpen, portalRef], | ||
); | ||
|
||
return ( | ||
<DialogContext.Provider value={context}> | ||
{children} | ||
<StyledDiv ref={portalRef}></StyledDiv> | ||
</DialogContext.Provider> | ||
); | ||
}; | ||
|
||
export function useDialogGlobalContext(open: boolean) { | ||
const id = useId(); | ||
const { openDialogs, setDialogOpen, ...context } = useContext(DialogContext); | ||
|
||
const isTopLevel = openDialogs.at(-1) === id; | ||
|
||
useEffect(() => { | ||
setDialogOpen(id, open); | ||
}, [open, id]); | ||
|
||
return { | ||
isTopLevel, | ||
...context, | ||
}; | ||
} | ||
|
||
const StyledDiv = styled.div` | ||
display: contents; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.