Skip to content

Commit e5a26d6

Browse files
authored
feat: support initially opened overlay for errors (#259)
1 parent e2ed493 commit e5a26d6

File tree

35 files changed

+511
-21
lines changed

35 files changed

+511
-21
lines changed

.changeset/clean-bottles-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vite-plugin-checker': patch
3+
---
4+
5+
feat: support initially open overlay for errors

docs/configuration/config.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ Shared configuration to control the checker behaviors of the plugin.
1616
| boolean
1717
| {
1818
/**
19-
* Set this true if you want the overlay to default to being open if
20-
* errors/warnings are found
19+
* Whether to default the overlay to being open
20+
* - Set `true` to initially open if errors/warnings are found
21+
* - Set `error` to initially open if errors are found
22+
* - Set `false` to initially collapse
2123
* @defaultValue `true`
2224
*/
23-
initialIsOpen?: boolean
25+
initialIsOpen?: boolean | 'error'
2426
/**
2527
* The position of the vite-plugin-checker badge to open and close
2628
* the diagnostics panel

packages/runtime/src/App.ce.vue

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts" setup>
2-
import { ref, computed } from 'vue'
2+
import { ref, computed, watch } from 'vue'
33
import Badge from './components/Badge.ce.vue'
44
import List from './components/List.ce.vue'
55
import { useChecker } from './useChecker'
@@ -21,11 +21,32 @@ const shouldRender = computed(() => {
2121
return checkerResults.value.some((p) => p.diagnostics.length > 0)
2222
})
2323
24-
const collapsed = ref<boolean>(!(props?.overlayConfig?.initialIsOpen ?? true))
24+
// collapsed state if requested (otherwise initially open)
25+
const initialCollapsed = ref<boolean>(
26+
props?.overlayConfig?.initialIsOpen === false || props?.overlayConfig?.initialIsOpen === 'error'
27+
)
28+
29+
// if initialIsOpen is 'error': watch for results and open if there are errors
30+
if (props?.overlayConfig?.initialIsOpen === 'error') {
31+
// checker result is always initially empty, so need to watch for changes
32+
const unwatch = watch(checkerResults, () => {
33+
if (!checkerResults.value.length) return
34+
// non-zero results length means the badge will appear. decide whether to open the overlay.
35+
if (checkerResults.value.some((p) => p.diagnostics.some((d: any) => d.level === 1))) {
36+
initialCollapsed.value = false
37+
}
38+
// stop watching and let the user control the overlay state
39+
unwatch()
40+
})
41+
}
42+
43+
const userCollapsed = ref<boolean | undefined>(undefined)
2544
2645
const toggle = () => {
27-
collapsed.value = !collapsed.value
46+
userCollapsed.value = !(userCollapsed.value ?? initialCollapsed.value)
2847
}
48+
49+
const collapsed = computed<boolean>(() => userCollapsed.value ?? initialCollapsed.value)
2950
</script>
3051

3152
<template>

packages/runtime/src/components/Badge.ce.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { computed } from 'vue'
33
44
const props = withDefaults(
55
defineProps<{
6-
checkerResults: any
6+
checkerResults: any[]
77
collapsed: boolean
88
position?: string
99
badgeStyle?: string

packages/runtime/src/useChecker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ function resumeErrorOverlay(data: any) {
2222

2323
export function useChecker() {
2424
const ws = prepareListen()
25-
listenToCustomMessage(updateErrorOverlay as any)
26-
listenToReconnectMessage(resumeErrorOverlay as any)
25+
listenToCustomMessage(updateErrorOverlay)
26+
listenToReconnectMessage(resumeErrorOverlay)
2727
ws.startListening()
2828

2929
return {

packages/runtime/src/ws.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export function listenToConfigMessage(cb: () => any) {
1212
onConfigMessage.push(cb)
1313
}
1414

15-
export function listenToCustomMessage(cb: () => any) {
15+
export function listenToCustomMessage(cb: (data: any) => any) {
1616
onCustomMessage.push(cb)
1717
}
1818

19-
export function listenToReconnectMessage(cb: () => any) {
19+
export function listenToReconnectMessage(cb: (data: any) => any) {
2020
onReconnectMessage.push(cb)
2121
}
2222

packages/vite-plugin-checker/__tests__/__snapshots__/logger.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`logger > diagnosticToTerminalLog > get error 1`] = `
44
" ERROR(ESLint) Unexpected var, use let or const instead.

packages/vite-plugin-checker/__tests__/__snapshots__/vlsConfig.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`VLS config > customized config 1`] = `
44
{

packages/vite-plugin-checker/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface SharedConfig {
123123
* errors/warnings are found
124124
* @defaultValue `true`
125125
*/
126-
initialIsOpen?: boolean
126+
initialIsOpen?: boolean | 'error'
127127
/**
128128
* The position of the vite-plugin-checker badge to open and close
129129
* the diagnostics panel
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"root": true,
3+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"]
4+
}

0 commit comments

Comments
 (0)