Skip to content

Commit 262cef2

Browse files
authored
refactor: dev overlay to make it easier to work with VT (#8966)
1 parent 463e036 commit 262cef2

File tree

10 files changed

+386
-293
lines changed

10 files changed

+386
-293
lines changed

.changeset/healthy-hornets-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix Dev Overlay not working properly when view transitions are enabled

packages/astro/src/@types/astro.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import type { TSConfig } from '../core/config/tsconfig.js';
2121
import type { AstroCookies } from '../core/cookies/index.js';
2222
import type { ResponseWithEncoding } from '../core/endpoint/index.js';
2323
import type { AstroIntegrationLogger, Logger, LoggerLevel } from '../core/logger/core.js';
24+
import type { AstroDevOverlay, DevOverlayCanvas } from '../runtime/client/dev-overlay/overlay.js';
25+
import type { DevOverlayHighlight } from '../runtime/client/dev-overlay/ui-library/highlight.js';
2426
import type { Icon } from '../runtime/client/dev-overlay/ui-library/icons.js';
27+
import type { DevOverlayTooltip } from '../runtime/client/dev-overlay/ui-library/tooltip.js';
28+
import type { DevOverlayWindow } from '../runtime/client/dev-overlay/ui-library/window.js';
2529
import type { AstroComponentFactory, AstroComponentInstance } from '../runtime/server/index.js';
2630
import type { OmitIndexSignature, Simplify } from '../type-utils.js';
2731
import type { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js';
@@ -2322,3 +2326,13 @@ export type DevOverlayMetadata = Window &
23222326
root: string;
23232327
};
23242328
};
2329+
2330+
declare global {
2331+
interface HTMLElementTagNameMap {
2332+
'astro-dev-overlay': AstroDevOverlay;
2333+
'astro-dev-overlay-window': DevOverlayWindow;
2334+
'astro-dev-overlay-plugin-canvas': DevOverlayCanvas;
2335+
'astro-dev-overlay-tooltip': DevOverlayTooltip;
2336+
'astro-dev-overlay-highlight': DevOverlayHighlight;
2337+
}
2338+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import type { DevOverlayPlugin as DevOverlayPluginDefinition } from '../../../@types/astro.js';
2+
import { type AstroDevOverlay, type DevOverlayPlugin } from './overlay.js';
3+
4+
let overlay: AstroDevOverlay;
5+
6+
document.addEventListener('DOMContentLoaded', async () => {
7+
const [
8+
{ loadDevOverlayPlugins },
9+
{ default: astroDevToolPlugin },
10+
{ default: astroAuditPlugin },
11+
{ default: astroXrayPlugin },
12+
{ AstroDevOverlay, DevOverlayCanvas },
13+
{ DevOverlayCard },
14+
{ DevOverlayHighlight },
15+
{ DevOverlayTooltip },
16+
{ DevOverlayWindow },
17+
] = await Promise.all([
18+
// @ts-expect-error
19+
import('astro:dev-overlay'),
20+
import('./plugins/astro.js'),
21+
import('./plugins/audit.js'),
22+
import('./plugins/xray.js'),
23+
import('./overlay.js'),
24+
import('./ui-library/card.js'),
25+
import('./ui-library/highlight.js'),
26+
import('./ui-library/tooltip.js'),
27+
import('./ui-library/window.js'),
28+
]);
29+
30+
// Register custom elements
31+
customElements.define('astro-dev-overlay', AstroDevOverlay);
32+
customElements.define('astro-dev-overlay-window', DevOverlayWindow);
33+
customElements.define('astro-dev-overlay-plugin-canvas', DevOverlayCanvas);
34+
customElements.define('astro-dev-overlay-tooltip', DevOverlayTooltip);
35+
customElements.define('astro-dev-overlay-highlight', DevOverlayHighlight);
36+
customElements.define('astro-dev-overlay-card', DevOverlayCard);
37+
38+
overlay = document.createElement('astro-dev-overlay');
39+
40+
const preparePlugin = (
41+
pluginDefinition: DevOverlayPluginDefinition,
42+
builtIn: boolean
43+
): DevOverlayPlugin => {
44+
const eventTarget = new EventTarget();
45+
const plugin = {
46+
...pluginDefinition,
47+
builtIn: builtIn,
48+
active: false,
49+
status: 'loading' as const,
50+
eventTarget: eventTarget,
51+
};
52+
53+
// Events plugins can send to the overlay to update their status
54+
eventTarget.addEventListener('plugin-notification', (evt) => {
55+
const target = overlay.shadowRoot?.querySelector(`[data-plugin-id="${plugin.id}"]`);
56+
if (!target) return;
57+
58+
let newState = true;
59+
if (evt instanceof CustomEvent) {
60+
newState = evt.detail.state ?? true;
61+
}
62+
63+
target.querySelector('.notification')?.toggleAttribute('data-active', newState);
64+
});
65+
66+
return plugin;
67+
};
68+
69+
const customPluginsDefinitions = (await loadDevOverlayPlugins()) as DevOverlayPluginDefinition[];
70+
const plugins: DevOverlayPlugin[] = [
71+
...[astroDevToolPlugin, astroXrayPlugin, astroAuditPlugin].map((pluginDef) =>
72+
preparePlugin(pluginDef, true)
73+
),
74+
...customPluginsDefinitions.map((pluginDef) => preparePlugin(pluginDef, false)),
75+
];
76+
77+
overlay.plugins = plugins;
78+
79+
document.body.append(overlay);
80+
81+
document.addEventListener('astro:after-swap', () => {
82+
document.body.append(overlay);
83+
});
84+
});

0 commit comments

Comments
 (0)