Skip to content

Instantly share code, notes, and snippets.

@jeremysmithco
Created February 18, 2025 20:06
Show Gist options
  • Save jeremysmithco/c7a1804939fc1f78c4affa1b8d89f0c1 to your computer and use it in GitHub Desktop.
Save jeremysmithco/c7a1804939fc1f78c4affa1b8d89f0c1 to your computer and use it in GitHub Desktop.
Stimulus.js RangeError Issue
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static values = { highlighted: Boolean };
static classes = ["highlighted"];
toggle() {
this.highlightedValue = !this.highlightedValue;
}
highlightedValueChanged() {
this.updateClasses();
this.dispatch("toggled");
}
updateClasses() {
if (this.highlightedValue) {
this.element.classList.add(this.highlightedClass);
} else {
this.element.classList.remove(this.highlightedClass);
}
}
}
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static outlets = ["highlightable"];
static targets = ["counter"];
connect() {
this.render();
}
render() {
this.counterTarget.innerText = this.highlightableOutlets.filter(
highlightableOutlet => highlightableOutlet.highlightedValue
).length;
}
}
<%= tag.div(data: {
controller: "highlightable-count",
action: "highlightable:toggled->highlightable-count#render",
highlightable_count_highlightable_outlet: ".highlightable" }) do %>
<%= tag.div(data: { highlightable_count_target: "counter" },
class: "text-sm w-20 p-1 bg-gray-500 text-white rounded mb-4") %>
<% 50.times.each do |i| %>
<%= tag.button(i, id: "highlightable-#{i}", data: {
controller: "highlightable",
action: "highlightable#toggle",
highlightable_highlighted_value: [true, false].sample,
highlightable_highlighted_class: "bg-yellow-100"
}, class: "highlightable text-sm w-20 p-1 border-2 border-gray-300 rounded mb-1") %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment