Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Badge] Custom variant #22140

Merged
merged 4 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/pages/api-docs/badge.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The `MuiBadge` name can be used for providing [default props](/customization/glo
| <span class="prop-name">max</span> | <span class="prop-type">number</span> | <span class="prop-default">99</span> | Max count to show. |
| <span class="prop-name">overlap</span> | <span class="prop-type">'circular'<br>&#124;&nbsp;'rectangular'</span> | <span class="prop-default">'rectangular'</span> | Wrapped shape the badge should overlap. |
| <span class="prop-name">showZero</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | Controls whether the badge is hidden when `badgeContent` is zero. |
| <span class="prop-name">variant</span> | <span class="prop-type">'dot'<br>&#124;&nbsp;'standard'</span> | <span class="prop-default">'standard'</span> | The variant to use. |
| <span class="prop-name">variant</span> | <span class="prop-type">'dot'<br>&#124;&nbsp;'standard'<br>&#124;&nbsp;string</span> | <span class="prop-default">'standard'</span> | The variant to use. |

The `ref` is forwarded to the root element.

Expand All @@ -54,6 +54,7 @@ Any other props supplied will be provided to the root element (native element).
| <span class="prop-name">colorSecondary</span> | <span class="prop-name">.MuiBadge-colorSecondary</span> | Styles applied to the root element if `color="secondary"`.
| <span class="prop-name">colorError</span> | <span class="prop-name">.MuiBadge-colorError</span> | Styles applied to the root element if `color="error"`.
| <span class="prop-name">dot</span> | <span class="prop-name">.MuiBadge-dot</span> | Styles applied to the root element if `variant="dot"`.
| <span class="prop-name">standard</span> | <span class="prop-name">.MuiBadge-standard</span> | Styles applied to the root element if `variant="standard"`.
| <span class="prop-name">anchorOriginTopRightRectangular</span> | <span class="prop-name">.MuiBadge-anchorOriginTopRightRectangular</span> | Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="rectangular"`.
| <span class="prop-name">anchorOriginBottomRightRectangular</span> | <span class="prop-name">.MuiBadge-anchorOriginBottomRightRectangular</span> | Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }} overlap="rectangular"`.
| <span class="prop-name">anchorOriginTopLeftRectangular</span> | <span class="prop-name">.MuiBadge-anchorOriginTopLeftRectangular</span> | Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }} overlap="rectangular"`.
Expand Down
7 changes: 0 additions & 7 deletions framer/Material-UI.framerfx/code/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ interface Props {
badgeContent: string;
max: number;
showZero: boolean;
variant: 'dot' | 'standard';
icon: string;
theme: 'Filled' | 'Outlined' | 'Rounded' | 'TwoTone' | 'Sharp';
badgeColor: 'default' | 'primary' | 'secondary' | 'error';
Expand Down Expand Up @@ -37,7 +36,6 @@ Badge.defaultProps = {
badgeContent: '8',
max: 99,
showZero: false,
variant: 'standard' as 'standard',
icon: '',
theme: 'Filled' as 'Filled',
badgeColor: 'primary' as 'primary',
Expand All @@ -58,11 +56,6 @@ addPropertyControls(Badge, {
type: ControlType.Boolean,
title: 'Show zero',
},
variant: {
type: ControlType.Enum,
title: 'Variant',
options: ['dot', 'standard'],
},
icon: {
type: ControlType.String,
title: 'Icon',
Expand Down
2 changes: 2 additions & 0 deletions framer/scripts/framerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const componentSettings = {
'disableFocusRipple',
'invisible',
'overlap',
// FIXME: `Union`
'variant',
],
propValues: {
icon: "''",
Expand Down
7 changes: 6 additions & 1 deletion packages/material-ui/src/Badge/Badge.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react';
import { OverridableStringUnion } from '@material-ui/types';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';

export interface BadgeOrigin {
vertical: 'top' | 'bottom';
horizontal: 'left' | 'right';
}

export interface BadgePropsVariantOverrides {}
export type BadgeVariantDefaults = Record<'standard' | 'dot', true>;

export interface BadgeTypeMap<P = {}, D extends React.ElementType = 'div'> {
props: P & {
/**
Expand Down Expand Up @@ -43,7 +47,7 @@ export interface BadgeTypeMap<P = {}, D extends React.ElementType = 'div'> {
/**
* The variant to use.
*/
variant?: 'standard' | 'dot';
variant?: OverridableStringUnion<BadgeVariantDefaults, BadgePropsVariantOverrides>;
};
defaultComponent: D;
classKey: BadgeClassKey;
Expand All @@ -56,6 +60,7 @@ export type BadgeClassKey =
| 'colorSecondary'
| 'colorError'
| 'dot'
| 'standard'
| 'anchorOriginTopRightRectangular'
| 'anchorOriginBottomRightRectangular'
| 'anchorOriginTopLeftRectangular'
Expand Down
27 changes: 25 additions & 2 deletions packages/material-ui/src/Badge/Badge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { useThemeVariants } from '@material-ui/styles';
import withStyles from '../styles/withStyles';
import capitalize from '../utils/capitalize';

Expand Down Expand Up @@ -62,6 +63,8 @@ export const styles = (theme) => ({
minWidth: RADIUS_DOT * 2,
padding: 0,
},
/* Styles applied to the root element if `variant="standard"`. */
standard: {},
/* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }} overlap="rectangular"`. */
anchorOriginTopRightRectangular: {
top: 0,
Expand Down Expand Up @@ -206,6 +209,22 @@ const Badge = React.forwardRef(function Badge(props, ref) {
variant = variantProp,
} = invisible ? prevProps : props;

const themeVariantsClasses = useThemeVariants(
{
...props,
anchorOrigin,
badgeContent,
color,
component: ComponentProp,
invisible,
max,
overlap,
showZero,
variant,
},
'MuiBadge',
);

let displayValue = '';

if (variant !== 'dot') {
Expand All @@ -218,6 +237,7 @@ const Badge = React.forwardRef(function Badge(props, ref) {
<span
className={clsx(
classes.badge,
classes[variant],
classes[
`anchorOrigin${capitalize(anchorOrigin.vertical)}${capitalize(
anchorOrigin.horizontal,
Expand All @@ -226,8 +246,8 @@ const Badge = React.forwardRef(function Badge(props, ref) {
{
[classes[`color${capitalize(color)}`]]: color !== 'default',
[classes.invisible]: invisible,
[classes.dot]: variant === 'dot',
},
themeVariantsClasses,
)}
>
{displayValue}
Expand Down Expand Up @@ -293,7 +313,10 @@ Badge.propTypes = {
/**
* The variant to use.
*/
variant: PropTypes.oneOf(['dot', 'standard']),
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['dot', 'standard']),
PropTypes.string,
]),
};

export default withStyles(styles, { name: 'MuiBadge' })(Badge);