From c1fefb5b8af51bbb20edff04ed191ef069e6b5ea Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 23 Aug 2026 20:12:46 +0200 Subject: [PATCH] fix(mobile): keep the reaction picker in place when the keyboard opens --- .../components/MobileSwipeDownModal.test.tsx | 48 ++++++++++ src/app/components/MobileSwipeDownModal.tsx | 2 +- src/app/components/ResponsiveMenu.tsx | 9 +- .../modals/GlobalModalManager.test.tsx | 82 +++++++++++++++++ .../message/modals/GlobalModalManager.tsx | 48 +++++++--- .../message/modals/MessageReactionPicker.tsx | 60 ++++++++++++ .../message/modals/MessageReproxyPicker.tsx | 50 ++++++++++ src/app/components/message/modals/Options.tsx | 70 +++++++------- .../overlay-stack/anchorContract.test.ts | 26 ++++++ src/app/features/room/RoomInput.tsx | 1 - src/app/features/room/message/Message.tsx | 1 + src/app/features/room/message/styles.css.ts | 24 ----- .../room/persona-picker/PersonaPicker.tsx | 3 + src/app/state/modal.test.ts | 92 +++++++++++++++++++ src/app/state/modal.ts | 29 +++++- 15 files changed, 469 insertions(+), 76 deletions(-) create mode 100644 src/app/components/message/modals/GlobalModalManager.test.tsx create mode 100644 src/app/components/message/modals/MessageReactionPicker.tsx create mode 100644 src/app/components/message/modals/MessageReproxyPicker.tsx create mode 100644 src/app/components/overlay-stack/anchorContract.test.ts create mode 100644 src/app/state/modal.test.ts diff --git a/src/app/components/MobileSwipeDownModal.test.tsx b/src/app/components/MobileSwipeDownModal.test.tsx index 32ae25282..5704313f4 100644 --- a/src/app/components/MobileSwipeDownModal.test.tsx +++ b/src/app/components/MobileSwipeDownModal.test.tsx @@ -214,6 +214,54 @@ describe('MobileSwipeDownModal', () => { } }); + it('lifts clear of the keyboard without being asked to', () => { + const viewport = mockVisualViewport(800); + + try { + setInnerHeight(800); + render( + void>()} sheetClassName="default-sheet"> + {() =>
} + + ); + + const panel = screen.getByTestId('default-content').closest('.default-sheet') as HTMLElement; + + viewport.resizeTo(500); + + expect(panel.style.transform).toBe('translate3d(0, -300px, 0)'); + } finally { + viewport.restore(); + } + }); + + it('stays put for a sheet that opts out', () => { + const viewport = mockVisualViewport(800); + + try { + setInnerHeight(800); + render( + void>()} + keyboardAware={false} + sheetClassName="opted-out-sheet" + > + {() =>
} + + ); + + const panel = screen + .getByTestId('opted-out-content') + .closest('.opted-out-sheet') as HTMLElement; + + viewport.resizeTo(500); + + expect(panel.style.transform).toBe(''); + } finally { + viewport.restore(); + } + }); + it('ignores the transient where only the visual viewport has shrunk', () => { const viewport = mockVisualViewport(807); diff --git a/src/app/components/MobileSwipeDownModal.tsx b/src/app/components/MobileSwipeDownModal.tsx index 76e13dd54..f98f26203 100644 --- a/src/app/components/MobileSwipeDownModal.tsx +++ b/src/app/components/MobileSwipeDownModal.tsx @@ -104,7 +104,7 @@ export function MobileSwipeDownModal({ skipReturnFocusRef, sheetClassName, sheetStyle, - keyboardAware = false, + keyboardAware = true, overlayDragHandle = false, }: MobileSwipeDownModalProps) { const sheetRef = useRef(null); diff --git a/src/app/components/ResponsiveMenu.tsx b/src/app/components/ResponsiveMenu.tsx index bec66edc4..97e825899 100644 --- a/src/app/components/ResponsiveMenu.tsx +++ b/src/app/components/ResponsiveMenu.tsx @@ -16,6 +16,7 @@ type FocusTrapOptions = NonNullable['focusTrapO type ResponsiveMenuProps = { anchor: RectCords | undefined; + open?: boolean; requestClose: () => void; menu: ReactNode; /** The element the menu hangs off on desktop. */ @@ -135,6 +136,7 @@ function InlineMenuDialog({ */ export function ResponsiveMenu({ anchor, + open, requestClose, menu, children, @@ -149,6 +151,7 @@ export function ResponsiveMenu({ overlayDragHandle = false, }: ResponsiveMenuProps) { const isMobile = useCompactLayout(); + const isOpen = open ?? !!anchor; const isKeyForward = (evt: KeyboardEvent) => evt.key === 'ArrowDown' || (arrowNavigation === 'both' && evt.key === 'ArrowRight'); @@ -172,7 +175,7 @@ export function ResponsiveMenu({ return ( <> {children} - {anchor && ( + {isOpen && anchor && ( {children} - {anchor && mobile === 'dialog' && ( + {isOpen && mobile === 'dialog' && ( {menu} )} - {anchor && mobile === 'sheet' && ( + {isOpen && mobile === 'sheet' && ( ({ + MobileOptionsInternal: () =>
, +})); +vi.mock('./MessageReactionPicker', () => ({ + MessageReactionPickerInternal: () =>
, +})); +vi.mock('./MessageReproxyPicker', () => ({ + MessageReproxyPickerInternal: () =>
, +})); +vi.mock('./MessageReport', () => ({ MessageReportInternal: () =>
})); +vi.mock('./MessageDelete', () => ({ MessageDeleteInternal: () =>
})); +vi.mock('./MessageEditHistory', () => ({ MessageEditHistoryInternal: () =>
})); +vi.mock('./MessageSource', () => ({ MessageSourceInternal: () =>
})); +vi.mock('./MessageForward', () => ({ MessageForwardInternal: () =>
})); +vi.mock('./MessageReactions', () => ({ MessageAllReactionInternal: () =>
})); +vi.mock('./MessageReadRecipts', () => ({ MessageReadReceiptInternal: () =>
})); + +const mEvent = {} as MatrixEvent; +const room = {} as Room; + +const openOptions = (store: ReturnType) => + store.set(modalAtom, { + type: ModalType.MobileOptions, + options: { mEvent, room, closeMenu: vi.fn<() => void>(), onReplyClick: vi.fn<() => void>() }, + }); + +const renderWith = (store: ReturnType) => + render( + + + + ); + +describe('GlobalModalManager', () => { + it('renders the options sheet on its own', () => { + const store = createStore(); + openOptions(store); + + renderWith(store); + + expect(screen.getByTestId('options-sheet')).toBeTruthy(); + }); + + it('unmounts the options sheet while the reaction picker is over it', () => { + const store = createStore(); + openOptions(store); + store.set(pushModalAtom, { + type: ModalType.ReactionPicker, + mEvent, + closeMenu: vi.fn<() => void>(), + }); + + renderWith(store); + + expect(screen.getByTestId('reaction-picker')).toBeTruthy(); + expect(screen.queryByTestId('options-sheet')).toBeNull(); + }); + + it('unmounts the options sheet while the persona picker is over it', () => { + const store = createStore(); + openOptions(store); + store.set(pushModalAtom, { + type: ModalType.ReproxyPicker, + room, + mEvent, + closeMenu: vi.fn<() => void>(), + }); + + renderWith(store); + + expect(screen.getByTestId('reproxy-picker')).toBeTruthy(); + expect(screen.queryByTestId('options-sheet')).toBeNull(); + }); +}); diff --git a/src/app/components/message/modals/GlobalModalManager.tsx b/src/app/components/message/modals/GlobalModalManager.tsx index 11df2aaad..fb46232c9 100644 --- a/src/app/components/message/modals/GlobalModalManager.tsx +++ b/src/app/components/message/modals/GlobalModalManager.tsx @@ -1,11 +1,10 @@ -import { useCallback } from 'react'; -import { useAtom } from 'jotai'; +import { useAtomValue, useSetAtom } from 'jotai'; import { OverlayBackdrop, OverlayCenter, Box, Modal } from 'folds'; import { Overlay } from '$components/overlay-stack'; import FocusTrap from 'focus-trap-react'; import { stopPropagation } from '$utils/keyboard'; import { useDismissOnBack } from '$utils/androidBack'; -import { modalAtom, ModalType } from '$state/modal'; +import { modalAtom, ModalType, popModalAtom } from '$state/modal'; import { MessageReportInternal } from './MessageReport'; import { MessageDeleteInternal } from './MessageDelete'; import { MessageEditHistoryInternal } from './MessageEditHistory'; @@ -14,23 +13,44 @@ import { MessageForwardInternal } from './MessageForward'; import { MessageAllReactionInternal } from './MessageReactions'; import { MessageReadReceiptInternal } from './MessageReadRecipts'; import { MobileOptionsInternal } from './Options'; +import { MessageReactionPickerInternal } from './MessageReactionPicker'; +import { MessageReproxyPickerInternal } from './MessageReproxyPicker'; + +const OWNS_BACK_HANDLER: ReadonlySet = new Set([ + ModalType.Forward, + ModalType.MobileOptions, + ModalType.ReactionPicker, + ModalType.ReproxyPicker, +]); export function GlobalModalManager() { - const [modal, setModal] = useAtom(modalAtom); + const modal = useAtomValue(modalAtom); + const close = useSetAtom(popModalAtom); - // stable so modals memoizing on it don't rebuild on every room view render - const close = useCallback(() => { - setModal(null); - }, [setModal]); - - // Forward and MobileOptions render their own back handlers via their children. - useDismissOnBack( - close, - !!modal && modal.type !== ModalType.Forward && modal.type !== ModalType.MobileOptions - ); + useDismissOnBack(close, !!modal && !OWNS_BACK_HANDLER.has(modal.type)); if (!modal) return null; + if (modal.type === ModalType.ReactionPicker) { + return ( + + ); + } + if (modal.type === ModalType.ReproxyPicker) { + return ( + + ); + } + if (modal.type === ModalType.Forward) { return ; } diff --git a/src/app/components/message/modals/MessageReactionPicker.tsx b/src/app/components/message/modals/MessageReactionPicker.tsx new file mode 100644 index 000000000..e72d33d38 --- /dev/null +++ b/src/app/components/message/modals/MessageReactionPicker.tsx @@ -0,0 +1,60 @@ +import { useRef } from 'react'; +import { useSetAtom } from 'jotai'; +import type { MatrixEvent, Room } from '$types/matrix-sdk'; +import { EmojiBoard } from '$components/emoji-board'; +import { MobileSwipeDownModal } from '$components/MobileSwipeDownModal'; +import { modalAtom, popModalAtom } from '$state/modal'; +import * as messageCss from '$features/room/message/styles.css'; + +type MessageReactionPickerInternalProps = { + mEvent: MatrixEvent; + imagePackRooms?: Room[]; + onReactionToggle?: (targetEventId: string, key: string, shortcode?: string) => void; + closeMenu: () => void; +}; + +export function MessageReactionPickerInternal({ + mEvent, + imagePackRooms, + onReactionToggle, + closeMenu, +}: MessageReactionPickerInternalProps) { + const setModal = useSetAtom(modalAtom); + const popModal = useSetAtom(popModalAtom); + const reactedRef = useRef(false); + + const requestClose = () => { + if (!reactedRef.current) { + popModal(); + return; + } + setModal(null); + closeMenu(); + }; + + const react = (key: string, shortcode?: string) => { + reactedRef.current = true; + onReactionToggle?.(mEvent.getId() ?? '', key, shortcode); + }; + + return ( + + {() => ( + react(key)} + onCustomEmojiSelect={(mxc, shortcode) => react(mxc, shortcode)} + /> + )} + + ); +} diff --git a/src/app/components/message/modals/MessageReproxyPicker.tsx b/src/app/components/message/modals/MessageReproxyPicker.tsx new file mode 100644 index 000000000..b2a4646dc --- /dev/null +++ b/src/app/components/message/modals/MessageReproxyPicker.tsx @@ -0,0 +1,50 @@ +import { useRef } from 'react'; +import { useSetAtom } from 'jotai'; +import type { MatrixEvent, Room, RoomMessageEventContent } from '$types/matrix-sdk'; +import { useMatrixClient } from '$hooks/useMatrixClient'; +import { TemporaryPersonaPicker } from '$features/room/persona-picker/PersonaPicker'; +import type { PerMessageProfileMsc4461 } from '$hooks/usePerMessageProfile'; +import { buildReplacementPmpContent } from '$features/room/buildReplacementContent'; +import { modalAtom, popModalAtom } from '$state/modal'; + +type MessageReproxyPickerInternalProps = { + room: Room; + mEvent: MatrixEvent; + closeMenu: () => void; +}; + +export function MessageReproxyPickerInternal({ + room, + mEvent, + closeMenu, +}: MessageReproxyPickerInternalProps) { + const mx = useMatrixClient(); + const setModal = useSetAtom(modalAtom); + const popModal = useSetAtom(popModalAtom); + const reproxiedRef = useRef(false); + + const requestClose = () => { + if (!reproxiedRef.current) { + popModal(); + return; + } + setModal(null); + closeMenu(); + }; + + const reproxyMessage = async (profile: PerMessageProfileMsc4461 | undefined) => { + reproxiedRef.current = true; + const content = buildReplacementPmpContent(mEvent.getContent(), mEvent.getId()!, profile); + await mx.sendMessage(room.roomId, content as RoomMessageEventContent); + requestClose(); + }; + + return ( + + ); +} diff --git a/src/app/components/message/modals/Options.tsx b/src/app/components/message/modals/Options.tsx index 7756026c4..86c8890e2 100644 --- a/src/app/components/message/modals/Options.tsx +++ b/src/app/components/message/modals/Options.tsx @@ -46,7 +46,7 @@ import { useCallback, useEffect, useLayoutEffect, useState } from 'react'; import { MessageDeleteItem } from './MessageDelete'; import FocusTrap from 'focus-trap-react'; import { stopPropagation } from '$utils/keyboard'; -import { modalAtom, ModalType } from '$state/modal'; +import { modalAtom, ModalType, pushModalAtom } from '$state/modal'; import { copyToClipboard } from '$utils/dom'; import { getMatrixToRoomEvent } from '$plugins/matrix-to'; import { getViaServers } from '$plugins/via-servers'; @@ -343,8 +343,6 @@ type OptionEmojiMenuProps = { emojiBoardAnchor?: RectCords; imagePackRooms?: Room[]; isQuickOptions?: boolean; - isModal?: boolean; - ActualMessage?: ReactNode; }; function OptionsEmojiBoard({ mEvent, @@ -354,8 +352,6 @@ function OptionsEmojiBoard({ emojiBoardAnchor, imagePackRooms, isQuickOptions, - isModal, - ActualMessage, }: OptionEmojiMenuProps) { const position = (!isQuickOptions && 'Left') || @@ -367,15 +363,12 @@ function OptionsEmojiBoard({ align={isQuickOptions ? 'End' : 'Start'} offset={undefined} anchor={emojiBoardAnchor} - style={isModal ? { width: '100%' } : {}} content={ - - {ActualMessage} + { onReactionToggle?.(mEvent.getId() ?? '', key); setEmojiBoardAnchor?.(undefined); @@ -576,6 +569,14 @@ export function OptionQuickMenu({ ); } +const triggerRect = (evt: Parameters>[0]): RectCords => + evt.currentTarget.parentElement?.parentElement?.getBoundingClientRect() ?? { + x: 0, + y: 0, + width: 0, + height: 0, + }; + export type OptionMenuProps = { mEvent: MatrixEvent; room: Room; @@ -603,6 +604,7 @@ export type OptionMenuProps = { setIsEmoji?: Dispatch>; ActualMessage?: ReactNode; isModal?: boolean; + closeMessageMenu?: () => void; }; function OptionMenu({ @@ -624,10 +626,12 @@ function OptionMenu({ ActualMessage, isModal, isGif, + closeMessageMenu, }: OptionMenuProps) { const mobileSheetClose = useMobileSheetClose(); const closeMenu = mobileSheetClose ?? requestClose; const setModal = useSetAtom(modalAtom); + const pushModal = useSetAtom(pushModalAtom); const store = useStore(); const mx = useMatrixClient(); const isThreadedMessage = isThreadRelationEvent(mEvent, mEvent.threadRootId); @@ -640,6 +644,8 @@ function OptionMenu({ const isEdited = !!edits?.length; const [showPersonaSetting] = useSetting(settingsAtom, 'showPersonaSetting'); + const closeAfterHandOff = closeMessageMenu ?? requestClose; + const onTotalClose = () => { setModal(null); closeMenu(); @@ -655,28 +661,30 @@ function OptionMenu({ const [reproxyPickerAnchor, setReproxyPickerAnchor] = useState(); const handleOpenReproxyPicker: MouseEventHandler = (evt) => { - const target = isModal - ? { x: 0, y: innerHeight, width: 0, height: 0 } - : (evt.currentTarget.parentElement?.parentElement?.getBoundingClientRect() ?? { - x: 0, - y: 0, - width: 0, - height: 0, - }); - setReproxyPickerAnchor(target); + if (isModal) { + pushModal({ + type: ModalType.ReproxyPicker, + room, + mEvent, + closeMenu: closeAfterHandOff, + }); + return; + } + setReproxyPickerAnchor(triggerRect(evt)); }; const handleOpenEmojiBoard: MouseEventHandler = (evt) => { - // THIS MAGIC NUMBER SHOULD BE FIXED WHEN SOMEONE FIGURES OUT WHY THE LACK OF IT CREATES A GAP IN THE EMOJIBOARD - const target = isModal - ? { x: 0, y: innerHeight + 10, width: 0, height: 0 } - : (evt.currentTarget.parentElement?.parentElement?.getBoundingClientRect() ?? { - x: 0, - y: 0, - width: 0, - height: 0, - }); - setEmojiBoardAnchor?.(target); + if (isModal) { + pushModal({ + type: ModalType.ReactionPicker, + mEvent, + imagePackRooms, + onReactionToggle, + closeMenu: closeAfterHandOff, + }); + return; + } + setEmojiBoardAnchor?.(triggerRect(evt)); setIsEmoji?.(true); }; @@ -690,8 +698,6 @@ function OptionMenu({ setEmojiBoardAnchor={setEmojiBoardAnchor} emojiBoardAnchor={emojiBoardAnchor} imagePackRooms={imagePackRooms} - isModal={isModal} - ActualMessage={} /> )} {reproxyPickerAnchor !== undefined && ( @@ -719,7 +725,7 @@ function OptionMenu({ }} > - {ActualMessage && !emojiBoardAnchor && ( + {ActualMessage && ( <> @@ -942,6 +948,8 @@ export function MobileOptionsInternal({ options }: { options: OptionMenuProps }) canPinEvent={options.canPinEvent} canDelete={options.canDelete} setIsEmoji={options.setIsEmoji} + imagePackRooms={options.imagePackRooms} + closeMessageMenu={options.closeMenu} ActualMessage={options.ActualMessage} canSendReaction={options.canSendReaction} isModal diff --git a/src/app/components/overlay-stack/anchorContract.test.ts b/src/app/components/overlay-stack/anchorContract.test.ts new file mode 100644 index 000000000..67ae221c0 --- /dev/null +++ b/src/app/components/overlay-stack/anchorContract.test.ts @@ -0,0 +1,26 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { describe, expect, it } from 'vitest'; + +const srcDir = path.resolve(__dirname, '../../..'); + +function sourceFiles(dir: string): string[] { + return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { + const full = path.join(dir, entry.name); + if (entry.isDirectory()) return sourceFiles(full); + if (!/\.tsx?$/.test(entry.name) || entry.name.includes('.test.')) return []; + return [full]; + }); +} + +const VIEWPORT_DERIVED_RECT = /\by:\s*(window\.)?innerHeight/; + +describe('popout anchor contract', () => { + it('builds every anchor from a measured element', () => { + const offenders = sourceFiles(srcDir).filter((file) => + VIEWPORT_DERIVED_RECT.test(fs.readFileSync(file, 'utf8')) + ); + + expect(offenders.map((file) => path.relative(srcDir, file))).toEqual([]); + }); +}); diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 97b27b28d..594df1da2 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -2457,7 +2457,6 @@ export const RoomInput = forwardRef( focusTrap dialogLabel="Emoji picker" sheetClassName={messageCss.MessageMobileOptionsContainerPicker} - keyboardAware skipReturnFocusRef={emojiBoardSkipReturnFocusRef} > {() => emojiBoard} diff --git a/src/app/features/room/message/Message.tsx b/src/app/features/room/message/Message.tsx index e1e62ee27..d976bd4e5 100644 --- a/src/app/features/room/message/Message.tsx +++ b/src/app/features/room/message/Message.tsx @@ -887,6 +887,7 @@ function MessageInternal( canPinEvent: canPinEvent, canDelete: canDelete, setIsEmoji: setIsEmoji, + imagePackRooms: imagePackRooms, ActualMessage: (
void | Promise; requestClose: () => void; anchor?: RectCords; + open?: boolean; }; export function useProfiles( @@ -501,6 +502,7 @@ export function TemporaryPersonaPicker({ mx, onPersonaSelect, anchor, + open, requestClose, }: TemporaryPersonaPickerProps) { const [AddPersonaMenuAnchor, setAddPersonaMenuAnchor] = useState(anchor); @@ -529,6 +531,7 @@ export function TemporaryPersonaPicker({ return ( = { + type: ModalType.MobileOptions, + options: { mEvent, room, closeMenu: vi.fn<() => void>(), onReplyClick: vi.fn<() => void>() }, +}; + +const picker: Extract = { + type: ModalType.ReactionPicker, + mEvent, + closeMenu: vi.fn<() => void>(), +}; + +const reactions: Extract = { + type: ModalType.Reactions, + room, + relations: {} as never, +}; + +describe('modalAtom', () => { + it('shows nothing until a modal opens', () => { + expect(createStore().get(modalAtom)).toBeNull(); + }); + + it('replaces rather than stacks when set directly', () => { + const store = createStore(); + + store.set(modalAtom, options); + store.set(modalAtom, reactions); + expect(store.get(modalAtom)).toBe(reactions); + + store.set(popModalAtom); + expect(store.get(modalAtom)).toBeNull(); + }); + + it('closes everything when set to null', () => { + const store = createStore(); + + store.set(modalAtom, options); + store.set(pushModalAtom, picker); + store.set(modalAtom, null); + + expect(store.get(modalAtom)).toBeNull(); + }); +}); + +describe('pushModalAtom', () => { + it('shows only the pushed modal, so the one beneath cannot render behind it', () => { + const store = createStore(); + + store.set(modalAtom, options); + store.set(pushModalAtom, picker); + + expect(store.get(modalAtom)).toBe(picker); + }); + + it('returns to the modal underneath when popped', () => { + const store = createStore(); + + store.set(modalAtom, options); + store.set(pushModalAtom, picker); + store.set(popModalAtom); + + expect(store.get(modalAtom)).toBe(options); + }); +}); + +describe('popModalAtom', () => { + it('closes a modal that opened on its own', () => { + const store = createStore(); + + store.set(modalAtom, reactions); + store.set(popModalAtom); + + expect(store.get(modalAtom)).toBeNull(); + }); + + it('does nothing when no modal is open', () => { + const store = createStore(); + + store.set(popModalAtom); + + expect(store.get(modalAtom)).toBeNull(); + }); +}); diff --git a/src/app/state/modal.ts b/src/app/state/modal.ts index e6399a686..ea4586e1f 100644 --- a/src/app/state/modal.ts +++ b/src/app/state/modal.ts @@ -4,6 +4,8 @@ import type { OptionMenuProps } from '$components/message/modals/Options'; export enum ModalType { MobileOptions = 'mobile_options', + ReactionPicker = 'reaction_picker', + ReproxyPicker = 'reproxy_picker', Delete = 'delete', // for forwarding a message to another room, not to be confused with the "share" action which is for sharing a message to another app Forward = 'forward', @@ -14,8 +16,16 @@ export enum ModalType { ReadReceipts = 'read_receipts', } -type ModalState = +export type ModalState = | { type: ModalType.MobileOptions; options: OptionMenuProps } + | { + type: ModalType.ReactionPicker; + mEvent: MatrixEvent; + imagePackRooms?: Room[]; + onReactionToggle?: (targetEventId: string, key: string, shortcode?: string) => void; + closeMenu: () => void; + } + | { type: ModalType.ReproxyPicker; room: Room; mEvent: MatrixEvent; closeMenu: () => void } | { type: ModalType.Delete; room: Room; mEvent: MatrixEvent } | { type: ModalType.Forward; room: Room; mEvent: MatrixEvent } | { type: ModalType.Report; room: Room; mEvent: MatrixEvent } @@ -25,4 +35,19 @@ type ModalState = | { type: ModalType.ReadReceipts; room: Room; eventId: string } | null; -export const modalAtom = atom(null); +const modalStackAtom = atom[]>([]); + +export const modalAtom = atom( + (get) => get(modalStackAtom).at(-1) ?? null, + (_get, set, next: ModalState) => { + set(modalStackAtom, next ? [next] : []); + } +); + +export const pushModalAtom = atom(null, (get, set, next: NonNullable) => { + set(modalStackAtom, [...get(modalStackAtom), next]); +}); + +export const popModalAtom = atom(null, (get, set) => { + set(modalStackAtom, get(modalStackAtom).slice(0, -1)); +});