I'd like to use this plugin in a stimulus controller
https://github.com/soxofaan/scrollocue/blob/master/js/scrollocue.js
// scrollocue.js
(function(window, $) {
$.fn.scrollocue = function(options) {
// Handle given options and default settings.
My stimulus controller
import { Controller } from '@hotwired/stimulus';
import $ from 'jquery';
window.$ = $;
import '../js/scrollocue.js';
Of course, this doesn't work, but I don't know how to pass the jQuery object to the function.
I can't use require, because everything is now an ES6 module. Most of the time these plugins are published on jsdelivr, then it's easy, but I'm not sure how to handle this directly.
You may be best to port this to Stimulus, this avoids an external dependency and lets you tweak the code to do what you want. Also, Stimulus gives you a really powerful way to change keyboard shortcuts and initial loading behaviour by just changing HTML attributes.
Stimulus controller
Here's a basic port of the jQuery code to Stimulus.
activethat allows you to determine what class name gets set (or a group of them if you are using Tailwind for example).index(the state that tracks what index is active, OR it can be used to set an initial index in the HTML) andlines(a CSS selector allowing the HTML to determine the elements to be focused on when moving up/down....valueChangedcallbacks means we can think about how the Controller reacts to state changes (the index changing).activeclasses.HTML
The rest of the 'event' listeners are set by Stimulus action attributes, here we can set up the click/up/down presses moving the item up and down.