Comments (2)
thanks so much for the clarification here! Your approach is about what I was thinking so I apprecaite you showing me that! Will have it implemented soon
from svelte-put.
@wesbos thank you for reporting. When shortcut
is placed on window
, it's essentially window.addEventListener('keydown', ...)
. So the event has already reached window
by the time your callback executes, calling preventDefault
or stopPropagation
effectively does nothing useful (except cases when you want to prevent browser default, for example overriding ctrl/cmd+k) .
That newly added example snippet might have been misleading, my apologies! I'll adjust to clarify this.
In your use case I think the issue can be resolved by either:
- checking for target in each callback, or
- use a 'centralized'
on:shortcut
event handler and check for target there before doing anything else, just as you wrote in syntaxfm/website#1464 (comment)
For example
-
turn your handlePlayPause into:
function handlePlayPause(detail) { if (ignoredTags.include(detail.originalEvent.target.tagName)) return; // ... }
-
turn your setup into the following:
<script> // your src/lib/hotkeys/Hotkeys.svelte // ... const allTriggers = [ // can reuse your hotkeys object here ...Object.entries(hotkeys).map(([id, hotkey]) => { // exclude callback here and use in onShortcut bellow const { callback, ...trigger } = hotkey.trigger; return { id, ...trigger, }; }), ]; function onShortcut(event) { const detail = event.detail; const { originalEvent, trigger } = detail; const ignoredTags = [`INPUT`, `TEXTAREA`, `SELECT`, `OPTION`, `BUTTON`]; if (ignoredTags.includes(originalEvent.target.tagName)) return; hotkeys[trigger.id].trigger.callback(); } </script> <svelte:window use:shortcut={{ trigger: allTriggers }} on:shortcut={onShortcut} />
I try to reuse your code here, which makes it not so elegant but I hope the idea gets through.
If you think the API is not ergonomic enough, I'd love to hear what public interface you have in mind.
ps thanks for using this I never thought something i write would be used by wesbos himself.
from svelte-put.
Related Issues (20)
- Configure pre commit hook
- [code] - Implement Code component with syntax highlighting
- [docs] Reorganize components & route structures
- Setup renovate for dependency PR and update
- All about Testing
- Notification: pause, resume, and $progress
- Focus & Modal HOT 5
- @svelte-put/lockscroll: show error when use:lockscroll opn svelte:document HOT 1
- movables do not behave as expected on mobile / with touch input HOT 2
- Change clickoutside node type to Element or Node to allow usage on SVG elements
- toc page mentions anchor config in passing but not explicitly HOT 2
- shortcut: get keyboard event / actual target
- Missing exports condition HOT 6
- Modal.svelte contains svelte a11y warning HOT 3
- Generated table of contents does not reflect on header order if it is a number HOT 8
- scrollock not working on ipadOs 17.2 same for ios HOT 2
- Movable - Cannot limit delta for only 1 axis. HOT 1
- Revamp Docs Site HOT 1
- [QR] margin will shift logo HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte-put.