Skip to content

Commit

Permalink
Merge pull request #14 from shgysk8zer0/bug/media-queries-module
Browse files Browse the repository at this point in the history
Add `media-queries.js` module
  • Loading branch information
shgysk8zer0 committed May 14, 2023
2 parents 35bff65 + f12dc25 commit c091f27
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v.0.0.10] - 2023-05-14

### Added
- Import `media-queries.js` module

## [v0.0.9] - 2023-05-14

## Added
Expand Down
46 changes: 46 additions & 0 deletions media-queries.js
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';
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shgysk8zer0/kazoo",
"version": "0.0.9",
"version": "0.0.10",
"private": false,
"description": "A JavaScript monorepo for all the things!",
"main": "index.js",
Expand Down

0 comments on commit c091f27

Please sign in to comment.