Skip to content

Commit

Permalink
fix(dialogs): backward compatible dialog usage (#2816)
Browse files Browse the repository at this point in the history
  • Loading branch information
pankgeorg authored Feb 20, 2024
1 parent afbf52e commit dec9a98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 8 additions & 4 deletions frontend/common/useDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ export const useDialog = () => {
const dialog_ref = useRef(/** @type {HTMLDialogElement?} */ (null))

useLayoutEffect(() => {
if (dialog_ref.current != null) dialogPolyfill.registerDialog(dialog_ref.current)
if (dialog_ref.current != null && typeof HTMLDialogElement !== "function") dialogPolyfill.registerDialog(dialog_ref.current)
}, [dialog_ref.current])

return useMemo(() => {
const open = () => dialog_ref.current?.showModal()
const close = () => dialog_ref.current?.close()
const toggle = () => (dialog_ref.current?.open === true ? dialog_ref.current?.close() : dialog_ref.current?.showModal())
const open = () => {
if (!dialog_ref.current?.open) dialog_ref.current?.showModal()
}
const close = () => {
if (dialog_ref.current?.open === true) dialog_ref.current?.close?.()
}
const toggle = () => (dialog_ref.current?.open === true ? dialog_ref.current?.close?.() : dialog_ref.current?.showModal?.())

return [dialog_ref, open, close, toggle]
}, [dialog_ref])
Expand Down
15 changes: 12 additions & 3 deletions frontend/components/ExportBanner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//@ts-ignore
import dialogPolyfill from "https://cdn.jsdelivr.net/npm/[email protected]/dist/dialog-polyfill.esm.min.js"

import { useEventListener } from "../common/useEventListener.js"
import { html, useLayoutEffect, useRef } from "../imports/Preact.js"

Expand Down Expand Up @@ -80,10 +83,16 @@ export const ExportBanner = ({ notebook_id, print_title, open, onClose, notebook
const element_ref = useRef(/** @type {HTMLDialogElement?} */ (null))

useLayoutEffect(() => {
if (element_ref.current != null && typeof HTMLDialogElement !== "function") dialogPolyfill.registerDialog(element_ref.current)
}, [element_ref.current])

useLayoutEffect(() => {
// Closing doesn't play well if the browser is old and the dialog not open
// https://github.com/GoogleChrome/dialog-polyfill/issues/149
if (open) {
element_ref.current?.show()
element_ref.current?.open === false && element_ref.current?.show?.()
} else {
element_ref.current?.close()
element_ref.current?.open && element_ref.current?.close?.()
}
}, [open, element_ref.current])

Expand All @@ -97,7 +106,7 @@ export const ExportBanner = ({ notebook_id, print_title, open, onClose, notebook
)

return html`
<dialog id="export" inert=${!open} ref=${element_ref}>
<dialog id="export" inert=${!open} open=${open} ref=${element_ref}>
<div id="container">
<div class="export_title">export</div>
<!-- no "download" attribute here: we want the jl contents to be shown in a new tab -->
Expand Down

0 comments on commit dec9a98

Please sign in to comment.