svecosystem / runed

Magical utilities for your Svelte applications (WIP)
https://runed.dev
MIT License
461 stars 23 forks source link

feat: add MediaQuery utility #63

Closed paoloricciuti closed 3 months ago

paoloricciuti commented 3 months ago

This adds a MediaQuery utility where you can pass a media query and get back true or false in case it matches (with reactivity).

You need to specifically pass the media query like this "(media-query-here)" so i was wondering if a warn in case the parentheses are not there could be a good thing. Maybe even add them automatically if not presents.

WDYT?

I did not add any test because for what i was able to see JSDom doesn't have a way to check media queries. I could mock that but i don't know if that's really useful.

Also also, i just created an effect in the constructor...this means it will likely fail if the instance is created in a .svelte.ts file. One possible way out of this could be to try to create the effect, if it fails wrap everything in $effect.root and expose a cleanup function that users need to call in the onDestroy by themselves...but i also think there should be a standard way for every utility.

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: b99fb948a048040cdac80d208a573bfc15e99fc5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ----- | ----- | | runed | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

github-actions[bot] commented 3 months ago
built with Refined Cloudflare Pages Action

âš¡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ✅ Ready (View Log) Visit Preview b99fb948a048040cdac80d208a573bfc15e99fc5
abdel-17 commented 3 months ago

I say we can have a helper for creating stuff outside of component init phase.

const [media, cleanup] = root(() => new MediaQuery(...));
paoloricciuti commented 3 months ago

I say we can have a helper for creating stuff outside of component init phase.

const [media, cleanup] = root(() => new MediaQuery(...));

Not a bad idea at all!

TGlide commented 3 months ago

Amazing stuff, thank you!