UNPKG

@material-ui/unstyled

Version:

Unstyled React components with which to implement custom design systems.

203 lines (180 loc) 5.63 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["anchorOrigin", "classes", "badgeContent", "component", "children", "className", "components", "componentsProps", "invisible", "max", "overlap", "showZero", "variant", "theme"]; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import { unstable_capitalize as capitalize, usePreviousProps } from '@material-ui/utils'; import isHostComponent from '../utils/isHostComponent'; import composeClasses from '../composeClasses'; import { getBadgeUtilityClass } from './badgeUnstyledClasses'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; const useUtilityClasses = ownerState => { const { variant, anchorOrigin, overlap, invisible, classes } = ownerState; const slots = { root: ['root'], badge: ['badge', variant, `anchorOrigin${capitalize(anchorOrigin.vertical)}${capitalize(anchorOrigin.horizontal)}${capitalize(overlap)}`, invisible && 'invisible'] }; return composeClasses(slots, getBadgeUtilityClass, classes); }; const BadgeUnstyled = /*#__PURE__*/React.forwardRef(function BadgeUnstyled(props, ref) { const { anchorOrigin: anchorOriginProp = { vertical: 'top', horizontal: 'right' }, classes: classesProp, badgeContent: badgeContentProp, component = 'span', children, className, components = {}, componentsProps = {}, invisible: invisibleProp, max: maxProp = 99, overlap: overlapProp = 'rectangular', showZero = false, variant: variantProp = 'standard', /* eslint-disable react/prop-types */ theme } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const prevProps = usePreviousProps({ anchorOrigin: anchorOriginProp, badgeContent: badgeContentProp, max: maxProp, overlap: overlapProp, variant: variantProp }); let invisible = invisibleProp; if (invisibleProp == null && (badgeContentProp === 0 && !showZero || badgeContentProp == null && variantProp !== 'dot')) { invisible = true; } const { anchorOrigin = anchorOriginProp, badgeContent, max = maxProp, overlap = overlapProp, variant = variantProp } = invisible ? prevProps : props; const ownerState = _extends({}, props, { anchorOrigin, badgeContent, classes: classesProp, invisible, max, overlap, variant }); let displayValue = ''; if (variant !== 'dot') { displayValue = badgeContent > max ? `${max}+` : badgeContent; } const classes = useUtilityClasses(ownerState); const Root = components.Root || component; const rootProps = componentsProps.root || {}; const Badge = components.Badge || 'span'; const badgeProps = componentsProps.badge || {}; return /*#__PURE__*/_jsxs(Root, _extends({}, rootProps, !isHostComponent(Root) && { as: component, ownerState: _extends({}, ownerState, rootProps.ownerState), theme }, { ref: ref }, other, { className: clsx(classes.root, rootProps.className, className), children: [children, /*#__PURE__*/_jsx(Badge, _extends({}, badgeProps, !isHostComponent(Badge) && { ownerState: _extends({}, ownerState, badgeProps.ownerState), theme }, { className: clsx(classes.badge, badgeProps.className), children: displayValue }))] })); }); process.env.NODE_ENV !== "production" ? BadgeUnstyled.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the d.ts file and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * The anchor of the badge. * @default { * vertical: 'top', * horizontal: 'right', * } */ anchorOrigin: PropTypes.shape({ horizontal: PropTypes.oneOf(['left', 'right']).isRequired, vertical: PropTypes.oneOf(['bottom', 'top']).isRequired }), /** * The content rendered within the badge. */ badgeContent: PropTypes.node, /** * The badge will be added relative to this node. */ children: PropTypes.node, /** * Override or extend the styles applied to the component. */ classes: PropTypes.object, /** * @ignore */ className: PropTypes.string, /** * The component used for the root node. * Either a string to use a HTML element or a component. */ component: PropTypes.elementType, /** * The components used for each slot inside the Badge. * Either a string to use a HTML element or a component. * @default {} */ components: PropTypes.shape({ Badge: PropTypes.elementType, Root: PropTypes.elementType }), /** * The props used for each slot inside the Badge. * @default {} */ componentsProps: PropTypes.object, /** * If `true`, the badge is invisible. */ invisible: PropTypes.bool, /** * Max count to show. * @default 99 */ max: PropTypes.number, /** * Wrapped shape the badge should overlap. * @default 'rectangular' */ overlap: PropTypes.oneOf(['circular', 'rectangular']), /** * Controls whether the badge is hidden when `badgeContent` is zero. * @default false */ showZero: PropTypes.bool, /** * The variant to use. * @default 'standard' */ variant: PropTypes.string } : void 0; export default BadgeUnstyled;