From 91df7122f234a40d4deb24f552b7a2a1d2d60b53 Mon Sep 17 00:00:00 2001 From: Garrett Bear Date: Thu, 13 Oct 2022 15:39:22 -0700 Subject: [PATCH] Feat/15089/add button link (#16115) * 15089: add button link component --- .../component-library/button-base/README.mdx | 2 + .../button-base/button-base.js | 3 +- .../button-base/button-base.test.js | 9 + .../component-library/button-link/README.mdx | 73 ++++++++ .../button-link/button-link.constants.js | 8 + .../button-link/button-link.js | 45 +++++ .../button-link/button-link.scss | 43 +++++ .../button-link/button-link.stories.js | 156 ++++++++++++++++++ .../button-link/button-link.test.js | 84 ++++++++++ .../component-library/button-link/index.js | 1 + .../component-library-components.scss | 1 + 11 files changed, 424 insertions(+), 1 deletion(-) create mode 100644 ui/components/component-library/button-link/README.mdx create mode 100644 ui/components/component-library/button-link/button-link.constants.js create mode 100644 ui/components/component-library/button-link/button-link.js create mode 100644 ui/components/component-library/button-link/button-link.scss create mode 100644 ui/components/component-library/button-link/button-link.stories.js create mode 100644 ui/components/component-library/button-link/button-link.test.js create mode 100644 ui/components/component-library/button-link/index.js diff --git a/ui/components/component-library/button-base/README.mdx b/ui/components/component-library/button-base/README.mdx index f7a4ff0f7..ad79e0747 100644 --- a/ui/components/component-library/button-base/README.mdx +++ b/ui/components/component-library/button-base/README.mdx @@ -65,6 +65,8 @@ import { ButtonBase } from '../ui/component-library'; Use the `as` box prop to change the element of `ButtonBase`. Defaults to `button`. +When an `href` prop is passed it will change the element to an anchor(`a`) tag. + Button `as` options: - `button` diff --git a/ui/components/component-library/button-base/button-base.js b/ui/components/component-library/button-base/button-base.js index 1f5a1f10f..9f3a3dffe 100644 --- a/ui/components/component-library/button-base/button-base.js +++ b/ui/components/component-library/button-base/button-base.js @@ -30,9 +30,10 @@ export const ButtonBase = ({ iconProps, ...props }) => { + const Tag = props?.href ? 'a' : as; return ( { expect(anchor).toBe(1); }); + it('should render anchor element correctly by href only being passed', () => { + const { getByTestId, container } = render( + , + ); + expect(getByTestId('button-base')).toHaveClass('mm-button'); + const anchor = container.getElementsByTagName('a').length; + expect(anchor).toBe(1); + }); + it('should render button as block', () => { const { getByTestId } = render(); expect(getByTestId('block')).toHaveClass(`mm-button--block`); diff --git a/ui/components/component-library/button-link/README.mdx b/ui/components/component-library/button-link/README.mdx new file mode 100644 index 000000000..d1a743912 --- /dev/null +++ b/ui/components/component-library/button-link/README.mdx @@ -0,0 +1,73 @@ +import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'; + +import { ButtonLink } from './button-link'; + +# ButtonLink + +The `ButtonLink` is an extension of `ButtonBase` to support link styles. + + + + + +## Props + +The `ButtonLink` accepts all props below as well as all [Box](/docs/ui-components-ui-box-box-stories-js--default-story#props) and [ButtonBase](/docs/ui-components-component-library-button-base-button-base-stories-js--default-story#props) component props + + + +### Size + +Use the `size` prop and the `SIZES` object from `./ui/helpers/constants/design-system.js` to change the size of `ButtonLink`. Defaults to `SIZES.MD` + +Optional: `BUTTON_SIZES` from `./button-base` object can be used instead of `SIZES`. + +Possible sizes include: + +- `SIZES.AUTO` inherits the font-size of the parent element. +- `SIZES.SM` 32px +- `SIZES.MD` 40px +- `SIZES.LG` 48px + + + + + +```jsx +import { SIZES } from '../../../helpers/constants/design-system'; +import { ButtonLink } from '../ui/component-library/button/button-link/button-link'; + + + + + +``` + +### Type + +Use the `type` prop and the `BUTTON_TYPES` object from `./ui/helpers/constants/design-system.js` to change the context of `ButtonLink`. + + + + + +```jsx +import { ButtonLink } from '../ui/component-library/button/button-link/button-link'; + +Normal +Danger +``` + +### Href + +When an `href` is passed the tag element will switch to an `anchor`(`a`) tag. + + + + + +```jsx +import { ButtonLink } from '../ui/component-library/button/button-link/button-link'; + +Href Example; +``` diff --git a/ui/components/component-library/button-link/button-link.constants.js b/ui/components/component-library/button-link/button-link.constants.js new file mode 100644 index 000000000..8133abf50 --- /dev/null +++ b/ui/components/component-library/button-link/button-link.constants.js @@ -0,0 +1,8 @@ +import { SIZES } from '../../../helpers/constants/design-system'; + +export const BUTTON_LINK_SIZES = { + SM: SIZES.SM, + MD: SIZES.MD, + LG: SIZES.LG, + AUTO: SIZES.AUTO, +}; diff --git a/ui/components/component-library/button-link/button-link.js b/ui/components/component-library/button-link/button-link.js new file mode 100644 index 000000000..f1bfc2c1f --- /dev/null +++ b/ui/components/component-library/button-link/button-link.js @@ -0,0 +1,45 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import classnames from 'classnames'; + +import { ButtonBase } from '../button-base'; +import { BUTTON_LINK_SIZES } from './button-link.constants'; + +export const ButtonLink = ({ + className, + danger, + size = BUTTON_LINK_SIZES.MD, + ...props +}) => { + return ( + + ); +}; + +ButtonLink.propTypes = { + /** + * An additional className to apply to the ButtonLink. + */ + className: PropTypes.string, + /** + * Boolean to change button type to Danger when true + */ + danger: PropTypes.bool, + /** + * The possible size values for ButtonLink: 'SIZES.AUTO', 'SIZES.SM', 'SIZES.MD', 'SIZES.LG', + * Default value is 'SIZES.MD'. + */ + size: PropTypes.oneOf(Object.values(BUTTON_LINK_SIZES)), + /** + * ButtonLink accepts all the props from ButtonBase + */ + ...ButtonBase.propTypes, +}; + +export default ButtonLink; diff --git a/ui/components/component-library/button-link/button-link.scss b/ui/components/component-library/button-link/button-link.scss new file mode 100644 index 000000000..c17815c6c --- /dev/null +++ b/ui/components/component-library/button-link/button-link.scss @@ -0,0 +1,43 @@ +.mm-button-link { + color: var(--color-primary-default); + background-color: transparent; + + &:hover { + color: var(--color-primary-default); + opacity: 0.5; + } + + &:active { + color: var(--color-primary-default); + opacity: 0.5; + } + + &.mm-button--disabled { + &:hover { + color: var(--color-primary-default); + opacity: 0.3; + } + + &:active { + opacity: 0.3; + } + } + + &--type-danger { + color: var(--color-error-default); + + &:hover { + color: var(--color-error-default); + opacity: 0.5; + } + + &:active { + color: var(--color-error-default); + opacity: 0.5; + } + + &.mm-button--disabled:hover { + color: var(--color-error-default); + } + } +} diff --git a/ui/components/component-library/button-link/button-link.stories.js b/ui/components/component-library/button-link/button-link.stories.js new file mode 100644 index 000000000..e721459f4 --- /dev/null +++ b/ui/components/component-library/button-link/button-link.stories.js @@ -0,0 +1,156 @@ +import React from 'react'; +import { + ALIGN_ITEMS, + DISPLAY, + SIZES, + TEXT, +} from '../../../helpers/constants/design-system'; +import Box from '../../ui/box/box'; +import { ICON_NAMES } from '../icon'; +import { Text } from '../text'; +import { ButtonLink } from './button-link'; +import { BUTTON_LINK_SIZES } from './button-link.constants'; +import README from './README.mdx'; + +const marginSizeControlOptions = [ + undefined, + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 'auto', +]; + +export default { + title: 'Components/ComponentLibrary/ButtonLink', + id: __filename, + component: ButtonLink, + parameters: { + docs: { + page: README, + }, + }, + argTypes: { + as: { + control: 'select', + options: ['button', 'a'], + table: { category: 'button base props' }, + }, + block: { + control: 'boolean', + table: { category: 'button base props' }, + }, + children: { + control: 'text', + }, + className: { + control: 'text', + }, + danger: { + control: 'boolean', + }, + disabled: { + control: 'boolean', + table: { category: 'button base props' }, + }, + href: { + control: 'text', + }, + icon: { + control: 'select', + options: Object.values(ICON_NAMES), + table: { category: 'button base props' }, + }, + iconPositionRight: { + control: 'boolean', + table: { category: 'button base props' }, + }, + iconProps: { + control: 'object', + table: { category: 'button base props' }, + }, + + loading: { + control: 'boolean', + table: { category: 'button base props' }, + }, + size: { + control: 'select', + options: Object.values(BUTTON_LINK_SIZES), + }, + marginTop: { + options: marginSizeControlOptions, + control: 'select', + table: { category: 'box props' }, + }, + marginRight: { + options: marginSizeControlOptions, + control: 'select', + table: { category: 'box props' }, + }, + marginBottom: { + options: marginSizeControlOptions, + control: 'select', + table: { category: 'box props' }, + }, + marginLeft: { + options: marginSizeControlOptions, + control: 'select', + table: { category: 'box props' }, + }, + }, + args: { + children: 'Button Link', + }, +}; + +export const DefaultStory = (args) => ; + +DefaultStory.storyName = 'Default'; + +export const Size = (args) => ( + <> + + + Small Button + + + Medium (Default) Button + + + Large Button + + + + + Button Auto + {' '} + inherits the font-size of the parent element. + + +); + +export const Type = (args) => ( + + Normal + {/* Test Anchor tag to match exactly as button */} + + Danger + + +); + +export const Href = (args) => Href Example; + +Href.args = { + href: '/metamask', +}; diff --git a/ui/components/component-library/button-link/button-link.test.js b/ui/components/component-library/button-link/button-link.test.js new file mode 100644 index 000000000..473714fb0 --- /dev/null +++ b/ui/components/component-library/button-link/button-link.test.js @@ -0,0 +1,84 @@ +/* eslint-disable jest/require-top-level-describe */ +import { render } from '@testing-library/react'; +import React from 'react'; +import { SIZES } from '../../../helpers/constants/design-system'; +import { ButtonLink } from './button-link'; + +describe('ButtonLink', () => { + it('should render button element correctly', () => { + const { getByText, getByTestId, container } = render( + Button Link, + ); + expect(getByText('Button Link')).toBeDefined(); + expect(container.querySelector('button')).toBeDefined(); + expect(getByTestId('button-link')).toHaveClass('mm-button'); + }); + + it('should render anchor element correctly', () => { + const { getByTestId, container } = render( + , + ); + expect(getByTestId('button-link')).toHaveClass('mm-button'); + const anchor = container.getElementsByTagName('a').length; + expect(anchor).toBe(1); + }); + + it('should render button as block', () => { + const { getByTestId } = render(); + expect(getByTestId('block')).toHaveClass(`mm-button--block`); + }); + + it('should render with added classname', () => { + const { getByTestId } = render( + , + ); + expect(getByTestId('classname')).toHaveClass('mm-button--test'); + }); + + it('should render with different size classes', () => { + const { getByTestId } = render( + <> + + + + + , + ); + + expect(getByTestId(SIZES.SM)).toHaveClass(`mm-button--size-${SIZES.SM}`); + expect(getByTestId(SIZES.MD)).toHaveClass(`mm-button--size-${SIZES.MD}`); + expect(getByTestId(SIZES.LG)).toHaveClass(`mm-button--size-${SIZES.LG}`); + expect(getByTestId(SIZES.AUTO)).toHaveClass( + `mm-button--size-${SIZES.AUTO}`, + ); + }); + + it('should render with different types', () => { + const { getByTestId } = render( + <> + + , + ); + + expect(getByTestId('danger')).toHaveClass('mm-button-link--type-danger'); + }); + + it('should render with different button states', () => { + const { getByTestId } = render( + <> + + + , + ); + expect(getByTestId('loading')).toHaveClass(`mm-button--loading`); + expect(getByTestId('disabled')).toHaveClass(`mm-button--disabled`); + }); + it('should render with icon', () => { + const { container } = render( + , + ); + + const icons = container.getElementsByClassName('icon').length; + expect(icons).toBe(1); + }); +}); diff --git a/ui/components/component-library/button-link/index.js b/ui/components/component-library/button-link/index.js new file mode 100644 index 000000000..64bf08bb6 --- /dev/null +++ b/ui/components/component-library/button-link/index.js @@ -0,0 +1 @@ +export { ButtonLink } from './button-link'; diff --git a/ui/components/component-library/component-library-components.scss b/ui/components/component-library/component-library-components.scss index 260ed6d73..a4d49cc93 100644 --- a/ui/components/component-library/component-library-components.scss +++ b/ui/components/component-library/component-library-components.scss @@ -6,6 +6,7 @@ @import 'avatar-with-badge/avatar-with-badge'; @import 'base-avatar/base-avatar'; @import 'button-base/button-base'; +@import 'button-link/button-link'; @import 'button-primary/button-primary'; @import 'button-secondary/button-secondary'; @import 'icon/icon';