-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from shgysk8zer0/bug/media-queries-module
Add `media-queries.js` module
- Loading branch information
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,46 @@ | ||
/** | ||
* @copyright 2023 Chris Zuber <admin@kernvalley.us> | ||
*/ | ||
import { when } from './dom.js'; | ||
export const supported = globalThis.matchMedia instanceof Function; | ||
|
||
export function mediaQuery(query = {}) { | ||
console.warn('`mediaQuery()` is deprecated. Please use `matches()` instead.'); | ||
return matches(query); | ||
} | ||
|
||
export function matches(what = {}) { | ||
return query(what).matches; | ||
} | ||
|
||
export function query(query = {}) { | ||
if (! supported) { | ||
return { matches: false, addEventListener: () => null, removeEventListener: () => null }; | ||
} else { | ||
const queries = Object.entries(query).map(([k, v]) => `(${k}: ${v})`).join(' and '); | ||
return matchMedia(queries); | ||
} | ||
} | ||
|
||
export async function whenMatches(what = {}, { signal } = {}) { | ||
const mq = query(what); | ||
|
||
if (! mq.matches) { | ||
await when(mq, 'change', { signal }); | ||
} | ||
|
||
return mq; | ||
} | ||
|
||
export function prefersReducedMotion() { | ||
return matches({ 'prefers-reduced-motion': 'reduce' }); | ||
} | ||
|
||
export function prefersColorScheme() { | ||
return matches({ 'prefers-color-scheme': 'dark' }) ? 'dark': 'light'; | ||
} | ||
|
||
export function displayMode() { | ||
const displays = ['browser', 'standalone', 'minimal-ui', 'fullscreen']; | ||
return displays.find(mode => matches({ 'display-mode': mode })) || 'browser'; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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