Build and publish Eldorado documentation / build (pull_request) Successful in 33s
28 lines
2.0 KiB
TypeScript
28 lines
2.0 KiB
TypeScript
import React, {useEffect, useId, useRef, useState} from 'react';
|
||
import useBaseUrl from '@docusaurus/useBaseUrl';
|
||
|
||
export default function ArchifyDiagram({name, title, compact = false}: {name: string; title: string; compact?: boolean}): React.ReactElement {
|
||
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)) throw new Error('Invalid diagram name');
|
||
const source = useBaseUrl(`/diagrams/${name}/`);
|
||
const dialog = useRef<HTMLDialogElement>(null);
|
||
const opener = useRef<HTMLButtonElement>(null);
|
||
const headingId = useId();
|
||
const [expanded, setExpanded] = useState(false);
|
||
useEffect(() => {
|
||
if (!expanded) return;
|
||
dialog.current?.showModal();
|
||
const previousOverflow = document.body.style.overflow;
|
||
document.body.style.overflow = 'hidden';
|
||
return () => {document.body.style.overflow = previousOverflow;};
|
||
}, [expanded]);
|
||
const close = () => {dialog.current?.close();};
|
||
return <section className={`archify-panel${compact ? ' archify-panel--compact' : ''}`} aria-label={title}>
|
||
<div className="archify-toolbar"><button ref={opener} type="button" className="archify-expand" aria-label={`Expand ${title}`} title="Expand diagram" onClick={() => setExpanded(true)}><svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true"><path d="M8 3H3v5M16 3h5v5M21 16v5h-5M8 21H3v-5" /></svg></button></div>
|
||
<iframe src={source} title={title} sandbox="allow-scripts allow-downloads" loading="lazy" referrerPolicy="no-referrer" />
|
||
<dialog ref={dialog} className="archify-modal" aria-labelledby={headingId} onClose={() => {setExpanded(false); opener.current?.focus();}}>
|
||
<div className="archify-modal-toolbar"><strong id={headingId}>{title}</strong><button type="button" onClick={close} autoFocus aria-label="Close full-screen diagram"><span aria-hidden="true">×</span></button></div>
|
||
{expanded && <iframe src={source} title={`${title} — expanded`} sandbox="allow-scripts allow-downloads" referrerPolicy="no-referrer" />}
|
||
</dialog>
|
||
</section>;
|
||
}
|