UNPKG

react-mdc-web

Version:

React web components for Material Design

197 lines (163 loc) 9.09 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames2 = require('classnames'); var _classnames3 = _interopRequireDefault(_classnames2); var _ToolbarRow = require('./ToolbarRow'); var _ToolbarRow2 = _interopRequireDefault(_ToolbarRow); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var ROOT = 'mdc-toolbar'; var FIXED = ROOT + '--fixed'; var WATERFALL = ROOT + '--waterfall'; var FIXED_LAST_ROW_ONLY = ROOT + '--fixed-lastrow-only'; var FLEXIBLE = ROOT + '--flexible'; var FLEXIBLE_DEFAULT_BEHAVIOR = ROOT + '--flexible-default-behavior'; var FLEXIBLE_MAX = ROOT + '--flexible-space-maximized'; var FLEXIBLE_MIN = ROOT + '--flexible-space-minimized'; var TOOLBAR_MOBILE_BREAKPOINT = 600; var TOOLBAR_ROW_HEIGHT = 64; var TOOLBAR_ROW_MOBILE_HEIGHT = 56; var Toolbar = function (_React$PureComponent) { _inherits(Toolbar, _React$PureComponent); function Toolbar(props) { _classCallCheck(this, Toolbar); var _this = _possibleConstructorReturn(this, (Toolbar.__proto__ || Object.getPrototypeOf(Toolbar)).call(this, props)); _this.state = { flexibleExpansionRatio: 0 }; _this.handleScroll = _this.handleScroll.bind(_this); _this.calculations = { toolbarRowHeight: 0 }; return _this; } _createClass(Toolbar, [{ key: 'componentDidMount', value: function componentDidMount() { this.getWindow().addEventListener('scroll', this.handleScroll); this.initKeyRatio(); this.setKeyHeights(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.getWindow().removeEventListener('scroll', this.handleScroll); } }, { key: 'getFlexibleExpansionRatio', value: function getFlexibleExpansionRatio() { // To prevent division by zero when there is no flexibleExpansionHeight var delta = 0.0001; var scrollTop = this.state.scrollTop; var flexibleExpansionHeight = this.calculations.flexibleExpansionHeight; return Math.max(0, 1 - scrollTop / (flexibleExpansionHeight + delta)); } }, { key: 'getWindow', value: function getWindow() { var windowContext = this.props.windowContext; if (windowContext) { return windowContext; } return window; } }, { key: 'getRowHeight', value: function getRowHeight() { var breakpoint = TOOLBAR_MOBILE_BREAKPOINT; var viewPortWidth = this.getWindow().innerWidth; return viewPortWidth < breakpoint ? TOOLBAR_ROW_MOBILE_HEIGHT : TOOLBAR_ROW_HEIGHT; } }, { key: 'setKeyHeights', value: function setKeyHeights() { var newToolbarRowHeight = this.getRowHeight(); var _calculations = this.calculations, toolbarRatio = _calculations.toolbarRatio, toolbarRowHeight = _calculations.toolbarRowHeight; if (newToolbarRowHeight !== this.calculations.toolbarRowHeight) { this.calculations.toolbarRowHeight = newToolbarRowHeight; this.calculations.toolbarHeight = toolbarRatio * toolbarRowHeight; this.calculations.flexibleExpansionHeight = this.calculations.flexibleExpansionRatio * this.calculations.toolbarRowHeight; this.calculations.maxTranslateYDistance = this.calculations.maxTranslateYRatio * this.calculations.toolbarRowHeight; this.calculations.scrollThreshold = this.calculations.scrollThresholdRatio * this.calculations.toolbarRowHeight; } } }, { key: 'initKeyRatio', value: function initKeyRatio() { var toolbarRowHeight = this.getRowHeight(); var firstRowMaxRatio = this.calculations.firstRowHeight / toolbarRowHeight; this.calculations.toolbarRatio = this.calculations.rootOffsetHeight / toolbarRowHeight; this.calculations.flexibleExpansionRatio = firstRowMaxRatio - 1; this.calculations.maxTranslateYRatio = this.fixedLastrow ? this.calculations.toolbarRatio - firstRowMaxRatio : 0; this.calculations.scrollThresholdRatio = (this.fixedLastrow ? this.calculations.toolbarRatio : firstRowMaxRatio) - 1; } }, { key: 'handleScroll', value: function handleScroll() { this.setState({ scrollTop: this.getWindow().pageYOffset }); } }, { key: 'render', value: function render() { var _this2 = this, _classnames; var _props = this.props, className = _props.className, fixed = _props.fixed, waterfall = _props.waterfall, fixedLastRowOnly = _props.fixedLastRowOnly, flexible = _props.flexible, flexibleDefaultBehavior = _props.flexibleDefaultBehavior, children = _props.children, windowContext = _props.windowContext, otherProps = _objectWithoutProperties(_props, ['className', 'fixed', 'waterfall', 'fixedLastRowOnly', 'flexible', 'flexibleDefaultBehavior', 'children', 'windowContext']); var firstRowFound = false; var childs = _react.Children.map(children, function (child) { if (!firstRowFound && child.type === _ToolbarRow2.default) { return (0, _react.cloneElement)(child, { onHeight: function onHeight(height) { _this2.calculations.firstRowHeight = height; } }); } return child; }); var flexibleExpansionRatio = this.getFlexibleExpansionRatio(); return _react2.default.createElement( 'header', _extends({ className: (0, _classnames3.default)(ROOT, (_classnames = {}, _defineProperty(_classnames, FIXED, fixed), _defineProperty(_classnames, FIXED_LAST_ROW_ONLY, fixedLastRowOnly), _defineProperty(_classnames, WATERFALL, waterfall), _defineProperty(_classnames, FLEXIBLE, flexible), _defineProperty(_classnames, FLEXIBLE_DEFAULT_BEHAVIOR, flexibleDefaultBehavior), _defineProperty(_classnames, FLEXIBLE_MAX, flexibleExpansionRatio === 1), _defineProperty(_classnames, FLEXIBLE_MIN, flexibleExpansionRatio === 0), _classnames), className), ref: function ref(native) { if (native) { _this2.calculations.rootOffsetHeight = native.offsetHeight; } } }, otherProps), childs ); } }]); return Toolbar; }(_react2.default.PureComponent); Toolbar.propTypes = { className: _propTypes2.default.string, children: _propTypes2.default.node, fixed: _propTypes2.default.bool, waterfall: _propTypes2.default.bool, flexible: _propTypes2.default.bool, fixedLastRowOnly: _propTypes2.default.bool, flexibleDefaultBehavior: _propTypes2.default.bool, onChange: _propTypes2.default.func, windowContext: _propTypes2.default.object // eslint-disable-line react/forbid-prop-types }; exports.default = Toolbar;