primevue-mirror/components/lib/focustrap/FocusTrap.js

139 lines
5.7 KiB
JavaScript
Raw Normal View History

2023-06-22 13:53:04 +00:00
import { BaseDirective } from 'primevue/basedirective';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
function bind(el, binding) {
const { onFocusIn, onFocusOut } = binding.value || {};
el.$_pfocustrap_mutationobserver = new MutationObserver((mutationList) => {
mutationList.forEach((mutation) => {
if (mutation.type === 'childList' && !el.contains(document.activeElement)) {
const findNextFocusableElement = (el) => {
const focusableElement = DomHandler.isFocusableElement(el) ? el : DomHandler.getFirstFocusableElement(el);
return ObjectUtils.isNotEmpty(focusableElement) ? focusableElement : findNextFocusableElement(el.nextSibling);
};
DomHandler.focus(findNextFocusableElement(mutation.nextSibling));
}
});
});
el.$_pfocustrap_mutationobserver.disconnect();
el.$_pfocustrap_mutationobserver.observe(el, {
childList: true
});
el.$_pfocustrap_focusinlistener = (event) => onFocusIn && onFocusIn(event);
el.$_pfocustrap_focusoutlistener = (event) => onFocusOut && onFocusOut(event);
el.addEventListener('focusin', el.$_pfocustrap_focusinlistener);
el.addEventListener('focusout', el.$_pfocustrap_focusoutlistener);
}
function unbind(el) {
el.$_pfocustrap_mutationobserver && el.$_pfocustrap_mutationobserver.disconnect();
el.$_pfocustrap_focusinlistener && el.removeEventListener('focusin', el.$_pfocustrap_focusinlistener) && (el.$_pfocustrap_focusinlistener = null);
el.$_pfocustrap_focusoutlistener && el.removeEventListener('focusout', el.$_pfocustrap_focusoutlistener) && (el.$_pfocustrap_focusoutlistener = null);
}
function autoFocus(el, binding) {
const { autoFocusSelector = '', firstFocusableSelector = '', autoFocus = false } = binding.value || {};
2023-06-22 13:53:04 +00:00
let focusableElement = DomHandler.getFirstFocusableElement(el, `[autofocus]:not([data-p-hidden-focusable="true"])${autoFocusSelector}`);
2022-12-08 11:04:25 +00:00
2023-06-22 13:53:04 +00:00
autoFocus && !focusableElement && (focusableElement = DomHandler.getFirstFocusableElement(el, `:not([data-p-hidden-focusable="true"])${firstFocusableSelector}`));
2022-12-08 11:04:25 +00:00
DomHandler.focus(focusableElement);
}
function onFirstHiddenElementFocus(event) {
const { currentTarget, relatedTarget } = event;
const focusableElement =
relatedTarget === currentTarget.$_pfocustrap_lasthiddenfocusableelement
2023-06-22 13:53:04 +00:00
? DomHandler.getFirstFocusableElement(currentTarget.parentElement, `:not([data-p-hidden-focusable="true"])${currentTarget.$_pfocustrap_focusableselector}`)
2022-12-08 11:04:25 +00:00
: currentTarget.$_pfocustrap_lasthiddenfocusableelement;
DomHandler.focus(focusableElement);
}
function onLastHiddenElementFocus(event) {
const { currentTarget, relatedTarget } = event;
const focusableElement =
relatedTarget === currentTarget.$_pfocustrap_firsthiddenfocusableelement
2023-06-22 13:53:04 +00:00
? DomHandler.getLastFocusableElement(currentTarget.parentElement, `:not([data-p-hidden-focusable="true"])${currentTarget.$_pfocustrap_focusableselector}`)
2022-12-08 11:04:25 +00:00
: currentTarget.$_pfocustrap_firsthiddenfocusableelement;
DomHandler.focus(focusableElement);
}
function createHiddenFocusableElements(el, binding) {
const { tabIndex = 0, firstFocusableSelector = '', lastFocusableSelector = '' } = binding.value || {};
const createFocusableElement = (onFocus) => {
const element = document.createElement('span');
2023-06-22 13:53:04 +00:00
if (binding.instance.$primevue && binding.instance.$primevue.config && binding.instance.$primevue.config.unstyled) {
element.style.border = '0';
element.style.clip = 'rect(0 0 0 0)';
element.style.height = '1px';
element.style.margin = '-1px';
element.style.overflow = 'hidden';
element.style.padding = '0';
element.style.position = 'absolute';
element.style.width = '1px';
} else {
element.classList = 'p-hidden-accessible p-hidden-focusable';
}
2022-12-08 11:04:25 +00:00
element.tabIndex = tabIndex;
element.setAttribute('aria-hidden', 'true');
element.setAttribute('role', 'presentation');
2023-06-22 13:53:04 +00:00
element.setAttribute('data-p-hidden-accessible', true);
element.setAttribute('data-p-hidden-focusable', true);
2022-12-08 11:04:25 +00:00
element.addEventListener('focus', onFocus);
return element;
};
const firstFocusableElement = createFocusableElement(onFirstHiddenElementFocus);
const lastFocusableElement = createFocusableElement(onLastHiddenElementFocus);
firstFocusableElement.$_pfocustrap_lasthiddenfocusableelement = lastFocusableElement;
firstFocusableElement.$_pfocustrap_focusableselector = firstFocusableSelector;
2023-06-22 13:53:04 +00:00
firstFocusableElement.setAttribute('data-pc-section', 'firstfocusableelement');
2022-12-08 11:04:25 +00:00
lastFocusableElement.$_pfocustrap_firsthiddenfocusableelement = firstFocusableElement;
lastFocusableElement.$_pfocustrap_focusableselector = lastFocusableSelector;
2023-06-22 13:53:04 +00:00
lastFocusableElement.setAttribute('data-pc-section', 'lastfocusableelement');
2022-12-08 11:04:25 +00:00
el.prepend(firstFocusableElement);
el.append(lastFocusableElement);
}
2023-06-22 13:53:04 +00:00
const FocusTrap = BaseDirective.extend('focustrap', {
2022-12-08 11:04:25 +00:00
mounted(el, binding) {
const { disabled } = binding.value || {};
if (!disabled) {
createHiddenFocusableElements(el, binding);
bind(el, binding);
autoFocus(el, binding);
}
2023-06-22 13:53:04 +00:00
el.setAttribute('data-pc-section', 'root');
el.setAttribute('data-pc-name', 'focustrap');
2023-06-23 09:34:16 +00:00
el.$pEl = el;
2023-06-22 13:53:04 +00:00
BaseDirective.handleCSS('focustrap', el, binding);
2022-12-08 11:04:25 +00:00
},
2023-06-22 13:53:04 +00:00
2022-12-08 11:04:25 +00:00
updated(el, binding) {
const { disabled } = binding.value || {};
disabled && unbind(el);
},
unmounted(el) {
unbind(el);
}
2023-06-22 13:53:04 +00:00
});
2022-12-08 11:04:25 +00:00
export default FocusTrap;