fix(component): prevent auto focus in mobile modal (#9247)

Continue [BS-2169](https://linear.app/affine-design/issue/BS-2169/新的-menu-不支持移动端)
This commit is contained in:
L-Sun
2024-12-23 09:56:58 +00:00
parent ba355d098d
commit 067469aa98
3 changed files with 17 additions and 6 deletions

View File

@@ -46,18 +46,18 @@ export const focusInteractive = style([
basicInteractive, basicInteractive,
{ {
selectors: { selectors: {
':not([data-mobile="true"]) &::before': { '&::before': {
opacity: 0, opacity: 0,
boxShadow: `0 0 0 2px ${cssVar('brandColor')}`, boxShadow: `0 0 0 2px ${cssVar('brandColor')}`,
}, },
':not([data-mobile="true"]) &::after': { '&::after': {
border: '1px solid transparent', border: '1px solid transparent',
}, },
':not([data-mobile="true"]) &:focus-visible::before': { '&:focus-visible::before': {
opacity: 0.5, opacity: 0.5,
}, },
':not([data-mobile="true"]) &:focus-visible::after': { '&:focus-visible::after': {
borderColor: cssVar('brandColor'), borderColor: cssVar('brandColor'),
}, },
}, },

View File

@@ -130,6 +130,7 @@ export const MobileMenu = ({
className: clsx(className, styles.mobileMenuModal), className: clsx(className, styles.mobileMenuModal),
...otherContentOptions, ...otherContentOptions,
}} }}
disableAutoFocus={true}
> >
<div <div
ref={setSliderElement} ref={setSliderElement}

View File

@@ -53,6 +53,7 @@ export interface ModalProps extends DialogProps {
* Whether to show the modal in full screen mode * Whether to show the modal in full screen mode
*/ */
fullScreen?: boolean; fullScreen?: boolean;
disableAutoFocus?: boolean;
} }
type PointerDownOutsideEvent = Parameters< type PointerDownOutsideEvent = Parameters<
Exclude<DialogContentProps['onPointerDownOutside'], undefined> Exclude<DialogContentProps['onPointerDownOutside'], undefined>
@@ -83,7 +84,7 @@ class ModalTransitionContainer extends HTMLElement {
this.requestTransition(); this.requestTransition();
return child; return child;
} else { } else {
// eslint-disable-next-line unicorn/prefer-dom-node-remove // oxlint-disable-next-line unicorn/prefer-dom-node-remove
return super.removeChild(child); return super.removeChild(child);
} }
} }
@@ -105,7 +106,7 @@ class ModalTransitionContainer extends HTMLElement {
}); });
startScopedViewTransition(styles.modalVTScope, () => { startScopedViewTransition(styles.modalVTScope, () => {
nodes.forEach(child => { nodes.forEach(child => {
// eslint-disable-next-line unicorn/prefer-dom-node-remove // oxlint-disable-next-line unicorn/prefer-dom-node-remove
super.removeChild(child); super.removeChild(child);
}); });
}); });
@@ -163,6 +164,7 @@ export const ModalInner = forwardRef<HTMLDivElement, ModalProps>(
contentWrapperStyle, contentWrapperStyle,
animation = BUILD_CONFIG.isMobileEdition ? 'slideBottom' : 'fadeScaleTop', animation = BUILD_CONFIG.isMobileEdition ? 'slideBottom' : 'fadeScaleTop',
fullScreen, fullScreen,
disableAutoFocus,
...otherProps ...otherProps
} = props; } = props;
const { className: closeButtonClassName, ...otherCloseButtonProps } = const { className: closeButtonClassName, ...otherCloseButtonProps } =
@@ -208,6 +210,13 @@ export const ModalInner = forwardRef<HTMLDivElement, ModalProps>(
[onEscapeKeyDown, persistent] [onEscapeKeyDown, persistent]
); );
const handleAutoFocus = useCallback(
(e: Event) => {
disableAutoFocus && e.preventDefault();
},
[disableAutoFocus]
);
if (!container) { if (!container) {
return; return;
} }
@@ -249,6 +258,7 @@ export const ModalInner = forwardRef<HTMLDivElement, ModalProps>(
onPointerDownOutside={handlePointerDownOutSide} onPointerDownOutside={handlePointerDownOutSide}
onEscapeKeyDown={handleEscapeKeyDown} onEscapeKeyDown={handleEscapeKeyDown}
className={clsx(styles.modalContent, contentClassName)} className={clsx(styles.modalContent, contentClassName)}
onOpenAutoFocus={handleAutoFocus}
style={{ style={{
...assignInlineVars({ ...assignInlineVars({
[styles.widthVar]: getVar( [styles.widthVar]: getVar(