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

Conversation

mournfulCoroner
Copy link
Contributor

@mournfulCoroner mournfulCoroner commented Jan 13, 2025

When the term is open, any click outside the term returns the focus to the definition of the term, which interferes with the operation of interactive elements.
Example: for the first click on the input, the focus immediately goes off

terms.broken.mov

I suggest returning focus only for keydown event, since it is only needed in this case.

@@ -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();
}

@martyanovandrey
Copy link
Contributor

unfortunately, it seems that it will not be possible to take into account only the case if (event.key === 'Escape' && openedDefinition) {

in this case, if we close the term by clicking in the region without selecting a new active entity, then our focus completely flies off

@mournfulCoroner
Copy link
Contributor Author

mournfulCoroner commented Jan 13, 2025

unfortunately, it seems that it will not be possible to take into account only the case if (event.key === 'Escape' && openedDefinition) {

in this case, if we close the term by clicking in the region without selecting a new active entity, then our focus completely flies off

But isn't this the standard browser behavior when clicking a mouse? If you click on an empty space, then the focus is lost.

@martyanovandrey
Copy link
Contributor

indeed, it looks like the current focus return in terms was redundant and doesn't need to be saved

@martyanovandrey martyanovandrey merged commit e8caa68 into diplodoc-platform:master Jan 13, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants