Skip to content

Commit a82fd40

Browse files
committed
reduce duplication
1 parent 6ecddaf commit a82fd40

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

src/views/BrowserTestEditor/EditableAction.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ interface EditableActionProps {
1111
action: BrowserActionInstance
1212
onRemove: (actionId: string) => void
1313
onChange: (action: BrowserActionInstance) => void
14-
leadingSlot?: ReactNode
14+
dragHandle?: ReactNode
1515
}
1616

1717
export function EditableAction({
1818
action,
19+
dragHandle,
1920
onRemove,
2021
onChange,
21-
leadingSlot,
2222
}: EditableActionProps) {
2323
const handleRemove = () => {
2424
onRemove(action.id)
@@ -36,7 +36,7 @@ export function EditableAction({
3636
`}
3737
>
3838
<Flex align="center" gap="2">
39-
{leadingSlot}
39+
{dragHandle}
4040
{editor.icon}
4141
{editor.render({ action, onChange })}
4242
<Tooltip content="Remove action">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { css } from '@emotion/react'
2+
import { IconButton, IconButtonProps } from '@radix-ui/themes'
3+
import { GripVerticalIcon } from 'lucide-react'
4+
5+
export function EditableActionDragHandle({
6+
overlay,
7+
...props
8+
}: IconButtonProps & { overlay?: true }) {
9+
return (
10+
<IconButton
11+
size="2"
12+
variant="ghost"
13+
color="gray"
14+
aria-label={overlay ? 'Drag to reorder' : undefined}
15+
data-overlay={overlay}
16+
css={css`
17+
cursor: grab;
18+
19+
&[data-overlay='true'] {
20+
cursor: grabbing;
21+
}
22+
`}
23+
{...props}
24+
>
25+
<GripVerticalIcon color="gray" aria-hidden />
26+
</IconButton>
27+
)
28+
}

src/views/BrowserTestEditor/SortableBrowserActionList.tsx

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import {
2121
} from '@dnd-kit/sortable'
2222
import { CSS } from '@dnd-kit/utilities'
2323
import { css } from '@emotion/react'
24-
import { Flex, IconButton } from '@radix-ui/themes'
25-
import { GripVerticalIcon } from 'lucide-react'
24+
import { Flex } from '@radix-ui/themes'
2625
import { useState } from 'react'
2726

2827
import { EditableAction } from './EditableAction'
28+
import { EditableActionDragHandle } from './EditableActionDragHandle'
2929
import { BrowserActionInstance } from './types'
3030

3131
enum Position {
@@ -60,13 +60,12 @@ export function SortableBrowserActionList({
6060
if (over && activeItem.id !== over.id) {
6161
onSwapActions(activeItem.id as string, over.id as string)
6262
}
63+
6364
setActive(null)
6465
}
6566

6667
function handleDragStart(event: DragStartEvent) {
67-
setActive(
68-
actions.find((action) => action.id === event.active.id) ?? null
69-
)
68+
setActive(actions.find((action) => action.id === event.active.id) ?? null)
7069
}
7170

7271
return (
@@ -86,29 +85,17 @@ export function SortableBrowserActionList({
8685
<SortableEditableAction
8786
key={action.id}
8887
action={action}
89-
onRemove={onRemoveAction}
9088
onChange={onChangeAction}
89+
onRemove={onRemoveAction}
9190
/>
9291
))}
9392
<DragOverlay modifiers={[restrictToFirstScrollableAncestor]}>
9493
{active ? (
9594
<EditableAction
9695
action={active}
97-
onRemove={onRemoveAction}
96+
dragHandle={<EditableActionDragHandle overlay />}
9897
onChange={onChangeAction}
99-
leadingSlot={
100-
<IconButton
101-
size="2"
102-
variant="ghost"
103-
color="gray"
104-
aria-hidden
105-
css={css`
106-
cursor: grabbing;
107-
`}
108-
>
109-
<GripVerticalIcon color="gray" aria-hidden />
110-
</IconButton>
111-
}
98+
onRemove={onRemoveAction}
11299
/>
113100
) : null}
114101
</DragOverlay>
@@ -178,23 +165,9 @@ function SortableEditableAction({
178165
>
179166
<EditableAction
180167
action={action}
181-
onRemove={onRemove}
168+
dragHandle={<EditableActionDragHandle {...attributes} {...listeners} />}
182169
onChange={onChange}
183-
leadingSlot={
184-
<IconButton
185-
size="2"
186-
variant="ghost"
187-
color="gray"
188-
aria-label="Drag to reorder"
189-
css={css`
190-
cursor: grab;
191-
`}
192-
{...attributes}
193-
{...listeners}
194-
>
195-
<GripVerticalIcon color="gray" aria-hidden />
196-
</IconButton>
197-
}
170+
onRemove={onRemove}
198171
/>
199172
</Flex>
200173
)

0 commit comments

Comments
 (0)