diff --git a/oxlint.config.ts b/oxlint.config.ts index b8705a0d7..9d1c04464 100644 --- a/oxlint.config.ts +++ b/oxlint.config.ts @@ -18,6 +18,19 @@ export default defineConfig({ }, rules: { 'import/no-cycle': ['error', { maxDepth: 3 }], + 'no-restricted-imports': [ + 'error', + { + paths: [ + { + name: 'folds', + importNames: ['Overlay', 'PopOut', 'TooltipProvider'], + message: + 'Import Overlay/PopOut/TooltipProvider from $components/overlay-stack so they stack by open order.', + }, + ], + }, + ], 'import/no-unassigned-import': 'off', 'import/no-named-as-default': 'off', 'import/no-named-as-default-member': 'off', diff --git a/src/app/components/BackupRestore.tsx b/src/app/components/BackupRestore.tsx index 515277942..e1602c6e3 100644 --- a/src/app/components/BackupRestore.tsx +++ b/src/app/components/BackupRestore.tsx @@ -13,11 +13,11 @@ import { IconButton, Menu, percent, - PopOut, ProgressBar, Spinner, Text, } from 'folds'; +import { PopOut } from '$components/overlay-stack'; import FocusTrap from 'focus-trap-react'; import type { SecretStorageKeyContent } from '$types/matrix/accountData'; import { storePrivateKey } from '$client/secretStorageKeys'; diff --git a/src/app/components/BetaNoticeBadge.tsx b/src/app/components/BetaNoticeBadge.tsx index a6ba55e08..694257976 100644 --- a/src/app/components/BetaNoticeBadge.tsx +++ b/src/app/components/BetaNoticeBadge.tsx @@ -1,4 +1,5 @@ -import { TooltipProvider, Tooltip, Box, Text, Badge, toRem } from 'folds'; +import { Tooltip, Box, Text, Badge, toRem } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; export function BetaNoticeBadge() { return ( diff --git a/src/app/components/HexColorPickerPopOut.tsx b/src/app/components/HexColorPickerPopOut.tsx index 0b4b8c072..ee35665f2 100644 --- a/src/app/components/HexColorPickerPopOut.tsx +++ b/src/app/components/HexColorPickerPopOut.tsx @@ -1,6 +1,7 @@ import FocusTrap from 'focus-trap-react'; import type { RectCords } from 'folds'; -import { Box, Button, config, Menu, PopOut, Text } from 'folds'; +import { Box, Button, config, Menu, Text } from 'folds'; +import { PopOut } from '$components/overlay-stack'; import type { MouseEventHandler, ReactNode } from 'react'; import { useState } from 'react'; import { stopPropagation } from '$utils/keyboard'; diff --git a/src/app/components/ManualVerification.tsx b/src/app/components/ManualVerification.tsx index 9ced6cf2a..2f01ecbc2 100644 --- a/src/app/components/ManualVerification.tsx +++ b/src/app/components/ManualVerification.tsx @@ -1,7 +1,8 @@ import type { MouseEventHandler, ReactNode } from 'react'; import { useCallback, useState } from 'react'; import type { RectCords } from 'folds'; -import { Box, Text, Chip, PopOut, Menu, config, MenuItem, color } from 'folds'; +import { Box, Text, Chip, Menu, config, MenuItem, color } from 'folds'; +import { PopOut } from '$components/overlay-stack'; import { CaretDown, sizedIcon } from '$components/icons/phosphor'; import FocusTrap from 'focus-trap-react'; import type { SecretStorageKeyContent } from '$types/matrix/accountData'; diff --git a/src/app/components/MobileSwipeDownModal.tsx b/src/app/components/MobileSwipeDownModal.tsx index 80cc1fdc7..3e8364259 100644 --- a/src/app/components/MobileSwipeDownModal.tsx +++ b/src/app/components/MobileSwipeDownModal.tsx @@ -12,6 +12,7 @@ import { Box } from 'folds'; import FocusTrap from 'focus-trap-react'; import { useDrag } from '@use-gesture/react'; import * as css from '$features/room/message/styles.css'; +import { useOverlayLayer } from '$components/overlay-stack'; import { useDismissOnBack } from '$utils/androidBack'; import { stopPropagation } from '$utils/keyboard'; import { getMobileSheetTiming, useMobileSheetAnimation } from './mobileSheetAnimation'; @@ -28,7 +29,6 @@ interface MobileSwipeDownModalProps { sheetClassName?: string; sheetStyle?: CSSProperties; keyboardAware?: boolean; - zIndex?: number; overlayDragHandle?: boolean; } @@ -105,7 +105,6 @@ export function MobileSwipeDownModal({ sheetClassName, sheetStyle, keyboardAware = false, - zIndex, overlayDragHandle = false, }: MobileSwipeDownModalProps) { const sheetRef = useRef(null); @@ -115,6 +114,7 @@ export function MobileSwipeDownModal({ const [closing, setClosing] = useState(false); const closingRef = useRef(false); const { shouldReduceMotion } = useMobileSheetAnimation(); + const zIndex = useOverlayLayer(); // Keyboard and drag offsets share one transform; `bottom` would relayout the subtree. const applySheetOffset = useCallback( @@ -367,8 +367,8 @@ export function MobileSwipeDownModal({ }`} data-gestures="ignore" style={{ - ...(closing ? { opacity: 0, transition: 'opacity 100ms ease-out' } : undefined), zIndex, + ...(closing ? { opacity: 0, transition: 'opacity 100ms ease-out' } : undefined), }} onClick={handleBackdropClick} // Touch events deliberately propagate: the drag binds them on `document`, and @@ -387,6 +387,7 @@ export function MobileSwipeDownModal({ portalRef ? css.MessageMobileOptionsContainerContained : '' } ${animationCss.SheetEntrance}`} style={{ + zIndex, ...(overlayDragHandle ? { overflow: 'hidden' } : undefined), ...(shouldReduceMotion ? { animation: 'none' } : undefined), ...sheetStyle, diff --git a/src/app/components/Pdf-viewer/PdfViewer.tsx b/src/app/components/Pdf-viewer/PdfViewer.tsx index 7e5d64e96..c608098eb 100644 --- a/src/app/components/Pdf-viewer/PdfViewer.tsx +++ b/src/app/components/Pdf-viewer/PdfViewer.tsx @@ -10,13 +10,13 @@ import { IconButton, Input, Menu, - PopOut, Scroll, Spinner, Text, as, config, } from 'folds'; +import { PopOut } from '$components/overlay-stack'; import { ArrowLeft, CaretLeft, diff --git a/src/app/components/ResponsiveMenu.tsx b/src/app/components/ResponsiveMenu.tsx index 670df7af0..e9d682091 100644 --- a/src/app/components/ResponsiveMenu.tsx +++ b/src/app/components/ResponsiveMenu.tsx @@ -1,7 +1,8 @@ import type { ComponentProps, CSSProperties, ReactNode } from 'react'; import { useEffect, useState } from 'react'; import type { RectCords } from 'folds'; -import { Box, Overlay, OverlayBackdrop, OverlayCenter, PopOut } from 'folds'; +import { Box, OverlayBackdrop, OverlayCenter } from 'folds'; +import { Overlay, PopOut } from '$components/overlay-stack'; import FocusTrap from 'focus-trap-react'; import { useCompactLayout } from '$hooks/useScreenSize'; import { stopPropagation } from '$utils/keyboard'; diff --git a/src/app/components/UIAFlowOverlay.tsx b/src/app/components/UIAFlowOverlay.tsx index 8185d1e99..be037e4aa 100644 --- a/src/app/components/UIAFlowOverlay.tsx +++ b/src/app/components/UIAFlowOverlay.tsx @@ -1,15 +1,7 @@ import type { ReactNode } from 'react'; -import { - Overlay, - OverlayBackdrop, - Box, - config, - Text, - TooltipProvider, - Tooltip, - Chip, - IconButton, -} from 'folds'; +import { OverlayBackdrop, Box, config, Text, Tooltip, Chip, IconButton } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; +import { Overlay } from '$components/overlay-stack'; import { sizedIcon, X } from '$components/icons/phosphor'; import FocusTrap from 'focus-trap-react'; diff --git a/src/app/components/app-shell/AppShell.tsx b/src/app/components/app-shell/AppShell.tsx index 639959767..c6c4f70bf 100644 --- a/src/app/components/app-shell/AppShell.tsx +++ b/src/app/components/app-shell/AppShell.tsx @@ -15,6 +15,7 @@ import { WebUpdater } from '$pages/client/WebUpdater'; import { GlobalBannerRenderer } from '$components/global-banner/GlobalBannerRenderer'; import { Toast } from '$components/toast/Toast'; import { ConfirmHost } from '$components/confirm/ConfirmHost'; +import { OverlayStackProvider } from '$components/overlay-stack'; import type { ScreenSize } from '$hooks/useScreenSize'; import { ScreenSizeProvider } from '$hooks/useScreenSize'; import { isReactQueryDevtoolsEnabled } from '$pages/reactQueryDevtoolsGate'; @@ -44,23 +45,25 @@ export function AppShell({ children, queryClient, screenSize, jotaiStore }: AppS - - - - - {children} - - - {reactQueryDevtoolsEnabled && ( - - - - )} - - + + + + + + {children} + + + {reactQueryDevtoolsEnabled && ( + + + + )} + + + diff --git a/src/app/components/create-room/AdditionalCreatorInput.tsx b/src/app/components/create-room/AdditionalCreatorInput.tsx index c7b7710d3..eb0b8f382 100644 --- a/src/app/components/create-room/AdditionalCreatorInput.tsx +++ b/src/app/components/create-room/AdditionalCreatorInput.tsx @@ -1,18 +1,6 @@ import type { RectCords } from 'folds'; -import { - Box, - Button, - Chip, - config, - Input, - Line, - Menu, - MenuItem, - PopOut, - Scroll, - Text, - toRem, -} from 'folds'; +import { Box, Button, Chip, config, Input, Line, Menu, MenuItem, Scroll, Text, toRem } from 'folds'; +import { PopOut } from '$components/overlay-stack'; import { Plus, X, sizedIcon } from '$components/icons/phosphor'; import { isKeyHotkey } from 'is-hotkey'; import FocusTrap from 'focus-trap-react'; diff --git a/src/app/components/editor/MarkdownToolbar.tsx b/src/app/components/editor/MarkdownToolbar.tsx index 35df7733c..ac0b7ce56 100644 --- a/src/app/components/editor/MarkdownToolbar.tsx +++ b/src/app/components/editor/MarkdownToolbar.tsx @@ -1,19 +1,8 @@ import FocusTrap from 'focus-trap-react'; import type { RectCords } from 'folds'; -import { - Badge, - Box, - config, - IconButton, - Line, - Menu, - PopOut, - Scroll, - Text, - Tooltip, - TooltipProvider, - toRem, -} from 'folds'; +import { Badge, Box, config, IconButton, Line, Menu, Scroll, Text, Tooltip, toRem } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; +import { PopOut } from '$components/overlay-stack'; import type { MouseEventHandler, ReactNode } from 'react'; import { useEffect, useState } from 'react'; import { useSetting } from '$state/hooks/settings'; diff --git a/src/app/components/emoji-board/components/Sidebar.tsx b/src/app/components/emoji-board/components/Sidebar.tsx index fdc82307e..98a10b8c1 100644 --- a/src/app/components/emoji-board/components/Sidebar.tsx +++ b/src/app/components/emoji-board/components/Sidebar.tsx @@ -1,5 +1,6 @@ import type { ReactNode } from 'react'; -import { Box, Scroll, Line, as, TooltipProvider, Tooltip, Text, IconButton } from 'folds'; +import { Box, Scroll, Line, as, Tooltip, Text, IconButton } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import classNames from 'classnames'; import { Image as MediaImage } from '$components/media'; import { sizedIcon, Image as ImageIcon, type PhosphorIcon } from '$components/icons/phosphor'; diff --git a/src/app/components/invite-user-prompt/InviteUserPrompt.tsx b/src/app/components/invite-user-prompt/InviteUserPrompt.tsx index 60a248f74..2ea6aab26 100644 --- a/src/app/components/invite-user-prompt/InviteUserPrompt.tsx +++ b/src/app/components/invite-user-prompt/InviteUserPrompt.tsx @@ -1,7 +1,7 @@ import type { ChangeEventHandler, FormEventHandler, KeyboardEventHandler } from 'react'; import { useCallback, useMemo, useRef, useState } from 'react'; +import { Overlay } from '$components/overlay-stack'; import { - Overlay, OverlayBackdrop, OverlayCenter, Box, diff --git a/src/app/components/message/RenderBody.tsx b/src/app/components/message/RenderBody.tsx index 6a40953cb..36278c79e 100644 --- a/src/app/components/message/RenderBody.tsx +++ b/src/app/components/message/RenderBody.tsx @@ -6,7 +6,9 @@ import Linkify from 'linkify-react'; import type { Opts } from 'linkifyjs'; import { find } from 'linkifyjs'; import type { RectCords } from 'folds'; -import { PopOut, Text, Tooltip, TooltipProvider, toRem } from 'folds'; +import { Text, Tooltip, toRem } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; +import { PopOut } from '$components/overlay-stack'; import { sanitizeCustomHtml } from '$utils/sanitize'; import { highlightText, scaleSystemEmoji } from '$plugins/react-custom-html-parser'; import { useRoomAbbreviationsContext } from '$hooks/useRoomAbbreviations'; diff --git a/src/app/components/message/Time.tsx b/src/app/components/message/Time.tsx index 75b7aa422..07bffdca0 100644 --- a/src/app/components/message/Time.tsx +++ b/src/app/components/message/Time.tsx @@ -1,5 +1,6 @@ import type { ComponentProps } from 'react'; -import { Text, as, Tooltip, TooltipProvider } from 'folds'; +import { Text, as, Tooltip } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import { timeDayMonYear, timeHourMinute, diff --git a/src/app/components/message/content/FileContent.tsx b/src/app/components/message/content/FileContent.tsx index fe44951e5..edc38a00e 100644 --- a/src/app/components/message/content/FileContent.tsx +++ b/src/app/components/message/content/FileContent.tsx @@ -1,6 +1,7 @@ import type { ReactNode } from 'react'; import { useCallback, useState } from 'react'; -import { Box, Button, Modal, Spinner, Text, Tooltip, TooltipProvider, as } from 'folds'; +import { Box, Button, Modal, Spinner, Text, Tooltip, as } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import { ArrowRight, Download, sizedIcon, Warning } from '$components/icons/phosphor'; import type { EncryptedAttachmentInfo } from 'browser-encrypt-attachment'; import type { IFileInfo } from '$types/matrix/common'; diff --git a/src/app/components/message/content/ImageContent.tsx b/src/app/components/message/content/ImageContent.tsx index 3fb36e788..d90053c92 100644 --- a/src/app/components/message/content/ImageContent.tsx +++ b/src/app/components/message/content/ImageContent.tsx @@ -11,12 +11,12 @@ import { Spinner, Text, Tooltip, - TooltipProvider, as, color, config, toRem, } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import { Eye, EyeSlash, diff --git a/src/app/components/message/content/VideoContent.tsx b/src/app/components/message/content/VideoContent.tsx index 5fe60bb28..f257da815 100644 --- a/src/app/components/message/content/VideoContent.tsx +++ b/src/app/components/message/content/VideoContent.tsx @@ -10,10 +10,10 @@ import { Spinner, Text, Tooltip, - TooltipProvider, as, config, } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import { Eye, EyeSlash, menuIcon, sizedIcon, Play, Warning } from '$components/icons/phosphor'; import classNames from 'classnames'; import { isTauri } from '@tauri-apps/api/core'; diff --git a/src/app/components/message/modals/GlobalModalManager.tsx b/src/app/components/message/modals/GlobalModalManager.tsx index 6b8594dd0..9fd4b9add 100644 --- a/src/app/components/message/modals/GlobalModalManager.tsx +++ b/src/app/components/message/modals/GlobalModalManager.tsx @@ -1,5 +1,6 @@ import { useAtom } from 'jotai'; -import { Overlay, OverlayBackdrop, OverlayCenter, Box, Modal } from 'folds'; +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'; diff --git a/src/app/components/message/modals/Options.tsx b/src/app/components/message/modals/Options.tsx index 25def1c85..79e47e4cb 100644 --- a/src/app/components/message/modals/Options.tsx +++ b/src/app/components/message/modals/Options.tsx @@ -13,7 +13,8 @@ import { } from '$utils/room/relations'; import { MessageReportItem } from './MessageReport'; import type { RectCords } from 'folds'; -import { as, Box, config, IconButton, Line, Menu, MenuItem, PopOut, Text } from 'folds'; +import { as, Box, config, IconButton, Line, Menu, MenuItem, Text } from 'folds'; +import { PopOut } from '$components/overlay-stack'; import { useMatrixClient } from '$hooks/useMatrixClient'; import { ArrowBendUpLeftIcon, diff --git a/src/app/components/modal-overlay/ModalOverlay.tsx b/src/app/components/modal-overlay/ModalOverlay.tsx index 1cd72370e..eee0d38a1 100644 --- a/src/app/components/modal-overlay/ModalOverlay.tsx +++ b/src/app/components/modal-overlay/ModalOverlay.tsx @@ -1,7 +1,8 @@ import { useRef } from 'react'; import type { ComponentProps, MutableRefObject, ReactNode } from 'react'; import FocusTrap from 'focus-trap-react'; -import { Box, Modal, Overlay, OverlayBackdrop, OverlayCenter } from 'folds'; +import { Box, Modal, OverlayBackdrop, OverlayCenter } from 'folds'; +import { Overlay } from '$components/overlay-stack'; import { ScreenSize, useScreenSizeOptionally } from '$hooks/useScreenSize'; import { stopPropagation } from '$utils/keyboard'; import { useDismissOnBack } from '$utils/androidBack'; diff --git a/src/app/components/notification-banner/NotificationBanner.css.ts b/src/app/components/notification-banner/NotificationBanner.css.ts index 7d67c85dd..73320a121 100644 --- a/src/app/components/notification-banner/NotificationBanner.css.ts +++ b/src/app/components/notification-banner/NotificationBanner.css.ts @@ -1,5 +1,6 @@ import { keyframes, style } from '@vanilla-extract/css'; import { color, config, toRem } from 'folds'; +import { OVERLAY_LAYER_TOP } from '$components/overlay-stack/layers'; const slideIn = keyframes({ from: { @@ -34,7 +35,7 @@ export const BannerContainer = style({ top: 'var(--safe-area-inset-top, env(safe-area-inset-top, 0px))', left: 0, right: 0, - zIndex: 9999, + zIndex: OVERLAY_LAYER_TOP, display: 'flex', flexDirection: 'column', gap: config.space.S200, diff --git a/src/app/components/overlay-stack/OverlayStack.test.tsx b/src/app/components/overlay-stack/OverlayStack.test.tsx new file mode 100644 index 000000000..47617db6b --- /dev/null +++ b/src/app/components/overlay-stack/OverlayStack.test.tsx @@ -0,0 +1,81 @@ +import { useState } from 'react'; +import { act, render, screen } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; + +import { OVERLAY_LAYER_BASE } from './layers'; +import { OverlayStackProvider, useOverlayLayer } from './OverlayStack'; + +function Layer({ name, children }: { name: string; children?: React.ReactNode }) { + const zIndex = useOverlayLayer(); + + return ( +
+ {children} +
+ ); +} + +const layerOf = (name: string) => Number(screen.getByTestId(name).dataset.z); + +/** `toggled` sits before `steady` in the tree, so tree order and open order disagree. */ +function Host({ toggled, steady, initiallyShown }: ToggleHostProps) { + const [shown, setShown] = useState(initiallyShown); + + return ( + <> + + {shown && } + + + ); +} + +type ToggleHostProps = { toggled: string; steady: string; initiallyShown: boolean }; + +const toggle = () => act(() => screen.getByRole('button').click()); + +describe('useOverlayLayer', () => { + it('puts an overlay opened from inside another above it', () => { + render( + + + + + + ); + + expect(layerOf('inner')).toBeGreaterThan(layerOf('outer')); + }); + + it('orders by open order, not by tree position', () => { + render( + + + + ); + toggle(); + + expect(layerOf('second')).toBeGreaterThan(layerOf('first')); + }); + + it('frees the layer again when an overlay closes', () => { + render( + + + + ); + + const before = layerOf('kept'); + toggle(); + + expect(layerOf('kept')).toBeLessThan(before); + }); + + it('falls back to the base layer outside a provider', () => { + render(); + + expect(layerOf('lone')).toBe(OVERLAY_LAYER_BASE); + }); +}); diff --git a/src/app/components/overlay-stack/OverlayStack.tsx b/src/app/components/overlay-stack/OverlayStack.tsx new file mode 100644 index 000000000..5ba40c97e --- /dev/null +++ b/src/app/components/overlay-stack/OverlayStack.tsx @@ -0,0 +1,77 @@ +import { createContext, useContext, useEffect, useId, useMemo, useRef, useState } from 'react'; +import type { ReactNode } from 'react'; +import { OVERLAY_LAYER_BASE } from './layers'; + +type Claim = { id: string; seq: number }; + +type OverlayStackActions = { + nextSeq: () => number; + claim: (id: string, seq: number) => void; + release: (id: string) => void; +}; + +const EMPTY_CLAIMS: readonly Claim[] = []; + +// Claims are kept apart from the actions so that taking a layer cannot invalidate +// the effect that took it, which would release and re-claim on every change. +const OverlayStackActionsContext = createContext(null); +const OverlayStackClaimsContext = createContext(EMPTY_CLAIMS); + +export function OverlayStackProvider({ children }: { children: ReactNode }) { + const [claims, setClaims] = useState(EMPTY_CLAIMS); + const seqRef = useRef(0); + + const actions = useMemo( + () => ({ + nextSeq: () => { + seqRef.current += 1; + return seqRef.current; + }, + claim: (id, seq) => + setClaims((prev) => + prev.some((claim) => claim.id === id) + ? prev + : [...prev, { id, seq }].sort((a, b) => a.seq - b.seq) + ), + release: (id) => setClaims((prev) => prev.filter((claim) => claim.id !== id)), + }), + [] + ); + + return ( + + + {children} + + + ); +} + +/** + * Reserves a layer for as long as `active` holds, and returns the z-index that + * keeps it above every overlay opened before it. Stacking follows open order + * rather than component identity, so no overlay has to know the others exist. + */ +export function useOverlayLayer(active = true): number { + const id = useId(); + const actions = useContext(OverlayStackActionsContext); + const claims = useContext(OverlayStackClaimsContext); + const seqRef = useRef(null); + + // Taken during render, which runs a parent before its children, so an overlay + // opened from inside another sorts above it. Effects run child-first and would + // invert exactly that case. Dropping it while closed lets a reopen go back on top. + if (!active) seqRef.current = null; + else if (seqRef.current === null && actions) seqRef.current = actions.nextSeq(); + const seq = seqRef.current; + + useEffect(() => { + if (!actions || !active || seq === null) return undefined; + actions.claim(id, seq); + return () => actions.release(id); + }, [actions, active, id, seq]); + + // The claim lands in an effect, so the first paint has no place in the stack yet. + const claimed = claims.findIndex((claim) => claim.id === id); + return OVERLAY_LAYER_BASE + (claimed === -1 ? claims.length : claimed); +} diff --git a/src/app/components/overlay-stack/StackedOverlay.tsx b/src/app/components/overlay-stack/StackedOverlay.tsx new file mode 100644 index 000000000..6e0a0fec8 --- /dev/null +++ b/src/app/components/overlay-stack/StackedOverlay.tsx @@ -0,0 +1,33 @@ +// oxlint-disable no-restricted-imports -- this file is the wrappers the rule points at +import type { ComponentProps } from 'react'; +import { + Overlay as FoldsOverlay, + PopOut as FoldsPopOut, + TooltipProvider as FoldsTooltipProvider, +} from 'folds'; +import { useOverlayLayer } from './OverlayStack'; +import { OVERLAY_LAYER_TOP } from './layers'; + +type FoldsOverlayProps = ComponentProps; +type FoldsPopOutProps = ComponentProps; +type FoldsTooltipProviderProps = ComponentProps; + +// Folds paints every overlay on one shared layer, leaving portal order to decide +// which of two overlapping ones wins. These take a layer from the overlay stack. +export function Overlay({ style, ...props }: FoldsOverlayProps) { + const zIndex = useOverlayLayer(!!props.open); + + return ; +} + +export function PopOut({ style, ...props }: FoldsPopOutProps) { + const zIndex = useOverlayLayer(!!props.anchor); + + return ; +} + +// A trigger stays mounted whether or not its tooltip is showing, so claiming a +// layer per trigger would flood the stack. Tooltips ride above it instead. +export function TooltipProvider({ style, ...props }: FoldsTooltipProviderProps) { + return ; +} diff --git a/src/app/components/overlay-stack/index.ts b/src/app/components/overlay-stack/index.ts new file mode 100644 index 000000000..af574aea2 --- /dev/null +++ b/src/app/components/overlay-stack/index.ts @@ -0,0 +1,3 @@ +export * from './layers'; +export * from './OverlayStack'; +export * from './StackedOverlay'; diff --git a/src/app/components/overlay-stack/layers.ts b/src/app/components/overlay-stack/layers.ts new file mode 100644 index 000000000..aa55cb758 --- /dev/null +++ b/src/app/components/overlay-stack/layers.ts @@ -0,0 +1,5 @@ +/** Overlays stack from here upwards, clear of the fixed app chrome below 10000. */ +export const OVERLAY_LAYER_BASE = 10000; + +/** For chrome that must stay visible however deep the overlay stack gets. */ +export const OVERLAY_LAYER_TOP = 20000; diff --git a/src/app/components/presence/Presence.tsx b/src/app/components/presence/Presence.tsx index 560d8f86e..bb91a40d0 100644 --- a/src/app/components/presence/Presence.tsx +++ b/src/app/components/presence/Presence.tsx @@ -1,5 +1,6 @@ import type { ContainerColor, MainColor } from 'folds'; -import { as, Badge, Box, color, Text, Tooltip, TooltipProvider, toRem } from 'folds'; +import { as, Badge, Box, color, Text, Tooltip, toRem } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import type { ReactNode } from 'react'; import { useId } from 'react'; import { Presence, usePresenceLabel } from '$hooks/useUserPresence'; diff --git a/src/app/components/setting-menu-selector/SettingMenuSelector.tsx b/src/app/components/setting-menu-selector/SettingMenuSelector.tsx index bb4625b14..c723d1e9c 100644 --- a/src/app/components/setting-menu-selector/SettingMenuSelector.tsx +++ b/src/app/components/setting-menu-selector/SettingMenuSelector.tsx @@ -1,5 +1,5 @@ import type { RectCords } from 'folds'; -import type { PopOut } from 'folds'; +import type { PopOut } from '$components/overlay-stack'; import { Box, Button, config, Menu, MenuItem, Scroll, Spinner, Text, toRem } from 'folds'; import { CaretDown, sizedIcon } from '$components/icons/phosphor'; import { diff --git a/src/app/components/sidebar/SidebarItem.tsx b/src/app/components/sidebar/SidebarItem.tsx index 486b66181..0df058958 100644 --- a/src/app/components/sidebar/SidebarItem.tsx +++ b/src/app/components/sidebar/SidebarItem.tsx @@ -1,6 +1,7 @@ import classNames from 'classnames'; import type { Position } from 'folds'; -import { as, Avatar, Text, Tooltip, TooltipProvider, toRem } from 'folds'; +import { as, Avatar, Text, Tooltip, toRem } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import type { ComponentProps, ReactNode, RefCallback } from 'react'; import { isMobileOrTablet } from '$utils/platform'; import * as css from './Sidebar.css'; diff --git a/src/app/components/theme/ThemePreviewCard.tsx b/src/app/components/theme/ThemePreviewCard.tsx index e113f7f1d..dc06f6174 100644 --- a/src/app/components/theme/ThemePreviewCard.tsx +++ b/src/app/components/theme/ThemePreviewCard.tsx @@ -1,5 +1,6 @@ import { type ReactNode, useCallback, useMemo } from 'react'; -import { Box, Button, IconButton, Text, Tooltip, TooltipProvider, toRem, config } from 'folds'; +import { Box, Button, IconButton, Text, Tooltip, toRem, config } from 'folds'; +import { TooltipProvider } from '$components/overlay-stack'; import { Check, Download, Link, Star, Warning, sizedIcon } from '$components/icons/phosphor'; import { useTimeoutToggle } from '$hooks/useTimeoutToggle'; diff --git a/src/app/components/toast/Toast.tsx b/src/app/components/toast/Toast.tsx index 790362e07..364899ae0 100644 --- a/src/app/components/toast/Toast.tsx +++ b/src/app/components/toast/Toast.tsx @@ -3,6 +3,7 @@ import { Box, Text, color, config, toRem } from 'folds'; import { Check, sizedIcon } from '$components/icons/phosphor'; import { useToastMessage } from '$state/toast'; +import { OVERLAY_LAYER_TOP } from '$components/overlay-stack'; type ToastProps = { container: HTMLElement | null; @@ -22,7 +23,7 @@ export function Toast({ container }: ToastProps) { display: 'flex', justifyContent: 'center', pointerEvents: 'none', - zIndex: 9999, + zIndex: OVERLAY_LAYER_TOP, }} > { expect(systemBarShell).toContain('ref={onPortalContainerChange}'); }); - it('keeps mobile sheets above Folds overlays', () => { + it('layers mobile sheets from the overlay stack rather than a fixed z-index', () => { const messageStyles = readWorkspaceFile('src/app/features/room/message/styles.css.ts'); + const sheet = readWorkspaceFile('src/app/components/MobileSwipeDownModal.tsx'); - expect(messageStyles).toContain('const mobileSheetZIndex = `calc(${config.zIndex.Max} + 1)`;'); - expect(messageStyles.match(/zIndex: mobileSheetZIndex/g)).toHaveLength(2); + expect(messageStyles).not.toContain('mobileSheetZIndex'); + expect(sheet).toContain('const zIndex = useOverlayLayer();'); + expect(sheet.match(/zIndex,/g)).toHaveLength(2); }); it('uses the App shell as the only safe-area owner', () => {