Skip to content

Commit 083f40f

Browse files
committed
fix: align menu
1 parent 71117b4 commit 083f40f

2 files changed

Lines changed: 41 additions & 84 deletions

File tree

src/components/editor/components/toolbar/selection-toolbar/actions/Align.tsx

Lines changed: 40 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Button from '@mui/material/Button';
2-
import { PopoverProps } from '@mui/material/Popover';
31
import { cloneElement, useCallback, useEffect, useRef, useState } from 'react';
42
import { useTranslation } from 'react-i18next';
53
import { Element } from 'slate';
@@ -12,28 +10,12 @@ import { AlignType, BlockData } from '@/application/types';
1210
import { ReactComponent as AlignCenterSvg } from '@/assets/icons/align_center.svg';
1311
import { ReactComponent as AlignLeftSvg } from '@/assets/icons/align_left.svg';
1412
import { ReactComponent as AlignRightSvg } from '@/assets/icons/align_right.svg';
15-
import { Popover } from '@/components/_shared/popover';
13+
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
1614
import { useSelectionToolbarContext } from '@/components/editor/components/toolbar/selection-toolbar/SelectionToolbar.hooks';
1715

1816
import { useKeyboardNavigation } from '../hooks/useKeyboardNavigation';
19-
2017
import ActionButton from './ActionButton';
21-
22-
const popoverProps: Partial<PopoverProps> = {
23-
anchorOrigin: {
24-
vertical: 'bottom',
25-
horizontal: 'center',
26-
},
27-
transformOrigin: {
28-
vertical: -8,
29-
horizontal: 'center',
30-
},
31-
slotProps: {
32-
paper: {
33-
className: 'bg-[var(--surface-primary)] rounded-[6px]',
34-
},
35-
},
36-
};
18+
import { MenuButton } from './MenuButton';
3719

3820
// Define allowed translation keys for align options
3921
const alignLabelKeys = [
@@ -67,7 +49,7 @@ export function Align({ blockId, enabled = true }: { blockId?: string; enabled?:
6749
const ref = useRef<HTMLButtonElement | null>(null);
6850
const { t } = useTranslation();
6951
const editor = useSlateStatic() as YjsEditor;
70-
const { rePosition } = useSelectionToolbarContext();
52+
const { rePosition, forceShow } = useSelectionToolbarContext();
7153

7254
const getNode = useCallback(() => {
7355
let node: Element;
@@ -93,18 +75,20 @@ export function Align({ blockId, enabled = true }: { blockId?: string; enabled?:
9375

9476
const handleClose = useCallback(() => {
9577
setOpen(false);
96-
}, []);
78+
forceShow(false);
79+
}, [forceShow]);
9780

9881
const handleOpen = useCallback(() => {
9982
setOpen(true);
100-
}, []);
83+
forceShow(true);
84+
}, [forceShow]);
10185

10286
const activeIcon = useCallback(() => {
10387
const align = getAlign();
10488
const option = alignOptions.find(opt => opt.type === align) || alignOptions[0];
10589

10690
return cloneElement(option.icon, {
107-
className: `h-5 w-5 ${align ? 'text-fill-default' : ''}`
91+
className: 'h-5 w-5'
10892
});
10993
}, [getAlign]);
11094

@@ -142,65 +126,38 @@ export function Align({ blockId, enabled = true }: { blockId?: string; enabled?:
142126

143127
return (
144128
<>
145-
<ActionButton
146-
ref={ref}
147-
onClick={(e) => {
148-
e.preventDefault();
149-
e.stopPropagation();
150-
handleOpen();
151-
}}
152-
tooltip={t('document.plugins.optionAction.align')}
153-
>
154-
{activeIcon()}
155-
</ActionButton>
156-
157-
<Popover
158-
keepMounted={false}
159-
disableAutoFocus={true}
160-
disableEnforceFocus={true}
161-
disableRestoreFocus={true}
162-
onClose={handleClose}
163-
open={open && enabled}
164-
anchorEl={ref.current}
165-
{...popoverProps}
166-
>
167-
<div className="flex flex-col w-[200px] rounded-[12px]" style={{ padding: 'var(--spacing-spacing-m)' }}>
168-
{alignOptions.map((option, index) => (
169-
<Button
170-
key={option.labelKey}
171-
{...getButtonProps(index)}
172-
startIcon={option.icon}
173-
color="inherit"
174-
onClick={() => {
175-
toggleAlign(option.type)();
176-
setOpen(false);
177-
}}
178-
className="text-foreground"
179-
disableRipple
180-
sx={{
181-
'.MuiButton-startIcon': {
182-
margin: 0,
183-
marginRight: 'var(--spacing-spacing-m)'
184-
},
185-
padding: '0 var(--spacing-spacing-m)',
186-
height: '32px',
187-
minHeight: '32px',
188-
borderRadius: '8px',
189-
justifyContent: 'flex-start',
190-
textAlign: 'left',
191-
fontWeight: 400,
192-
...(getAlign() === option.type && {
193-
backgroundColor: 'var(--fill-list-active)'
194-
}),
195-
...(selectedIndex === index && {
196-
backgroundColor: 'var(--fill-list-hover)'
197-
})
198-
}}
199-
>
200-
{t(option.labelKey)}
201-
</Button>
202-
))}
203-
</div>
129+
<Popover open={open && enabled} onOpenChange={setOpen}>
130+
<PopoverTrigger asChild>
131+
<ActionButton
132+
ref={ref}
133+
onClick={(e) => {
134+
e.preventDefault();
135+
e.stopPropagation();
136+
handleOpen();
137+
}}
138+
tooltip={t('document.plugins.optionAction.align')}
139+
>
140+
{activeIcon()}
141+
</ActionButton>
142+
</PopoverTrigger>
143+
<PopoverContent className="w-[200px] p-2 bg-surface-primary rounded-[6px]">
144+
<div className="flex flex-col rounded-[12px]">
145+
{alignOptions.map((option, index) => (
146+
<MenuButton
147+
key={option.labelKey}
148+
icon={option.icon}
149+
label={t(option.labelKey)}
150+
isActive={getAlign() === option.type}
151+
onClick={() => {
152+
toggleAlign(option.type)();
153+
handleClose();
154+
}}
155+
selected={selectedIndex === index}
156+
buttonProps={getButtonProps(index)}
157+
/>
158+
))}
159+
</div>
160+
</PopoverContent>
204161
</Popover>
205162
</>
206163
);

src/components/editor/components/toolbar/selection-toolbar/actions/MenuButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function MenuButton({ icon, label, isActive, onClick, selected, buttonPro
2727
`}
2828
onClick={onClick}
2929
>
30-
<span className="mr-2">{icon}</span>
30+
<span className="mr-1">{icon}</span>
3131
{label}
3232
{isActive && (
3333
<span className="ml-auto flex items-center">

0 commit comments

Comments
 (0)