-
Notifications
You must be signed in to change notification settings - Fork 46
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
fix: force focus on closing term prevents switching to other elements on a click #599
Conversation
src/js/term/utils.ts
Outdated
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
transform/src/js/term/utils.ts
Line 154 in f10be7f
closeDefinition(openedDefinition); |
I don't know why the search isn't detecting this.
There was a problem hiding this comment.
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();
}
unfortunately, it seems that it will not be possible to take into account only the case 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. |
indeed, it looks like the current focus return in terms was redundant and doesn't need to be saved |
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.