locomotivemtl / locomotive-boilerplate

šŸš‚ Front-end boilerplate for projects by Locomotive.
https://locomotivemtl-boilerplate.vercel.app/
MIT License
458 stars 71 forks source link

Add custom JS events #122

Open lucasvallenet opened 2 years ago

lucasvallenet commented 2 years ago

Add /utils/events.js with addStartEvent and andEndEvents utils to easily bind an even on start/end with a debounce delay. Also added addScrollUpEvent and addScrollDownEvent to easily listen when the scroll direction updates

Usage of events :

addStartEvent

import { addStartEvent } from './utils/events'

// Detect document mousemove start with 1000ms delay
addStartEvent(document, 'mousemove', 1000)

document.addEventListener('mousemoveStart', () => {
   // ...callback
})

addEndEvent

import { addEndEvent } from './utils/events'

// Detect window resize end with 200ms delay (default value)
addEndEvent(window, 'resize')

window.addEventListener('resizeEnd', () => {
   // ...callback
})

addScrollUpEvent

import { addScrollUpEvent } from './utils/events'

// Add scroll up listener (binded by default to window)
addScrollUpEvent()

window.addEventListener('scrollUp', () => {
   // ...callback
})

addScrollDownEvent

import { addScrollDownEvent } from './utils/events'

// Add scroll down listener to specific DOM element
const $div = document.getElementById('#div')
addScrollUpEvent($div)

$div.addEventListener('scrollDown', () => {
   // ...callback
})
lucasvallenet commented 2 years ago

I made the requested changed.

I also blended the addScrollUpEvent / addScrollDownEvent to a unique function. There is only one scroll listener and if we want to know when we scroll up, we usually also want to know when we scroll down...

lucasvallenet commented 2 years ago

All that is missing is to exclude /www/assets/styles.

Didn't it reset on [0610a7d] ?

mcaskill commented 2 years ago

Didn't it reset on [0610a7d] ?

Oh, you reverted changes. I would suggest rebasing the master branch and dropping commits like that. Makes for a cleaner history.

Jerek0 commented 2 years ago

šŸ‘‹ Thanks for the PR!

addStartEvent / addEndEvent

Some things worry me about this:

Can you provide a concrete example where this would be useful aside from a global resizeend with a defined debounce delay? I wonder if that sort of custom event with a debounce should be done manually where needed only?

addScrollDirectionEvents

Regarding the scroll up/down events, it works well but I wonder if it's the right place for such a feature. Maybe it should be inside the Scroll module of the project instead? Also, we may want to prevent calling addScrollDirectionEvents multiple times on the same EventTarget.


By the way, it didn't work right away during my tests. I figured some variables/functions calls didn't exist, it's now fixed āœ…

arnvvd commented 1 year ago

Outcome from meeting on 2022-10-05 Status: On hold