Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add missing styles warning #1415

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fresh-wolves-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": minor
---

Add warning when style imports are missing.
17 changes: 17 additions & 0 deletions packages/core/src/composables/useStylesLoadedWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { onMounted } from 'vue'
import { ErrorCode, VueFlowError, isDev } from '../utils'
import { useVueFlow } from './useVueFlow'

export function useStylesLoadedWarning() {
const { emits } = useVueFlow()

onMounted(() => {
if (isDev()) {
const pane = document.querySelector('.vue-flow__pane')

if (pane && !(window.getComputedStyle(pane).zIndex === '1')) {
emits.error(new VueFlowError(ErrorCode.MISSING_STYLES))
}
}
})
}
3 changes: 3 additions & 0 deletions packages/core/src/container/VueFlow/VueFlow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useVueFlow } from '../../composables/useVueFlow'
import { useHooks } from '../../store/hooks'
import EdgeRenderer from '../EdgeRenderer/EdgeRenderer.vue'
import NodeRenderer from '../NodeRenderer/NodeRenderer.vue'
import { useStylesLoadedWarning } from '../../composables/useStylesLoadedWarning'

const props = withDefaults(defineProps<FlowProps>(), {
snapToGrid: undefined,
Expand Down Expand Up @@ -70,6 +71,8 @@ useHooks(emit, hooks)

useOnInitHandler()

useStylesLoadedWarning()

// slots will be passed via provide
// this is to avoid having to pass them down through all the components
// as that would require a lot of boilerplate and causes significant performance drops
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ErrorCode {
MISSING_STYLES = 'MISSING_STYLES',
MISSING_VIEWPORT_DIMENSIONS = 'MISSING_VIEWPORT_DIMENSIONS',
NODE_INVALID = 'NODE_INVALID',
NODE_NOT_FOUND = 'NODE_NOT_FOUND',
Expand All @@ -16,6 +17,8 @@ export enum ErrorCode {
}

const messages = {
[ErrorCode.MISSING_STYLES]: () =>
`It seems that you haven't loaded the necessary styles. Please import '@vue-flow/core/dist/style.css' to ensure that the graph is rendered correctly`,
[ErrorCode.MISSING_VIEWPORT_DIMENSIONS]: () => 'The Vue Flow parent container needs a width and a height to render the graph',
[ErrorCode.NODE_INVALID]: (id?: string) => `Node is invalid\nNode: ${id}`,
[ErrorCode.NODE_NOT_FOUND]: (id: string | null) => `Node not found\nNode: ${id}`,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const productionEnvs = ['production', 'prod']

export function warn(message: string, ...args: any[]) {
if (!productionEnvs.includes(__ENV__ || '')) {
if (isDev()) {
console.warn(`[Vue Flow]: ${message}`, ...args)
}
}

export function isDev() {
return !productionEnvs.includes(__ENV__ || '')
}
Loading