2
0

🐛 Fix background image popover closing on variable select

Closes #1755
This commit is contained in:
Baptiste Arnaud
2024-09-02 12:07:44 +02:00
parent d51cf00f69
commit 64b3439521

View File

@ -4,11 +4,12 @@ import {
Flex, Flex,
Popover, Popover,
PopoverContent, PopoverContent,
PopoverTrigger,
Text, Text,
Image, Image,
Button, Button,
Portal, Portal,
PopoverAnchor,
useDisclosure,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { isNotEmpty } from '@typebot.io/lib' import { isNotEmpty } from '@typebot.io/lib'
import { Background } from '@typebot.io/schemas' import { Background } from '@typebot.io/schemas'
@ -20,6 +21,7 @@ import {
defaultBackgroundType, defaultBackgroundType,
} from '@typebot.io/schemas/features/typebot/theme/constants' } from '@typebot.io/schemas/features/typebot/theme/constants'
import { useTranslate } from '@tolgee/react' import { useTranslate } from '@tolgee/react'
import { useOutsideClick } from '@/hooks/useOutsideClick'
type BackgroundContentProps = { type BackgroundContentProps = {
background?: Background background?: Background
@ -30,35 +32,51 @@ export const BackgroundContent = ({
background, background,
onBackgroundContentChange, onBackgroundContentChange,
}: BackgroundContentProps) => { }: BackgroundContentProps) => {
const { isOpen, onClose, onOpen } = useDisclosure()
const { t } = useTranslate() const { t } = useTranslate()
const { typebot } = useTypebot() const { typebot } = useTypebot()
const handleContentChange = (content: string) => const handleContentChange = (content: string) =>
onBackgroundContentChange(content) onBackgroundContentChange(content)
const popoverContainerRef = React.useRef<HTMLDivElement>(null)
useOutsideClick({
ref: popoverContainerRef,
handler: onClose,
isEnabled: isOpen,
})
if ((background?.type ?? defaultBackgroundType) === BackgroundType.IMAGE) { if ((background?.type ?? defaultBackgroundType) === BackgroundType.IMAGE) {
if (!typebot) return null if (!typebot) return null
return ( return (
<Popover isLazy placement="top"> <Flex ref={popoverContainerRef}>
<PopoverTrigger> <Popover isLazy isOpen={isOpen} placement="top">
<PopoverAnchor>
{isNotEmpty(background?.content) ? ( {isNotEmpty(background?.content) ? (
<Image <Image
src={background?.content} src={background?.content}
alt={t('theme.sideMenu.global.background.image.alt')} alt={t('theme.sideMenu.global.background.image.alt')}
onClick={onOpen}
cursor="pointer" cursor="pointer"
_hover={{ filter: 'brightness(.9)' }} _hover={{ filter: 'brightness(.9)' }}
transition="filter 200ms" transition="filter 200ms"
rounded="md" rounded="md"
w="full"
maxH="200px" maxH="200px"
objectFit="cover" objectFit="cover"
/> />
) : ( ) : (
<Button> <Button onClick={onOpen} w="full">
{t('theme.sideMenu.global.background.image.button')} {t('theme.sideMenu.global.background.image.button')}
</Button> </Button>
)} )}
</PopoverTrigger> </PopoverAnchor>
<Portal> <Portal>
<PopoverContent p="4" w="500px"> <PopoverContent
p="4"
w="500px"
onMouseDown={(e) => e.stopPropagation()}
onPointerDown={(e) => e.stopPropagation()}
>
<ImageUploadContent <ImageUploadContent
uploadFileProps={{ uploadFileProps={{
workspaceId: typebot.workspaceId, workspaceId: typebot.workspaceId,
@ -72,6 +90,7 @@ export const BackgroundContent = ({
</PopoverContent> </PopoverContent>
</Portal> </Portal>
</Popover> </Popover>
</Flex>
) )
} }
if ((background?.type ?? defaultBackgroundType) === BackgroundType.COLOR) { if ((background?.type ?? defaultBackgroundType) === BackgroundType.COLOR) {