Skip to content

Commit

Permalink
fix: popover positioning on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Dec 22, 2023
1 parent 0656e71 commit 292c96b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const PositionedPopoverContent = ({
hideOnClickInModal = false,
setAnimationElement,
containerClassName,
documentElement,
}: PopoverContentProps) => {
const [popoverElement, setPopoverElement] = useState<HTMLDivElement | null>(null)
const popoverRect = useAutoElementRect(popoverElement)
Expand All @@ -45,13 +46,13 @@ const PositionedPopoverContent = ({
y: anchorPoint?.y,
})
const anchorRect = anchorPoint ? anchorPointRect : anchorElementRect
const documentRect = useDocumentRect()
const _documentRect = useDocumentRect()
const isDesktopScreen = useMediaQuery(MediaQueryBreakpoints.md)

const styles = getPositionedPopoverStyles({
align,
anchorRect,
documentRect,
documentRect: documentElement?.getBoundingClientRect() ?? _documentRect,
popoverRect: popoverRect ?? popoverElement?.getBoundingClientRect(),
side,
disableMobileFullscreenTakeover,
Expand All @@ -73,7 +74,7 @@ const PositionedPopoverContent = ({
let adjustedStyles: PopoverCSSProperties | undefined = undefined

if (!portal && popoverElement && styles) {
adjustedStyles = getAdjustedStylesForNonPortalPopover(popoverElement, styles)
adjustedStyles = getAdjustedStylesForNonPortalPopover(popoverElement, styles, documentElement)
}

usePopoverCloseOnClickOutside({
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/javascripts/Components/Popover/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type CommonPopoverProps = {
offset?: number
hideOnClickInModal?: boolean
open: boolean
documentElement?: HTMLElement
}

export type PopoverContentProps = CommonPopoverProps & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ const ToolbarPlugin = () => {
toolbarStore,
])

const popoverDocumentElement = editor.getRootElement()?.parentElement ?? document.body

return (
<>
{modal}
Expand Down Expand Up @@ -758,6 +760,7 @@ const ToolbarPlugin = () => {
disableMobileFullscreenTakeover
disableFlip
portal={false}
documentElement={isMobile ? popoverDocumentElement : undefined}
>
<div className="mb-1.5 mt-1 px-3 text-sm font-semibold uppercase text-text">Table of Contents</div>
<LexicalTableOfContents>
Expand Down Expand Up @@ -813,6 +816,7 @@ const ToolbarPlugin = () => {
disableFlip
containerClassName="md:!min-w-60 md:!w-auto"
portal={false}
documentElement={isMobile ? popoverDocumentElement : undefined}
>
<Menu a11yLabel="Text formatting options" className="!px-0" onClick={() => setIsTextFormatMenuOpen(false)}>
<ToolbarMenuItem
Expand Down Expand Up @@ -853,6 +857,7 @@ const ToolbarPlugin = () => {
disableFlip
containerClassName="md:!min-w-60 md:!w-auto"
portal={false}
documentElement={isMobile ? popoverDocumentElement : undefined}
>
<Menu a11yLabel="Text style" className="!px-0" onClick={() => setIsTextStyleMenuOpen(false)}>
<ToolbarMenuItem
Expand Down Expand Up @@ -925,6 +930,7 @@ const ToolbarPlugin = () => {
disableFlip
containerClassName="md:!min-w-60 md:!w-auto"
portal={false}
documentElement={isMobile ? popoverDocumentElement : undefined}
>
<Menu a11yLabel="Alignment" className="!px-0" onClick={() => setIsAlignmentMenuOpen(false)}>
<ToolbarMenuItem
Expand Down Expand Up @@ -965,6 +971,7 @@ const ToolbarPlugin = () => {
disableFlip
containerClassName="md:!min-w-60 md:!w-auto"
portal={false}
documentElement={isMobile ? popoverDocumentElement : undefined}
>
<Menu a11yLabel="Insert" className="!px-0" onClick={() => setIsInsertMenuOpen(false)}>
<ToolbarMenuItem
Expand Down

0 comments on commit 292c96b

Please sign in to comment.