1
- # Theming
1
+ <script setup >
2
+ import LogosJavascript from ' ~icons/logos/javascript' ;
3
+ import LogosTypescript from ' ~icons/logos/typescript-icon' ;
4
+ import { ref , h } from ' vue' ;
5
+ import { Handle , Position , VueFlow } from ' @vue-flow/core' ;
6
+
7
+ const CustomNode = (props ) => h (' div' , [
8
+ h (Handle, { connectable: false , type: ' target' , position: Position .Top }),
9
+ h (' div' , props .label ),
10
+ h (Handle, { connectable: false , type: ' source' , position: Position .Bottom }),
11
+ ]);
12
+
13
+ const elements = ref ([
14
+ { id: ' 1' , label: ' Node 1' , position: { x: 0 , y: 0 }, draggable: false , deletable: false , selectable: false , type: ' custom' },
15
+ { id: ' 2' , label: ' Node 2' , position: { x: 75 , y: 75 }, draggable: false , deletable: false , selectable: false , type: ' custom' },
16
+ { id: ' e1-2' , source: ' 1' , target: ' 2' , animated: true , selectable: false , deletable: false },
17
+ ])
18
+ </script >
19
+
20
+ # Getting Creative with Theming
21
+
22
+ Let's take a tour around the library styles, customization opportunities, and other features that Vue Flow offers out of
23
+ the box.
24
+
25
+ ## Library Styles
26
+
27
+ Vue Flow values flexibility and allows you to take the lead when it comes to styling.
28
+ It showcases some obligatory stylings that must be imported, while leaving optional features, such as the default theme,
29
+ up to your preference.
2
30
3
- ## Library styles
4
- VueFlow comes without any pre-injected stylings. Some necessary stylings have to be imported, though optional styles (i.e.
5
- the default theme) can be skipped.
31
+ To import the necessary and optional styles:
6
32
7
33
``` css
8
34
/* these are necessary styles for vue flow */
@@ -12,38 +38,75 @@ the default theme) can be skipped.
12
38
@import ' @vue-flow/core/dist/theme-default.css' ;
13
39
```
14
40
15
- ## Customizing default theme
41
+ ## Tweaking the Default Theme
16
42
17
- When you are using the default theme there are three ways how you can style the graph pane and the elements. You can create
18
- your own CSS rules, pass style/ class properties to the components or use the available css variables.
43
+ The Vue Flow default theme functions as your baseline, which you can customize and decorate as per your liking using CSS
44
+ rules, style and class properties, and CSS variables.
19
45
20
- ### Using classes
46
+ ### Styling with CSS Classes
21
47
22
- You can customize the default theme by simply overwriting the class definitions with your own custom ones.
48
+ Here's how you can use CSS classes to add a pop of color or alter the font style for the theme:
23
49
24
50
``` css
25
- .vue-flow {
26
- background : red ;
51
+ /* Use a purple theme for our custom node */
52
+ .vue-flow__node-custom {
53
+ background : purple ;
54
+ color : white ;
55
+ border : 1px solid purple ;
56
+ border-radius : 4px ;
57
+ box-shadow : 0 0 0 1px purple ;
58
+ padding : 8px ;
27
59
}
28
60
```
29
61
30
- ### Using styles
62
+ <div class =" mt-4 bg-[var(--vp-code-block-bg)] rounded-lg h-50 " >
63
+ <VueFlow v-model =" elements " :pan-activation-key-code =" null " :pan-on-scroll =" false " :zoom-on-scroll =" false " :pan-on-drag =" false " fit-view-on-init >
64
+ <template #node-custom="props">
65
+ <CustomNode v-bind="props" />
66
+ </template>
67
+ </VueFlow >
68
+ </div >
69
+
70
+ <style >
71
+ .vue-flow__node-custom {
72
+ background : purple ;
73
+ color : white ;
74
+ border : 1px solid purple ;
75
+ border-radius : 4px ;
76
+ box-shadow : 0 0 0 1px purple ;
77
+ padding : 8px ;
78
+ }
79
+ </style >
80
+ s
81
+ ### Using CSS Properties
82
+
83
+ You can also directly pass a style or class attribute to Vue Flow components and nodes/edges.
31
84
32
- You can also pass a style or class attribute directly to the Vue Flow component.
85
+ Below are a couple of examples on how you can do this:
33
86
34
- ``` vue{4}
87
+ Directly styling the Vue Flow component:
88
+
89
+ ``` vue{5-6,8-9}
35
90
<div style="height: 300px">
36
91
<VueFlow
37
92
v-model="elements"
93
+
94
+ <!-- You can pass a class name to the component -->
95
+ class="my-diagram-class"
96
+
97
+ <!-- You can pass an object containing CSSProperties or CSS variables -->
38
98
:style="{ background: 'red' }"
39
99
/>
40
100
</div>
41
101
```
42
102
43
- Nodes/Edges can also be styled with a style or class attribute.
103
+ Styling nodes/edges with a style or class attribute:
44
104
45
- ``` ts{7-11,19-26}
46
- const nodes = ref<Node[]>([
105
+ ::: code-group
106
+
107
+ ``` js{8-12} [<LogosJavascript />]
108
+ /* Customizing node by assigning class and style properties */
109
+ const nodes = ref([
47
110
{
48
111
id: '1',
49
112
label: 'Node 1',
@@ -55,98 +118,114 @@ const nodes = ref<Node[]>([
55
118
// You can pass an object containing CSSProperties or CSS variables
56
119
style: { backgroundColor: 'green', width: '200px', height: '100px' },
57
120
},
58
-
121
+ ])
122
+ ```
123
+
124
+ ``` ts{10-14} [<LogosTypescript />]
125
+ import type { Node } from '@vue-flow/core';
126
+
127
+ /* Customizing node by assigning class and style properties */
128
+ const nodes = ref<Node[]>([
59
129
{
60
- id: '2 ',
61
- label: 'Node 2 ',
62
- position: { x: 100 , y: 100 },
130
+ id: '1 ',
131
+ label: 'Node 1 ',
132
+ position: { x: 250 , y: 5 },
63
133
64
- /*
65
- * You can also use a function which will receive your current element as it's input.
66
- * Useful if you want to add styles if the element is selected
67
- */
68
- style: (el) => {
69
- if (el.selected) return { background: 'purple' }
70
- return { background: 'green' }
71
- }
134
+ // Add a class name to the node
135
+ class: 'my-custom-node-class',
136
+
137
+ // You can pass an object containing CSSProperties or CSS variables
138
+ style: { backgroundColor: 'green', width: '200px', height: '100px' },
72
139
},
73
140
])
74
141
```
75
142
76
- ### [ Using CSS variables] ( /typedocs/types/CSSVars )
143
+ :::
144
+
145
+ ### [ Redefining Styles with CSS variables] ( /typedocs/types/CSSVars )
77
146
78
- Some theme stylings can be overwritten by using css variables.
79
- These variables can either be applied globally or you can define them on an element basis.
147
+ Some of the defined theme styles can be overwritten using CSS variables.
148
+ These alterations can be implemented either on a global scale or to individual elements.
149
+
150
+ ::: code-group
80
151
81
152
``` css
82
- /* global defaults */
153
+ /* Global default CSS variable values */
83
154
:root {
84
- --vf-node-bg : #fff ;
85
- --vf-node-text : #222 ;
86
- --vf-connection-path : #b1b1b7 ;
87
- --vf-handle : #555 ;
155
+ --vf-node-bg : #fff ;
156
+ --vf-node-text : #222 ;
157
+ --vf-connection-path : #b1b1b7 ;
158
+ --vf-handle : #555 ;
88
159
}
89
160
```
90
161
91
- ``` ts{4-5}
162
+ ``` js{2-3} [<LogosJavascript />]
163
+ const elements = ref([
164
+ /* Overriding the `--vf-node-color` variable to change node border, box-shadow and handle color */
165
+ { id: '1', label: 'Node 1', position: { x: 100, y: 100 }, style: { '--vf-node-color': 'blue' } },
166
+ ])
167
+ ```
168
+
169
+ ``` ts{4-5} [<LogosTypescript />]
170
+ import type { Elements } from '@vue-flow/core';
171
+
92
172
const elements = ref<Elements>([
93
- { id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 } },
94
-
95
- // Overwrite the `--vf-node-color` variable to change the border, box-shadow and handle color of a node
96
- { id: '2', label: 'Node 2', position: { x: 100, y: 100 }, style: { '--vf-node-color': 'blue' } },
97
-
98
- { id: '3', label: 'Node 3', position: { x: 400, y: 100 } },
99
- { id: '4', label: 'Node 4', position: { x: 400, y: 200 } },
100
- { id: 'e1-2', source: '1', target: '2', animated: true },
101
- { id: 'e1-3', source: '1', target: '3' },
173
+ /* Overriding the `--vf-node-color` variable to change node border, box-shadow and handle color */
174
+ { id: '1', label: 'Node 1', position: { x: 100, y: 100 }, style: { '--vf-node-color': 'blue' } },
102
175
])
103
176
```
104
177
178
+ :::
179
+
105
180
## CSS Variables
106
181
107
- | Variable | Effect |
108
- | -----------------------| ------------------------------------------|
109
- | --vf-node-color | Node border, box-shadow and handle color |
110
- | --vf-box-shadow | Node box-shadow color |
111
- | --vf-node-bg | Node background color |
112
- | --vf-node-text | Node font color |
113
- | --vf-handle | Node handle color |
114
- | --vf-connection-path | Connectionline color |
115
-
116
- ## Classes
117
-
118
- | Name | Container |
119
- | -------------------------------| ----------------------------------------------------------|
120
- | .vue-flow | Outer container |
121
- | .vue-flow__ container | Wrapping container elements |
122
- | .vue-flow__ viewport | Inner container |
123
- | .vue-flow__ transformationpane | Zoom & pan pane |
124
- | .vue-flow__ selectionpane | Selection pane |
125
- | .vue-flow__ selection | User selection |
126
- | .vue-flow__ edges | Edges renderer wrapper |
127
- | .vue-flow__ edge | Edge element wrapper |
128
- | .vue-flow__ edge-{type} | Edge type, either a custom or default type |
129
- | .vue-flow__ edge .selected | Selected Edge |
130
- | .vue-flow__ edge .animated | Animated edge |
131
- | .vue-flow__ edge-path | Edge element svg path |
132
- | .vue-flow__ edge-text | Edge label wrapper |
133
- | .vue-flow__ edge-textbg | Edge label wrapper background |
134
- | .vue-flow__ connectionline | Connection line container element |
135
- | .vue-flow__ connection | Connection line element |
136
- | .vue-flow__ connection-path | Connection line svg path |
137
- | .vue-flow__ nodes | Nodes renderer wrapper |
138
- | .vue-flow__ node | Node element wrapper |
139
- | .vue-flow__ node .selected | Selected Node |
140
- | .vue-flow__ node-{type} | Node type, either a custom or default type |
141
- | .vue-flow__ nodesselection | Nodes selection rect |
142
- | .vue-flow__ handle | Node handle element wrapper |
143
- | .vue-flow__ handle-bottom | Handle position bottom |
144
- | .vue-flow__ handle-top | Handle position top |
145
- | .vue-flow__ handle-left | Handle position left |
146
- | .vue-flow__ handle-right | Handle position right |
147
- | .vue-flow__ handle-connecting | Connectionline is above handle |
148
- | .vue-flow__ handle-valid | Connectionline is above handle & the connection is valid |
149
- | .vue-flow__ background | Background component |
150
- | .vue-flow__ minimap | Mini map component |
151
- | .vue-flow__ controls | Controls component |
182
+ Here's a concise list of CSS variables you can consider, along with their effects:
183
+
184
+ | Variable | Effect |
185
+ | ----------------------| ----------------------------------------------------|
186
+ | --vf-node-color | Defines node border, box-shadow, and handle colors |
187
+ | --vf-box-shadow | Defines color of node box-shadow |
188
+ | --vf-node-bg | Defines node background color |
189
+ | --vf-node-text | Defines node text color |
190
+ | --vf-handle | Defines node handle color |
191
+ | --vf-connection-path | Defines connection line color |
192
+
193
+ ## CSS Class Names
194
+
195
+ Here you'll find a handy reference guide of class names and their respective containers:
196
+
197
+ | Name | Container |
198
+ | -------------------------------| ---------------------------------------------------|
199
+ | .vue-flow | The outer container |
200
+ | .vue-flow__ container | Wrapper for container elements |
201
+ | .vue-flow__ viewport | The inner container |
202
+ | .vue-flow__ transformationpane | Pane for movements like zooming and panning |
203
+ | .vue-flow__ selectionpane | Pane for handling user selection |
204
+ | .vue-flow__ selection | Defines current user selection box |
205
+ | .vue-flow__ edges | Wrapper rendering edges |
206
+ | .vue-flow__ edge | Wrapper around each edge element |
207
+ | .vue-flow__ edge-{type} | Edge type (either custom or default) |
208
+ | .vue-flow__ edge .selected | Defines the currently selected edge(s) |
209
+ | .vue-flow__ edge .animated | Defines an animated edge |
210
+ | .vue-flow__ edge-path | SVG path for edge elements |
211
+ | .vue-flow__ edge-text | Wrapper around edge label |
212
+ | .vue-flow__ edge-textbg | Background wrapper around edge label |
213
+ | .vue-flow__ connectionline | Container for the connection line elements |
214
+ | .vue-flow__ connection | Individual connection line element |
215
+ | .vue-flow__ connection-path | SVG path for connection line |
216
+ | .vue-flow__ nodes | Rendering wrapper around nodes |
217
+ | .vue-flow__ node | Wrapper around each node element |
218
+ | .vue-flow__ node .selected | Defines the currently selected node(s) |
219
+ | .vue-flow__ node-{type} | Node type (either custom or default) |
220
+ | .vue-flow__ nodesselection | Defines selection rectangle for nodes |
221
+ | .vue-flow__ handle | Wrapper around node handle elements |
222
+ | .vue-flow__ handle-bottom | Defines a handle at bottom |
223
+ | .vue-flow__ handle-top | Defines a handle at top |
224
+ | .vue-flow__ handle-left | Defines a handle at left |
225
+ | .vue-flow__ handle-right | Defines a handle at right |
226
+ | .vue-flow__ handle-connecting | Connection line is over the handle |
227
+ | .vue-flow__ handle-valid | Connection line over handle with valid connection |
228
+ | .vue-flow__ background | Background component |
229
+ | .vue-flow__ minimap | MiniMap component |
230
+ | .vue-flow__ controls | Controls component |
152
231
0 commit comments