Skip to content

Commit

Permalink
Cb 3910 focus fix (#1962)
Browse files Browse the repository at this point in the history
* CB-3910: fix focusable elements selector

* CB-3910: update focus when ref changed

---------

Co-authored-by: EvgeniaBzzz <[email protected]>
  • Loading branch information
teunlao and EvgeniaBzzz authored Sep 13, 2023
1 parent 751a9c7 commit 4a04023
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions webapp/packages/core-blocks/src/useFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export function useFocus<T extends HTMLElement>({ autofocus, focusFirstChild, on
setRef(ref: T | null) {
if (this.reference !== ref) {
this.reference = ref;

this.updateFocus();
}
},
updateFocus() {
Expand All @@ -64,12 +62,12 @@ export function useFocus<T extends HTMLElement>({ autofocus, focusFirstChild, on
focusFirstChild() {
if (this.reference !== null && optionsRef.focusFirstChild) {
const firstFocusable = this.reference.querySelectorAll<T>(`
button:not([disabled=disabled]),
button:not([disabled]):not([disabled=disabled]),
[href],
input:not([disabled=disabled]):not([readonly=readonly]),
select:not([disabled=disabled]):not([readonly=readonly]),
textarea:not([disabled=disabled]):not([readonly=readonly]),
[tabndex]:not([tabndex="-1"])`);
input:not([disabled]):not([readonly]):not([disabled=disabled]):not([readonly=readonly]),
select:not([disabled]):not([readonly]):not([disabled=disabled]):not([readonly=readonly]),
textarea:not([disabled]):not([readonly]):not([disabled=disabled]):not([readonly=readonly]),
[tabindex]:not([tabindex="-1"])`);

let tabIndex = -1;
let lastElement: T | undefined;
Expand Down Expand Up @@ -134,6 +132,8 @@ export function useFocus<T extends HTMLElement>({ autofocus, focusFirstChild, on
reference.addEventListener('focusin', focusHandler);
reference.addEventListener('focusout', blurHandler);

state.updateFocus();

return () => {
reference.removeEventListener('focusin', focusHandler);
reference.removeEventListener('focusout', blurHandler);
Expand Down

0 comments on commit 4a04023

Please sign in to comment.