UNPKG

material-ui

Version:

React Components that Implement Google's Material Design.

392 lines (335 loc) 13.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _simpleAssign = require('simple-assign'); var _simpleAssign2 = _interopRequireDefault(_simpleAssign); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _transitions = require('../styles/transitions'); var _transitions2 = _interopRequireDefault(_transitions); var _colorManipulator = require('../utils/colorManipulator'); var _EnhancedButton = require('../internal/EnhancedButton'); var _EnhancedButton2 = _interopRequireDefault(_EnhancedButton); var _FontIcon = require('../FontIcon'); var _FontIcon2 = _interopRequireDefault(_FontIcon); var _Paper = require('../Paper'); var _Paper2 = _interopRequireDefault(_Paper); var _childUtils = require('../utils/childUtils'); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _propTypes3 = require('../utils/propTypes'); var _propTypes4 = _interopRequireDefault(_propTypes3); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function getStyles(props, context) { var floatingActionButton = context.muiTheme.floatingActionButton; var backgroundColor = props.backgroundColor || floatingActionButton.color; var iconColor = floatingActionButton.iconColor; if (props.disabled) { backgroundColor = props.disabledColor || floatingActionButton.disabledColor; iconColor = floatingActionButton.disabledTextColor; } else if (props.secondary) { backgroundColor = floatingActionButton.secondaryColor; iconColor = floatingActionButton.secondaryIconColor; } return { root: { transition: _transitions2.default.easeOut(), display: 'inline-block', backgroundColor: 'transparent' }, container: { backgroundColor: backgroundColor, transition: _transitions2.default.easeOut(), height: floatingActionButton.buttonSize, width: floatingActionButton.buttonSize, padding: 0, overflow: 'hidden', borderRadius: '50%', textAlign: 'center', verticalAlign: 'bottom' }, containerWhenMini: { height: floatingActionButton.miniSize, width: floatingActionButton.miniSize }, overlay: { transition: _transitions2.default.easeOut(), top: 0 }, overlayWhenHovered: { backgroundColor: (0, _colorManipulator.fade)(iconColor, 0.4) }, icon: { height: floatingActionButton.buttonSize, lineHeight: floatingActionButton.buttonSize + 'px', fill: iconColor, color: iconColor }, iconWhenMini: { height: floatingActionButton.miniSize, lineHeight: floatingActionButton.miniSize + 'px' } }; } var FloatingActionButton = function (_Component) { (0, _inherits3.default)(FloatingActionButton, _Component); function FloatingActionButton() { var _ref; var _temp, _this, _ret; (0, _classCallCheck3.default)(this, FloatingActionButton); 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 = FloatingActionButton.__proto__ || (0, _getPrototypeOf2.default)(FloatingActionButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = { hovered: false, touch: false, zDepth: undefined }, _this.handleMouseDown = function (event) { // only listen to left clicks if (event.button === 0) { _this.setState({ zDepth: _this.props.zDepth + 1 }); } if (_this.props.onMouseDown) _this.props.onMouseDown(event); }, _this.handleMouseUp = function (event) { _this.setState({ zDepth: _this.props.zDepth }); if (_this.props.onMouseUp) { _this.props.onMouseUp(event); } }, _this.handleMouseLeave = function (event) { if (!_this.refs.container.isKeyboardFocused()) { _this.setState({ zDepth: _this.props.zDepth, hovered: false }); } if (_this.props.onMouseLeave) { _this.props.onMouseLeave(event); } }, _this.handleMouseEnter = function (event) { if (!_this.refs.container.isKeyboardFocused() && !_this.state.touch) { _this.setState({ hovered: true }); } if (_this.props.onMouseEnter) { _this.props.onMouseEnter(event); } }, _this.handleTouchStart = function (event) { _this.setState({ touch: true, zDepth: _this.props.zDepth + 1 }); if (_this.props.onTouchStart) { _this.props.onTouchStart(event); } }, _this.handleTouchEnd = function (event) { _this.setState({ touch: true, zDepth: _this.props.zDepth }); if (_this.props.onTouchEnd) { _this.props.onTouchEnd(event); } }, _this.handleKeyboardFocus = function (event, keyboardFocused) { if (keyboardFocused && !_this.props.disabled) { _this.setState({ zDepth: _this.props.zDepth + 1 }); _this.refs.overlay.style.backgroundColor = (0, _colorManipulator.fade)(getStyles(_this.props, _this.context).icon.color, 0.4); } else if (!_this.state.hovered) { _this.setState({ zDepth: _this.props.zDepth }); _this.refs.overlay.style.backgroundColor = 'transparent'; } }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret); } (0, _createClass3.default)(FloatingActionButton, [{ key: 'componentWillMount', value: function componentWillMount() { this.setState({ zDepth: this.props.disabled ? 0 : this.props.zDepth }); } }, { key: 'componentDidMount', value: function componentDidMount() { process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!this.props.iconClassName || !this.props.children, 'Material-UI: You have set both an iconClassName and a child icon. ' + 'It is recommended you use only one method when adding ' + 'icons to FloatingActionButtons.') : void 0; } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { var nextState = {}; if (nextProps.disabled !== this.props.disabled) { nextState.zDepth = nextProps.disabled ? 0 : this.props.zDepth; } if (nextProps.disabled) { nextState.hovered = false; } this.setState(nextState); } }, { key: 'render', value: function render() { var _props = this.props, backgroundColor = _props.backgroundColor, className = _props.className, childrenProp = _props.children, disabled = _props.disabled, disabledColor = _props.disabledColor, mini = _props.mini, secondary = _props.secondary, iconStyle = _props.iconStyle, iconClassName = _props.iconClassName, zDepth = _props.zDepth, other = (0, _objectWithoutProperties3.default)(_props, ['backgroundColor', 'className', 'children', 'disabled', 'disabledColor', 'mini', 'secondary', 'iconStyle', 'iconClassName', 'zDepth']); var prepareStyles = this.context.muiTheme.prepareStyles; var styles = getStyles(this.props, this.context); var iconElement = void 0; if (iconClassName) { iconElement = _react2.default.createElement(_FontIcon2.default, { className: iconClassName, style: (0, _simpleAssign2.default)({}, styles.icon, mini && styles.iconWhenMini, iconStyle) }); } var children = void 0; if (childrenProp) { children = (0, _childUtils.extendChildren)(childrenProp, function (child) { return { style: (0, _simpleAssign2.default)({}, styles.icon, mini && styles.iconWhenMini, iconStyle, child.props.style) }; }); } var buttonEventHandlers = disabled ? null : { onMouseDown: this.handleMouseDown, onMouseUp: this.handleMouseUp, onMouseLeave: this.handleMouseLeave, onMouseEnter: this.handleMouseEnter, onTouchStart: this.handleTouchStart, onTouchEnd: this.handleTouchEnd, onKeyboardFocus: this.handleKeyboardFocus }; return _react2.default.createElement( _Paper2.default, { className: className, style: (0, _simpleAssign2.default)(styles.root, this.props.style), zDepth: this.state.zDepth, circle: true }, _react2.default.createElement( _EnhancedButton2.default, (0, _extends3.default)({}, other, buttonEventHandlers, { ref: 'container', disabled: disabled, style: (0, _simpleAssign2.default)(styles.container, this.props.mini && styles.containerWhenMini, iconStyle), focusRippleColor: styles.icon.color, touchRippleColor: styles.icon.color }), _react2.default.createElement( 'div', { ref: 'overlay', style: prepareStyles((0, _simpleAssign2.default)(styles.overlay, this.state.hovered && !this.props.disabled && styles.overlayWhenHovered)) }, iconElement, children ) ) ); } }]); return FloatingActionButton; }(_react.Component); FloatingActionButton.defaultProps = { disabled: false, mini: false, secondary: false, zDepth: 2 }; FloatingActionButton.contextTypes = { muiTheme: _propTypes2.default.object.isRequired }; FloatingActionButton.propTypes = process.env.NODE_ENV !== "production" ? { /** * This value will override the default background color for the button. * However it will not override the default disabled background color. * This has to be set separately using the disabledColor attribute. */ backgroundColor: _propTypes2.default.string, /** * This is what displayed inside the floating action button; for example, a SVG Icon. */ children: _propTypes2.default.node, /** * The css class name of the root element. */ className: _propTypes2.default.string, /** * Disables the button if set to true. */ disabled: _propTypes2.default.bool, /** * This value will override the default background color for the button when it is disabled. */ disabledColor: _propTypes2.default.string, /** * The URL to link to when the button is clicked. */ href: _propTypes2.default.string, /** * The icon within the FloatingActionButton is a FontIcon component. * This property is the classname of the icon to be displayed inside the button. * An alternative to adding an iconClassName would be to manually insert a * FontIcon component or custom SvgIcon component or as a child of FloatingActionButton. */ iconClassName: _propTypes2.default.string, /** * This is the equivalent to iconClassName except that it is used for * overriding the inline-styles of the FontIcon component. */ iconStyle: _propTypes2.default.object, /** * If true, the button will be a small floating action button. */ mini: _propTypes2.default.bool, /** * Callback function fired when the button is clicked. * * @param {object} event Click event targeting the button. */ onClick: _propTypes2.default.func, /** @ignore */ onMouseDown: _propTypes2.default.func, /** @ignore */ onMouseEnter: _propTypes2.default.func, /** @ignore */ onMouseLeave: _propTypes2.default.func, /** @ignore */ onMouseUp: _propTypes2.default.func, /** @ignore */ onTouchEnd: _propTypes2.default.func, /** @ignore */ onTouchStart: _propTypes2.default.func, /** * If true, the button will use the secondary button colors. */ secondary: _propTypes2.default.bool, /** * Override the inline-styles of the root element. */ style: _propTypes2.default.object, /** * The zDepth of the underlying `Paper` component. */ zDepth: _propTypes4.default.zDepth } : {}; exports.default = FloatingActionButton;