Skip to content

Instantly share code, notes, and snippets.

@poqudrof
Last active October 17, 2023 22:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poqudrof/8607dd7a24190ef2963579024c67ad62 to your computer and use it in GitHub Desktop.
Save poqudrof/8607dd7a24190ef2963579024c67ad62 to your computer and use it in GitHub Desktop.
Stimulus controller we created to replace ActiveLink .
// src/controllers/highlight_sidebar_controller.js
import { Controller } from "stimulus"
export default class extends Controller {
onCurrentController() {
let activeLinks = this.data.get("active-on"));
return activeLinks == App.controller;
}
connect() {
if (this.onCurrentController()) {
this.element.classList.add("active");
} else {
this.element.classList.remove("active");
}
}
}
<script>
App.controller = "Users";
</script>
(...)
<a class="nav-link"
data-controller="highlight-sidebar"
data-highlight-sidebar-active-on="Users"
href="/Users">
<span>show Users</span>
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment