Skip to content

Instantly share code, notes, and snippets.

@bjo3rnf
Created April 2, 2021 08:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjo3rnf/fdff918d068f9e150d6deb562a6bef16 to your computer and use it in GitHub Desktop.
Save bjo3rnf/fdff918d068f9e150d6deb562a6bef16 to your computer and use it in GitHub Desktop.
Stimulus.js driven confirmable submit button
<button type="submit"
class="btn"
data-controller="confirm-submit"
data-confirm-submit-confirmation-label-value="Are you sure?"
data-confirm-submit-warning-class="btn--warning"
data-action="click->confirm-submit#click">
Submit
</button>
import { Controller } from 'stimulus'
export default class extends Controller {
static classes = [ 'warning' ]
static values = { confirmationLabel: String }
connect() {
this.confirmed = false
}
click(event) {
if (false === this.confirmed) {
event.preventDefault()
this.confirmed = true
this.element.classList.add(this.warningClass)
this.element.innerText = this.confirmationLabelValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment