kamrify commited on
Commit
4fb4c20
·
1 Parent(s): 3ddddd9

Fix event handlers not firing in drive

Browse files
docs/package.json CHANGED
@@ -16,7 +16,7 @@
16
  "@types/react": "^18.0.21",
17
  "@types/react-dom": "^18.0.6",
18
  "astro": "^2.7.0",
19
- "driver.js": "1.1.7-next.0",
20
  "react": "^18.0.0",
21
  "react-dom": "^18.0.0",
22
  "react-fast-marquee": "^1.6.0",
 
16
  "@types/react": "^18.0.21",
17
  "@types/react-dom": "^18.0.6",
18
  "astro": "^2.7.0",
19
+ "driver.js": "1.1.8-next.0",
20
  "react": "^18.0.0",
21
  "react-dom": "^18.0.0",
22
  "react-fast-marquee": "^1.6.0",
docs/pnpm-lock.yaml CHANGED
@@ -24,8 +24,8 @@ dependencies:
24
  specifier: ^2.7.0
25
  version: 2.7.0
26
  driver.js:
27
- specifier: 1.1.7-next.0
28
- version: 1.1.7-next.0
29
  react:
30
  specifier: ^18.0.0
31
  version: 18.0.0
@@ -1386,8 +1386,8 @@ packages:
1386
1387
  resolution: {integrity: sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==}
1388
 
1389
- /[email protected].7-next.0:
1390
- resolution: {integrity: sha512-NRjJXcOWR7ZpijJqNrGME1qJ4yS2xr0uSQpmYjHTJiiZ770Q6Sw5xVh+fRxggcyMpJT45bx51YPZig4zbhtEXw==}
1391
  dev: false
1392
 
1393
 
24
  specifier: ^2.7.0
25
  version: 2.7.0
26
  driver.js:
27
+ specifier: 1.1.8-next.0
28
+ version: 1.1.8-next.0
29
  react:
30
  specifier: ^18.0.0
31
  version: 18.0.0
 
1386
1387
  resolution: {integrity: sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==}
1388
 
1389
+ /[email protected].8-next.0:
1390
+ resolution: {integrity: sha512-3xI442GHwr6IvGnkmuEihOWTCXHaBa/M+iSgTVUpOz0JdkC6oyx8Eqtk7x8zS3YiehngEHEkDpJ5tl7l3yNPfA==}
1391
  dev: false
1392
 
1393
docs/src/content/guides/buttons.mdx ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: "Popover Buttons"
3
+ groupTitle: "Examples"
4
+ sort: 9
5
+ ---
6
+
7
+ import { CodeSample } from "../../components/CodeSample.tsx";
8
+
9
+ You can use the `showButtons` option to choose which buttons to show in the popover. The default value is `['next', 'previous', 'close']`.
10
+
11
+ <div id="driver-note" className="mb-5">
12
+ > **Note:** When using the `highlight` method to highlight a single element, the only button shown is the `close`
13
+ button. However, you can use the `showButtons` option to show other buttons as well. But the buttons won't do
14
+ anything. You will have to use the `onNextClick` and `onPreviousClick` callbacks to implement the functionality.
15
+ </div>
16
+
17
+ <div className='flex flex-col gap-1'>
18
+ <CodeSample
19
+ buttonText={"Show All Buttons"}
20
+ config={{
21
+ showButtons: [
22
+ 'next',
23
+ 'previous',
24
+ 'close'
25
+ ],
26
+ }}
27
+ tour={[
28
+ {
29
+ element: '#driver-note',
30
+ popover: {
31
+ title: 'Popover Title',
32
+ description: 'Popover Description'
33
+ }
34
+ },
35
+ {
36
+ element: '#driver-note p code:nth-child(4)',
37
+ popover: {
38
+ title: 'Popover Title',
39
+ description: 'Popover Description'
40
+ }
41
+ }
42
+ ]}
43
+ id={"code-sample"}
44
+ client:load>
45
+ ```js
46
+ import { driver } from "driver.js";
47
+ import "driver.js/dist/driver.css";
48
+
49
+ const driverObj = driver({
50
+ showButtons: [
51
+ 'next',
52
+ 'previous',
53
+ 'close'
54
+ ],
55
+ steps: [
56
+ {
57
+ element: '#first-element',
58
+ popover: {
59
+ title: 'Popover Title',
60
+ description: 'Popover Description'
61
+ }
62
+ },
63
+ {
64
+ element: '#second-element',
65
+ popover: {
66
+ title: 'Popover Title',
67
+ description: 'Popover Description'
68
+ }
69
+ }
70
+ ]
71
+ });
72
+
73
+ driverObj.drive();
74
+ ```
75
+ </CodeSample>
76
+ <CodeSample
77
+ buttonText="No Close Button"
78
+ config={{
79
+ showButtons: [
80
+ 'next',
81
+ 'previous',
82
+ ],
83
+ }}
84
+ tour={[
85
+ {
86
+ element: '#driver-note',
87
+ popover: {
88
+ title: 'Popover Title',
89
+ description: 'Popover Description'
90
+ }
91
+ },
92
+ {
93
+ element: '#driver-note code:nth-child(2)',
94
+ popover: {
95
+ title: 'Popover Title',
96
+ description: 'Popover Description'
97
+ }
98
+ }
99
+ ]}
100
+ id={"code-sample"}
101
+ client:load />
102
+ <CodeSample
103
+ buttonText="No Buttons (Use Arrows)"
104
+ config={{
105
+ showButtons: [undefined],
106
+ }}
107
+ tour={[
108
+ {
109
+ element: '#driver-note',
110
+ popover: {
111
+ title: 'Popover Title',
112
+ description: 'Popover Description'
113
+ }
114
+ },
115
+ {
116
+ element: '#driver-note code:nth-child(2)',
117
+ popover: {
118
+ title: 'Popover Title',
119
+ description: 'Popover Description',
120
+ side: 'bottom',
121
+ align: 'start'
122
+ }
123
+ }
124
+ ]}
125
+ id={"code-sample"}
126
+ client:load />
127
+ </div>
128
+
129
+ ## Change Button Text
130
+
131
+ You can also change the text of buttons using `nextBtnText`, `prevBtnText` and `doneBtnText` options.
132
+
133
+ <div className='flex flex-col gap-1'>
134
+ <CodeSample
135
+ heading="Change Button Text"
136
+ buttonText={"Change Button Text"}
137
+ config={{
138
+ showProgress: true,
139
+ nextBtnText: '—>',
140
+ prevBtnText: '<--',
141
+ doneBtnText: 'X',
142
+ }}
143
+ tour={[
144
+ {
145
+ element: '#code-sample-3',
146
+ popover: {
147
+ title: 'Popover Title',
148
+ description: 'Popover Description'
149
+ }
150
+ },
151
+ {
152
+ element: '#code-sample-3 code',
153
+ popover: {
154
+ title: 'Popover Title',
155
+ description: 'Popover Description'
156
+ }
157
+ }
158
+ ]}
159
+ id={"code-sample-3"}
160
+ client:load>
161
+ ```js
162
+ import { driver } from "driver.js";
163
+ import "driver.js/dist/driver.css";
164
+
165
+ const driverObj = driver({
166
+ nextBtnText: '—›',
167
+ prevBtnText: '‹—',
168
+ doneBtnText: '✕',
169
+ showProgress: true,
170
+ steps: [
171
+ // ...
172
+ ]
173
+ });
174
+
175
+ driverObj.drive();
176
+ ```
177
+ </CodeSample>
178
+ </div>
179
+
180
+ ## Event Handlers
181
+
182
+ You can use the `onNextClick`, `onPreviousClick` and `onCloseClick` callbacks to implement custom functionality when the user clicks on the next and previous buttons.
183
+
184
+ > Please note that when you configure these callbacks, the default functionality of the buttons will be disabled. You will have to implement the functionality yourself.
185
+
186
+ <CodeSample
187
+ buttonText={"Show Example"}
188
+ config={{
189
+ onNextClick:() => {
190
+ alert('Next Button Clicked');
191
+ },
192
+ onPrevClick:() => {
193
+ alert('Previous Button Clicked');
194
+ },
195
+ onCloseClick:() => {
196
+ alert('Close Button Clicked');
197
+ },
198
+ }}
199
+ tour={[
200
+ {
201
+ element: '#code-sample-4',
202
+ popover: {
203
+ title: 'Popover Title',
204
+ description: 'Popover Description'
205
+ }
206
+ },
207
+ {
208
+ element: '#code-sample-4 code',
209
+ popover: {
210
+ title: 'Popover Title',
211
+ description: 'Popover Description'
212
+ }
213
+ }
214
+ ]}
215
+ id={"code-sample-4"}
216
+ client:load>
217
+ ```js
218
+ import { driver } from "driver.js";
219
+ import "driver.js/dist/driver.css";
220
+
221
+ const driverObj = driver({
222
+ onNextClick:() => {
223
+ console.log('Next Button Clicked');
224
+ // Implement your own functionality here
225
+ driverObj.moveNext();
226
+ },
227
+ onPrevClick:() => {
228
+ console.log('Previous Button Clicked');
229
+ // Implement your own functionality here
230
+ driverObj.movePrevious();
231
+ },
232
+ onCloseClick:() => {
233
+ console.log('Close Button Clicked');
234
+ // Implement your own functionality here
235
+ driverObj.destroy();
236
+ },
237
+ steps: [
238
+ // ...
239
+ ]
240
+ });
241
+
242
+ driverObj.drive();
243
+ ```
244
+ </CodeSample>
docs/src/content/guides/{customize-overlay.mdx → styling-overlay.mdx} RENAMED
@@ -8,6 +8,8 @@ import { CodeSample } from "../../components/CodeSample.tsx";
8
 
9
  You can customize the overlay opacity and color using `overlayOpacity` and `overlayColor` options to change the look of the overlay.
10
 
 
 
11
  ## Overlay Color
12
 
13
  Here are some driver.js examples with different overlay colors.
@@ -17,8 +19,9 @@ import { driver } from "driver.js";
17
  import "driver.js/dist/driver.css";
18
 
19
  const driverObj = driver({
20
- overlayColor: 'blue'
21
  });
 
22
  driverObj.highlight({
23
  popover: {
24
  title: 'Pass any RGB Color',
@@ -27,17 +30,52 @@ driverObj.highlight({
27
  });
28
  ```
29
 
30
- <CodeSample
31
- buttonText={"Blue Color"}
32
- config={{
33
- overlayColor: 'blue'
34
- }}
35
- highlight={{
36
- popover: {
37
- title: 'Pass any RGB Color',
38
- description: 'Here we have set the overlay color to be blue. You can pass any RGB color to overlayColor option.',
39
- }
40
- }}
41
- id={"left-start"}
42
- client:load
43
- />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  You can customize the overlay opacity and color using `overlayOpacity` and `overlayColor` options to change the look of the overlay.
10
 
11
+ > **Note:** In the examples below we have used `highlight` method to highlight the elements. The same configuration applies to the tour steps as well.
12
+
13
  ## Overlay Color
14
 
15
  Here are some driver.js examples with different overlay colors.
 
19
  import "driver.js/dist/driver.css";
20
 
21
  const driverObj = driver({
22
+ overlayColor: 'red'
23
  });
24
+
25
  driverObj.highlight({
26
  popover: {
27
  title: 'Pass any RGB Color',
 
30
  });
31
  ```
32
 
33
+ <div className='flex flex-col gap-1 -mt-5'>
34
+ <CodeSample
35
+ buttonText={"Red Color"}
36
+ config={{
37
+ overlayColor: 'red',
38
+ overlayOpacity: 0.3
39
+ }}
40
+ highlight={{
41
+ popover: {
42
+ title: 'Pass any RGB Color',
43
+ description: 'Here we have set the overlay color to be blue. You can pass any RGB color to overlayColor option.',
44
+ }
45
+ }}
46
+ id={"left-start"}
47
+ client:load
48
+ />
49
+
50
+ <CodeSample
51
+ buttonText={"Blue Color"}
52
+ config={{
53
+ overlayColor: 'blue',
54
+ overlayOpacity: 0.3
55
+ }}
56
+ highlight={{
57
+ popover: {
58
+ title: 'Pass any RGB Color',
59
+ description: 'Here we have set the overlay color to be blue. You can pass any RGB color to overlayColor option.',
60
+ }
61
+ }}
62
+ id={"left-start"}
63
+ client:load
64
+ />
65
+
66
+ <CodeSample
67
+ buttonText={"Yellow Color"}
68
+ config={{
69
+ overlayColor: 'yellow',
70
+ overlayOpacity: 0.3
71
+ }}
72
+ highlight={{
73
+ popover: {
74
+ title: 'Pass any RGB Color',
75
+ description: 'Here we have set the overlay color to be blue. You can pass any RGB color to overlayColor option.',
76
+ }
77
+ }}
78
+ id={"left-start"}
79
+ client:load
80
+ />
81
+ </div>
index.html CHANGED
@@ -187,6 +187,7 @@
187
  <button id="button-texts">Button Texts</button>
188
  <button id="disabled-buttons">Disabled Buttons</button>
189
  <button id="button-config-events">Button Listeners</button>
 
190
  </div>
191
 
192
  <br />
@@ -712,8 +713,12 @@ npm install driver.js</pre
712
 
713
  document.getElementById("button-config-events").addEventListener("click", () => {
714
  const driverObj = driver({
715
- onNextClick: () => alert("Next Clicked"),
716
- onPrevClick: () => alert("Previous Clicked"),
 
 
 
 
717
  onCloseClick: () => {
718
  driverObj.destroy();
719
  },
@@ -729,6 +734,29 @@ npm install driver.js</pre
729
  });
730
  });
731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
732
  document.getElementById("is-active-btn").addEventListener("click", () => {
733
  alert(driver().isActive());
734
  });
 
187
  <button id="button-texts">Button Texts</button>
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 />
 
713
 
714
  document.getElementById("button-config-events").addEventListener("click", () => {
715
  const driverObj = driver({
716
+ onNextClick: () => {
717
+ alert("Next Clicked");
718
+ },
719
+ onPrevClick: () => {
720
+ alert("Previous Clicked");
721
+ },
722
  onCloseClick: () => {
723
  driverObj.destroy();
724
  },
 
734
  });
735
  });
736
 
737
+ document.getElementById("button-tour-events").addEventListener("click", () => {
738
+ const driverObj = driver({
739
+ onNextClick: () => {
740
+ alert("Next Clicked");
741
+ driverObj.moveNext();
742
+ },
743
+ onPrevClick: () => {
744
+ alert("Previous Clicked");
745
+ driverObj.movePrevious();
746
+ },
747
+ onCloseClick: () => {
748
+ driverObj.destroy();
749
+ },
750
+ steps: [
751
+ { popover: { title: "Some title", description: "Some description" }},
752
+ { popover: { title: "Another title", description: "Some description" }},
753
+ { popover: { title: "Yet another title", description: "Some description" }},
754
+ ]
755
+ });
756
+
757
+ driverObj.drive();
758
+ });
759
+
760
  document.getElementById("is-active-btn").addEventListener("click", () => {
761
  alert(driver().isActive());
762
  });
src/driver.ts CHANGED
@@ -157,10 +157,18 @@ export function driver(options: Config = {}) {
157
  .replace("{{total}}", `${steps.length}`);
158
 
159
  const configuredButtons = currentStep.popover?.showButtons || getConfig("showButtons");
160
- const calculatedButtons: AllowedButtons[] = ["next", "previous", ...(allowsClosing ? ["close" as AllowedButtons] : [])].filter((b) => {
 
 
 
 
161
  return !configuredButtons?.length || configuredButtons.includes(b as AllowedButtons);
162
  }) as AllowedButtons[];
163
 
 
 
 
 
164
  highlight({
165
  ...currentStep,
166
  popover: {
@@ -169,17 +177,17 @@ export function driver(options: Config = {}) {
169
  disableButtons: [...(!hasPreviousStep ? ["previous" as AllowedButtons] : [])],
170
  showProgress: showProgress,
171
  progressText: progressTextReplaced,
172
- onNextClick: () => {
173
  if (!hasNextStep) {
174
  destroy();
175
  } else {
176
  drive(stepIndex + 1);
177
  }
178
  },
179
- onPrevClick: () => {
180
  drive(stepIndex - 1);
181
  },
182
- onCloseClick: () => {
183
  destroy();
184
  },
185
  ...(currentStep?.popover || {}),
 
157
  .replace("{{total}}", `${steps.length}`);
158
 
159
  const configuredButtons = currentStep.popover?.showButtons || getConfig("showButtons");
160
+ const calculatedButtons: AllowedButtons[] = [
161
+ "next",
162
+ "previous",
163
+ ...(allowsClosing ? ["close" as AllowedButtons] : []),
164
+ ].filter(b => {
165
  return !configuredButtons?.length || configuredButtons.includes(b as AllowedButtons);
166
  }) as AllowedButtons[];
167
 
168
+ const onNextClick = currentStep.popover?.onNextClick || getConfig("onNextClick");
169
+ const onPrevClick = currentStep.popover?.onPrevClick || getConfig("onPrevClick");
170
+ const onCloseClick = currentStep.popover?.onCloseClick || getConfig("onCloseClick");
171
+
172
  highlight({
173
  ...currentStep,
174
  popover: {
 
177
  disableButtons: [...(!hasPreviousStep ? ["previous" as AllowedButtons] : [])],
178
  showProgress: showProgress,
179
  progressText: progressTextReplaced,
180
+ onNextClick: onNextClick ? onNextClick : () => {
181
  if (!hasNextStep) {
182
  destroy();
183
  } else {
184
  drive(stepIndex + 1);
185
  }
186
  },
187
+ onPrevClick: onPrevClick ? onPrevClick : () => {
188
  drive(stepIndex - 1);
189
  },
190
+ onCloseClick: onCloseClick ? onCloseClick : () => {
191
  destroy();
192
  },
193
  ...(currentStep?.popover || {}),