@6thquake/react-material
Version:
React components that implement Google's Material Design.
407 lines (351 loc) • 13.3 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.styles = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
var _utils = require("@material-ui/utils");
var _helpers = require("../utils/helpers");
var _Badge = _interopRequireDefault(require("@material-ui/core/Badge"));
var styles = function styles(theme) {
var _ribbonContentText;
return {
/* Styles applied to the root element. */
root: {
position: 'relative',
display: 'inline-flex',
// For correct alignment with the text.
verticalAlign: 'middle',
fontFamily: theme.typography.fontFamily,
fontWeight: theme.typography.fontWeightMedium,
fontSize: theme.typography.pxToRem(12)
},
/* Styles applied to the badge `span` element. */
badge: {
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
alignContent: 'center',
alignItems: 'center',
position: 'absolute',
top: 0,
right: 0,
boxSizing: 'border-box',
minWidth: RADIUS * 2,
padding: '0 4px',
height: RADIUS * 2,
borderRadius: RADIUS,
backgroundColor: theme.palette.background.default,
color: theme.palette.text.primary,
zIndex: 1,
// Render the badge on top of potential ripples.
transform: 'scale(1) translate(50%, -50%)',
transformOrigin: '100% 0%',
transition: theme.transitions.create('transform', {
easing: theme.transitions.easing.easeInOut,
duration: theme.transitions.duration.enteringScreen
})
},
/* Styles applied to the root element if `color="primary"`. */
colorPrimary: {
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastText
},
/* Styles applied to the root element if `color="secondary"`. */
colorSecondary: {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText
},
/* Styles applied to the root element if `color="error"`. */
colorError: {
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText
},
/* Styles applied to the badge `span` element if `invisible={true}`. */
invisible: {
transition: theme.transitions.create('transform', {
easing: theme.transitions.easing.easeInOut,
duration: theme.transitions.duration.leavingScreen
}),
transform: 'scale(0) translate(50%, -50%)',
transformOrigin: '100% 0%'
},
/* Styles applied to the root element if `variant="dot"`. */
dot: {
height: 6,
minWidth: 6,
padding: 0
},
mark: {
position: 'absolute',
zIndex: 1,
display: 'inline-block',
textAlign: 'center',
top: 0,
right: theme.spacing(1),
padding: '4px 0px 0px 0px',
width: 24,
wordBreak: 'break-all',
backgroundColor: theme.palette.background.default,
color: theme.palette.text.primary,
'&:before': {
content: '""',
position: 'absolute',
zIndex: 1,
bottom: -theme.spacing(1),
left: 0,
right: 0,
borderLeft: "12px solid ".concat(theme.palette.background.default),
borderRight: "12px solid ".concat(theme.palette.background.default),
borderTop: "4px solid ".concat(theme.palette.background.default),
borderBottom: '4px solid transparent'
}
},
/* Styles applied to the root element if `color="primary" and variant="mark"`. */
markColorPrimary: {
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastText,
'&:before': {
borderLeftColor: theme.palette.primary.main,
borderRightColor: theme.palette.primary.main,
borderTopColor: theme.palette.primary.main
}
},
/* Styles applied to the root element if `color="secondary" and variant="mark"`. */
markColorSecondary: {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
'&:before': {
borderLeftColor: theme.palette.secondary.main,
borderRightColor: theme.palette.secondary.main,
borderTopColor: theme.palette.secondary.main
}
},
/* Styles applied to the root element if `color="error" and variant="mark"`. */
markColorError: {
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText,
'&:before': {
borderLeftColor: theme.palette.error.main,
borderRightColor: theme.palette.error.main,
borderTopColor: theme.palette.error.main
}
},
ribbon: {
position: 'absolute',
zIndex: 1,
width: 'auto',
height: 'auto',
top: 0,
right: 0
},
ribbonWrapper: {
position: 'absolute',
top: 0,
width: '100%',
height: '100%',
overflow: 'hidden'
},
ribbonContent: {
width: '100%',
height: '100%',
backgroundImage: 'none !important',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center center',
whiteSpace: 'nowrap',
padding: '4px 0px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
},
ribbonContentText: (_ribbonContentText = {
display: 'flex',
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center',
flex: 1,
whiteSpace: 'nowrap'
}, (0, _defineProperty2.default)(_ribbonContentText, "textAlign", 'center'), (0, _defineProperty2.default)(_ribbonContentText, "padding", '0px 32px'), (0, _defineProperty2.default)(_ribbonContentText, "height", '100%'), _ribbonContentText)
};
};
exports.styles = styles;
var RADIUS = 10;
var RMBadge =
/*#__PURE__*/
function (_React$Component) {
(0, _inherits2.default)(RMBadge, _React$Component);
function RMBadge() {
var _getPrototypeOf2;
var _this;
(0, _classCallCheck2.default)(this, RMBadge);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(RMBadge)).call.apply(_getPrototypeOf2, [this].concat(args)));
_this.state = {};
return _this;
}
(0, _createClass2.default)(RMBadge, [{
key: "componentDidMount",
value: function componentDidMount() {
var variant = this.props.variant;
if (variant === 'ribbon') {
var res = this.refElem.getBoundingClientRect();
var width = res.width,
height = res.height;
var w = Math.ceil(width);
var h = Math.ceil(height);
var top = Math.ceil(Math.sqrt(2) / 2 * h) + 2;
var right = Math.ceil((1 - 1 / Math.sqrt(2)) * w + h / Math.sqrt(2)) + 2;
this.setState({
width: "".concat(w, "px"),
height: "".concat(h, "px"),
top: "-".concat(top, "px"),
right: "-".concat(right, "px"),
transform: 'rotate(45deg)',
transformOrigin: 'left top'
});
}
}
}, {
key: "render",
value: function render() {
var _classNames,
_classNames2,
_this2 = this;
var _this$props = this.props,
badgeContent = _this$props.badgeContent,
children = _this$props.children,
classes = _this$props.classes,
className = _this$props.className,
color = _this$props.color,
ComponentProp = _this$props.component,
invisibleProp = _this$props.invisible,
showZero = _this$props.showZero,
max = _this$props.max,
variant = _this$props.variant,
other = (0, _objectWithoutProperties2.default)(_this$props, ["badgeContent", "children", "classes", "className", "color", "component", "invisible", "showZero", "max", "variant"]);
console.log(this.state);
var invisible = invisibleProp;
if (invisibleProp === null && Number(badgeContent) === 0 && !showZero) {
invisible = true;
}
var displayValue = variant === 'standard' && badgeContent > max ? "".concat(max, "+") : badgeContent;
switch (variant) {
case 'mark':
return _react.default.createElement(ComponentProp, (0, _extends2.default)({
className: (0, _classnames.default)(classes.root, className)
}, other), children, _react.default.createElement("span", {
className: (0, _classnames.default)(classes.mark, (_classNames = {}, (0, _defineProperty2.default)(_classNames, classes["markColor".concat((0, _helpers.capitalize)(color))], color !== 'default'), (0, _defineProperty2.default)(_classNames, classes.invisible, invisible), _classNames))
}, displayValue));
case 'ribbon':
var _this$state = this.state,
width = _this$state.width,
height = _this$state.height,
top = _this$state.top,
right = _this$state.right,
transform = _this$state.transform,
transformOrigin = _this$state.transformOrigin;
var style = {
width: width,
height: height,
top: top,
right: right,
transform: transform,
transformOrigin: transformOrigin
};
return _react.default.createElement(ComponentProp, (0, _extends2.default)({
className: (0, _classnames.default)(classes.root, className)
}, other), children, _react.default.createElement("span", {
className: classes.ribbonWrapper
}, _react.default.createElement("span", {
className: (0, _classnames.default)(classes.ribbon, (_classNames2 = {}, (0, _defineProperty2.default)(_classNames2, classes["color".concat((0, _helpers.capitalize)(color))], color !== 'default'), (0, _defineProperty2.default)(_classNames2, classes.invisible, invisible), _classNames2)),
style: style,
ref: function ref(node) {
_this2.refElem = node;
}
}, _react.default.createElement("span", {
className: classes.ribbonContent
}, _react.default.createElement("span", {
className: classes.ribbonContentText
}, displayValue)))));
case 'text':
case 'dot':
case 'standard':
default:
return _react.default.createElement(_Badge.default, this.props, children);
}
}
}]);
return RMBadge;
}(_react.default.Component);
RMBadge.propTypes = {
/**
* The content rendered within the badge.
*/
badgeContent: _propTypes.default.node,
/**
* The badge will be added relative to this node.
*/
children: _propTypes.default.node.isRequired,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css-api) below for more details.
*/
classes: _propTypes.default.object.isRequired,
/**
* @ignore
*/
className: _propTypes.default.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
*/
color: _propTypes.default.oneOf(['default', 'primary', 'secondary', 'error']),
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: _utils.componentPropType,
/**
* If `true`, the badge will be invisible.
*/
invisible: _propTypes.default.bool,
/**
* Max count to show.
*/
max: _propTypes.default.number,
/**
* Controls whether the badge is hidden when `badgeContent` is zero.
*/
showZero: _propTypes.default.bool,
/**
* The variant to use.
*/
variant: _propTypes.default.oneOf(['standard', 'dot', 'text', 'ribbon', 'mark'])
};
RMBadge.defaultProps = {
color: 'default',
component: 'span',
max: 99,
showZero: false,
variant: 'standard'
};
var _default = (0, _withStyles.default)(styles, {
name: 'RMBadge',
withTheme: true
})(RMBadge);
exports.default = _default;