-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: provide MediaQuery / prefersReducedMotion
#14422
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c5c4bea
feat: provide `MediaQuery` / `prefersReducedMotion`
dummdidumm 62d370a
matches -> current, server fallback
dummdidumm 9da3a10
createStartStopNotifier
dummdidumm 7c3fb02
test polyfill
dummdidumm defa646
more tests fixes
dummdidumm 905556b
feedback
dummdidumm 6e2bc3e
rename
dummdidumm 8088719
tweak, types
dummdidumm c996cc4
hnnnggh
dummdidumm 42cd563
mark as pure
dummdidumm 075b6db
fix type check
dummdidumm 1a6b8f3
notify -> subscribe
Rich-Harris d7b331f
add links to inline docs
Rich-Harris 75f85c0
Merge branch 'main' into media-query
dummdidumm 5afa6f3
better API, more docs
dummdidumm c6d29d7
Merge branch 'main' into media-query
Rich-Harris 7900d9b
add example to prefersReducedMotion
Rich-Harris 4e59abc
add example for MediaQuery
Rich-Harris 0e689c8
typo
Rich-Harris c3b3e14
fix example
Rich-Harris 1f0ad0a
tweak docs
Rich-Harris 60aaaac
changesets
Rich-Harris 8288220
note when APIs were added
Rich-Harris df97184
add note
Rich-Harris f2406b2
regenerate
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| import { MediaQuery } from 'svelte/reactivity'; | ||
|
|
||
| export * from './spring.js'; | ||
| export * from './tweened.js'; | ||
|
|
||
| /** | ||
| * A media query that matches if the user has requested reduced motion. | ||
| * @type {MediaQuery} | ||
| */ | ||
| export const prefersReducedMotion = new MediaQuery('(prefers-reduced-motion: reduce)'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { get } from '../internal/client/runtime.js'; | ||
| import { set, source } from '../internal/client/reactivity/sources.js'; | ||
| import { effect_tracking } from '../internal/client/reactivity/effects.js'; | ||
| import { createStartStopNotifier } from './start-stop-notifier.js'; | ||
|
|
||
| /** | ||
| * Creates a media query and provides a `current` property that reflects whether or not it matches. | ||
| */ | ||
| export class MediaQuery { | ||
| #version = source(0); | ||
| #query; | ||
| #notify; | ||
|
|
||
| get current() { | ||
| if (effect_tracking()) { | ||
| get(this.#version); | ||
| this.#notify(); | ||
| } | ||
|
|
||
| return this.#query.matches; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} query A media query string (don't forget the braces) | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param {boolean} [matches] Fallback value for the server | ||
| */ | ||
| constructor(query, matches) { | ||
| this.#query = window.matchMedia(query); | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.#notify = createStartStopNotifier(() => { | ||
| const listener = () => set(this.#version, this.#version.v + 1); | ||
| this.#query.addEventListener('change', listener); | ||
| return () => this.#query.removeEventListener('change', listener); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { tick, untrack } from '../internal/client/runtime.js'; | ||
| import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js'; | ||
|
|
||
| /** | ||
| * Returns a function that, when invoked in a reactive context, calls the `start` function once, | ||
| * and calls the `stop` function returned from `start` when all reactive contexts it's called in | ||
| * are destroyed. This is useful for creating a notifier that starts and stops when the | ||
| * "subscriber" count goes from 0 to 1 and back to 0. | ||
| * @param {() => () => void} start | ||
| */ | ||
| export function createStartStopNotifier(start) { | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let subscribers = 0; | ||
| /** @type {() => void} */ | ||
| let stop; | ||
|
|
||
| return () => { | ||
| if (effect_tracking()) { | ||
| render_effect(() => { | ||
| if (subscribers === 0) { | ||
| stop = untrack(start); | ||
| } | ||
|
|
||
| subscribers += 1; | ||
|
|
||
| return () => { | ||
| tick().then(() => { | ||
| // Only count down after timeout, else we would reach 0 before our own render effect reruns, | ||
| // but reach 1 again when the tick callback of the prior teardown runs. That would mean we | ||
| // re-subcribe unnecessarily and create a memory leak because the old subscription is never cleaned up. | ||
| subscribers -= 1; | ||
|
|
||
| if (subscribers === 0) { | ||
| stop(); | ||
| } | ||
| }); | ||
| }; | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.