Skip to content

Commit

Permalink
Add matchMedia to config type
Browse files Browse the repository at this point in the history
  • Loading branch information
ianobermiller committed Sep 13, 2015
1 parent fed7fe5 commit 69e9e2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions .agignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
lib
14 changes: 13 additions & 1 deletion modules/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

import type {PluginConfig, PluginResult} from './plugins';

/* eslint-disable no-use-before-define */
type MediaQueryListListener = (mql: MediaQueryList) => void;
/* eslint-enable no-use-before-define */

type MediaQueryList = {
matches: bool;
addListener(listener: MediaQueryListListener): void;
removeListener(listener: MediaQueryListListener): void;
};

export type Plugin = (pluginConfig: PluginConfig) => PluginResult;
export type MatchMediaType = (mediaQueryString: string) => MediaQueryList;

export type Config = {
plugins?: Array<Plugin>
matchMedia?: MatchMediaType;
plugins?: Array<Plugin>;
};
8 changes: 5 additions & 3 deletions modules/plugins/resolve-media-queries-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @flow */

import type {MatchMediaType} from '../config';
import type {PluginConfig, PluginResult} from '.';

var _windowMatchMedia;
Expand All @@ -8,7 +9,8 @@ var _getWindowMatchMedia = function (ExecutionEnvironment) {
_windowMatchMedia = !!ExecutionEnvironment.canUseDOM &&
!!window &&
!!window.matchMedia &&
(mediaQueryString => window.matchMedia(mediaQueryString));
(mediaQueryString => window.matchMedia(mediaQueryString)) ||
null;
}
return _windowMatchMedia;
};
Expand All @@ -24,7 +26,7 @@ var resolveMediaQueries = function ({
}: PluginConfig): PluginResult {
var newComponentFields = {};
var newStyle = style;
var matchMedia = config.matchMedia ||
var matchMedia: ?MatchMediaType = config.matchMedia ||
_getWindowMatchMedia(ExecutionEnvironment);
if (!matchMedia) {
return newStyle;
Expand All @@ -41,7 +43,7 @@ var resolveMediaQueries = function ({

// Create a global MediaQueryList if one doesn't already exist
var mql = mediaQueryListByQueryString[query];
if (!mql) {
if (!mql && matchMedia) {
mediaQueryListByQueryString[query] = mql = matchMedia(query);
}

Expand Down

0 comments on commit 69e9e2d

Please sign in to comment.