Fixed FocusTrap issue
parent
374317689c
commit
ecf2336c03
|
@ -27,7 +27,7 @@ const FocusTrap = BaseFocusTrap.extend('focustrap', {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
getComputedSelector(selector) {
|
getComputedSelector(selector) {
|
||||||
return `:not(.p-hidden-focusable):not([data-p-hidden-focusable="true"])${selector}`;
|
return `:not(.p-hidden-focusable):not([data-p-hidden-focusable="true"])${selector ?? ''}`;
|
||||||
},
|
},
|
||||||
bind(el, binding) {
|
bind(el, binding) {
|
||||||
const { onFocusIn, onFocusOut } = binding.value || {};
|
const { onFocusIn, onFocusOut } = binding.value || {};
|
||||||
|
@ -35,10 +35,14 @@ const FocusTrap = BaseFocusTrap.extend('focustrap', {
|
||||||
el.$_pfocustrap_mutationobserver = new MutationObserver((mutationList) => {
|
el.$_pfocustrap_mutationobserver = new MutationObserver((mutationList) => {
|
||||||
mutationList.forEach((mutation) => {
|
mutationList.forEach((mutation) => {
|
||||||
if (mutation.type === 'childList' && !el.contains(document.activeElement)) {
|
if (mutation.type === 'childList' && !el.contains(document.activeElement)) {
|
||||||
const findNextFocusableElement = (el) => {
|
const findNextFocusableElement = (_el) => {
|
||||||
const focusableElement = DomHandler.isFocusableElement(el) ? el : DomHandler.getFirstFocusableElement(el);
|
const focusableElement = DomHandler.isFocusableElement(_el)
|
||||||
|
? DomHandler.isFocusableElement(_el, this.getComputedSelector(el.$_pfocustrap_focusableselector))
|
||||||
|
? _el
|
||||||
|
: DomHandler.getFirstFocusableElement(el, this.getComputedSelector(el.$_pfocustrap_focusableselector))
|
||||||
|
: DomHandler.getFirstFocusableElement(_el);
|
||||||
|
|
||||||
return ObjectUtils.isNotEmpty(focusableElement) ? focusableElement : findNextFocusableElement(el.nextSibling);
|
return ObjectUtils.isNotEmpty(focusableElement) ? focusableElement : findNextFocusableElement(_el.nextSibling);
|
||||||
};
|
};
|
||||||
|
|
||||||
DomHandler.focus(findNextFocusableElement(mutation.nextSibling));
|
DomHandler.focus(findNextFocusableElement(mutation.nextSibling));
|
||||||
|
@ -72,7 +76,7 @@ const FocusTrap = BaseFocusTrap.extend('focustrap', {
|
||||||
onFirstHiddenElementFocus(event) {
|
onFirstHiddenElementFocus(event) {
|
||||||
const { currentTarget, relatedTarget } = event;
|
const { currentTarget, relatedTarget } = event;
|
||||||
const focusableElement =
|
const focusableElement =
|
||||||
relatedTarget === currentTarget.$_pfocustrap_lasthiddenfocusableelement
|
relatedTarget === currentTarget.$_pfocustrap_lasthiddenfocusableelement || !this.$el?.contains(relatedTarget)
|
||||||
? DomHandler.getFirstFocusableElement(currentTarget.parentElement, this.getComputedSelector(currentTarget.$_pfocustrap_focusableselector))
|
? DomHandler.getFirstFocusableElement(currentTarget.parentElement, this.getComputedSelector(currentTarget.$_pfocustrap_focusableselector))
|
||||||
: currentTarget.$_pfocustrap_lasthiddenfocusableelement;
|
: currentTarget.$_pfocustrap_lasthiddenfocusableelement;
|
||||||
|
|
||||||
|
@ -81,7 +85,7 @@ const FocusTrap = BaseFocusTrap.extend('focustrap', {
|
||||||
onLastHiddenElementFocus(event) {
|
onLastHiddenElementFocus(event) {
|
||||||
const { currentTarget, relatedTarget } = event;
|
const { currentTarget, relatedTarget } = event;
|
||||||
const focusableElement =
|
const focusableElement =
|
||||||
relatedTarget === currentTarget.$_pfocustrap_firsthiddenfocusableelement
|
relatedTarget === currentTarget.$_pfocustrap_firsthiddenfocusableelement || !this.$el?.contains(relatedTarget)
|
||||||
? DomHandler.getLastFocusableElement(currentTarget.parentElement, this.getComputedSelector(currentTarget.$_pfocustrap_focusableselector))
|
? DomHandler.getLastFocusableElement(currentTarget.parentElement, this.getComputedSelector(currentTarget.$_pfocustrap_focusableselector))
|
||||||
: currentTarget.$_pfocustrap_firsthiddenfocusableelement;
|
: currentTarget.$_pfocustrap_firsthiddenfocusableelement;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue