Skip to content

Commit 549513d

Browse files
authored
feat: object syntax
Allow media query to be an object
2 parents e842cb3 + 76f56db commit 549513d

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
import {useMedia} from 'use-media';
1010

1111
const Demo = () => {
12-
const isWide = useMedia('(min-width: 1000px)');
12+
// Accepts an object of features to test
13+
const isWide = useMedia({ minWidth: 1000 });
14+
// Or a regular media query string
15+
const reduceMotion = useMedia('(prefers-reduced-motion: reduce)');
1316

1417
return (
1518
<div>

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
"url": "https://github.com/streamich/use-media/issues"
2323
},
2424
"homepage": "https://github.com/streamich/use-media#readme",
25-
"dependencies": {
26-
},
25+
"dependencies": {},
2726
"devDependencies": {
28-
"semantic-release": "^15.10.6",
2927
"@semantic-release/changelog": "^3.0.1",
30-
"@semantic-release/npm": "^5.0.5",
3128
"@semantic-release/git": "^7.0.5",
29+
"@semantic-release/npm": "^5.0.5",
30+
"@types/react": "^16.8.2",
31+
"semantic-release": "^15.10.6",
3232
"typescript": "^3.1.3"
3333
},
3434
"peerDependencies": {
35-
"react": "*"
35+
"react": "^16.8.1"
3636
},
3737
"config": {
3838
"commitizen": {

src/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
import * as React from 'react';
22

3-
const {useState, useEffect} = React as any;
3+
const { useState, useEffect } = React;
44

5-
export const useMedia = (query: string, defaultState: boolean = false) => {
6-
const [state, setState] = useState(defaultState);
5+
type MediaQueryObject = { [key: string]: string | number | boolean };
6+
7+
const camelToHyphen = (str: string) =>
8+
str.replace(/[A-Z]/g, m => `-${m.toLowerCase()}`).toLowerCase();
79

10+
const objectToString = (query: string | MediaQueryObject) => {
11+
if (typeof query === 'string') return query;
12+
return Object.entries(query)
13+
.map(([feature, value]) => {
14+
feature = camelToHyphen(feature);
15+
if (typeof value === 'boolean') {
16+
return value ? feature : `not ${feature}`;
17+
}
18+
if (typeof value === 'number' && /[height|width]$/.test(feature)) {
19+
value = `${value}px`;
20+
}
21+
return `(${feature}: ${value})`;
22+
})
23+
.join(' and ');
24+
};
25+
26+
export const useMedia = (
27+
rawQuery: string | MediaQueryObject,
28+
defaultState: boolean = false
29+
) => {
30+
const [state, setState] = useState(defaultState);
31+
const query = objectToString(rawQuery);
832
useEffect(() => {
933
let mounted = true;
1034
const mql = window.matchMedia(query);

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@
131131
into-stream "^4.0.0"
132132
lodash "^4.17.4"
133133

134+
"@types/prop-types@*":
135+
version "15.5.8"
136+
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.8.tgz#8ae4e0ea205fe95c3901a5a1df7f66495e3a56ce"
137+
integrity sha512-3AQoUxQcQtLHsK25wtTWIoIpgYjH3vSDroZOUr7PpCHw/jLY1RB9z9E8dBT/OSmwStVgkRNvdh+ZHNiomRieaw==
138+
139+
"@types/react@^16.8.2":
140+
version "16.8.2"
141+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.8.2.tgz#3b7a7f7ea89d1c7d68b00849fb5de839011c077b"
142+
integrity sha512-6mcKsqlqkN9xADrwiUz2gm9Wg4iGnlVGciwBRYFQSMWG6MQjhOZ/AVnxn+6v8nslFgfYTV8fNdE6XwKu6va5PA==
143+
dependencies:
144+
"@types/prop-types" "*"
145+
csstype "^2.2.0"
146+
134147
JSONStream@^1.0.4, JSONStream@^1.3.4:
135148
version "1.3.5"
136149
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
@@ -881,6 +894,11 @@ crypto-random-string@^1.0.0:
881894
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
882895
integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=
883896

897+
csstype@^2.2.0:
898+
version "2.6.2"
899+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.2.tgz#3043d5e065454579afc7478a18de41909c8a2f01"
900+
integrity sha512-Rl7PvTae0pflc1YtxtKbiSqq20Ts6vpIYOD5WBafl4y123DyHUeLrRdQP66sQW8/6gmX8jrYJLXwNeMqYVJcow==
901+
884902
currently-unhandled@^0.4.1:
885903
version "0.4.1"
886904
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"

0 commit comments

Comments
 (0)