chore: close currently open modals/popovers when sharing text into sn
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { mergeRefs } from '@/Hooks/mergeRefs'
|
import { mergeRefs } from '@/Hooks/mergeRefs'
|
||||||
import { Dialog, DialogOptions, useDialogStore } from '@ariakit/react'
|
import { Dialog, DialogOptions, useDialogStore } from '@ariakit/react'
|
||||||
import { ForwardedRef, forwardRef, ReactNode } from 'react'
|
import { ForwardedRef, forwardRef, ReactNode, useCallback } from 'react'
|
||||||
import { useModalAnimation } from '../Modal/useModalAnimation'
|
import { useModalAnimation } from '../Modal/useModalAnimation'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
@@ -10,6 +10,8 @@ type Props = {
|
|||||||
close: () => void
|
close: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DialogWithClose = HTMLDivElement & { close: () => void }
|
||||||
|
|
||||||
const ModalOverlay = forwardRef(
|
const ModalOverlay = forwardRef(
|
||||||
(
|
(
|
||||||
{ isOpen, children, animationVariant, close, ...props }: Props & Partial<DialogOptions>,
|
{ isOpen, children, animationVariant, close, ...props }: Props & Partial<DialogOptions>,
|
||||||
@@ -25,6 +27,15 @@ const ModalOverlay = forwardRef(
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const addCloseMethod = useCallback(
|
||||||
|
(element: HTMLDivElement | null) => {
|
||||||
|
if (element) {
|
||||||
|
;(element as DialogWithClose).close = close
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[close],
|
||||||
|
)
|
||||||
|
|
||||||
if (!isMounted) {
|
if (!isMounted) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -33,7 +44,7 @@ const ModalOverlay = forwardRef(
|
|||||||
<Dialog
|
<Dialog
|
||||||
tabIndex={0}
|
tabIndex={0}
|
||||||
className="fixed top-0 left-0 z-modal h-full w-full"
|
className="fixed top-0 left-0 z-modal h-full w-full"
|
||||||
ref={mergeRefs([setElement, ref])}
|
ref={mergeRefs([setElement, addCloseMethod, ref])}
|
||||||
store={dialog}
|
store={dialog}
|
||||||
modal={false}
|
modal={false}
|
||||||
portal={true}
|
portal={true}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { useDisableBodyScrollOnMobile } from '@/Hooks/useDisableBodyScrollOnMobile'
|
import { useDisableBodyScrollOnMobile } from '@/Hooks/useDisableBodyScrollOnMobile'
|
||||||
import { classNames } from '@standardnotes/snjs'
|
import { classNames } from '@standardnotes/snjs'
|
||||||
import { ReactNode } from 'react'
|
import { ReactNode, useCallback } from 'react'
|
||||||
import Portal from '../Portal/Portal'
|
import Portal from '../Portal/Portal'
|
||||||
import MobileModalAction from '../Modal/MobileModalAction'
|
import MobileModalAction from '../Modal/MobileModalAction'
|
||||||
import { useModalAnimation } from '../Modal/useModalAnimation'
|
import { useModalAnimation } from '../Modal/useModalAnimation'
|
||||||
import MobileModalHeader from '../Modal/MobileModalHeader'
|
import MobileModalHeader from '../Modal/MobileModalHeader'
|
||||||
|
import { mergeRefs } from '@/Hooks/mergeRefs'
|
||||||
|
|
||||||
const DisableScroll = () => {
|
const DisableScroll = () => {
|
||||||
useDisableBodyScrollOnMobile()
|
useDisableBodyScrollOnMobile()
|
||||||
@@ -12,6 +13,8 @@ const DisableScroll = () => {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PopoverWithClose = HTMLDivElement & { close: () => void }
|
||||||
|
|
||||||
const MobilePopoverContent = ({
|
const MobilePopoverContent = ({
|
||||||
open,
|
open,
|
||||||
requestClose,
|
requestClose,
|
||||||
@@ -29,6 +32,15 @@ const MobilePopoverContent = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const [isMounted, setPopoverElement] = useModalAnimation(open)
|
const [isMounted, setPopoverElement] = useModalAnimation(open)
|
||||||
|
|
||||||
|
const addCloseMethod = useCallback(
|
||||||
|
(element: HTMLDivElement | null) => {
|
||||||
|
if (element) {
|
||||||
|
;(element as PopoverWithClose).close = requestClose
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[requestClose],
|
||||||
|
)
|
||||||
|
|
||||||
if (!isMounted) {
|
if (!isMounted) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -37,7 +49,7 @@ const MobilePopoverContent = ({
|
|||||||
<Portal>
|
<Portal>
|
||||||
<DisableScroll />
|
<DisableScroll />
|
||||||
<div
|
<div
|
||||||
ref={setPopoverElement}
|
ref={mergeRefs([setPopoverElement, addCloseMethod])}
|
||||||
className="fixed top-0 left-0 z-modal flex h-full w-full flex-col bg-default pt-safe-top pb-safe-bottom"
|
className="fixed top-0 left-0 z-modal flex h-full w-full flex-col bg-default pt-safe-top pb-safe-bottom"
|
||||||
data-popover={id}
|
data-popover={id}
|
||||||
data-mobile-popover
|
data-mobile-popover
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import { Persistable } from './Abstract/Persistable'
|
|||||||
import { CrossControllerEvent } from './CrossControllerEvent'
|
import { CrossControllerEvent } from './CrossControllerEvent'
|
||||||
import { ItemListController } from './ItemList/ItemListController'
|
import { ItemListController } from './ItemList/ItemListController'
|
||||||
import { PaneLayout } from './PaneController/PaneLayout'
|
import { PaneLayout } from './PaneController/PaneLayout'
|
||||||
|
import { requestCloseAllOpenModalsAndPopovers } from '@/Utils/CloseOpenModalsAndPopovers'
|
||||||
|
|
||||||
export class SelectedItemsController
|
export class SelectedItemsController
|
||||||
extends AbstractViewController
|
extends AbstractViewController
|
||||||
@@ -252,6 +253,10 @@ export class SelectedItemsController
|
|||||||
if (!this.application.paneController.isInMobileView || userTriggered) {
|
if (!this.application.paneController.isInMobileView || userTriggered) {
|
||||||
void this.application.paneController.setPaneLayout(PaneLayout.Editing)
|
void this.application.paneController.setPaneLayout(PaneLayout.Editing)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.application.paneController.isInMobileView && userTriggered) {
|
||||||
|
requestCloseAllOpenModalsAndPopovers()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export const requestCloseAllOpenModalsAndPopovers = () => {
|
||||||
|
document.querySelectorAll('[role="dialog"], [data-popover]').forEach((element) => {
|
||||||
|
if ('close' in element && typeof element.close === 'function') {
|
||||||
|
element.close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user