Skip to content

Commit 34b2523

Browse files
committed
refactor(core): move background component to core pkg
Signed-off-by: braks <[email protected]>
1 parent bbb54be commit 34b2523

File tree

6 files changed

+91
-79
lines changed

6 files changed

+91
-79
lines changed

packages/background/src/types.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/background/src/Background.vue renamed to packages/core/src/addons/Background/Background.vue

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
<script lang="ts" setup>
2-
import { useVueFlow } from '@vue-flow/core'
3-
import { computed, toRef } from 'vue'
4-
import { BackgroundVariant } from './types'
2+
import { computed } from 'vue'
3+
import { useVueFlow } from '../../composables/useVueFlow'
54
import type { BackgroundProps } from './types'
65
import { DefaultBgColors, DotPattern, LinePattern } from './patterns'
76
8-
const {
9-
id,
10-
variant = BackgroundVariant.Dots,
7+
const props = withDefaults(defineProps<BackgroundProps>(), {
8+
variant = 'dots',
119
gap = 20,
1210
size = 1,
1311
lineWidth = 1,
14-
height = 100,
15-
width = 100,
1612
x = 0,
1713
y = 0,
18-
bgColor,
19-
patternColor: initialPatternColor,
20-
color: _patternColor,
2114
offset = 2,
22-
} = defineProps<BackgroundProps>()
15+
})
2316
2417
const { id: vueFlowId, viewport } = useVueFlow()
2518
2619
const background = computed(() => {
27-
const [gapX, gapY] = Array.isArray(gap) ? gap : [gap, gap]
20+
const [gapX, gapY] = Array.isArray(props.gap) ? props.gap : [props.gap, props.gap]
2821
2922
const scaledGap: [number, number] = [gapX * viewport.value.zoom || 1, gapY * viewport.value.zoom || 1]
3023
31-
const scaledSize = size * viewport.value.zoom
24+
const scaledSize = props.size * viewport.value.zoom
3225
3326
const patternOffset =
34-
variant === BackgroundVariant.Dots
35-
? [scaledSize / offset, scaledSize / offset]
36-
: [scaledGap[0] / offset, scaledGap[1] / offset]
27+
props.variant === 'dots'
28+
? [scaledSize / props.offset, scaledSize / props.offset]
29+
: [scaledGap[0] / props.offset, scaledGap[1] / props.offset]
3730
3831
return {
3932
scaledGap,
@@ -43,9 +36,9 @@ const background = computed(() => {
4336
})
4437
4538
// when there are multiple flows on a page we need to make sure that every background gets its own pattern.
46-
const patternId = toRef(() => `pattern-${vueFlowId}${id ? `-${id}` : ''}`)
39+
const patternId = computed(() => `pattern-${vueFlowId}${props.id ? `-${props.id}` : ''}`)
4740
48-
const patternColor = toRef(() => _patternColor || initialPatternColor || DefaultBgColors[variant || BackgroundVariant.Dots])
41+
const patternColor = computed(() => props.color || DefaultBgColors[props.variant || 'dots'])
4942
</script>
5043

5144
<script lang="ts">
@@ -56,13 +49,7 @@ export default {
5649
</script>
5750

5851
<template>
59-
<svg
60-
class="vue-flow__background vue-flow__container"
61-
:style="{
62-
height: `${height > 100 ? 100 : height}%`,
63-
width: `${width > 100 ? 100 : width}%`,
64-
}"
65-
>
52+
<svg class="vue-flow__background vue-flow__container">
6653
<slot :id="patternId" name="pattern-container">
6754
<pattern
6855
:id="patternId"
@@ -74,17 +61,13 @@ export default {
7461
patternUnits="userSpaceOnUse"
7562
>
7663
<slot name="pattern">
77-
<template v-if="variant === BackgroundVariant.Lines">
64+
<template v-if="variant === 'lines'">
7865
<LinePattern :size="lineWidth" :color="patternColor" :dimensions="background.scaledGap" />
7966
</template>
8067

81-
<template v-else-if="variant === BackgroundVariant.Dots">
68+
<template v-else-if="variant === 'dots'">
8269
<DotPattern :color="patternColor" :radius="background.size / offset" />
8370
</template>
84-
85-
<svg v-if="bgColor" height="100" width="100">
86-
<rect width="100%" height="100%" :fill="bgColor" />
87-
</svg>
8871
</slot>
8972
</pattern>
9073
</slot>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as Background } from './Background.vue'
2+
export * from './types'

packages/background/src/patterns.ts renamed to packages/core/src/addons/Background/patterns.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FunctionalComponent } from 'vue'
22
import { h } from 'vue'
3-
import { BackgroundVariant } from './types'
3+
import type { BackgroundVariant } from './types'
44

55
interface LinePatternProps {
66
dimensions: [number, number]
@@ -26,11 +26,11 @@ export const DotPattern: FunctionalComponent<DotPatternProps> = function ({ radi
2626
}
2727

2828
export const Patterns = {
29-
[BackgroundVariant.Lines]: LinePattern,
30-
[BackgroundVariant.Dots]: DotPattern,
29+
lines: LinePattern,
30+
dots: DotPattern,
3131
}
3232

3333
export const DefaultBgColors: Record<BackgroundVariant, string> = {
34-
[BackgroundVariant.Dots]: '#81818a',
35-
[BackgroundVariant.Lines]: '#eee',
34+
lines: '#81818a',
35+
dots: '#eee',
3636
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* The Background can be either a dotted one or a lined one
3+
*
4+
* @default 'dots'
5+
*/
6+
export type BackgroundVariant = 'dots' | 'lines'
7+
8+
export interface BackgroundProps {
9+
/**
10+
* `<Background>` component id
11+
*
12+
* This is necessary when you have multiple flows with backgrounds visible at the same time.
13+
* If no id is explicitly assigned, an auto-generated one is used.
14+
*
15+
* @default `pattern-${vueFlowId}${id ? `-${id}` : ''}`
16+
*/
17+
id?: string
18+
/**
19+
* The background pattern variant {@link BackgroundVariant}
20+
*
21+
* @default 'dots'
22+
*/
23+
variant?: BackgroundVariant
24+
/**
25+
* The background pattern gap
26+
*
27+
* Can be either a number or [xGap: number, yGap: number], defining the gap on the X and Y axis respectively
28+
*
29+
* @default 20
30+
*/
31+
gap?: number | number[]
32+
/**
33+
* Background pattern size
34+
*
35+
* @default 1
36+
*/
37+
size?: number
38+
/**
39+
* @default 1
40+
*/
41+
lineWidth?: number
42+
/**
43+
* The background pattern color
44+
*
45+
* This only changes the color of the *pattern*, not the background color itself.
46+
*
47+
* If you want to change the background color itself, you can apply a bg-color to the `<VueFlow>` element instead
48+
*/
49+
color?: string
50+
/**
51+
* Background x-coordinate (offset x)
52+
*
53+
* @default 0
54+
*/
55+
x?: number
56+
/**
57+
* Background y-coordinate (offset y)
58+
* @default 0
59+
*/
60+
y?: number
61+
/**
62+
* Background pattern offset
63+
*
64+
* @default 2
65+
*/
66+
offset?: number
67+
}

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export { default as BaseEdge } from './components/Edges/BaseEdge.vue'
1818
export { default as EdgeText } from './components/Edges/EdgeText.vue'
1919
export { default as EdgeLabelRenderer } from './components/Edges/EdgeLabelRenderer.vue'
2020

21+
export * from './addons/Background'
22+
2123
export {
2224
getBezierPath,
2325
getSimpleBezierPath,

0 commit comments

Comments
 (0)