Skip to content

Commit

Permalink
feat(flow): ✨ Edge menu on click
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 2, 2022
1 parent 5a06bb0 commit 3c67837
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
59 changes: 50 additions & 9 deletions apps/builder/components/shared/Graph/Edges/Edge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
getSourceEndpointId,
} from 'services/graph'
import { Edge as EdgeProps } from 'models'
import { Portal, useDisclosure } from '@chakra-ui/react'
import { useTypebot } from 'contexts/TypebotContext'
import { EdgeMenu } from './EdgeMenu'

export type AnchorsPositionProps = {
sourcePosition: Coordinates
Expand All @@ -16,14 +19,19 @@ export type AnchorsPositionProps = {
}

export const Edge = ({ edge }: { edge: EdgeProps }) => {
const { deleteEdge } = useTypebot()
const {
previewingEdge,
sourceEndpoints,
targetEndpoints,
blocksCoordinates,
graphOffsetY,
} = useGraph()
const isPreviewing = previewingEdge?.id === edge.id
const [isMouseOver, setIsMouseOver] = useState(false)
const { isOpen, onOpen, onClose } = useDisclosure()
const [edgeMenuPosition, setEdgeMenuPosition] = useState({ x: 0, y: 0 })

const isPreviewing = isMouseOver || previewingEdge?.id === edge.id

const sourceBlockCoordinates =
blocksCoordinates && blocksCoordinates[edge.from.blockId]
Expand Down Expand Up @@ -74,14 +82,47 @@ export const Edge = ({ edge }: { edge: EdgeProps }) => {
sourceTop,
])

const handleMouseEnter = () => setIsMouseOver(true)

const handleMouseLeave = () => setIsMouseOver(false)

const handleEdgeClick = (e: React.MouseEvent) => {
setEdgeMenuPosition({ x: e.clientX, y: e.clientY })
onOpen()
}

const handleDeleteEdge = () => deleteEdge(edge.id)

return (
<path
data-testid="edge"
d={path}
stroke={isPreviewing ? '#1a5fff' : '#718096'}
strokeWidth="2px"
markerEnd={isPreviewing ? 'url(#blue-arrow)' : 'url(#arrow)'}
fill="none"
/>
<>
<path
data-testid="clickable-edge"
d={path}
strokeWidth="12px"
stroke="white"
fill="none"
pointerEvents="stroke"
style={{ cursor: 'pointer', visibility: 'hidden' }}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onClick={handleEdgeClick}
/>
<path
data-testid="edge"
d={path}
stroke={isPreviewing ? '#1a5fff' : '#718096'}
strokeWidth="2px"
markerEnd={isPreviewing ? 'url(#blue-arrow)' : 'url(#arrow)'}
fill="none"
/>
<Portal>
<EdgeMenu
isOpen={isOpen}
position={edgeMenuPosition}
onDeleteEdge={handleDeleteEdge}
onClose={onClose}
/>
</Portal>
</>
)
}
37 changes: 37 additions & 0 deletions apps/builder/components/shared/Graph/Edges/EdgeMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Menu, MenuButton, MenuList, MenuItem } from '@chakra-ui/react'
import { TrashIcon } from 'assets/icons'
import React from 'react'

type Props = {
isOpen: boolean
position: { x: number; y: number }
onDeleteEdge: () => void
onClose: () => void
}

export const EdgeMenu = ({
isOpen,
onClose,
position,
onDeleteEdge,
}: Props) => {
return (
<Menu isOpen={isOpen} gutter={0} onClose={onClose} isLazy>
<MenuButton
aria-hidden={true}
w={1}
h={1}
pos="absolute"
style={{
left: position.x,
top: position.y,
}}
/>
<MenuList>
<MenuItem icon={<TrashIcon />} onClick={onDeleteEdge}>
Delete
</MenuItem>
</MenuList>
</Menu>
)
}
5 changes: 5 additions & 0 deletions apps/builder/playwright/tests/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ test.describe.parallel('Editor', () => {
)
await expect(page.locator('[data-testid="edge"] >> nth=0')).toBeVisible()
await expect(page.locator('[data-testid="edge"] >> nth=1')).toBeVisible()

await page.click('[data-testid="clickable-edge"] >> nth=0', { force: true })
await page.click('text=Delete')
const total = await page.locator('[data-testid="edge"]').count()
expect(total).toBe(1)
})
test('Drag and drop steps and items should work', async ({ page }) => {
const typebotId = generate()
Expand Down

2 comments on commit 3c67837

@vercel
Copy link

@vercel vercel bot commented on 3c67837 Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 3c67837 Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.