Skip to content

Commit a205b9e

Browse files
committed
Fix home page examples not firing
The Examples script imported mountDummyElement/removeDummyElement from CodeSample.tsx. Importing a React module into a vanilla Astro <script> tripped @vitejs/plugin-react's dev preamble guard, which threw and prevented any example button listeners from attaching. Move the two framework-free DOM helpers into src/lib/dummyElement.ts and import them from there in both the Examples script and CodeSample.
1 parent 452e8be commit a205b9e

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

docs/src/components/CodeSample.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Config, DriveStep, PopoverDOM } from "driver.js";
22
import { driver } from "driver.js";
33
import "driver.js/dist/driver.css";
4+
import { mountDummyElement, removeDummyElement } from "../lib/dummyElement";
45

56
type CodeSampleProps = {
67
heading?: string;
@@ -15,30 +16,6 @@ type CodeSampleProps = {
1516
buttonText?: string;
1617
};
1718

18-
export function removeDummyElement() {
19-
const el = document.querySelector(".dynamic-el");
20-
if (el) {
21-
el.remove();
22-
}
23-
}
24-
25-
export function mountDummyElement() {
26-
const newDiv = (document.querySelector(".dynamic-el") || document.createElement("div")) as HTMLElement;
27-
28-
newDiv.innerHTML = "This is a new Element";
29-
newDiv.style.display = "block";
30-
newDiv.style.padding = "20px";
31-
newDiv.style.backgroundColor = "black";
32-
newDiv.style.color = "white";
33-
newDiv.style.fontSize = "14px";
34-
newDiv.style.position = "fixed";
35-
newDiv.style.top = `${Math.random() * (500 - 30) + 30}px`;
36-
newDiv.style.left = `${Math.random() * (500 - 30) + 30}px`;
37-
newDiv.className = "dynamic-el";
38-
39-
document.body.appendChild(newDiv);
40-
}
41-
4219
function attachFirstButton(popover: PopoverDOM) {
4320
const firstButton = document.createElement("button");
4421
firstButton.innerText = "Go to First";

docs/src/components/Examples.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ExampleButton } from "./ExampleButton";
2828
<script>
2929
import { driver } from "driver.js";
3030
import "driver.js/dist/driver.css";
31-
import { mountDummyElement, removeDummyElement } from "../components/CodeSample";
31+
import { mountDummyElement, removeDummyElement } from "../lib/dummyElement";
3232

3333
function markDone(btn) {
3434
btn.classList.add("bg-gray-300", "hover:bg-gray-200", "line-through");

docs/src/lib/dummyElement.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Plain DOM helpers shared by the home-page examples script and the CodeSample
2+
// component. Kept in a framework-free module so it can be imported from vanilla
3+
// Astro <script> tags without pulling in React (which would otherwise trip the
4+
// @vitejs/plugin-react dev preamble guard and crash the script).
5+
6+
export function removeDummyElement() {
7+
const el = document.querySelector(".dynamic-el");
8+
if (el) {
9+
el.remove();
10+
}
11+
}
12+
13+
export function mountDummyElement() {
14+
const newDiv = (document.querySelector(".dynamic-el") || document.createElement("div")) as HTMLElement;
15+
16+
newDiv.innerHTML = "This is a new Element";
17+
newDiv.style.display = "block";
18+
newDiv.style.padding = "20px";
19+
newDiv.style.backgroundColor = "black";
20+
newDiv.style.color = "white";
21+
newDiv.style.fontSize = "14px";
22+
newDiv.style.position = "fixed";
23+
newDiv.style.top = `${Math.random() * (500 - 30) + 30}px`;
24+
newDiv.style.left = `${Math.random() * (500 - 30) + 30}px`;
25+
newDiv.className = "dynamic-el";
26+
27+
document.body.appendChild(newDiv);
28+
}

0 commit comments

Comments
 (0)