How do I use matchMedia in svelte?
I have a svelte component here.
I plan on making the menu open up whenever I scroll to the top.
However, I don't want this listener to work when the window is like.. x pixels or less.
In vanilla JS, I would use
window.matchMedia
, and attach a listener to it, checking when it changes, toggling the menu and removing the listener. What would be the svelte-y way to do so?4 Replies
Very new to learning svelte but I just came across a need for this the other day and ended up installing the library
svelte-match-media has a way to attach event listeners to it? Its docs dont give any examples
-svelte-match-media
Alternatively, -svelte-media:
-svelte-match-media
From what I’m reading you’d choose -svelte-match-media if you need global access to your media query , and -svelte-media if you only need a one off on one component
Oh, I didn't see this last night 😅 I wrote a tiny utility for this and it worked