Skip to content

Commit 8c485ba

Browse files
authored
fix(astro): Reset button state correctly (#49)
1 parent 1bb952e commit 8c485ba

File tree

7 files changed

+129
-166
lines changed

7 files changed

+129
-166
lines changed

.changeset/flat-mayflies-hang.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@spotlightjs/spotlight-website': patch
3+
'@spotlightjs/astro': patch
4+
'@spotlightjs/core': patch
5+
---
6+
7+
fix(astro): Correctly reset toolbar button state

packages/astro/src/overlay/index.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ export default {
2323
}),
2424
);
2525

26-
eventTarget.addEventListener('plugin-toggle', () => {
27-
Spotlight.toggleSpotlight();
26+
eventTarget.addEventListener('plugin-toggled', e => {
27+
// e.detail.state exists, see https://docs.astro.build/en/reference/dev-overlay-plugin-reference/#plugin-toggled
28+
const shouldOpen = (e as CustomEvent).detail.state;
29+
shouldOpen ? Spotlight.openSpotlight() : Spotlight.closeSpotlight();
30+
});
31+
32+
Spotlight.onClose(() => {
33+
eventTarget.dispatchEvent(
34+
new CustomEvent('toggle-plugin', {
35+
detail: {
36+
state: false,
37+
},
38+
}),
39+
);
2840
});
2941
},
3042
} satisfies DevOverlayPlugin;

packages/astro/src/snippets.ts

-15
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ init({
88
],
99
showTriggerButton: false,
1010
});
11-
12-
setTimeout(() => {
13-
console.log("this message should show up in the Console tab eventually")
14-
}, 3000);
15-
16-
setTimeout(() => {
17-
console.log("this one, too ;)")
18-
}, 6000);
19-
20-
setTimeout(() => {
21-
console.warn("this warning, too ;)")
22-
}, 6000);
2311
`;
2412

2513
export const SPOTLIGHT_SERVER_SNIPPET = `
@@ -61,7 +49,4 @@ console.log('[Spotlight]', globalThis.__SENTRY__);
6149
}
6250
);
6351
64-
// setTimeout(() => {
65-
Sentry.captureException(new Error('does this now show up in spotlight?'));
66-
// }, 2000);
6752
`;

packages/core/src/App.tsx

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function App({
2525
const [integrationData, setIntegrationData] = useState<IntegrationData<unknown>>({});
2626
const [isOnline, setOnline] = useState(false);
2727
const [triggerButtonCount, setTriggerButtonCount] = useState<TriggerButtonCount>({ general: 0, severe: 0 });
28+
const [isOpen, setOpen] = useState(fullScreen);
2829

2930
useEffect(() => {
3031
// Map that holds the information which kind of content type should be dispatched to which integration(s)
@@ -53,12 +54,20 @@ export default function App({
5354
};
5455
}, [integrations]);
5556

56-
const [isOpen, setOpen] = useState(fullScreen);
57+
eventTarget.addEventListener('open', () => {
58+
setOpen(true);
59+
});
5760

58-
eventTarget.addEventListener('toggle', () => {
59-
setOpen(!isOpen);
61+
eventTarget.addEventListener('close', () => {
62+
setOpen(false);
6063
});
6164

65+
useEffect(() => {
66+
if (!isOpen) {
67+
eventTarget.dispatchEvent(new CustomEvent('closed'));
68+
}
69+
}, [isOpen, eventTarget]);
70+
6271
console.log('[Spotlight] Integrations', integrationData);
6372

6473
return (

packages/core/src/index.tsx

+17-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ function createStyleSheet(styles: string) {
1919
const spotlightEventTarget: EventTarget = new EventTarget();
2020

2121
/**
22-
* Open or close the Spotlight UI
22+
* Open the Spotlight debugger Window
2323
*/
24-
export async function toggleSpotlight() {
25-
spotlightEventTarget.dispatchEvent(new Event('toggle'));
24+
export async function openSpotlight() {
25+
spotlightEventTarget.dispatchEvent(new CustomEvent('open'));
26+
}
27+
28+
/**
29+
* Close the Spotlight debugger Window
30+
*/
31+
export async function closeSpotlight() {
32+
spotlightEventTarget.dispatchEvent(new CustomEvent('close'));
33+
}
34+
35+
/**
36+
* Invokes the passed in callback when the Spotlight debugger Window is closed
37+
*/
38+
export async function onClose(cb: () => void) {
39+
spotlightEventTarget.addEventListener('closed', cb);
2640
}
2741

2842
export async function init({

packages/website/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@astrojs/starlight-tailwind": "^2.0.0",
1616
"@astrojs/tailwind": "^5.0.2",
1717
"@sentry/astro": "^7.80.0",
18-
"astro": "^3.4.0",
18+
"astro": "^3.5.4",
1919
"sharp": "^0.32.5",
2020
"tailwindcss": "^3.0.24"
2121
},

0 commit comments

Comments
 (0)