forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputNode.tsx
More file actions
66 lines (60 loc) · 2.3 KB
/
Copy pathInputNode.tsx
File metadata and controls
66 lines (60 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { useCallback, useRef, useLayoutEffect, useState } from 'react'
import { Handle, Position, useReactFlow } from '@xyflow/react'
import type { WFNodeData } from '@shared/types/electron.d'
import BaseNode from './BaseNode'
const HANDLE_COLOR: Record<string, string> = {
image: '#38bdf8',
text: '#fbbf24',
}
export default function InputNode({ id, data, selected }: { id: string; data: WFNodeData; selected?: boolean }) {
const { updateNodeData } = useReactFlow()
const inputType = data.inputType ?? 'image'
const handleColor = HANDLE_COLOR[inputType] ?? '#38bdf8'
// Align handle with the type-toggle row
const toggleRowRef = useRef<HTMLDivElement>(null)
const [handleTop, setHandleTop] = useState('50%')
useLayoutEffect(() => {
if (toggleRowRef.current) {
const center = toggleRowRef.current.offsetTop + toggleRowRef.current.offsetHeight / 2
setHandleTop(`${center}px`)
}
}, [])
const setType = useCallback((t: 'image' | 'text') => {
updateNodeData(id, { inputType: t })
}, [id, updateNodeData])
return (
<BaseNode
id={id}
selected={selected}
title="Input"
deletable={false}
minWidth={160}
icon={<div className="w-2 h-2 rounded-full bg-zinc-500" />}
handles={
<Handle type="source" position={Position.Right}
style={{ background: handleColor, width: 14, height: 14, border: '2.5px solid #18181b', top: handleTop }} />
}
>
<div ref={toggleRowRef} className="px-3 pb-3 pt-2.5 flex gap-1.5">
<button
onClick={() => setType('image')}
className={`nodrag flex-1 py-1 rounded-lg text-[10px] font-medium transition-colors
${inputType === 'image'
? 'bg-sky-500/20 text-sky-400 border border-sky-500/40'
: 'bg-zinc-800 text-zinc-500 border border-zinc-700 hover:border-zinc-600'}`}
>
Image
</button>
<button
onClick={() => setType('text')}
className={`nodrag flex-1 py-1 rounded-lg text-[10px] font-medium transition-colors
${inputType === 'text'
? 'bg-amber-500/20 text-amber-400 border border-amber-500/40'
: 'bg-zinc-800 text-zinc-500 border border-zinc-700 hover:border-zinc-600'}`}
>
Text
</button>
</div>
</BaseNode>
)
}