feat(svelte): add color selector bg example

This commit is contained in:
moklick
2023-03-20 18:12:08 +01:00
parent 77d14f9ad0
commit ca94d18a30
5 changed files with 143 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
import { goto } from '$app/navigation';
const routes = [
'customnode',
'drag-n-drop',
'overview',
'stress',

View File

@@ -95,7 +95,7 @@
<div
bind:this={domNode}
style={style}
{style}
class={cc(['svelte-flow', className])}
data-testid="svelte-flow__wrapper"
on:dragover

View File

@@ -0,0 +1,119 @@
<script lang="ts">
import type { ChangeEventHandler } from 'svelte/elements';
import { writable } from 'svelte/store';
import SvelteFlow, {
SvelteFlowProvider,
Controls,
Background,
BackgroundVariant,
Minimap,
createNodes,
createEdges,
type NodeTypes,
Position
} from '../../lib/index';
import { CustomNode } from './CustomNode';
const nodeTypes: NodeTypes = {
colorNode: CustomNode
};
const bgColor = writable('#1A192B')
const onChange: ChangeEventHandler<HTMLInputElement> = (event) => {
nodes.update((nds) =>
nds.map((node) => {
if (node.id !== '2') {
return node;
}
const color = (event.target as HTMLInputElement)?.value;
bgColor.set(color);
return {
...node,
data: {
...node.data,
color,
},
};
})
);
}
const nodes = createNodes([
{
id: '1',
type: 'input',
data: { label: 'An input node' },
position: { x: 0, y: 50 },
sourcePosition: Position.Right,
},
{
id: '2',
type: 'colorNode',
data: { onChange: onChange, color: $bgColor },
style: 'border: 1px solid #777; padding: 10px',
position: { x: 250, y: 50 },
},
{
id: '3',
type: 'output',
data: { label: 'Output A' },
position: { x: 650, y: 25 },
targetPosition: Position.Left,
},
{
id: '4',
type: 'output',
data: { label: 'Output B' },
position: { x: 650, y: 120 },
targetPosition: Position.Left,
},
]);
const edges = createEdges([
{
id: 'e1-2',
source: '1',
target: '2',
animated: true,
},
{
id: 'e2a-3',
source: '2',
sourceHandle: 'a',
target: '3',
animated: true,
},
{
id: 'e2b-4',
source: '2',
sourceHandle: 'b',
target: '4',
animated: true,
},
], { animated: true });
</script>
<SvelteFlowProvider
{nodes}
{edges}
>
<SvelteFlow
{nodeTypes}
style="--bgcolor: {$bgColor}"
fitView
>
<Controls />
<Background variant={BackgroundVariant.Dots} />
<Minimap />
</SvelteFlow>
</SvelteFlowProvider>
<style>
:global(.svelte-flow) {
background: var(--bgcolor);
}
</style>

View File

@@ -0,0 +1,21 @@
<script lang="ts">
import { Position } from '@reactflow/system';
import { Handle } from '../../../lib/index';
export let data: { color: string, onChange: () => void } = { color: '#111', onChange: () => {} };
export let xPos: number = 0;
export let yPos: number = 0;
</script>
<Handle type="target" position={Position.Left} />
<div>
Custom Color Picker Node: <strong>{data.color}</strong>
</div>
<input class="nodrag" type="color" on:input={data.onChange} value={data.color} />
<Handle
type="source"
position={Position.Right}
id="a"
style="top: 10px;"
/>
<Handle type="source" position={Position.Right} id="b" style="top: auto; bottom: 10px;" />

View File

@@ -0,0 +1 @@
export { default as CustomNode } from './Custom.svelte';