-
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.
Replace enquire.js with matchmedia.js for staticMatch compatibility
- Loading branch information
Showing
9 changed files
with
178 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
- 0.1.4 Replace enquire.js with matchmedia.js for staticMatch compatibility. | ||
- 0.1.3 Added initial tests for responsive HOC. | ||
- 0.1.2 Added tests for ResponsiveProvider. | ||
- 0.1.1 Added option `only` to the responsive wrapper. | ||
- 0.1.0 Initial release. |
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
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,98 @@ | ||
/** | ||
* Full credit to react-responsive for the mediaShape declaration: | ||
* https://github.com/contra/react-responsive/blob/master/src/mediaQuery.js | ||
*/ | ||
import { PropTypes } from 'react' | ||
|
||
const { number, string, bool, oneOf, oneOfType } = PropTypes | ||
const stringOrNumber = oneOfType([ string, number ]) | ||
|
||
// properties that match media queries | ||
const matchers = { | ||
orientation: oneOf([ | ||
'portrait', | ||
'landscape' | ||
]), | ||
|
||
scan: oneOf([ | ||
'progressive', | ||
'interlace' | ||
]), | ||
|
||
aspectRatio: string, | ||
deviceAspectRatio: string, | ||
|
||
height: stringOrNumber, | ||
deviceHeight: stringOrNumber, | ||
|
||
width: stringOrNumber, | ||
deviceWidth: stringOrNumber, | ||
|
||
color: bool, | ||
|
||
colorIndex: bool, | ||
|
||
monochrome: bool, | ||
resolution: stringOrNumber | ||
} | ||
|
||
// media features | ||
const features = { | ||
minAspectRatio: string, | ||
maxAspectRatio: string, | ||
minDeviceAspectRatio: string, | ||
maxDeviceAspectRatio: string, | ||
|
||
minHeight: stringOrNumber, | ||
maxHeight: stringOrNumber, | ||
minDeviceHeight: stringOrNumber, | ||
maxDeviceHeight: stringOrNumber, | ||
|
||
minWidth: stringOrNumber, | ||
maxWidth: stringOrNumber, | ||
minDeviceWidth: stringOrNumber, | ||
maxDeviceWidth: stringOrNumber, | ||
|
||
minColor: number, | ||
maxColor: number, | ||
|
||
minColorIndex: number, | ||
maxColorIndex: number, | ||
|
||
minMonochrome: number, | ||
maxMonochrome: number, | ||
|
||
minResolution: stringOrNumber, | ||
maxResolution: stringOrNumber | ||
} | ||
|
||
Object.assign(features, matchers) | ||
|
||
// media types | ||
const types = { | ||
all: bool, | ||
grid: bool, | ||
aural: bool, | ||
braille: bool, | ||
handheld: bool, | ||
print: bool, | ||
projection: bool, | ||
screen: bool, | ||
tty: bool, | ||
tv: bool, | ||
embossed: bool | ||
} | ||
|
||
const all = {} | ||
Object.assign(all, types) | ||
Object.assign(all, features) | ||
|
||
// add the type property | ||
Object.assign(matchers, { type: Object.keys(types) }) | ||
|
||
module.exports = { | ||
all: all, | ||
types: types, | ||
matchers: matchers, | ||
features: features | ||
} |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,43 @@ | ||
/** | ||
* Full credit to react-responsive for the mediaShape declaration: | ||
* https://github.com/contra/react-responsive/blob/master/src/toQuery.js | ||
*/ | ||
import hyphenate from 'hyphenate-style-name' | ||
import mq from './mediaQuery' | ||
|
||
const re = /[height|width]$/ | ||
function negate(cond) { | ||
return 'not ' + cond | ||
} | ||
|
||
function isDimension(feature) { | ||
return re.test(feature) | ||
function keyVal(k, v) { | ||
// px shorthand | ||
if (typeof v === 'number') { | ||
v = `${v}px` | ||
} | ||
if (v === true) { | ||
return k | ||
} | ||
if (v === false) { | ||
return negate(k) | ||
} | ||
|
||
const realKey = hyphenate(k) | ||
return `(${realKey}: ${v})` | ||
} | ||
function negate(cond) { | ||
return `not ${cond}` | ||
|
||
function join(conds) { | ||
return conds.join(' and ') | ||
} | ||
|
||
export default function toQuery(obj) { | ||
let mq = '' | ||
const features = Object.keys(obj) | ||
export default function(obj) { | ||
const rules = [] | ||
|
||
features.forEach(function (feature, index) { | ||
let value = obj[feature] | ||
feature = hyphenate(feature) | ||
// Add px to dimension features | ||
if (isDimension(feature) && typeof value === 'number') { | ||
value = `${value}px` | ||
} | ||
if (value === true) { | ||
mq += feature | ||
} else if (value === false) { | ||
mq += negate(feature) | ||
} else { | ||
mq += `(${feature}: ${value})` | ||
} | ||
if (index < features.length-1) { | ||
mq += ' and ' | ||
Object.keys(mq.all).forEach(function(k) { | ||
var v = obj[k] | ||
if (v != null) { | ||
rules.push(keyVal(k, v)) | ||
} | ||
}) | ||
|
||
return mq | ||
return join(rules) | ||
} |
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
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 |
---|---|---|
@@ -1,8 +1,5 @@ | ||
import { jsdom } from 'jsdom' | ||
import matchMedia from 'matchmedia' | ||
|
||
global.document = jsdom('<!doctype html><html><body><div id="app"></div></body></html>') | ||
global.window = document.defaultView | ||
global.navigator = global.window.navigator | ||
|
||
global.window.matchMedia = matchMedia |