Skip to content

Instantly share code, notes, and snippets.

@jonathan-s
Last active December 22, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-s/7fc7cbe8c2b80831fb804565524316bb to your computer and use it in GitHub Desktop.
Save jonathan-s/7fc7cbe8c2b80831fb804565524316bb to your computer and use it in GitHub Desktop.
Stimulus.js snippets that are useful for controllers, see it as a playground for now.
import { Controller } from 'stimulus'
export default class extends Controller {
addClasses(element) {
let toId = element.dataset.toId
let toElement
if (toId) {
toElement = document.getElementById(toId)
} else {
toElement = element
}
let classes = element.dataset.addClasses.split(' ')
toElement.classList.add(...classes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment