question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to trigger the modal declaratively

See original GitHub issue

I want to throw in one more idea out there for your consideration. I also use Micromodal.js in my projects and they have this nice little feature that lets you trigger the modal declaratively without any code. It’s explained if you scroll to the third section. Here’s what it could look like with SweetAlert:

<button data-swal-trigger="hello-world-alert">Click me!</button>

<template id='hello-world-alert'>
    <swal-title>Hello world!</swal-title>
    <swal-html>Here I come...</swal-html>
</template>

I’m not familiar with the SweetAlert basecode so I don’t know how hard this would be. I guess you would need a MutationObserver to bind a click event listenr to any tag with the data-swal-trigger attribute.

_Originally posted by @pomartel in https://github.com/sweetalert2/sweetalert2/issues/2112#issuecomment-734835853_

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
limontecommented, Dec 2, 2020

Great advice, thank you @zenflow

I’m also planning to add the attributeName = 'data-swal-trigger' argument to the bindClickHandler() method, so users will be able to do this:

<button data-swal-trigger="hello-world-alert">Click me!</button>

<button data-swal-toast-trigger="hello-world-alert">Click me for a toast!</button>

<template id='hello-world-alert'>
  <swal-title>Hello world!</swal-title>
  <swal-html>Here I come...</swal-html>
</template>

<script>
  Swal.bindClickHandler() // data-swal-trigger will be used

  Swal.mixin({
    toast: true
  }).bindClickHandler('data-swal-toast-trigger')
</script>
2reactions
limontecommented, Dec 2, 2020

Should it be data-swal-template instead of data-swal-trigger, since the value of the attribute is a template id? Or if ergonomics is more important than explicit (& verbose) naming, maybe just data-swal?

<!-- data-swal-trigger -->
<button data-swal-trigger="hello-world-alert">Click me!</button>
<button data-swal-toast-trigger="hello-world-alert">Click me for a toast!</button>

<!-- data-swal-template -->
<button data-swal-template="hello-world-alert">Click me!</button>
<button data-swal-toast-template="hello-world-alert">Click me for a toast!</button>

<!-- data-swal-->
<button data-swal="hello-world-alert">Click me!</button>
<button data-swal-toast="hello-world-alert">Click me for a toast!</button>

My vote goes for data-swal-template 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I trigger a Bootstrap modal programmatically?
I'm using the modal as part of my signup process and there is server side validation involved. If there are problems I want...
Read more >
How to Trigger Modal for React Bootstrap | Pluralsight
In this guide, you will learn how to trigger a React Bootstrap Modal programmatically using state.
Read more >
UIB: UI Action to initiate Modal Dialog created in... - ServiceNow
Navigate to the Declarative Action that was created as part of the pre-requsities · Update the payload to have the route, size and...
Read more >
ion-modal: Ionic Mobile App Custom Modal API Component
A Modal is a dialog that appears on top of the app's content, and must be dismissed by the app before interaction can...
Read more >
Modal | Components - BootstrapVue
Modals are streamlined, but flexible dialog prompts powered by JavaScript and CSS. They support a number of use cases from user notification to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found