Skip to content

Commit

Permalink
fix(mobile): close menu when detail page navigate away (#8027)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Aug 31, 2024
1 parent 592997b commit df34e2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 11 additions & 5 deletions packages/frontend/component/src/ui/menu/mobile/root.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useI18n } from '@affine/i18n';
import { ArrowLeftSmallIcon } from '@blocksuite/icons/rc';
import { Slot } from '@radix-ui/react-slot';
import clsx from 'clsx';
Expand Down Expand Up @@ -48,10 +49,13 @@ export const MobileMenu = ({
[onPointerDownOutside, rootOptions]
);

const onItemClick = useCallback((e: any) => {
e.preventDefault();
setOpen(prev => !prev);
}, []);
const onItemClick = useCallback(
(e: any) => {
e.preventDefault();
onOpenChange(!open);
},
[onOpenChange, open]
);

// dynamic height for slider
useEffect(() => {
Expand All @@ -78,6 +82,8 @@ export const MobileMenu = ({
};
}, [activeIndex, finalOpen]);

const t = useI18n();

/**
* For cascading menu usage
* ```tsx
Expand Down Expand Up @@ -139,7 +145,7 @@ export const MobileMenu = ({
onClick={() => setSubMenus(prev => prev.slice(0, index))}
prefixStyle={{ width: 20, height: 20 }}
>
Back
{t['com.affine.backButton']()}
</Button>

{sub.items}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useFavorite } from '@affine/core/components/blocksuite/block-suite-head
import { IsFavoriteIcon } from '@affine/core/components/pure/icons';
import { track } from '@affine/core/mixpanel';
import { EditorService } from '@affine/core/modules/editor';
import { ViewService } from '@affine/core/modules/workbench/services/view';
import { EditorOutlinePanel } from '@affine/core/pages/workspace/detail-page/tabs/outline';
import { preventDefault } from '@affine/core/utils';
import { useI18n } from '@affine/i18n';
Expand All @@ -19,7 +20,7 @@ import {
TocIcon,
} from '@blocksuite/icons/rc';
import { useLiveData, useService } from '@toeverything/infra';
import { useCallback } from 'react';
import { useCallback, useEffect, useState } from 'react';

import * as styles from './page-header-more-button.css';
import { DocInfoSheet } from './sheets/doc-info';
Expand All @@ -34,6 +35,9 @@ export const PageHeaderMenuButton = ({ docId }: PageMenuProps) => {
const editorService = useService(EditorService);
const editorContainer = useLiveData(editorService.editor.editorContainer$);

const [open, setOpen] = useState(false);
const location = useLiveData(useService(ViewService).view.location$);

const isInTrash = useLiveData(
editorService.editor.doc.meta$.map(meta => meta.trash)
);
Expand All @@ -57,8 +61,14 @@ export const PageHeaderMenuButton = ({ docId }: PageMenuProps) => {
if (open) {
track.$.header.docOptions.open();
}
setOpen(open);
}, []);

useEffect(() => {
// when the location is changed, close the menu
handleMenuOpenChange(false);
}, [handleMenuOpenChange, location.pathname]);

const handleToggleFavorite = useCallback(() => {
track.$.header.docOptions.toggleFavorite();
toggleFavorite();
Expand Down Expand Up @@ -117,6 +127,7 @@ export const PageHeaderMenuButton = ({ docId }: PageMenuProps) => {
align: 'center',
}}
rootOptions={{
open,
onOpenChange: handleMenuOpenChange,
}}
>
Expand Down

0 comments on commit df34e2c

Please sign in to comment.