"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Create a Resizable Navigation with Stimulus

Create a Resizable Navigation with Stimulus

Published on 2024-11-08
Browse:154

This article was originally published on Rails Designer—UI components library for Rails app, built with ViewComponent, designed with Tailwind CSS and enhanced with Hotwire.


If your app has a sidebar navigation, which is quite common with most screens being wide enough to support these days, making it resizable might be a great feature to add. Giving this customization allows your users to tweak the screen for the task at hand. Maybe they want to focus on writing their next big piece or maybe they split their screen, making the default width a bit too wide.

Create a Resizable Navigation with Stimulus

Whatever the reason, resizing the sidebar navigation (or any other component) is easy with JavaScript and thus with Stimulus. Let's dive right in. Let's set up the basics in HTML:

Content for your app here

Above HTML is using Tailwind CSS classes, but that's not required for this example. You can of course style it however you like.

Now the stimulus controller. As you noticed from above the handler (the element that can be dragged to resize) is not added in the HTML, it will instead be injected with JS.

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="resizer"
export default class extends Controller {
  static targets = ["container"];
  static values = {
    resizing: { type: Boolean, default: false },
    maxWidth: { type: Number, default: 360 } // in px
  };

  connect() {
    this.containerTarget.insertAdjacentHTML("beforeend", this.#handler);
  }

  // private

  get #handler() {
    return `
      
    `
  }
}

This will inject the handler next to the nav-element (absolute positioned). It also has a action to fire setup() on the mousedown event. Let's add it.

export default class extends Controller {
  // …
  connect() {
    this.containerTarget.insertAdjacentHTML("beforeend", this.#handler);

    this.resize = this.#resize.bind(this);
    this.stop = this.#stop.bind(this);
  }

  setup() {
      this.resizingValue = true;

      document.addEventListener('mousemove', this.resize);
      document.addEventListener('mouseup', this.stop);
  }
  // …
}

What's going on here? Why not add the #resize() and #stop() on respectively mousemove and mouseup events. This is to ensure that this refers to the controller instance when resize and stop are called as event listeners, preserving access to controller properties and methods.

Let's add the private functions #resize() and #stop().

export default class extends Controller {
  // …

  // private

  #resize(event) {
    if (!this.resizingValue) return;

    const width = event.clientX - this.containerTarget.offsetLeft;

    if (width 



The #resize() function calculates the new width of the container based on the mouse position (event.clientX) and updates the container's width, ensuring it doesn't exceed the maximum allowed width (set in values). The #stop() function stops the resizing process by setting the resizingValue to false and removing the event listeners.

If you head over to your browser, you are now able to resize the browser and not make it wider than the value set as maxWidth (360px by default).

Awsome! ? That's all you need to resize an element in your app using Stimulus. From here you can improve by storing the value in the user's settings (eg. via Redis) to be the same across browsers or store it in browser's LocalStorage to store it for that session (Rails Designer helps you by providing JS utilities for that).

Release Statement This article is reproduced at: https://dev.to/railsdesigner/create-a-resizable-navigation-with-stimulus-mkk?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3