Skip to content

Commit

Permalink
fix(checkbox): prevent race condition in setting ariaBusy
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezamirian committed Oct 23, 2024
1 parent dee7149 commit 214132c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
3 changes: 0 additions & 3 deletions packages/jui/src/Checkbox/Checkbox.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ describe("Checkbox", () => {
matchImageSnapshot("Checkbox-unchecked");
cy.contains(CHECKBOX_LABEL_TEXT).click();

// svg is already loaded, so the workaround in matchImageSnapshot won't work :/ can be removed when icons loading
// in tests is improved in general
cy.wait(50);
matchImageSnapshot("Checkbox-checked-and-focused");
});

Expand Down
8 changes: 4 additions & 4 deletions packages/jui/src/Icon/useSvgIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function useSvgIcon(
const itemState = useContext(ItemStateContext);
const selected = itemState?.isSelected || itemState?.isContainerFocused;
useEffect(() => {
let unmounted = false;
let canceled = false;
const fetchIcon = async () => {
if (!path) {
console.error("icon path is empty");
Expand All @@ -35,13 +35,13 @@ export function useSvgIcon(
throw e;
})
.finally(() => {
if (ref?.current) {
if (ref?.current && !canceled) {
ref.current.ariaBusy = "false";
}
});
if (svg) {
const element = ref?.current;
if (!unmounted && element) {
if (!canceled && element) {
element.querySelector("svg")?.remove();
const svgElement = document.createElement("svg");
element.appendChild(svgElement);
Expand All @@ -53,7 +53,7 @@ export function useSvgIcon(
};
fetchIcon().catch(console.error);
return () => {
unmounted = true;
canceled = true;
};
}, [path, selected]);
}
Expand Down

0 comments on commit 214132c

Please sign in to comment.