Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,8 @@ | ||
# [1.2.0](https://github.com/streamich/use-media/compare/v1.1.0...v1.2.0) (2019-02-18) | ||
### Features | ||
* transpile down to ES5 ([c389ab8](https://github.com/streamich/use-media/commit/c389ab8)) | ||
# [1.1.0](https://github.com/streamich/use-media/compare/v1.0.0...v1.1.0) (2019-02-10) | ||
@@ -2,0 +9,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const React = require("react"); | ||
const { useState, useEffect } = React; | ||
const camelToHyphen = (str) => str.replace(/[A-Z]/g, m => `-${m.toLowerCase()}`).toLowerCase(); | ||
const objectToString = (query) => { | ||
var React = require("react"); | ||
var useState = React.useState, useEffect = React.useEffect; | ||
var camelToHyphen = function (str) { | ||
return str.replace(/[A-Z]/g, function (m) { return "-" + m.toLowerCase(); }).toLowerCase(); | ||
}; | ||
var objectToString = function (query) { | ||
if (typeof query === 'string') | ||
return query; | ||
return Object.entries(query) | ||
.map(([feature, value]) => { | ||
.map(function (_a) { | ||
var feature = _a[0], value = _a[1]; | ||
feature = camelToHyphen(feature); | ||
if (typeof value === 'boolean') { | ||
return value ? feature : `not ${feature}`; | ||
return value ? feature : "not " + feature; | ||
} | ||
if (typeof value === 'number' && /[height|width]$/.test(feature)) { | ||
value = `${value}px`; | ||
value = value + "px"; | ||
} | ||
return `(${feature}: ${value})`; | ||
return "(" + feature + ": " + value + ")"; | ||
}) | ||
.join(' and '); | ||
}; | ||
exports.useMedia = (rawQuery, defaultState = false) => { | ||
const [state, setState] = useState(defaultState); | ||
const query = objectToString(rawQuery); | ||
useEffect(() => { | ||
let mounted = true; | ||
const mql = window.matchMedia(query); | ||
const onChange = () => { | ||
exports.useMedia = function (rawQuery, defaultState) { | ||
if (defaultState === void 0) { defaultState = false; } | ||
var _a = useState(defaultState), state = _a[0], setState = _a[1]; | ||
var query = objectToString(rawQuery); | ||
useEffect(function () { | ||
var mounted = true; | ||
var mql = window.matchMedia(query); | ||
var onChange = function () { | ||
if (!mounted) | ||
@@ -35,3 +39,3 @@ return; | ||
setState(mql.matches); | ||
return () => { | ||
return function () { | ||
mounted = false; | ||
@@ -38,0 +42,0 @@ mql.removeListener(onChange); |
{ | ||
"name": "use-media", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "useMedia React hook", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4271
51