Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force focus on closing term prevents switching to other elements on a click #599

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/term/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (typeof document !== 'undefined') {
}

if (event.key === 'Escape' && openedDefinition) {
closeDefinition(openedDefinition);
closeDefinition(openedDefinition, true);
}
});

Expand Down
17 changes: 7 additions & 10 deletions src/js/term/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ export function openDefinition(target: HTMLElement) {

definitionElement.classList.toggle(openClass);

trapFocus(definitionElement, target);
trapFocus(definitionElement);
}

export function closeDefinition(definition: HTMLElement) {
export function closeDefinition(definition: HTMLElement, forceFocus?: boolean) {
definition.classList.remove(openClass);
const termId = definition.getAttribute('term-id') || '';
const term = document.getElementById(termId);
Expand All @@ -187,7 +187,10 @@ export function closeDefinition(definition: HTMLElement) {
}

termParent.removeEventListener('scroll', termOnResize);
term?.focus(); // Set focus back to open button after closing popup

if (forceFocus) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeDefinition has only one usage here https://github.com/diplodoc-platform/transform/blob/master/src/js/term/index.ts#L28C13-L28C28 why this if (forceFocus) needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There're some usages in utils.ts file (for example

closeDefinition(openedDefinition);
)
I don't know why the search isn't detecting this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see it now

Let's refactor the function a little then

let's add the function getTermByDefinition to utils.ts

function getTermByDefinition(definition: HTMLElement) {
    const termId = definition.getAttribute('term-id');

    return termId ? document.getElementById(termId) : null;
}

Use it in closeDefinition and remove focus management from it. And then we'll adjust the logic to:

if (event.key === 'Escape' && openedDefinition) {
    closeDefinition(openedDefinition);
    getTermByDefinition(openedDefinition)?.focus();
}

term?.focus(); // Set focus back to open button after closing popup
}

isListenerNeeded = true;
}
Expand All @@ -210,19 +213,13 @@ function getCoords(elem: HTMLElement) {
return {top: Math.round(top), left: Math.round(left)};
}

export function trapFocus(element: HTMLElement, termButton: HTMLElement) {
export function trapFocus(element: HTMLElement) {
const focusableElements = element.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
);
const firstFocusableElement = focusableElements[0] as HTMLElement;
const lastFocusableElement = focusableElements[focusableElements.length - 1] as HTMLElement;

// if another term was previously closed, the focus may still be on it
if (!firstFocusableElement && document.activeElement !== termButton) {
termButton.focus();
return;
}

if (firstFocusableElement) {
firstFocusableElement.focus();
}
Expand Down
Loading