Completely updated React, fixed #11, (hopefully)

This commit is contained in:
2018-03-04 19:11:49 -05:00
parent 6e0afd6e2a
commit 34e5f5139a
13674 changed files with 333464 additions and 473223 deletions

View File

@@ -28,15 +28,21 @@ var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactDom = require('react-dom');
var _reactEventListener = require('react-event-listener');
var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _ownerDocument = require('dom-helpers/ownerDocument');
var babelPluginFlowReactPropTypes_proptype_Node = require('react').babelPluginFlowReactPropTypes_proptype_Node || require('prop-types').any;
var _ownerDocument2 = _interopRequireDefault(_ownerDocument);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var isDescendant = function isDescendant(el, target) {
if (target !== null && target.parentNode) {
@@ -45,14 +51,10 @@ var isDescendant = function isDescendant(el, target) {
return false;
};
var babelPluginFlowReactPropTypes_proptype_Props = {
children: typeof babelPluginFlowReactPropTypes_proptype_Node === 'function' ? babelPluginFlowReactPropTypes_proptype_Node.isRequired ? babelPluginFlowReactPropTypes_proptype_Node.isRequired : babelPluginFlowReactPropTypes_proptype_Node : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Node).isRequired,
onClickAway: require('prop-types').func.isRequired
};
/**
* Listen for click events that are triggered outside of the component children.
*/
var ClickAwayListener = function (_React$Component) {
(0, _inherits3.default)(ClickAwayListener, _React$Component);
@@ -76,8 +78,9 @@ var ClickAwayListener = function (_React$Component) {
// IE11 support, which trigger the handleClickAway even after the unbind
if (_this.mounted) {
var el = (0, _reactDom.findDOMNode)(_this);
var doc = (0, _ownerDocument2.default)(el);
if (event.target instanceof HTMLElement && document.documentElement && document.documentElement.contains(event.target) && !isDescendant(el, event.target)) {
if (doc.documentElement && doc.documentElement.contains(event.target) && !isDescendant(el, event.target)) {
_this.props.onClickAway(event);
}
}
@@ -112,7 +115,8 @@ var ClickAwayListener = function (_React$Component) {
}(_react2.default.Component);
ClickAwayListener.propTypes = process.env.NODE_ENV !== "production" ? {
children: typeof babelPluginFlowReactPropTypes_proptype_Node === 'function' ? babelPluginFlowReactPropTypes_proptype_Node.isRequired ? babelPluginFlowReactPropTypes_proptype_Node.isRequired : babelPluginFlowReactPropTypes_proptype_Node : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Node).isRequired,
onClickAway: require('prop-types').func.isRequired
children: _propTypes2.default.node.isRequired,
onClickAway: _propTypes2.default.func.isRequired
} : {};
exports.default = ClickAwayListener;

View File

@@ -1,9 +1,8 @@
// @flow
import React from 'react';
import type { Node } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import EventListener from 'react-event-listener';
import ownerDocument from 'dom-helpers/ownerDocument';
const isDescendant = (el, target) => {
if (target !== null && target.parentNode) {
@@ -12,15 +11,10 @@ const isDescendant = (el, target) => {
return false;
};
export type Props = {
children: Node,
onClickAway: (event: Event) => void,
};
/**
* Listen for click events that are triggered outside of the component children.
*/
class ClickAwayListener extends React.Component<Props> {
class ClickAwayListener extends React.Component {
componentDidMount() {
this.mounted = true;
}
@@ -31,7 +25,7 @@ class ClickAwayListener extends React.Component<Props> {
mounted = false;
handleClickAway = (event: Event) => {
handleClickAway = event => {
// Ignore events that have been `event.preventDefault()` marked.
if (event.defaultPrevented) {
return;
@@ -40,11 +34,11 @@ class ClickAwayListener extends React.Component<Props> {
// IE11 support, which trigger the handleClickAway even after the unbind
if (this.mounted) {
const el = findDOMNode(this);
const doc = ownerDocument(el);
if (
event.target instanceof HTMLElement &&
document.documentElement &&
document.documentElement.contains(event.target) &&
doc.documentElement &&
doc.documentElement.contains(event.target) &&
!isDescendant(el, event.target)
) {
this.props.onClickAway(event);
@@ -65,4 +59,9 @@ class ClickAwayListener extends React.Component<Props> {
}
}
ClickAwayListener.propTypes = {
children: PropTypes.node.isRequired,
onClickAway: PropTypes.func.isRequired,
};
export default ClickAwayListener;

View File

@@ -2,5 +2,5 @@ export default function addEventListener(
node: Node,
event: string,
handler: (e: Event) => never,
capture?: boolean
capture?: boolean,
): { remove(): void };

View File

@@ -1,24 +1,14 @@
'use strict';
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (node, event, handler, capture) {
(0, _on2.default)(node, event, handler, capture);
node.addEventListener(event, handler, capture);
return {
remove: function remove() {
(0, _off2.default)(node, event, handler, capture);
node.removeEventListener(event, handler, capture);
}
};
};
var _on = require('dom-helpers/events/on');
var _on2 = _interopRequireDefault(_on);
var _off = require('dom-helpers/events/off');
var _off2 = _interopRequireDefault(_off);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
};

View File

@@ -1,13 +1,10 @@
// @flow
import addEventListener from 'dom-helpers/events/on';
import removeEventListener from 'dom-helpers/events/off';
export default function(node: Node, event: string, handler: EventHandler, capture?: boolean) {
addEventListener(node, event, handler, capture);
node.addEventListener(event, handler, capture);
return {
remove() {
removeEventListener(node, event, handler, capture);
node.removeEventListener(event, handler, capture);
},
};
}

View File

@@ -1,7 +1,7 @@
export function capitalizeFirstLetter(str: string): string;
export function contains(obj: Object, pred: Object): boolean;
export function capitalize(str: string): string;
export function contains<O1 extends O2, O2>(obj: O1, pred: O2): boolean;
export function findIndex(arr: any[], pred: any): number;
export function find<T>(arr: T[], pred: any): T;
export function createChainedFunction(
...funcs: Function[]
): (...args: any[]) => never;
export function createChainedFunction(...funcs: ChainedFunction[]): (...args: any[]) => never;
type ChainedFunction = ((...args: any[]) => void) | undefined | null;

View File

@@ -12,7 +12,7 @@ var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
exports.capitalizeFirstLetter = capitalizeFirstLetter;
exports.capitalize = capitalize;
exports.contains = contains;
exports.findIndex = findIndex;
exports.find = find;
@@ -24,8 +24,10 @@ var _warning2 = _interopRequireDefault(_warning);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function capitalizeFirstLetter(string) {
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(typeof string === 'string', 'Material-UI: capitalizeFirstLetter(string) expects a string argument.') : void 0;
function capitalize(string) {
if (process.env.NODE_ENV !== 'production' && typeof string !== 'string') {
throw new Error('Material-UI: capitalize(string) expects a string argument.');
}
return string.charAt(0).toUpperCase() + string.slice(1);
} // weak

View File

@@ -2,11 +2,10 @@
import warning from 'warning';
export function capitalizeFirstLetter(string) {
warning(
typeof string === 'string',
'Material-UI: capitalizeFirstLetter(string) expects a string argument.',
);
export function capitalize(string) {
if (process.env.NODE_ENV !== 'production' && typeof string !== 'string') {
throw new Error('Material-UI: capitalize(string) expects a string argument.');
}
return string.charAt(0).toUpperCase() + string.slice(1);
}

View File

@@ -6,7 +6,7 @@ export function detectKeyboardFocus(
keyboardFocusMaxCheckTimes: number;
},
element: Element,
cb: Function,
attempt: number
): never;
export function listenForFocusKeys(): never;
cb: () => void,
attempt: number,
): void;
export function listenForFocusKeys(window: Window): void;

View File

@@ -19,16 +19,15 @@ var _contains = require('dom-helpers/query/contains');
var _contains2 = _interopRequireDefault(_contains);
var _addEventListener = require('../utils/addEventListener');
var _ownerDocument = require('dom-helpers/ownerDocument');
var _addEventListener2 = _interopRequireDefault(_addEventListener);
var _ownerDocument2 = _interopRequireDefault(_ownerDocument);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// weak
var internal = {
listening: false,
focusKeyPressed: false
};
@@ -47,7 +46,9 @@ function detectKeyboardFocus(instance, element, callback) {
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(instance.keyboardFocusMaxCheckTimes, 'Material-UI: missing instance.keyboardFocusMaxCheckTimes') : void 0;
instance.keyboardFocusTimeout = setTimeout(function () {
if (focusKeyPressed() && (document.activeElement === element || (0, _contains2.default)(element, document.activeElement))) {
var doc = (0, _ownerDocument2.default)(element);
if (focusKeyPressed() && (doc.activeElement === element || (0, _contains2.default)(element, doc.activeElement))) {
callback();
} else if (attempt < instance.keyboardFocusMaxCheckTimes) {
detectKeyboardFocus(instance, element, callback, attempt + 1);
@@ -61,15 +62,15 @@ function isFocusKey(event) {
return FOCUS_KEYS.indexOf((0, _keycode2.default)(event)) !== -1;
}
function listenForFocusKeys() {
// It's a singleton, we only need to listen once.
// Also, this logic is client side only, we don't need a teardown.
if (!internal.listening) {
(0, _addEventListener2.default)(window, 'keyup', function (event) {
if (isFocusKey(event)) {
internal.focusKeyPressed = true;
}
});
internal.listening = true;
var handleKeyUpEvent = function handleKeyUpEvent(event) {
if (isFocusKey(event)) {
internal.focusKeyPressed = true;
}
};
function listenForFocusKeys(win) {
// The event listener will only be added once per window.
// Duplicate event listeners will be ignored by addEventListener.
// Also, this logic is client side only, we don't need a teardown.
win.addEventListener('keyup', handleKeyUpEvent);
}

View File

@@ -3,10 +3,9 @@
import keycode from 'keycode';
import warning from 'warning';
import contains from 'dom-helpers/query/contains';
import addEventListener from '../utils/addEventListener';
import ownerDocument from 'dom-helpers/ownerDocument';
const internal = {
listening: false,
focusKeyPressed: false,
};
@@ -26,9 +25,11 @@ export function detectKeyboardFocus(instance, element, callback, attempt = 1) {
);
instance.keyboardFocusTimeout = setTimeout(() => {
const doc = ownerDocument(element);
if (
focusKeyPressed() &&
(document.activeElement === element || contains(element, document.activeElement))
(doc.activeElement === element || contains(element, doc.activeElement))
) {
callback();
} else if (attempt < instance.keyboardFocusMaxCheckTimes) {
@@ -43,15 +44,15 @@ function isFocusKey(event) {
return FOCUS_KEYS.indexOf(keycode(event)) !== -1;
}
export function listenForFocusKeys() {
// It's a singleton, we only need to listen once.
// Also, this logic is client side only, we don't need a teardown.
if (!internal.listening) {
addEventListener(window, 'keyup', event => {
if (isFocusKey(event)) {
internal.focusKeyPressed = true;
}
});
internal.listening = true;
const handleKeyUpEvent = event => {
if (isFocusKey(event)) {
internal.focusKeyPressed = true;
}
};
export function listenForFocusKeys(win) {
// The event listener will only be added once per window.
// Duplicate event listeners will be ignored by addEventListener.
// Also, this logic is client side only, we don't need a teardown.
win.addEventListener('keyup', handleKeyUpEvent);
}

View File

@@ -1,3 +0,0 @@
export function ariaHidden(show: boolean, node: Node): never;
export function hideSiblings(container: Element, mountNode: Node): never;
export function showSiblings(container: Element, mountNode: Node): never;

View File

@@ -1,49 +0,0 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ariaHidden = ariaHidden;
exports.hideSiblings = hideSiblings;
exports.showSiblings = showSiblings;
// weak
var BLACKLIST = ['template', 'script', 'style'];
var isHidable = function isHidable(_ref) {
var nodeType = _ref.nodeType,
tagName = _ref.tagName;
return nodeType === 1 && BLACKLIST.indexOf(tagName.toLowerCase()) === -1;
};
var siblings = function siblings(container, mount, cb) {
mount = [].concat(mount); // eslint-disable-line no-param-reassign
[].forEach.call(container.children, function (node) {
if (mount.indexOf(node) === -1 && isHidable(node)) {
cb(node);
}
});
};
function ariaHidden(show, node) {
if (!node) {
return;
}
if (show) {
node.setAttribute('aria-hidden', 'true');
} else {
node.removeAttribute('aria-hidden');
}
}
function hideSiblings(container, mountNode) {
siblings(container, mountNode, function (node) {
return ariaHidden(true, node);
});
}
function showSiblings(container, mountNode) {
siblings(container, mountNode, function (node) {
return ariaHidden(false, node);
});
}

View File

@@ -1,34 +0,0 @@
// @flow weak
const BLACKLIST = ['template', 'script', 'style'];
const isHidable = ({ nodeType, tagName }) =>
nodeType === 1 && BLACKLIST.indexOf(tagName.toLowerCase()) === -1;
const siblings = (container, mount, cb) => {
mount = [].concat(mount); // eslint-disable-line no-param-reassign
[].forEach.call(container.children, node => {
if (mount.indexOf(node) === -1 && isHidable(node)) {
cb(node);
}
});
};
export function ariaHidden(show, node) {
if (!node) {
return;
}
if (show) {
node.setAttribute('aria-hidden', 'true');
} else {
node.removeAttribute('aria-hidden');
}
}
export function hideSiblings(container, mountNode) {
siblings(container, mountNode, node => ariaHidden(true, node));
}
export function showSiblings(container, mountNode) {
siblings(container, mountNode, node => ariaHidden(false, node));
}

View File

@@ -1,4 +1,15 @@
export function cloneChildrenWithClassName<T>(
children: React.ReactNode,
className: string
): T[];
import * as React from 'react';
import { StandardProps } from '../';
export function cloneChildrenWithClassName<T>(children: React.ReactNode, className: string): T[];
type NamedMuiComponent = React.ComponentType<{}> & { muiName: string };
interface NamedMuiElement {
type: NamedMuiComponent;
props: StandardProps<{}, never>;
key: string | number | null;
}
export function isMuiElement(element: any, muiNames: string[]): element is NamedMuiElement;
export function isMuiComponent(element: any, muiNames: string[]): element is NamedMuiComponent;

View File

@@ -9,19 +9,26 @@ exports.isMuiComponent = isMuiComponent;
var _react = require('react');
var babelPluginFlowReactPropTypes_proptype_Node = require('react').babelPluginFlowReactPropTypes_proptype_Node || require('prop-types').any;
var _react2 = _interopRequireDefault(_react);
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable import/prefer-default-export */
function cloneChildrenWithClassName(children, className) {
return _react.Children.map(children, function (child) {
return (0, _react.isValidElement)(child) && (0, _react.cloneElement)(child, {
className: child.props.hasOwnProperty('className') ? child.props.className + ' ' + className : className
return _react2.default.Children.map(children, function (child) {
return _react2.default.isValidElement(child) && _react2.default.cloneElement(child, {
className: (0, _classnames2.default)(child.props.className, className)
});
});
}
function isMuiElement(element, muiNames) {
return (0, _react.isValidElement)(element) && muiNames.indexOf(element.type.muiName) !== -1;
return _react2.default.isValidElement(element) && muiNames.indexOf(element.type.muiName) !== -1;
}
function isMuiComponent(element, muiNames) {

View File

@@ -1,24 +1,23 @@
// @flow
/* eslint-disable import/prefer-default-export */
import { cloneElement, Children, isValidElement } from 'react';
import React from 'react';
import type { Node } from 'react';
import classNames from 'classnames';
export function cloneChildrenWithClassName(children?: Node, className: string) {
return Children.map(children, child => {
export function cloneChildrenWithClassName(children: Node, className: string) {
return React.Children.map(children, child => {
return (
isValidElement(child) &&
cloneElement(child, {
className: child.props.hasOwnProperty('className')
? `${child.props.className} ${className}`
: className,
React.isValidElement(child) &&
React.cloneElement(child, {
className: classNames(child.props.className, className),
})
);
});
}
export function isMuiElement(element: any, muiNames: Array<string>) {
return isValidElement(element) && muiNames.indexOf(element.type.muiName) !== -1;
return React.isValidElement(element) && muiNames.indexOf(element.type.muiName) !== -1;
}
export function isMuiComponent(element: any, muiNames: Array<string>) {

View File

@@ -8,14 +8,20 @@ export interface WithWidthProps {
width: Breakpoint;
}
export function isWidthUp(
export function isWidthDown(
breakpoint: Breakpoint,
screenWidth: number,
inclusive?: boolean
screenWidth: Breakpoint,
inclusive?: boolean,
): boolean;
export default function withWidth<P = {}>(
options?: WithWidthOptions
): (
component: React.ComponentType<P>
) => React.ComponentClass<P & WithWidthProps>;
export function isWidthUp(
breakpoint: Breakpoint,
screenWidth: Breakpoint,
inclusive?: boolean,
): boolean;
export default function withWidth(
options?: WithWidthOptions,
): <P>(
component: React.ComponentType<P & WithWidthProps>,
) => React.ComponentClass<P & Partial<WithWidthProps>>;

View File

@@ -37,6 +37,10 @@ var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactEventListener = require('react-event-listener');
var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
@@ -49,6 +53,10 @@ var _wrapDisplayName = require('recompose/wrapDisplayName');
var _wrapDisplayName2 = _interopRequireDefault(_wrapDisplayName);
var _hoistNonReactStatics = require('hoist-non-react-statics');
var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
var _withTheme = require('../styles/withTheme');
var _withTheme2 = _interopRequireDefault(_withTheme);
@@ -57,98 +65,56 @@ var _createBreakpoints = require('../styles/createBreakpoints');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var babelPluginFlowReactPropTypes_proptype_HigherOrderComponent = require('react-flow-types').babelPluginFlowReactPropTypes_proptype_HigherOrderComponent || require('prop-types').any; // weak
// flow sanity check (DO NOT DELETE) https://flow.org/try/#0JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wG4AoUSWOGATzCTgG84BhXSAOyS5gBUGTAL5xsuAkXQwy5OQHp5cALSq16jZuVwdccorgB3YDAAW-U0hBMAEgHk25JAA9qWAK5cMwCFyMnzS2sAHgAFHDAAZwAuFmEAPgAKcl12Tl9eGFiOcAy+QUZg1jMrJFi7ACMAKyQMOFEAMjhwiCj4gBpyAEps9J58oTCIyPiWOR00ABsUSMi4AHUAi1K4FxheABM55GkAOhzuTKHWyPaWWiCyuEqauoSx1KIuDaQoRK6H1LgiGHcoP2CBzy8GYuzBZmAkV2YGGohK1gAvMwIVDIjAUOtdvCkKJ5PEKKlhAT6ilvkhfv8FktLuRhAolFpGUy1PolMYzMtrHAAKqRFAAcyQ5CmMzmAEFVs51s9tsQYPs+kdipdytVavBGiwULEuO4QBVXmcKjq9QaoPdmHS0L40XBOUgNkD+vAEf4OZdEmKuhQDPMmBtfPh4DwHbQIHAwKK4MA-AADbGx1YAN14Fwg7n5pjgsYAsnQnZlE0QAI7uYBEOYmXbkYL2x2KvhwFBIgCMogqSIATLj4vSVMyB6lWW7TIsNmY4PZHC43LQhHAAEJSADWkBjLoIzki+DgAB8CJEQDv9-gQBtjwRJvyL-hnJNZOR6IwqePTC0onBXcxSTGTMAUJMY5mAA-LES6oKuEDrp0OjGK+oGLiua58J0dJOK40AeF4MA+H47KjsAr7vJ8mCeN4virFwpgoF4SDHFEsRAW+wxJKSqQFnwvS5M6BR0cwcFmGBSFQShcBgrs76RAkMFwD0aTcZkvH0SMYxsXAIqzFSZhMZK0pbIgcoKgpfDKaM35fGSzyvMR5kWepNogr+OEAUxZwCaYoiuii0LDGpjzkn8AIcSC4neTCJyiO5SL4Ie+A9sShIJSSak-IFWkEa+xJEuMZIUn4vDUbRFBoQYA5leow7uHygrCtMmkLrpmyynswVFO5QkQchMBnNqcC6vqhrGn1pqvBapJPC8bwfLZEwOSw7meRckI+ScKUBZSwQbMASZwHipJ0lac1MQ6wWfiOTHvIkC7esOfpwAGXBBn1SChjA4aRppMbZu5iZICmfhmOmmbZnmwVFkgpblkglbyjWx31sZ8DNswbZwB2zDdrt+JAA
/**
* By default, returns true if screen width is the same or greater than the given breakpoint.
*
* @param screenWidth
* @param breakpoint
* @param inclusive - defaults to true
*/
var babelPluginFlowReactPropTypes_proptype_Breakpoint = require('../styles/createBreakpoints').babelPluginFlowReactPropTypes_proptype_Breakpoint || require('prop-types').any;
var isWidthUp = exports.isWidthUp = function isWidthUp(breakpoint, screenWidth) {
// By default, returns true if screen width is the same or greater than the given breakpoint.
var isWidthUp = exports.isWidthUp = function isWidthUp(breakpoint, width) {
var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (inclusive) {
return _createBreakpoints.keys.indexOf(breakpoint) <= _createBreakpoints.keys.indexOf(screenWidth);
return _createBreakpoints.keys.indexOf(breakpoint) <= _createBreakpoints.keys.indexOf(width);
}
return _createBreakpoints.keys.indexOf(breakpoint) < _createBreakpoints.keys.indexOf(screenWidth);
return _createBreakpoints.keys.indexOf(breakpoint) < _createBreakpoints.keys.indexOf(width);
};
/**
* By default, returns true if screen width is the same or less than the given breakpoint.
*
* @param screenWidth
* @param breakpoint
* @param inclusive - defaults to true
*/
var isWidthDown = exports.isWidthDown = function isWidthDown(breakpoint, screenWidth) {
// By default, returns true if screen width is the same or less than the given breakpoint.
var isWidthDown = exports.isWidthDown = function isWidthDown(breakpoint, width) {
var inclusive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (inclusive) {
return _createBreakpoints.keys.indexOf(screenWidth) <= _createBreakpoints.keys.indexOf(breakpoint);
return _createBreakpoints.keys.indexOf(width) <= _createBreakpoints.keys.indexOf(breakpoint);
}
return _createBreakpoints.keys.indexOf(screenWidth) < _createBreakpoints.keys.indexOf(breakpoint);
return _createBreakpoints.keys.indexOf(width) < _createBreakpoints.keys.indexOf(breakpoint);
};
// optional props introduced by this HOC
var babelPluginFlowReactPropTypes_proptype_HOCProps = {
/**
* As `window.innerWidth` is unavailable on the server,
* we default to rendering an empty componenent during the first mount.
* In some situation you might want to use an heristic to approximate
* the screen width of the client browser screen width.
*
* For instance, you could be using the user-agent or the client-hints.
* http://caniuse.com/#search=client%20hint
*/
initialWidth: typeof babelPluginFlowReactPropTypes_proptype_Breakpoint === 'function' ? babelPluginFlowReactPropTypes_proptype_Breakpoint : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Breakpoint),
/**
* Bypass the width calculation logic.
*/
width: typeof babelPluginFlowReactPropTypes_proptype_Breakpoint === 'function' ? babelPluginFlowReactPropTypes_proptype_Breakpoint : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Breakpoint)
};
var babelPluginFlowReactPropTypes_proptype_InjectedProps = {
width: typeof babelPluginFlowReactPropTypes_proptype_Breakpoint === 'function' ? babelPluginFlowReactPropTypes_proptype_Breakpoint.isRequired ? babelPluginFlowReactPropTypes_proptype_Breakpoint.isRequired : babelPluginFlowReactPropTypes_proptype_Breakpoint : require('prop-types').shape(babelPluginFlowReactPropTypes_proptype_Breakpoint).isRequired
};
var withWidth = function withWidth() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (Component) {
var _options$resizeInterv = options.resizeInterval,
resizeInterval = _options$resizeInterv === undefined ? 166 : _options$resizeInterv;
resizeInterval = _options$resizeInterv === undefined ? 166 : _options$resizeInterv,
_options$withTheme = options.withTheme,
withThemeOption = _options$withTheme === undefined ? false : _options$withTheme;
// `theme` is injected below by withTheme
var WithWidth = function (_React$Component) {
(0, _inherits3.default)(WithWidth, _React$Component);
var Width = function (_React$Component) {
(0, _inherits3.default)(Width, _React$Component);
function Width() {
function WithWidth() {
var _ref;
var _temp, _this, _ret;
(0, _classCallCheck3.default)(this, Width);
(0, _classCallCheck3.default)(this, WithWidth);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Width.__proto__ || (0, _getPrototypeOf2.default)(Width)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = WithWidth.__proto__ || (0, _getPrototypeOf2.default)(WithWidth)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
width: undefined
}, _this.handleResize = (0, _debounce2.default)(function () {
_this.updateWidth(window.innerWidth);
}, resizeInterval), _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
}
(0, _createClass3.default)(Width, [{
(0, _createClass3.default)(WithWidth, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.updateWidth(window.innerWidth);
@@ -161,37 +127,35 @@ var withWidth = function withWidth() {
}, {
key: 'updateWidth',
value: function updateWidth(innerWidth) {
if (this.props.theme) {
var breakpoints = this.props.theme.breakpoints;
var _width = null;
var breakpoints = this.props.theme.breakpoints;
var width = null;
/**
* Start with the slowest value as low end devices often have a small screen.
*
* innerWidth |0 xs sm md lg xl
* |-------|-------|-------|-------|-------|------>
* width | xs | xs | sm | md | lg | xl
*/
var index = 1;
while (_width === null && index < _createBreakpoints.keys.length) {
var currentWidth = _createBreakpoints.keys[index];
/**
* Start with the slowest value as low end devices often have a small screen.
*
* innerWidth |xs sm md lg xl
* |-------|-------|-------|-------|------>
* width | xs | sm | md | lg | xl
*/
var index = 1;
while (width === null && index < _createBreakpoints.keys.length) {
var currentWidth = _createBreakpoints.keys[index];
// @media are inclusive, so reproduce the behavior here.
if (innerWidth < breakpoints.values[currentWidth]) {
_width = _createBreakpoints.keys[index - 1];
break;
}
index += 1;
// @media are inclusive, so reproduce the behavior here.
if (innerWidth < breakpoints.values[currentWidth]) {
width = _createBreakpoints.keys[index - 1];
break;
}
_width = _width || 'xl';
index += 1;
}
if (_width !== this.state.width) {
this.setState({
width: _width
});
}
width = width || 'xl';
if (width !== this.state.width) {
this.setState({
width: width
});
}
}
}, {
@@ -206,6 +170,11 @@ var withWidth = function withWidth() {
var props = (0, _extends3.default)({
width: width || this.state.width || initialWidth
}, other);
var more = {};
if (withThemeOption) {
more.theme = theme;
}
// When rendering the component on the server,
// we have no idea about the client browser screen width.
@@ -220,18 +189,41 @@ var withWidth = function withWidth() {
return _react2.default.createElement(
_reactEventListener2.default,
{ target: 'window', onResize: this.handleResize },
_react2.default.createElement(Component, props)
_react2.default.createElement(Component, (0, _extends3.default)({}, more, props))
);
}
}]);
return Width;
return WithWidth;
}(_react2.default.Component);
WithWidth.propTypes = process.env.NODE_ENV !== "production" ? {
/**
* As `window.innerWidth` is unavailable on the server,
* we default to rendering an empty componenent during the first mount.
* In some situation you might want to use an heristic to approximate
* the screen width of the client browser screen width.
*
* For instance, you could be using the user-agent or the client-hints.
* http://caniuse.com/#search=client%20hint
*/
initialWidth: _propTypes2.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
/**
* @ignore
*/
theme: _propTypes2.default.object.isRequired,
/**
* Bypass the width calculation logic.
*/
width: _propTypes2.default.oneOf(['xs', 'sm', 'md', 'lg', 'xl'])
} : {};
if (process.env.NODE_ENV !== 'production') {
Width.displayName = (0, _wrapDisplayName2.default)(Component, 'withWidth');
WithWidth.displayName = (0, _wrapDisplayName2.default)(Component, 'WithWidth');
}
return (0, _withTheme2.default)()(Width);
(0, _hoistNonReactStatics2.default)(WithWidth, Component);
return (0, _withTheme2.default)()(WithWidth);
};
};

View File

@@ -1,73 +1,35 @@
// @flow weak
// flow sanity check (DO NOT DELETE) https://flow.org/try/#0JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wG4AoUSWOGATzCTgG84BhXSAOyS5gBUGTAL5xsuAkXQwy5OQHp5cALSq16jZuVwdccorgB3YDAAW-U0hBMAEgHk25JAA9qWAK5cMwCFyMnzS2sAHgAFHDAAZwAuFmEAPgAKcl12Tl9eGFiOcAy+QUZg1jMrJFi7ACMAKyQMOFEAMjhwiCj4gBpyAEps9J58oTCIyPiWOR00ABsUSMi4AHUAi1K4FxheABM55GkAOhzuTKHWyPaWWiCyuEqauoSx1KIuDaQoRK6H1LgiGHcoP2CBzy8GYuzBZmAkV2YGGohK1gAvMwIVDIjAUOtdvCkKJ5PEKKlhAT6ilvkhfv8FktLuRhAolFpGUy1PolMYzMtrHAAKqRFAAcyQ5CmMzmAEFVs51s9tsQYPs+kdipdytVavBGiwULEuO4QBVXmcKjq9QaoPdmHS0L40XBOUgNkD+vAEf4OZdEmKuhQDPMmBtfPh4DwHbQIHAwKK4MA-AADbGx1YAN14Fwg7n5pjgsYAsnQnZlE0QAI7uYBEOYmXbkYL2x2KvhwFBIgCMogqSIATLj4vSVMyB6lWW7TIsNmY4PZHC43LQhHAAEJSADWkBjLoIzki+DgAB8CJEQDv9-gQBtjwRJvyL-hnJNZOR6IwqePTC0onBXcxSTGTMAUJMY5mAA-LES6oKuEDrp0OjGK+oGLiua58J0dJOK40AeF4MA+H47KjsAr7vJ8mCeN4virFwpgoF4SDHFEsRAW+wxJKSqQFnwvS5M6BR0cwcFmGBSFQShcBgrs76RAkMFwD0aTcZkvH0SMYxsXAIqzFSZhMZK0pbIgcoKgpfDKaM35fGSzyvMR5kWepNogr+OEAUxZwCaYoiuii0LDGpjzkn8AIcSC4neTCJyiO5SL4Ie+A9sShIJSSak-IFWkEa+xJEuMZIUn4vDUbRFBoQYA5leow7uHygrCtMmkLrpmyynswVFO5QkQchMBnNqcC6vqhrGn1pqvBapJPC8bwfLZEwOSw7meRckI+ScKUBZSwQbMASZwHipJ0lac1MQ6wWfiOTHvIkC7esOfpwAGXBBn1SChjA4aRppMbZu5iZICmfhmOmmbZnmwVFkgpblkglbyjWx31sZ8DNswbZwB2zDdrt+JAA
import React from 'react';
import type { HigherOrderComponent } from 'react-flow-types';
import PropTypes from 'prop-types';
import EventListener from 'react-event-listener';
import debounce from 'lodash/debounce';
import wrapDisplayName from 'recompose/wrapDisplayName';
import hoistNonReactStatics from 'hoist-non-react-statics';
import withTheme from '../styles/withTheme';
import { keys as breakpointKeys } from '../styles/createBreakpoints';
import type { Breakpoint } from '../styles/createBreakpoints';
/**
* By default, returns true if screen width is the same or greater than the given breakpoint.
*
* @param screenWidth
* @param breakpoint
* @param inclusive - defaults to true
*/
export const isWidthUp = (breakpoint, screenWidth, inclusive = true) => {
// By default, returns true if screen width is the same or greater than the given breakpoint.
export const isWidthUp = (breakpoint, width, inclusive = true) => {
if (inclusive) {
return breakpointKeys.indexOf(breakpoint) <= breakpointKeys.indexOf(screenWidth);
return breakpointKeys.indexOf(breakpoint) <= breakpointKeys.indexOf(width);
}
return breakpointKeys.indexOf(breakpoint) < breakpointKeys.indexOf(screenWidth);
return breakpointKeys.indexOf(breakpoint) < breakpointKeys.indexOf(width);
};
/**
* By default, returns true if screen width is the same or less than the given breakpoint.
*
* @param screenWidth
* @param breakpoint
* @param inclusive - defaults to true
*/
export const isWidthDown = (breakpoint, screenWidth, inclusive = true) => {
// By default, returns true if screen width is the same or less than the given breakpoint.
export const isWidthDown = (breakpoint, width, inclusive = true) => {
if (inclusive) {
return breakpointKeys.indexOf(screenWidth) <= breakpointKeys.indexOf(breakpoint);
return breakpointKeys.indexOf(width) <= breakpointKeys.indexOf(breakpoint);
}
return breakpointKeys.indexOf(screenWidth) < breakpointKeys.indexOf(breakpoint);
return breakpointKeys.indexOf(width) < breakpointKeys.indexOf(breakpoint);
};
// optional props introduced by this HOC
export type HOCProps = {
/**
* As `window.innerWidth` is unavailable on the server,
* we default to rendering an empty componenent during the first mount.
* In some situation you might want to use an heristic to approximate
* the screen width of the client browser screen width.
*
* For instance, you could be using the user-agent or the client-hints.
* http://caniuse.com/#search=client%20hint
*/
initialWidth?: Breakpoint,
/**
* Bypass the width calculation logic.
*/
width?: Breakpoint,
};
export type InjectedProps = { width: Breakpoint };
const withWidth = (
// eslint-disable-line prettier/prettier
options = {},
): HigherOrderComponent<{}, InjectedProps> => (Component: any): any => {
const withWidth = (options = {}) => Component => {
const {
resizeInterval = 166, // Corresponds to 10 frames at 60 Hz.
withTheme: withThemeOption = false,
} = options;
// `theme` is injected below by withTheme
class Width extends React.Component<{ theme: Object } & HOCProps, { width: Breakpoint }> {
class WithWidth extends React.Component {
state = {
width: undefined,
};
@@ -85,37 +47,35 @@ const withWidth = (
}, resizeInterval);
updateWidth(innerWidth) {
if (this.props.theme) {
const breakpoints = this.props.theme.breakpoints;
let width = null;
const breakpoints = this.props.theme.breakpoints;
let width = null;
/**
* Start with the slowest value as low end devices often have a small screen.
*
* innerWidth |0 xs sm md lg xl
* |-------|-------|-------|-------|-------|------>
* width | xs | xs | sm | md | lg | xl
*/
let index = 1;
while (width === null && index < breakpointKeys.length) {
const currentWidth = breakpointKeys[index];
/**
* Start with the slowest value as low end devices often have a small screen.
*
* innerWidth |xs sm md lg xl
* |-------|-------|-------|-------|------>
* width | xs | sm | md | lg | xl
*/
let index = 1;
while (width === null && index < breakpointKeys.length) {
const currentWidth = breakpointKeys[index];
// @media are inclusive, so reproduce the behavior here.
if (innerWidth < breakpoints.values[currentWidth]) {
width = breakpointKeys[index - 1];
break;
}
index += 1;
// @media are inclusive, so reproduce the behavior here.
if (innerWidth < breakpoints.values[currentWidth]) {
width = breakpointKeys[index - 1];
break;
}
width = width || 'xl';
index += 1;
}
if (width !== this.state.width) {
this.setState({
width,
});
}
width = width || 'xl';
if (width !== this.state.width) {
this.setState({
width,
});
}
}
@@ -125,6 +85,11 @@ const withWidth = (
width: width || this.state.width || initialWidth,
...other,
};
const more = {};
if (withThemeOption) {
more.theme = theme;
}
// When rendering the component on the server,
// we have no idea about the client browser screen width.
@@ -138,17 +103,40 @@ const withWidth = (
return (
<EventListener target="window" onResize={this.handleResize}>
<Component {...props} />
<Component {...more} {...props} />
</EventListener>
);
}
}
WithWidth.propTypes = {
/**
* As `window.innerWidth` is unavailable on the server,
* we default to rendering an empty componenent during the first mount.
* In some situation you might want to use an heristic to approximate
* the screen width of the client browser screen width.
*
* For instance, you could be using the user-agent or the client-hints.
* http://caniuse.com/#search=client%20hint
*/
initialWidth: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
/**
* @ignore
*/
theme: PropTypes.object.isRequired,
/**
* Bypass the width calculation logic.
*/
width: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl']),
};
if (process.env.NODE_ENV !== 'production') {
Width.displayName = wrapDisplayName(Component, 'withWidth');
WithWidth.displayName = wrapDisplayName(Component, 'WithWidth');
}
return withTheme()(Width);
hoistNonReactStatics(WithWidth, Component);
return withTheme()(WithWidth);
};
export default withWidth;