UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

347 lines (273 loc) 11.5 kB
"use strict"; 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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireDefault(require("react")); var _warning = _interopRequireDefault(require("warning")); var _propTypes = _interopRequireDefault(require("prop-types")); var _classnames = _interopRequireDefault(require("classnames")); var _withStyles = _interopRequireDefault(require("../styles/withStyles")); var _helpers = require("../utils/helpers"); var _typographyMigration = _interopRequireDefault(require("../styles/typographyMigration")); var styles = function styles(theme) { return { /* Styles applied to the root element. */ root: { display: 'block', margin: 0 }, /* Styles applied to the root element if `variant="display4"`. */ display4: theme.typography.display4, /* Styles applied to the root element if `variant="display3"`. */ display3: theme.typography.display3, /* Styles applied to the root element if `variant="display2"`. */ display2: theme.typography.display2, /* Styles applied to the root element if `variant="display1"`. */ display1: theme.typography.display1, /* Styles applied to the root element if `variant="headline"`. */ headline: theme.typography.headline, /* Styles applied to the root element if `variant="title"`. */ title: theme.typography.title, /* Styles applied to the root element if `variant="subheading"`. */ subheading: theme.typography.subheading, /* Styles applied to the root element if `variant="body2"`. */ body2: theme.typography.body2, /* Styles applied to the root element if `variant="body1"`. */ body1: theme.typography.body1, /* Styles applied to the root element if `variant="caption"`. */ caption: theme.typography.caption, /* Styles applied to the root element if `variant="button"`. */ button: theme.typography.button, /* Styles applied to the root element if `variant="h1"`. */ h1: theme.typography.h1, /* Styles applied to the root element if `variant="h2"`. */ h2: theme.typography.h2, /* Styles applied to the root element if `variant="h3"`. */ h3: theme.typography.h3, /* Styles applied to the root element if `variant="h4"`. */ h4: theme.typography.h4, /* Styles applied to the root element if `variant="h5"`. */ h5: theme.typography.h5, /* Styles applied to the root element if `variant="h6"`. */ h6: theme.typography.h6, /* Styles applied to the root element if `variant="subtitle1"`. */ subtitle1: theme.typography.subtitle1, /* Styles applied to the root element if `variant="subtitle2"`. */ subtitle2: theme.typography.subtitle2, /* Styles applied to the root element if `variant="overline"`. */ overline: theme.typography.overline, /* Styles applied to the root element if `variant="srOnly"`. Only accessible to screen readers. */ srOnly: { position: 'absolute', height: 1, width: 1, overflow: 'hidden' }, /* Styles applied to the root element if `align="left"`. */ alignLeft: { textAlign: 'left' }, /* Styles applied to the root element if `align="center"`. */ alignCenter: { textAlign: 'center' }, /* Styles applied to the root element if `align="right"`. */ alignRight: { textAlign: 'right' }, /* Styles applied to the root element if `align="justify"`. */ alignJustify: { textAlign: 'justify' }, /* Styles applied to the root element if `align="nowrap"`. */ noWrap: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }, /* Styles applied to the root element if `gutterBottom={true}`. */ gutterBottom: { marginBottom: '0.35em' }, /* Styles applied to the root element if `paragraph={true}`. */ paragraph: { marginBottom: 16 }, /* Styles applied to the root element if `color="inherit"`. */ colorInherit: { color: 'inherit' }, /* Styles applied to the root element if `color="primary"`. */ colorPrimary: { color: theme.palette.primary.main }, /* Styles applied to the root element if `color="secondary"`. */ colorSecondary: { color: theme.palette.secondary.main }, /* Styles applied to the root element if `color="textPrimary"`. */ colorTextPrimary: { color: theme.palette.text.primary }, /* Styles applied to the root element if `color="textSecondary"`. */ colorTextSecondary: { color: theme.palette.text.secondary }, /* Styles applied to the root element if `color="error"`. */ colorError: { color: theme.palette.error.main } }; }; exports.styles = styles; function nextVariantMapping(variant) { var nextVariant = { display4: 'h1', display3: 'h2', display2: 'h3', display1: 'h4', headline: 'h5', title: 'h6', subheading: 'subtitle1' }[variant]; // already v2 if (!nextVariant) { return variant; } return nextVariant; } function getVariant(theme, props, variantProp) { var typography = theme.typography; var variant = variantProp; if (!variant) { variant = typography.useNextVariants ? 'body2' : 'body1'; } process.env.NODE_ENV !== "production" ? (0, _warning.default)(typography.suppressDeprecationWarnings || props.internalDeprecatedVariant && typography.useNextVariants || !_typographyMigration.default.deprecatedVariants.includes(variant), 'Material-UI: You are using the deprecated typography variant ' + "".concat(variant, " that will be removed in the next major release. ").concat(_typographyMigration.default.migrationGuideMessage)) : void 0; // complete v2 switch if (typography.useNextVariants) { return nextVariantMapping(variant); } // v1 => restyle warnings process.env.NODE_ENV !== "production" ? (0, _warning.default)(typography.suppressDeprecationWarnings || !_typographyMigration.default.restyledVariants.includes(variant), 'Material-UI: You are using the typography variant ' + "".concat(variant, " which will be restyled in the next major release. ").concat(_typographyMigration.default.migrationGuideMessage)) : void 0; return variant; } var defaultHeadlineMapping = { h1: 'h1', h2: 'h2', h3: 'h3', h4: 'h4', h5: 'h5', h6: 'h6', subtitle1: 'h6', subtitle2: 'h6', body1: 'p', body2: 'p', // deprecated display4: 'h1', display3: 'h1', display2: 'h1', display1: 'h1', headline: 'h1', title: 'h2', subheading: 'h3' }; function Typography(props) { var _classNames; var align = props.align, classes = props.classes, classNameProp = props.className, color = props.color, componentProp = props.component, gutterBottom = props.gutterBottom, headlineMapping = props.headlineMapping, internalDeprecatedVariant = props.internalDeprecatedVariant, noWrap = props.noWrap, paragraph = props.paragraph, theme = props.theme, variantProp = props.variant, other = (0, _objectWithoutProperties2.default)(props, ["align", "classes", "className", "color", "component", "gutterBottom", "headlineMapping", "internalDeprecatedVariant", "noWrap", "paragraph", "theme", "variant"]); var variant = getVariant(theme, props, variantProp); var className = (0, _classnames.default)(classes.root, (_classNames = {}, (0, _defineProperty2.default)(_classNames, classes[variant], variant !== 'inherit'), (0, _defineProperty2.default)(_classNames, classes["color".concat((0, _helpers.capitalize)(color))], color !== 'default'), (0, _defineProperty2.default)(_classNames, classes.noWrap, noWrap), (0, _defineProperty2.default)(_classNames, classes.gutterBottom, gutterBottom), (0, _defineProperty2.default)(_classNames, classes.paragraph, paragraph), (0, _defineProperty2.default)(_classNames, classes["align".concat((0, _helpers.capitalize)(align))], align !== 'inherit'), _classNames), classNameProp); var Component = componentProp || (paragraph ? 'p' : headlineMapping[variant] || defaultHeadlineMapping[variant]) || 'span'; return _react.default.createElement(Component, (0, _extends2.default)({ className: className }, other)); } Typography.propTypes = process.env.NODE_ENV !== "production" ? { /** * Set the text-align on the component. */ align: _propTypes.default.oneOf(['inherit', 'left', 'center', 'right', 'justify']), /** * The content of the component. */ children: _propTypes.default.node, /** * 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', 'error', 'inherit', 'primary', 'secondary', 'textPrimary', 'textSecondary']), /** * The component used for the root node. * Either a string to use a DOM element or a component. * By default, it maps the variant to a good default headline component. */ component: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func, _propTypes.default.object]), /** * If `true`, the text will have a bottom margin. */ gutterBottom: _propTypes.default.bool, /** * We are empirically mapping the variant property to a range of different DOM element types. * For instance, subtitle1 to `<h6>`. * If you wish to change that mapping, you can provide your own. * Alternatively, you can use the `component` property. * The default mapping is the following: */ headlineMapping: _propTypes.default.object, /** * A deprecated variant is used from an internal component. Users don't need * a deprecation warning here if they switched to the v2 theme. They already * get the mapping that will be applied in the next major release. * * @internal */ internalDeprecatedVariant: _propTypes.default.bool, /** * If `true`, the text will not wrap, but instead will truncate with an ellipsis. */ noWrap: _propTypes.default.bool, /** * If `true`, the text will have a bottom margin. */ paragraph: _propTypes.default.bool, /** * Applies the theme typography styles. * Use `body1` as the default value with the legacy implementation and `body2` with the new one. */ variant: _propTypes.default.oneOf(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'subtitle1', 'subtitle2', 'body1', 'body2', 'caption', 'button', 'overline', 'srOnly', 'inherit', // deprecated 'display4', 'display3', 'display2', 'display1', 'headline', 'title', 'subheading']) } : {}; Typography.defaultProps = { align: 'inherit', color: 'default', gutterBottom: false, headlineMapping: defaultHeadlineMapping, noWrap: false, paragraph: false }; var _default = (0, _withStyles.default)(styles, { name: 'MuiTypography', withTheme: true })(Typography); exports.default = _default;