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(dialog, modal, popover, sheet): restore focusTrapDisabled reactiveness #11586

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Changes from all 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
26 changes: 17 additions & 9 deletions packages/calcite-components/src/controllers/useFocusTrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import { createFocusTrapOptions } from "../utils/focusTrapComponent";
export interface UseFocusTrap {
/**
* Activates the focus trap.
*
* @see https://github.com/focus-trap/focus-trap#trapactivate
*/
activate: (options?: Parameters<FocusTrap["activate"]>[0]) => void;
activate: () => void;

/**
* Deactivates the focus trap.
*
* @see https://github.com/focus-trap/focus-trap#trapdeactivate
*/
deactivate: (options?: Parameters<FocusTrap["deactivate"]>[0]) => void;
deactivate: () => void;

/**
* By default, the host element will be used as the focus-trap element, but if the focus-trap element needs to be a different element, use this method prior to activating to set the focus-trap element.
Expand Down Expand Up @@ -110,13 +106,23 @@ export const useFocusTrap = <T extends FocusTrapComponent>(

controller.onConnected(() => {
if (component[options.triggerProp] && focusTrap) {
focusTrap.activate();
utils.activate();
}
});

controller.onDisconnected(() => focusTrap?.deactivate());
controller.onUpdate((changes) => {
if (component.hasUpdated && changes.has("focusTrapDisabled")) {
if (component.focusTrapDisabled) {
utils.deactivate();
} else {
utils.activate();
}
}
});

controller.onDisconnected(() => utils.deactivate());

return {
const utils: UseFocusTrap = {
activate: () => {
const targetEl = focusTrapEl || component.el;

Expand Down Expand Up @@ -158,5 +164,7 @@ export const useFocusTrap = <T extends FocusTrapComponent>(
return focusTrap?.updateContainerElements(effectiveContainers);
},
};

return utils;
});
};
Loading