Skip to content

Commit

Permalink
fix(component): prevent auto focus in mobile modal (#9247)
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Sun committed Dec 23, 2024
1 parent ba355d0 commit 067469a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ export const focusInteractive = style([
basicInteractive,
{
selectors: {
':not([data-mobile="true"]) &::before': {
'&::before': {
opacity: 0,
boxShadow: `0 0 0 2px ${cssVar('brandColor')}`,
},
':not([data-mobile="true"]) &::after': {
'&::after': {
border: '1px solid transparent',
},

':not([data-mobile="true"]) &:focus-visible::before': {
'&:focus-visible::before': {
opacity: 0.5,
},
':not([data-mobile="true"]) &:focus-visible::after': {
'&:focus-visible::after': {
borderColor: cssVar('brandColor'),
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/component/src/ui/menu/mobile/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const MobileMenu = ({
className: clsx(className, styles.mobileMenuModal),
...otherContentOptions,
}}
disableAutoFocus={true}
>
<div
ref={setSliderElement}
Expand Down
14 changes: 12 additions & 2 deletions packages/frontend/component/src/ui/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface ModalProps extends DialogProps {
* Whether to show the modal in full screen mode
*/
fullScreen?: boolean;
disableAutoFocus?: boolean;
}
type PointerDownOutsideEvent = Parameters<
Exclude<DialogContentProps['onPointerDownOutside'], undefined>
Expand Down Expand Up @@ -83,7 +84,7 @@ class ModalTransitionContainer extends HTMLElement {
this.requestTransition();
return child;
} else {
// eslint-disable-next-line unicorn/prefer-dom-node-remove
// oxlint-disable-next-line unicorn/prefer-dom-node-remove
return super.removeChild(child);
}
}
Expand All @@ -105,7 +106,7 @@ class ModalTransitionContainer extends HTMLElement {
});
startScopedViewTransition(styles.modalVTScope, () => {
nodes.forEach(child => {
// eslint-disable-next-line unicorn/prefer-dom-node-remove
// oxlint-disable-next-line unicorn/prefer-dom-node-remove
super.removeChild(child);
});
});
Expand Down Expand Up @@ -163,6 +164,7 @@ export const ModalInner = forwardRef<HTMLDivElement, ModalProps>(
contentWrapperStyle,
animation = BUILD_CONFIG.isMobileEdition ? 'slideBottom' : 'fadeScaleTop',
fullScreen,
disableAutoFocus,
...otherProps
} = props;
const { className: closeButtonClassName, ...otherCloseButtonProps } =
Expand Down Expand Up @@ -208,6 +210,13 @@ export const ModalInner = forwardRef<HTMLDivElement, ModalProps>(
[onEscapeKeyDown, persistent]
);

const handleAutoFocus = useCallback(
(e: Event) => {
disableAutoFocus && e.preventDefault();
},
[disableAutoFocus]
);

if (!container) {
return;
}
Expand Down Expand Up @@ -249,6 +258,7 @@ export const ModalInner = forwardRef<HTMLDivElement, ModalProps>(
onPointerDownOutside={handlePointerDownOutSide}
onEscapeKeyDown={handleEscapeKeyDown}
className={clsx(styles.modalContent, contentClassName)}
onOpenAutoFocus={handleAutoFocus}
style={{
...assignInlineVars({
[styles.widthVar]: getVar(
Expand Down

0 comments on commit 067469a

Please sign in to comment.