This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useId, useRef, useState } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import {
|
||||
formFieldGuidance,
|
||||
formHelpPlacement,
|
||||
@@ -29,6 +30,7 @@ export default function FormFieldHelp() {
|
||||
const [position, setPosition] = useState<'left' | 'right'>('right');
|
||||
const [vertical, setVertical] = useState<'top' | 'bottom'>('bottom');
|
||||
const [panelHeight, setPanelHeight] = useState<number | undefined>();
|
||||
const [dialog, setDialog] = useState<HTMLElement | null>(null);
|
||||
const automaticRef = useRef(true);
|
||||
const paused = useRef(false);
|
||||
const current = useRef<HTMLElement | null>(null);
|
||||
@@ -62,6 +64,9 @@ export default function FormFieldHelp() {
|
||||
current.current = target;
|
||||
setControl(target);
|
||||
setStep(formHelpStep(target));
|
||||
setDialog(
|
||||
target.closest<HTMLElement>('[role="dialog"],dialog[open]'),
|
||||
);
|
||||
place(target);
|
||||
if (show) setOpen(true);
|
||||
}
|
||||
@@ -73,6 +78,7 @@ export default function FormFieldHelp() {
|
||||
paused.current = false;
|
||||
lastExplicitStep = '';
|
||||
setOpen(false);
|
||||
setDialog(null);
|
||||
}
|
||||
const controls = formHelpControls(document);
|
||||
const explicit = document.querySelector<HTMLElement>(
|
||||
@@ -102,6 +108,7 @@ export default function FormFieldHelp() {
|
||||
current.current = null;
|
||||
setControl(null);
|
||||
setOpen(false);
|
||||
setDialog(null);
|
||||
} else if (!current.current && controls[0] && !explicit) {
|
||||
select(controls[0], false);
|
||||
}
|
||||
@@ -131,7 +138,9 @@ export default function FormFieldHelp() {
|
||||
);
|
||||
if (
|
||||
target &&
|
||||
target.closest('main,[role="main"],[role="dialog"]') &&
|
||||
target.closest(
|
||||
'main,[role="main"],[role="dialog"],dialog[open]',
|
||||
) &&
|
||||
formHelpControlVisible(target)
|
||||
)
|
||||
select(target, automaticRef.current && !paused.current);
|
||||
@@ -243,12 +252,16 @@ export default function FormFieldHelp() {
|
||||
}
|
||||
const fields = formHelpGroupControls(document, control);
|
||||
const index = control ? fields.indexOf(control) : -1;
|
||||
return (
|
||||
const content = (
|
||||
<aside
|
||||
ref={root}
|
||||
data-form-field-help
|
||||
aria-label="Ajuda ao preenchimento"
|
||||
className={`fixed z-40 max-w-[calc(100%-1.5rem)] text-slate-900 ${vertical === 'top' ? 'top-20' : 'bottom-3'} ${position === 'left' ? 'left-3' : 'right-3'}`}
|
||||
className={
|
||||
dialog
|
||||
? 'mt-3 max-w-full min-w-0 text-slate-900'
|
||||
: `fixed z-40 max-w-[calc(100%-1.5rem)] text-slate-900 ${vertical === 'top' ? 'top-20' : 'bottom-3'} ${position === 'left' ? 'left-3' : 'right-3'}`
|
||||
}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Escape' && open) {
|
||||
event.preventDefault();
|
||||
@@ -262,7 +275,11 @@ export default function FormFieldHelp() {
|
||||
data-form-help-panel
|
||||
tabIndex={-1}
|
||||
aria-labelledby={`${id}-title`}
|
||||
style={{ maxHeight: panelHeight }}
|
||||
style={{
|
||||
maxHeight: dialog
|
||||
? Math.min(innerHeight * 0.3, 260)
|
||||
: panelHeight,
|
||||
}}
|
||||
className="mb-2 max-h-[45dvh] w-96 max-w-full space-y-3 overflow-y-auto rounded-xl border-2 border-emerald-700 bg-white p-4 shadow-xl focus-visible:outline-2"
|
||||
>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
@@ -381,4 +398,6 @@ export default function FormFieldHelp() {
|
||||
</button>
|
||||
</aside>
|
||||
);
|
||||
// Keep help inside a modal's focus scope instead of behind its backdrop.
|
||||
return dialog?.isConnected ? createPortal(content, dialog) : content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user