feat: support closing modal via back button
This commit is contained in:
@@ -51,6 +51,24 @@ const Content = memo(
|
||||
const imageInfos = event.tags
|
||||
.map((tag) => extractImageInfoFromTag(tag))
|
||||
.filter(Boolean) as TImageInfo[]
|
||||
const allImages = nodes
|
||||
.map((node) => {
|
||||
if (node.type === 'image') {
|
||||
const imageInfo = imageInfos.find((image) => image.url === node.data)
|
||||
return imageInfo ?? { url: node.data }
|
||||
}
|
||||
if (node.type === 'images') {
|
||||
const urls = Array.isArray(node.data) ? node.data : [node.data]
|
||||
return urls.map((url) => {
|
||||
const imageInfo = imageInfos.find((image) => image.url === url)
|
||||
return imageInfo ?? { url }
|
||||
})
|
||||
}
|
||||
return null
|
||||
})
|
||||
.filter(Boolean)
|
||||
.flat() as TImageInfo[]
|
||||
let imageIndex = 0
|
||||
|
||||
const emojiInfos = extractEmojiInfosFromTags(event.tags)
|
||||
|
||||
@@ -65,17 +83,18 @@ const Content = memo(
|
||||
return node.data
|
||||
}
|
||||
if (node.type === 'image' || node.type === 'images') {
|
||||
const imageUrls = Array.isArray(node.data) ? node.data : [node.data]
|
||||
const images = imageUrls.map(
|
||||
(url) => imageInfos.find((image) => image.url === url) ?? { url }
|
||||
)
|
||||
const start = imageIndex
|
||||
const end = imageIndex + (Array.isArray(node.data) ? node.data.length : 1)
|
||||
imageIndex = end
|
||||
return (
|
||||
<ImageGallery
|
||||
className={`${size === 'small' ? 'mt-1' : 'mt-2'}`}
|
||||
key={index}
|
||||
images={images}
|
||||
images={allImages}
|
||||
isNsfw={isNsfwEvent(event)}
|
||||
size={size}
|
||||
start={start}
|
||||
end={end}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { randomString } from '@/lib/random'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { useScreenSize } from '@/providers/ScreenSizeProvider'
|
||||
import modalManager from '@/services/modal-manager.service'
|
||||
import { TImageInfo } from '@/types'
|
||||
import { ReactNode, useState } from 'react'
|
||||
import { ReactNode, useEffect, useMemo, useState } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import Lightbox from 'yet-another-react-lightbox'
|
||||
import Zoom from 'yet-another-react-lightbox/plugins/zoom'
|
||||
@@ -12,24 +14,39 @@ export default function ImageGallery({
|
||||
className,
|
||||
images,
|
||||
isNsfw = false,
|
||||
size = 'normal'
|
||||
size = 'normal',
|
||||
start = 0,
|
||||
end = images.length
|
||||
}: {
|
||||
className?: string
|
||||
images: TImageInfo[]
|
||||
isNsfw?: boolean
|
||||
size?: 'normal' | 'small'
|
||||
start?: number
|
||||
end?: number
|
||||
}) {
|
||||
const id = useMemo(() => `image-gallery-${randomString()}`, [])
|
||||
const { isSmallScreen } = useScreenSize()
|
||||
const [index, setIndex] = useState(-1)
|
||||
useEffect(() => {
|
||||
if (index >= 0) {
|
||||
modalManager.register(id, () => {
|
||||
setIndex(-1)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [index])
|
||||
|
||||
const handlePhotoClick = (event: React.MouseEvent, current: number) => {
|
||||
event.stopPropagation()
|
||||
event.preventDefault()
|
||||
setIndex(current)
|
||||
setIndex(start + current)
|
||||
}
|
||||
|
||||
const displayImages = images.slice(start, end)
|
||||
let imageContent: ReactNode | null = null
|
||||
if (images.length === 1) {
|
||||
if (displayImages.length === 1) {
|
||||
imageContent = (
|
||||
<Image
|
||||
key={0}
|
||||
@@ -37,14 +54,14 @@ export default function ImageGallery({
|
||||
classNames={{
|
||||
errorPlaceholder: cn('aspect-square', size === 'small' ? 'h-[15vh]' : 'h-[30vh]')
|
||||
}}
|
||||
image={images[0]}
|
||||
image={displayImages[0]}
|
||||
onClick={(e) => handlePhotoClick(e, 0)}
|
||||
/>
|
||||
)
|
||||
} else if (size === 'small') {
|
||||
imageContent = (
|
||||
<div className="grid grid-cols-4 gap-2">
|
||||
{images.map((image, i) => (
|
||||
{displayImages.map((image, i) => (
|
||||
<Image
|
||||
key={i}
|
||||
className={cn('aspect-square w-full rounded-lg')}
|
||||
@@ -54,10 +71,10 @@ export default function ImageGallery({
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
} else if (isSmallScreen && (images.length === 2 || images.length === 4)) {
|
||||
} else if (isSmallScreen && (displayImages.length === 2 || displayImages.length === 4)) {
|
||||
imageContent = (
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
{images.map((image, i) => (
|
||||
{displayImages.map((image, i) => (
|
||||
<Image
|
||||
key={i}
|
||||
className={cn('aspect-square w-full rounded-lg')}
|
||||
@@ -70,7 +87,7 @@ export default function ImageGallery({
|
||||
} else {
|
||||
imageContent = (
|
||||
<div className="grid grid-cols-3 gap-2 w-full">
|
||||
{images.map((image, i) => (
|
||||
{displayImages.map((image, i) => (
|
||||
<Image
|
||||
key={i}
|
||||
className={cn('aspect-square w-full rounded-lg')}
|
||||
@@ -83,13 +100,19 @@ export default function ImageGallery({
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn('relative', images.length === 1 ? 'w-fit max-w-full' : 'w-full', className)}>
|
||||
<div
|
||||
className={cn(
|
||||
'relative',
|
||||
displayImages.length === 1 ? 'w-fit max-w-full' : 'w-full',
|
||||
className
|
||||
)}
|
||||
>
|
||||
{imageContent}
|
||||
{index >= 0 &&
|
||||
createPortal(
|
||||
<div onClick={(e) => e.stopPropagation()}>
|
||||
<Lightbox
|
||||
index={index}
|
||||
index={start + index}
|
||||
slides={images.map(({ url }) => ({ src: url }))}
|
||||
plugins={[Zoom]}
|
||||
open={index >= 0}
|
||||
|
||||
@@ -76,7 +76,6 @@ export default function NoteOptions({ event, className }: { event: Event; classN
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setIsDrawerOpen(false)
|
||||
setIsRawEventDialogOpen(true)
|
||||
}}
|
||||
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogOverlay,
|
||||
DialogTitle
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
@@ -99,8 +98,7 @@ export default function ZapDialog({
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogOverlay onClick={() => setOpen(false)} />
|
||||
<DialogContent hideOverlay onOpenAutoFocus={(e) => e.preventDefault()}>
|
||||
<DialogContent onOpenAutoFocus={(e) => e.preventDefault()}>
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex gap-2 items-center">
|
||||
<div className="shrink-0">{t('Zap to')}</div>
|
||||
|
||||
@@ -1,10 +1,49 @@
|
||||
import * as React from 'react'
|
||||
import * as DialogPrimitive from '@radix-ui/react-dialog'
|
||||
import { X } from 'lucide-react'
|
||||
import * as React from 'react'
|
||||
|
||||
import { randomString } from '@/lib/random'
|
||||
import { cn } from '@/lib/utils'
|
||||
import modalManager from '@/services/modal-manager.service'
|
||||
|
||||
const Dialog = DialogPrimitive.Root
|
||||
const Dialog = ({ children, open, onOpenChange, ...props }: DialogPrimitive.DialogProps) => {
|
||||
const [innerOpen, setInnerOpen] = React.useState(open ?? false)
|
||||
const id = React.useMemo(() => `dialog-${randomString()}`, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
modalManager.register(id, () => {
|
||||
onOpenChange?.(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [open])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open !== undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
if (innerOpen) {
|
||||
modalManager.register(id, () => {
|
||||
setInnerOpen(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [innerOpen])
|
||||
|
||||
return (
|
||||
<DialogPrimitive.Root
|
||||
open={open ?? innerOpen}
|
||||
onOpenChange={onOpenChange ?? setInnerOpen}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</DialogPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
const DialogTrigger = DialogPrimitive.Trigger
|
||||
|
||||
@@ -31,11 +70,10 @@ const DialogContent = React.forwardRef<
|
||||
React.ElementRef<typeof DialogPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
|
||||
withoutClose?: boolean
|
||||
hideOverlay?: boolean
|
||||
}
|
||||
>(({ className, children, withoutClose, hideOverlay, ...props }, ref) => (
|
||||
>(({ className, children, withoutClose, ...props }, ref) => (
|
||||
<DialogPortal>
|
||||
{!hideOverlay && <DialogOverlay />}
|
||||
<DialogOverlay />
|
||||
<DialogPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(
|
||||
@@ -95,13 +133,13 @@ DialogDescription.displayName = DialogPrimitive.Description.displayName
|
||||
|
||||
export {
|
||||
Dialog,
|
||||
DialogPortal,
|
||||
DialogOverlay,
|
||||
DialogTrigger,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogOverlay,
|
||||
DialogPortal,
|
||||
DialogTitle,
|
||||
DialogDescription
|
||||
DialogTrigger
|
||||
}
|
||||
|
||||
@@ -1,14 +1,52 @@
|
||||
import * as React from 'react'
|
||||
import { Drawer as DrawerPrimitive } from 'vaul'
|
||||
|
||||
import { randomString } from '@/lib/random'
|
||||
import { cn } from '@/lib/utils'
|
||||
import modalManager from '@/services/modal-manager.service'
|
||||
|
||||
const Drawer = ({
|
||||
shouldScaleBackground = true,
|
||||
open,
|
||||
onOpenChange,
|
||||
...props
|
||||
}: React.ComponentProps<typeof DrawerPrimitive.Root>) => (
|
||||
<DrawerPrimitive.Root shouldScaleBackground={shouldScaleBackground} {...props} />
|
||||
)
|
||||
}: React.ComponentProps<typeof DrawerPrimitive.Root>) => {
|
||||
const [innerOpen, setInnerOpen] = React.useState(open ?? false)
|
||||
const id = React.useMemo(() => `drawer-${randomString()}`, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
modalManager.register(id, () => {
|
||||
onOpenChange?.(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [open])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open !== undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
if (innerOpen) {
|
||||
modalManager.register(id, () => {
|
||||
setInnerOpen(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [innerOpen])
|
||||
|
||||
return (
|
||||
<DrawerPrimitive.Root
|
||||
shouldScaleBackground={shouldScaleBackground}
|
||||
open={open ?? innerOpen}
|
||||
onOpenChange={onOpenChange ?? setInnerOpen}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Drawer.displayName = 'Drawer'
|
||||
|
||||
const DrawerTrigger = DrawerPrimitive.Trigger
|
||||
@@ -90,13 +128,13 @@ DrawerDescription.displayName = DrawerPrimitive.Description.displayName
|
||||
|
||||
export {
|
||||
Drawer,
|
||||
DrawerPortal,
|
||||
DrawerOverlay,
|
||||
DrawerTrigger,
|
||||
DrawerClose,
|
||||
DrawerContent,
|
||||
DrawerHeader,
|
||||
DrawerDescription,
|
||||
DrawerFooter,
|
||||
DrawerHeader,
|
||||
DrawerOverlay,
|
||||
DrawerPortal,
|
||||
DrawerTitle,
|
||||
DrawerDescription
|
||||
DrawerTrigger
|
||||
}
|
||||
|
||||
@@ -3,9 +3,48 @@ import * as SheetPrimitive from '@radix-ui/react-dialog'
|
||||
import { cva, type VariantProps } from 'class-variance-authority'
|
||||
import { X } from 'lucide-react'
|
||||
|
||||
import { randomString } from '@/lib/random'
|
||||
import { cn } from '@/lib/utils'
|
||||
import modalManager from '@/services/modal-manager.service'
|
||||
|
||||
const Sheet = SheetPrimitive.Root
|
||||
const Sheet = ({ children, open, onOpenChange, ...props }: SheetPrimitive.DialogProps) => {
|
||||
const [innerOpen, setInnerOpen] = React.useState(open ?? false)
|
||||
const id = React.useMemo(() => `sheet-${randomString()}`, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
modalManager.register(id, () => {
|
||||
onOpenChange?.(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [open])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open !== undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
if (innerOpen) {
|
||||
modalManager.register(id, () => {
|
||||
setInnerOpen(false)
|
||||
})
|
||||
} else {
|
||||
modalManager.unregister(id)
|
||||
}
|
||||
}, [innerOpen])
|
||||
|
||||
return (
|
||||
<SheetPrimitive.Root
|
||||
open={open ?? innerOpen}
|
||||
onOpenChange={onOpenChange ?? setInnerOpen}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</SheetPrimitive.Root>
|
||||
)
|
||||
}
|
||||
|
||||
const SheetTrigger = SheetPrimitive.Trigger
|
||||
|
||||
|
||||
Reference in New Issue
Block a user