Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactors On #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
🔀 Syncs on code to Alpine
  • Loading branch information
ekwoka committed Jun 30, 2024
commit 339007fbf674a45bdb904884cb624a4b6e5acd57
88 changes: 53 additions & 35 deletions packages/alpinets/src/utils/on.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementWithXAttributes } from '../types';
import type { ElementWithXAttributes } from '../types';
import { debounce } from './debounce';
import { camelCase, dotSyntax, kebabCase } from './stringTransformers';
import { throttle } from './throttle';
@@ -34,20 +34,46 @@ export const on = (
if (modifiers.includes('capture')) options.capture = true;
if (modifiers.includes('window')) listenerTarget = window;
if (modifiers.includes('document')) listenerTarget = document;

if (modifiers.includes('debounce')) {
const nextModifier =
modifiers[modifiers.indexOf('debounce') + 1] || 'invalid-wait';
const wait = isNumeric(nextModifier.split('ms')[0])
? Number(nextModifier.split('ms')[0])
: 250;

handler = debounce(handler, wait);
}

if (modifiers.includes('throttle')) {
const nextModifier =
modifiers[modifiers.indexOf('throttle') + 1] || 'invalid-limit';
const limit = isNumeric(nextModifier.split('ms')[0])
? Number(nextModifier.split('ms')[0])
: 250;

handler = throttle(handler, limit);
}

if (modifiers.includes('prevent'))
handler = wrapHandler(handler, (next, e) => {
e.preventDefault();
next(e);
});

if (modifiers.includes('stop'))
handler = wrapHandler(handler, (next, e) => {
e.stopPropagation();
next(e);
});
if (modifiers.includes('self'))

if (modifiers.includes('once')) {
handler = wrapHandler(handler, (next, e) => {
e.target === el && next(e);
next(e);

listenerTarget.removeEventListener(event, handler, options);
});
}

if (modifiers.includes('away') || modifiers.includes('outside')) {
listenerTarget = document;
@@ -67,43 +93,20 @@ export const on = (
});
}

if (modifiers.includes('once')) {
if (modifiers.includes('self'))
handler = wrapHandler(handler, (next, e) => {
next(e);

listenerTarget.removeEventListener(event, handler, options);
e.target === el && next(e);
});
}

// Handle :keydown and :keyup listeners.
handler = wrapHandler(handler, (next, e) => {
if (isKeyEvent(event)) {
if (isKeyEvent(event) || isClickEvent(event)) {
handler = wrapHandler(handler, (next, e) => {
if (isListeningForASpecificKeyThatHasntBeenPressed(e, modifiers)) {
return;
}
}

next(e);
});

if (modifiers.includes('debounce')) {
const nextModifier =
modifiers[modifiers.indexOf('debounce') + 1] || 'invalid-wait';
const wait = isNumeric(nextModifier.split('ms')[0])
? Number(nextModifier.split('ms')[0])
: 250;

handler = debounce(handler, wait);
}

if (modifiers.includes('throttle')) {
const nextModifier =
modifiers[modifiers.indexOf('throttle') + 1] || 'invalid-limit';
const limit = isNumeric(nextModifier.split('ms')[0])
? Number(nextModifier.split('ms')[0])
: 250;

handler = throttle(handler, limit);
next(e);
});
}

listenerTarget.addEventListener(event, handler, options);
@@ -121,15 +124,28 @@ export const isNumeric = (subject: unknown): subject is number =>
const isKeyEvent = (event: string): event is 'keydown' | 'keyup' =>
['keydown', 'keyup'].includes(event);

const isClickEvent = (event: string) => {
return ['contextmenu', 'click', 'mouse'].some((i) => event.includes(i));
};

const isListeningForASpecificKeyThatHasntBeenPressed = (
e: Event,
modifiers: string[],
) => {
let keyModifiers = modifiers.filter(
(mod) =>
!['window', 'document', 'prevent', 'stop', 'once', 'capture'].includes(
mod,
),
![
'window',
'document',
'prevent',
'stop',
'once',
'capture',
'self',
'away',
'outside',
'passive',
].includes(mod),
);

if (keyModifiers.includes('debounce')) {
@@ -189,6 +205,8 @@ const isListeningForASpecificKeyThatHasntBeenPressed = (
if (
activelyPressedKeyModifiers.length === selectedSystemKeyModifiers.length
) {
// AND the event is a click. It's a pass.
if (isClickEvent(e.type)) return false;
// AND the remaining key is pressed as well. It's a press.
if (keyToModifiers((e as KeyboardEvent).key).includes(keyModifiers[0]))
return false;
8 changes: 4 additions & 4 deletions size.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"alpinets": {
"minified": {
"pretty": "39.8 kB",
"raw": 39829
"pretty": "39.9 kB",
"raw": 39949
},
"brotli": {
"pretty": "13.5 kB",
"raw": 13534
"pretty": "13.6 kB",
"raw": 13573
}
},
"anchor": {