Commit 5fcf2eea authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Use new ModalDialog for use profile modal

parent 78386e60
...@@ -6,9 +6,15 @@ type DialogProps = { ...@@ -6,9 +6,15 @@ type DialogProps = {
closed: boolean; closed: boolean;
children: ComponentChildren; children: ComponentChildren;
onClose: () => void; onClose: () => void;
'data-testid'?: string;
}; };
const NativeDialog = ({ closed, children, onClose }: DialogProps) => { function NativeDialog({
closed,
onClose,
children,
'data-testid': testId,
}: DialogProps) {
const dialogRef = useRef<HTMLDialogElement | null>(null); const dialogRef = useRef<HTMLDialogElement | null>(null);
useEffect(() => { useEffect(() => {
...@@ -32,32 +38,30 @@ const NativeDialog = ({ closed, children, onClose }: DialogProps) => { ...@@ -32,32 +38,30 @@ const NativeDialog = ({ closed, children, onClose }: DialogProps) => {
<dialog <dialog
ref={dialogRef} ref={dialogRef}
className="relative w-full h-full backdrop:bg-black/50" className="relative w-full h-full backdrop:bg-black/50"
data-testid="notebook-outer" data-testid={testId}
> >
{children} {children}
</dialog> </dialog>
); );
}; }
/** /**
* Temporary fallback used in browsers not supporting `dialog` element. * Temporary fallback used in browsers not supporting `dialog` element.
* It can be removed once all browsers we support can use it. * It can be removed once all browsers we support can use it.
*/ */
const FallbackDialog = ({ closed, children }: DialogProps) => { function FallbackDialog({ closed, children, ...rest }: DialogProps) {
return ( return (
<div <div
{...rest}
className={classnames( className={classnames(
'fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50', 'fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50',
{ hidden: closed }, { hidden: closed },
)} )}
data-testid="notebook-outer"
> >
<div className="relative w-full h-full" data-testid="notebook-inner"> <div className="relative w-full h-full">{children}</div>
{children}
</div>
</div> </div>
); );
}; }
/** Checks if the browser supports native modal dialogs */ /** Checks if the browser supports native modal dialogs */
function isModalDialogSupported(document: Document) { function isModalDialogSupported(document: Document) {
......
...@@ -108,7 +108,11 @@ export default function NotebookModal({ ...@@ -108,7 +108,11 @@ export default function NotebookModal({
} }
return ( return (
<ModalDialog closed={isHidden} onClose={onClose}> <ModalDialog
closed={isHidden}
onClose={onClose}
data-testid="notebook-outer"
>
<div className="absolute right-0 m-3"> <div className="absolute right-0 m-3">
<IconButton <IconButton
title="Close the Notebook" title="Close the Notebook"
......
...@@ -3,6 +3,7 @@ import classnames from 'classnames'; ...@@ -3,6 +3,7 @@ import classnames from 'classnames';
import { useEffect, useRef, useState } from 'preact/hooks'; import { useEffect, useRef, useState } from 'preact/hooks';
import type { Emitter, EventBus } from '../util/emitter'; import type { Emitter, EventBus } from '../util/emitter';
import ModalDialog from './ModalDialog';
export type ProfileConfig = { profileAppUrl: string } & Record<string, unknown>; export type ProfileConfig = { profileAppUrl: string } & Record<string, unknown>;
...@@ -37,33 +38,32 @@ export default function ProfileModal({ eventBus, config }: ProfileModalProps) { ...@@ -37,33 +38,32 @@ export default function ProfileModal({ eventBus, config }: ProfileModalProps) {
} }
return ( return (
<div <ModalDialog
className="fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50" closed={isHidden}
onClose={onClose}
data-testid="profile-outer" data-testid="profile-outer"
> >
<div className="relative w-full h-full" data-testid="profile-inner"> <div className="absolute right-0 m-3">
<div className="absolute right-0 m-3"> <IconButton
<IconButton title="Close profile dialog"
title="Close the Profile" onClick={onClose}
onClick={onClose} variant="dark"
variant="dark" classes={classnames(
classes={classnames( // Remove the dark variant's background color to avoid
// Remove the dark variant's background color to avoid // interfering with modal overlays. Re-activate the dark variant's
// interfering with modal overlays. Re-activate the dark variant's // background color on hover.
// background color on hover. // See https://github.com/hypothesis/client/issues/3676
// See https://github.com/hypothesis/client/issues/3676 '!bg-transparent enabled:hover:!bg-grey-3',
'!bg-transparent enabled:hover:!bg-grey-3', )}
)} >
> <CancelIcon className="w-4 h-4" />
<CancelIcon className="w-4 h-4" /> </IconButton>
</IconButton>
</div>
<iframe
title={'Hypothesis profile'}
className="h-full w-full border-0"
src={config.profileAppUrl}
/>
</div> </div>
</div> <iframe
title={'Hypothesis profile'}
className="h-full w-full border-0"
src={config.profileAppUrl}
/>
</ModalDialog>
); );
} }
...@@ -6,13 +6,13 @@ describe('ModalDialog', () => { ...@@ -6,13 +6,13 @@ describe('ModalDialog', () => {
let components; let components;
const createComponent = props => { const createComponent = props => {
const attachTo = document.createElement('div'); const container = document.createElement('div');
document.body.appendChild(attachTo); document.body.appendChild(container);
const component = mount(<ModalDialog open {...props} />, { const component = mount(<ModalDialog open {...props} />, {
attachTo, attachTo: container,
}); });
components.push([component, attachTo]); components.push([component, container]);
return component; return component;
}; };
......
...@@ -12,7 +12,7 @@ describe('NotebookModal', () => { ...@@ -12,7 +12,7 @@ describe('NotebookModal', () => {
let eventBus; let eventBus;
let emitter; let emitter;
const outerSelector = '[data-testid="notebook-outer"]'; const outerSelector = 'dialog[data-testid="notebook-outer"]';
const createComponent = config => { const createComponent = config => {
const attachTo = document.createElement('div'); const attachTo = document.createElement('div');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment