Components

A Stimulus controller that add a mouse-tracing glow effect.


Installation

  1. Install the package

    $ yarn add stimulus-glow
    
  2. Register the controller in your application

    app/javascript/controllers/index.js
    import { Application } from '@hotwired/stimulus'
    import Glow from 'stimulus-glow'
    
    const application = Application.start()
    application.register('glow', Glow)
    

How it works

The concept of the effect is to clone an element, the overlay, and applying a CSS mask with a radia-gradiant property that follow the mouse.

Every element that has a glow class will be revealed by the mask but not the rest.

Usage

TailwindCSS is required to use this CSS, because we will use a custom variant to limit the glow effect to the overlay.

In your tailwind.config.js file, add this plugin:

tailwind.config.js
const plugin = require("tailwindcss/plugin")

module.exports = {
  content: ["./index.html", "./src/**/*.{js,ts}"],

  plugins: [
    plugin(
      ({ addVariant }) => {
        addVariant("glow", ".glow-capture .glow-overlay &")
      },
      {
        theme: {
          extend: {
            colors: {
              glow: "color-mix(in srgb, var(--glow-color) calc(<alpha-value> * 100%), transparent)",
            },
          },
        },
      },
    ),
  ],
}

Now in your CSS, add this class:

app/javascript/stylesheets/application.css
.glow-overlay {
  --glow-size: 25rem; /* Change the size of the glow effect here */

  position: absolute;
  inset: 0;
  pointer-events: none;
  user-select: none;
  opacity: var(--glow-opacity, 0);
  mask: radial-gradient(
    var(--glow-size) var(--glow-size) at var(--glow-x) var(--glow-y),
    var(--glow-color) 1%,
    transparent 50%
  );
  transition: 400ms mask ease;
  will-change: mask;
}

Here is a simplified version of the minimum markup you need:

app/views/index.html
<div data-controller="glow" class="relative glow-capture">
  <div data-glow-target="child" class="glow glow:ring-1 glow:border-glow glow:ring-glow glow:bg-glow/[.15]">
    <h2 class="font-bold text-2xl mb-4 text-gray-200 glow:text-glow/[.8]">Chicken Shawarma & Veggies</h2>

    <p class="text-sm text-gray-300 glow:text-glow">Vitae ducimus harum earum ratione autem esse ea!</p>

    <button class="glow:text-glow glow:border-glow glow:ring glow:ring-glow">Add to cart</button>
  </div>

  <div data-glow-target="overlay" class="glow-overlay" style="--glow-color: #f97316"></div>
</div>

I suggest you the inspect the index.html of the demo for a complete example.

Extending Controller

You can use inheritance to extend the functionality of any Stimulus component:

app/javascript/controllers/glow_controller.js
import Glow from "stimulus-glow"

export default class extends Glow {
  connect() {
    super.connect()
    console.log("Do what you want here.")
  }
}

This controller will automatically have access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.

Sponsors

Stimulus Component is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support Stimulus Components development on GitHub Sponsors. 🙏

Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Don't forget to drop a 🌟 on GitHub to support the project.

License

This project is released under the MIT license.