Fix custom button clicks not working
Browse files- docs/src/components/CodeSample.tsx +20 -5
- docs/src/content/guides/styling-popover.mdx +114 -29
- docs/src/layouts/BaseLayout.astro +63 -0
- index.html +22 -0
- src/events.ts +3 -3
- src/popover.ts +4 -2
docs/src/components/CodeSample.tsx
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
import type { Config, DriveStep } from "driver.js";
|
2 |
import { driver } from "driver.js";
|
3 |
import "driver.js/dist/driver.css";
|
4 |
import { useState } from "react";
|
|
|
5 |
|
6 |
type CodeSampleProps = {
|
7 |
heading?: string;
|
@@ -40,18 +41,32 @@ function mountDummyElement() {
|
|
40 |
document.body.appendChild(newDiv);
|
41 |
}
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
export function CodeSample(props: CodeSampleProps) {
|
44 |
-
const [driverObj, setDriverObj] = useState<any>(null);
|
45 |
const { heading, id, children, buttonText = "Show me an Example", className, config, highlight, tour } = props;
|
46 |
|
|
|
|
|
|
|
|
|
47 |
function onClick() {
|
48 |
if (highlight) {
|
49 |
const driverObj = driver({
|
50 |
...config,
|
51 |
});
|
52 |
-
driverObj.highlight(highlight);
|
53 |
|
54 |
-
|
|
|
55 |
} else if (tour) {
|
56 |
if (id === "confirm-destroy") {
|
57 |
config!.onDestroyStarted = () => {
|
@@ -113,8 +128,8 @@ export function CodeSample(props: CodeSampleProps) {
|
|
113 |
steps: tour,
|
114 |
});
|
115 |
|
|
|
116 |
driverObj.drive();
|
117 |
-
setDriverObj(driverObj);
|
118 |
}
|
119 |
}
|
120 |
|
|
|
1 |
+
import type { Config, DriveStep, PopoverDOM } from "driver.js";
|
2 |
import { driver } from "driver.js";
|
3 |
import "driver.js/dist/driver.css";
|
4 |
import { useState } from "react";
|
5 |
+
import { onDriverClick } from "../../../src/events";
|
6 |
|
7 |
type CodeSampleProps = {
|
8 |
heading?: string;
|
|
|
41 |
document.body.appendChild(newDiv);
|
42 |
}
|
43 |
|
44 |
+
function attachFirstButton(popover: PopoverDOM) {
|
45 |
+
const firstButton = document.createElement("button");
|
46 |
+
firstButton.innerText = "First";
|
47 |
+
popover.footerButtons.appendChild(firstButton);
|
48 |
+
|
49 |
+
console.log(firstButton);
|
50 |
+
firstButton.addEventListener("click", () => {
|
51 |
+
console.log('clicked');
|
52 |
+
});
|
53 |
+
}
|
54 |
+
|
55 |
export function CodeSample(props: CodeSampleProps) {
|
|
|
56 |
const { heading, id, children, buttonText = "Show me an Example", className, config, highlight, tour } = props;
|
57 |
|
58 |
+
if (id === "demo-hook-theme") {
|
59 |
+
config!.onPopoverRendered = attachFirstButton;
|
60 |
+
}
|
61 |
+
|
62 |
function onClick() {
|
63 |
if (highlight) {
|
64 |
const driverObj = driver({
|
65 |
...config,
|
66 |
});
|
|
|
67 |
|
68 |
+
window.driverObj = driverObj;
|
69 |
+
driverObj.highlight(highlight);
|
70 |
} else if (tour) {
|
71 |
if (id === "confirm-destroy") {
|
72 |
config!.onDestroyStarted = () => {
|
|
|
128 |
steps: tour,
|
129 |
});
|
130 |
|
131 |
+
window.driverObj = driverObj;
|
132 |
driverObj.drive();
|
|
|
133 |
}
|
134 |
}
|
135 |
|
docs/src/content/guides/styling-popover.mdx
CHANGED
@@ -12,38 +12,123 @@ Alternatively, if want to modify the Popover DOM, you can use the `onPopoverRend
|
|
12 |
|
13 |
We have added a few examples below but have a look at the [theming section](/docs/theming#styling-popover) for detailed guide including class names to target etc.
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
overlayColor: 'red',
|
38 |
-
overlayOpacity: 0.3
|
39 |
-
}}
|
40 |
-
highlight={{
|
41 |
popover: {
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
}
|
45 |
-
}
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
We have added a few examples below but have a look at the [theming section](/docs/theming#styling-popover) for detailed guide including class names to target etc.
|
14 |
|
15 |
+
<CodeSample
|
16 |
+
heading="Using CSS"
|
17 |
+
buttonText={"Driver.js Website Theme"}
|
18 |
+
config={{
|
19 |
+
prevBtnText: '← Previous',
|
20 |
+
nextBtnText: 'Next →',
|
21 |
+
doneBtnText: 'Done',
|
22 |
+
showButtons: ['next', 'previous'],
|
23 |
+
popoverClass: 'driverjs-theme'
|
24 |
+
}}
|
25 |
+
tour={[
|
26 |
+
{
|
27 |
+
element: '#demo-theme',
|
28 |
+
popover: {
|
29 |
+
align: 'start',
|
30 |
+
side: 'left',
|
31 |
+
title: 'Style However You Want',
|
32 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
33 |
+
}
|
34 |
+
},
|
35 |
+
{
|
36 |
+
element: 'h1',
|
37 |
+
popover: {
|
38 |
+
align: 'start',
|
39 |
+
side: 'bottom',
|
40 |
+
title: 'Style However You Want',
|
41 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
42 |
+
}
|
43 |
+
},
|
44 |
+
{
|
45 |
+
element: 'p a',
|
46 |
+
popover: {
|
47 |
+
align: 'start',
|
48 |
+
side: 'left',
|
49 |
+
title: 'Style However You Want',
|
50 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
51 |
+
}
|
52 |
+
}
|
53 |
+
]}
|
54 |
+
id={"demo-theme"}
|
55 |
+
client:load
|
56 |
+
>
|
57 |
+
```js
|
58 |
+
import { driver } from "driver.js";
|
59 |
+
import "driver.js/dist/driver.css";
|
60 |
|
61 |
+
const driverObj = driver({
|
62 |
+
popoverClass: 'driverjs-theme'
|
63 |
+
});
|
64 |
|
65 |
+
driverObj.highlight({
|
66 |
+
element: '#demo-theme',
|
67 |
+
popover: {
|
68 |
+
title: 'Style However You Want',
|
69 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
70 |
+
}
|
71 |
+
});
|
72 |
+
```
|
73 |
+
</CodeSample>
|
74 |
|
75 |
+
<br/>
|
76 |
|
77 |
+
<CodeSample
|
78 |
+
heading="Using Hook"
|
79 |
+
buttonText={"Driver.js Website Theme"}
|
80 |
+
config={{
|
81 |
+
prevBtnText: '← Previous',
|
82 |
+
nextBtnText: 'Next →',
|
83 |
+
doneBtnText: 'Done',
|
84 |
+
showButtons: ['next', 'previous'],
|
85 |
+
}}
|
86 |
+
tour={[
|
87 |
+
{
|
88 |
+
element: '#demo-hook-theme',
|
|
|
|
|
|
|
|
|
89 |
popover: {
|
90 |
+
align: 'start',
|
91 |
+
side: 'left',
|
92 |
+
title: 'Style However You Want',
|
93 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
94 |
}
|
95 |
+
},
|
96 |
+
{
|
97 |
+
element: 'h1',
|
98 |
+
popover: {
|
99 |
+
align: 'start',
|
100 |
+
side: 'bottom',
|
101 |
+
title: 'Style However You Want',
|
102 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
103 |
+
}
|
104 |
+
},
|
105 |
+
{
|
106 |
+
element: 'p a',
|
107 |
+
popover: {
|
108 |
+
align: 'start',
|
109 |
+
side: 'left',
|
110 |
+
title: 'Style However You Want',
|
111 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
112 |
+
}
|
113 |
+
}
|
114 |
+
]}
|
115 |
+
id={"demo-hook-theme"}
|
116 |
+
client:load
|
117 |
+
>
|
118 |
+
```js
|
119 |
+
import { driver } from "driver.js";
|
120 |
+
import "driver.js/dist/driver.css";
|
121 |
+
|
122 |
+
const driverObj = driver({
|
123 |
+
popoverClass: 'driverjs-theme'
|
124 |
+
});
|
125 |
+
|
126 |
+
driverObj.highlight({
|
127 |
+
element: '#demo-theme',
|
128 |
+
popover: {
|
129 |
+
title: 'Style However You Want',
|
130 |
+
description: 'You can use the default class names and override the styles or you can pass a custom class name to the popoverClass option either globally or per step.'
|
131 |
+
}
|
132 |
+
});
|
133 |
+
```
|
134 |
+
</CodeSample>
|
docs/src/layouts/BaseLayout.astro
CHANGED
@@ -17,6 +17,69 @@ const { title } = Astro.props;
|
|
17 |
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
18 |
<meta name="generator" content={Astro.generator} />
|
19 |
<title>{title}</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
</head>
|
21 |
<body>
|
22 |
<slot />
|
|
|
17 |
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
18 |
<meta name="generator" content={Astro.generator} />
|
19 |
<title>{title}</title>
|
20 |
+
|
21 |
+
<style is:global>
|
22 |
+
.driver-popover.driverjs-theme {
|
23 |
+
background-color: #fde047;
|
24 |
+
color: #000;
|
25 |
+
}
|
26 |
+
|
27 |
+
.driver-popover.driverjs-theme .driver-popover-title {
|
28 |
+
font-size: 20px;
|
29 |
+
}
|
30 |
+
|
31 |
+
.driver-popover.driverjs-theme .driver-popover-title,
|
32 |
+
.driver-popover.driverjs-theme .driver-popover-description,
|
33 |
+
.driver-popover.driverjs-theme .driver-popover-progress-text {
|
34 |
+
color: #000;
|
35 |
+
}
|
36 |
+
|
37 |
+
.driver-popover.driverjs-theme button {
|
38 |
+
flex: 1;
|
39 |
+
text-align: center;
|
40 |
+
background-color: #000;
|
41 |
+
color: #ffffff;
|
42 |
+
border: 2px solid #000;
|
43 |
+
text-shadow: none;
|
44 |
+
font-size: 14px;
|
45 |
+
padding: 5px 8px;
|
46 |
+
border-radius: 6px;
|
47 |
+
}
|
48 |
+
|
49 |
+
.driver-popover.driverjs-theme button:hover {
|
50 |
+
background-color: #000;
|
51 |
+
color: #ffffff;
|
52 |
+
}
|
53 |
+
|
54 |
+
.driver-popover.driverjs-theme .driver-popover-navigation-btns {
|
55 |
+
justify-content: space-between;
|
56 |
+
gap: 3px;
|
57 |
+
}
|
58 |
+
|
59 |
+
.driver-popover.driverjs-theme .driver-popover-close-btn {
|
60 |
+
color: #9b9b9b;
|
61 |
+
}
|
62 |
+
|
63 |
+
.driver-popover.driverjs-theme .driver-popover-close-btn:hover {
|
64 |
+
color: #000;
|
65 |
+
}
|
66 |
+
|
67 |
+
.driver-popover.driverjs-theme .driver-popover-arrow-side-left.driver-popover-arrow {
|
68 |
+
border-left-color: #fde047;
|
69 |
+
}
|
70 |
+
|
71 |
+
.driver-popover.driverjs-theme .driver-popover-arrow-side-right.driver-popover-arrow {
|
72 |
+
border-right-color: #fde047;
|
73 |
+
}
|
74 |
+
|
75 |
+
.driver-popover.driverjs-theme .driver-popover-arrow-side-top.driver-popover-arrow {
|
76 |
+
border-top-color: #fde047;
|
77 |
+
}
|
78 |
+
|
79 |
+
.driver-popover.driverjs-theme .driver-popover-arrow-side-bottom.driver-popover-arrow {
|
80 |
+
border-bottom-color: #fde047;
|
81 |
+
}
|
82 |
+
</style>
|
83 |
</head>
|
84 |
<body>
|
85 |
<slot />
|
index.html
CHANGED
@@ -188,6 +188,7 @@
|
|
188 |
<button id="disabled-buttons">Disabled Buttons</button>
|
189 |
<button id="button-config-events">Button Listeners</button>
|
190 |
<button id="button-tour-events">Tour Button Listeners</button>
|
|
|
191 |
</div>
|
192 |
|
193 |
<br />
|
@@ -757,6 +758,27 @@ npm install driver.js</pre
|
|
757 |
driverObj.drive();
|
758 |
});
|
759 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
760 |
document.getElementById("is-active-btn").addEventListener("click", () => {
|
761 |
alert(driver().isActive());
|
762 |
});
|
|
|
188 |
<button id="disabled-buttons">Disabled Buttons</button>
|
189 |
<button id="button-config-events">Button Listeners</button>
|
190 |
<button id="button-tour-events">Tour Button Listeners</button>
|
191 |
+
<button id="popover-hook-config">Popover Modified in Hook</button>
|
192 |
</div>
|
193 |
|
194 |
<br />
|
|
|
758 |
driverObj.drive();
|
759 |
});
|
760 |
|
761 |
+
document.getElementById("popover-hook-config").addEventListener("click", () => {
|
762 |
+
const driverObj = driver({
|
763 |
+
onPopoverRendered: popover => {
|
764 |
+
const firstButton = document.createElement("button");
|
765 |
+
firstButton.innerText = "First";
|
766 |
+
popover.footerButtons.appendChild(firstButton);
|
767 |
+
|
768 |
+
firstButton.addEventListener("click", () => {
|
769 |
+
console.log("First Button Clicked");
|
770 |
+
});
|
771 |
+
},
|
772 |
+
steps: [
|
773 |
+
{ popover: { title: "Some title", description: "Some description" }},
|
774 |
+
{ popover: { title: "Another title", description: "Some description" }},
|
775 |
+
{ popover: { title: "Yet another title", description: "Some description" }},
|
776 |
+
]
|
777 |
+
});
|
778 |
+
|
779 |
+
driverObj.drive();
|
780 |
+
});
|
781 |
+
|
782 |
document.getElementById("is-active-btn").addEventListener("click", () => {
|
783 |
alert(driver().isActive());
|
784 |
});
|
src/events.ts
CHANGED
@@ -50,11 +50,10 @@ export function onDriverClick(
|
|
50 |
|
51 |
if (!shouldPreventDefault || shouldPreventDefault(target)) {
|
52 |
e.preventDefault();
|
|
|
|
|
53 |
}
|
54 |
|
55 |
-
e.stopPropagation();
|
56 |
-
e.stopImmediatePropagation();
|
57 |
-
|
58 |
listener?.(e);
|
59 |
};
|
60 |
|
@@ -84,6 +83,7 @@ export function initEvents() {
|
|
84 |
}
|
85 |
|
86 |
export function destroyEvents() {
|
|
|
87 |
window.removeEventListener("resize", requireRefresh);
|
88 |
window.removeEventListener("scroll", requireRefresh);
|
89 |
}
|
|
|
50 |
|
51 |
if (!shouldPreventDefault || shouldPreventDefault(target)) {
|
52 |
e.preventDefault();
|
53 |
+
e.stopPropagation();
|
54 |
+
e.stopImmediatePropagation();
|
55 |
}
|
56 |
|
|
|
|
|
|
|
57 |
listener?.(e);
|
58 |
};
|
59 |
|
|
|
83 |
}
|
84 |
|
85 |
export function destroyEvents() {
|
86 |
+
window.removeEventListener("keyup", onKeyup);
|
87 |
window.removeEventListener("resize", requireRefresh);
|
88 |
window.removeEventListener("scroll", requireRefresh);
|
89 |
}
|
src/popover.ts
CHANGED
@@ -193,9 +193,11 @@ export function renderPopover(element: Element, step: DriveStep) {
|
|
193 |
return undefined;
|
194 |
},
|
195 |
target => {
|
196 |
-
// Only prevent the default action if we're clicking on a button
|
197 |
// This allows us to have links inside the popover title and description
|
198 |
-
return !popover?.description.contains(target)
|
|
|
|
|
199 |
}
|
200 |
);
|
201 |
|
|
|
193 |
return undefined;
|
194 |
},
|
195 |
target => {
|
196 |
+
// Only prevent the default action if we're clicking on a driver button
|
197 |
// This allows us to have links inside the popover title and description
|
198 |
+
return !popover?.description.contains(target)
|
199 |
+
&& !popover?.title.contains(target)
|
200 |
+
&& target.className.includes("driver-popover");
|
201 |
}
|
202 |
);
|
203 |
|