Skip to content

Commit

Permalink
Merge pull request #195 from palcarazm/fix/touch-event
Browse files Browse the repository at this point in the history
Fix/touch event
  • Loading branch information
palcarazm authored Dec 8, 2024
2 parents c72ab08 + dae116c commit a5be5c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
37 changes: 19 additions & 18 deletions js/bootstrap5-toggle.ecmas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
*/


"use strict";
function sanitize(text) {
if (!text) return text; // handle null or undefined
Expand Down Expand Up @@ -311,33 +310,35 @@ function sanitize(text) {

// 9: Add listeners
ecmasToggle.addEventListener(
"touchstart",
"pointerdown",
(e) => {
this.#toggleActionPerformed(e);
},
{ passive: true }
);
ecmasToggle.addEventListener("click", (e) => {
this.#toggleActionPerformed(e);
});
ecmasToggle.addEventListener("keypress", (e) => {
if (e.key == " ") {
this.#toggleActionPerformed(e);
}
});
ecmasToggle.addEventListener(
"keypress",
(e) => {
if (e.key == " ") {
this.#toggleActionPerformed(e);
}
},
{ passive: true }
);

if (this.element.id) {
document
.querySelectorAll('label[for="' + this.element.id + '"]')
.forEach((label) => {
label.addEventListener("touchstart", (_e) => {
this.toggle();
ecmasToggle.focus();
});
label.addEventListener("click", (_e) => {
this.toggle();
ecmasToggle.focus();
});
label.addEventListener(
"pointerdown",
(e) => {
e.preventDefault();
this.toggle();
ecmasToggle.focus();
},
{ passive: true }
);
});
}

Expand Down
11 changes: 4 additions & 7 deletions js/bootstrap5-toggle.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
*/


"use strict";
function sanitize(text) {
if (!text) return text; // handle null or undefined
Expand Down Expand Up @@ -272,10 +271,7 @@ function sanitize(text) {
}

// 9: Add listeners
this.$toggle.on("touchstart", (e) => {
toggleActionPerformed(e, this);
});
this.$toggle.on("click", (e) => {
this.$toggle.on("pointerdown", (e) => {
toggleActionPerformed(e, this);
});
this.$toggle.on("keypress", (e) => {
Expand All @@ -286,8 +282,9 @@ function sanitize(text) {

if (this.$element.prop("id")) {
$('label[for="' + this.$element.prop("id") + '"]').on(
"touchstart click",
(_e) => {
"click touchstart",
(e) => {
e.preventDefault();
this.toggle();
this.$toggle.focus();
}
Expand Down

0 comments on commit a5be5c8

Please sign in to comment.