21.2.0 • Published 25 days ago

@leafygreen-ui/button v21.2.0

Weekly downloads
560
License
Apache-2.0
Repository
github
Last release
25 days ago

Button

npm (scoped)

View on MongoDB.design

Installation

Yarn

yarn add @leafygreen-ui/button

NPM

npm install @leafygreen-ui/button

Peer Dependencies

PackageVersion
@leafygreen-ui/leafygreen-provider^1.1.0

Example

<Button
  variant={Variant.Default}
  darkMode={true}
  size={Size.default}
  disabled={false}
  leftGlyph={<Icon glyph={leftGlyph} />}
  rightGlyph={<Icon glyph={rightGlyph} />}
>
  Button Test Content
</Button>

Output HTML

<button type="button" class="leafygreen-ui-1m13q2j" aria-disabled="false">
  <div class="leafygreen-ui-1igr8p9">
    <svg
      width="16"
      height="16"
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 16 16"
      class="leafygreen-ui-1s3nqii"
      role="presentation"
      aria-hidden="true"
      alt=""
    >
      <path
        d="M10.623 1a.886.886 0 01.041 1.772V5.34c0 .114.035.239.095.343l3.516 5.647.002.003c.232.387.356.818.356 1.254 0 .4-.106.804-.318 1.17l-.003.006-.004.007-.095.158A2.36 2.36 0 0112.217 15h-8.4c-.44 0-.86-.118-1.22-.333a2.448 2.448 0 01-.875-.904l-.001-.003a2.428 2.428 0 01-.301-1.163c0-.438.123-.877.367-1.268l3.53-5.647a.695.695 0 00.094-.343V2.772A.885.885 0 015.452 1zM8.904 2.774H7.185V5.34c0 .457-.136.892-.365 1.27l-.001.001-3.522 5.657a.644.644 0 00-.103.343c0 .102.026.2.081.296l.003.005a.62.62 0 00.553.329h8.4c.12 0 .223-.031.316-.087a.675.675 0 00.238-.898L9.272 6.612a2.324 2.324 0 01-.368-1.273V2.774zm.606 6.633l1.553 2.49a.227.227 0 01.01.243.223.223 0 01-.206.122H5.195c-.122 0-.187-.075-.206-.122-.028-.056-.056-.15.01-.243l1.553-2.49H9.51z"
        fill="currentColor"
      ></path></svg
    >Button Test Content<svg
      width="16"
      height="16"
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 16 16"
      class="leafygreen-ui-voftg9"
      role="presentation"
      aria-hidden="true"
      alt=""
    >
      <path
        d="M14 4v7H2V4c0-1.102.898-2 2-2h8c1.102 0 2 .898 2 2zm-2 5V4H4v5h8zM0 12h16v1c0 .555-.445 1-1 1H1c-.555 0-1-.445-1-1v-1z"
        fill="currentColor"
        fill-rule="evenodd"
      ></path>
    </svg>
  </div>
</button>

Properties

PropTypeDescriptionDefault
variant'default', 'primary', 'primaryOutline', 'danger', 'dangerOutline', 'baseGreen'Sets the style variant of the button.'default'
darkModebooleanDetermines if the component renders in dark modefalse
size'xsmall', 'small', 'default', 'large'Sets the size variant of the button.'default'
childrennodeThe content that will appear inside of the <Button /> component.null
classNamestringAdds a className to the class attribute.''
disabledbooleanDisables the buttonfalse
as'a' \| 'button'Determines the root element. An a tags can be supplied to replace button from being the DOM element that wraps the component.button
hrefstringIf a href is supplied it will change the as value, such that the component renders inside of an a tag instead of inside of a button tag.
leftGlyphReact.ReactElementGlyph that will appear to the left of text, if there is text provided via the children prop. If no children are supplied to the component, passing an Icon here will render the button as an icon-only button.
rightGlyphReact.ReactElementGlyph that will appear to the right of text, if there is text provided via the children prop. If no children are supplied to the component, passing an Icon here will render the button as an icon-only button.
isLoadingbooleanIndicates whether the Button is in a loading statefalse
loadingTextstringString displayed in place of children while the button is in a loading state
baseFontSize14, 16Determines the base font-size of the component14
loadingIndicatorReact.ReactElementElement to be rendered as loading indicator
...native attributes of component passed to as propAny other properties will be spread on the root element

Note: In order to make this Component act as a submit button, the recommended approach is to pass submit as the type prop. Note it is also valid to pass input to the as prop, and the button's content's to the value prop -- in this case, do not supply children to the component.

Test Harnesses

getTestUtils()

getTestUtils() is a util that allows consumers to reliably interact with LG Button in a product test suite. If the Button component cannot be found, an error will be thrown.

Usage

import Button, { getTestUtils } from '@leafygreen-ui/button';

const utils = getTestUtils(lgId?: string); // lgId refers to the custom `data-lgid` attribute passed to `Button`. It defaults to 'lg-button' if left empty.

Single Button

import { render } from '@testing-library/react';
import Button, { getTestUtils } from '@leafygreen-ui/button';

...

test('button', () => {
  render(<Button>Click me</Button>);
  const { getButton } = getTestUtils();

  expect(getButton()).toBeInTheDocument();
});

Multiple Button components

When testing multiple Button components it is recommended to add the custom data-lgid attribute to each Button.

import { render } from '@testing-library/react';
import Button, { getTestUtils } from '@leafygreen-ui/button';

...

test('button', () => {
  render(
    <>
      <Button data-lgid="button-1">Click 1</Button>
      <Button data-lgid="button-2" disabled>Click 2</Button>
    </>,
  );
  const utilsOne = getTestUtils('button-1'); // data-lgid
  const utilsTwo = getTestUtils('button-2'); // data-lgid
  // First Button
  expect(utilsOne.getButton()).toBeInTheDocument();
  expect(utilsOne.isDisabled()).toBe(false);

  // Second Button
  expect(utilsTwo.getButton()).toBeInTheDocument();
  expect(utilsTwo.isDisabled()).toBe(true);
});

Test Utils

const { getButton, isDisabled } = getTestUtils();
UtilDescriptionReturns
getButtonReturns the input nodeHTMLButtonElement
isDisabledReturns whether the input is disabledboolean
21.2.0

25 days ago

21.1.0

1 month ago

21.0.12

3 months ago

21.0.10

6 months ago

21.0.2

9 months ago

21.0.11

6 months ago

21.0.1

9 months ago

21.0.4

9 months ago

21.0.3

9 months ago

21.0.0

10 months ago

21.0.9

7 months ago

21.0.6

8 months ago

21.0.5

9 months ago

21.0.8

7 months ago

21.0.7

7 months ago

20.2.2

10 months ago

20.3.1

10 months ago

20.3.0

10 months ago

21.0.0-alpha.0

10 months ago

21.0.0-alpha.1

10 months ago

20.2.1

11 months ago

20.2.0

11 months ago

20.0.7

12 months ago

20.0.6

12 months ago

20.0.8-next.1

12 months ago

20.0.8-next.0

12 months ago

20.0.8-next.3

12 months ago

20.0.8-next.2

12 months ago

20.0.8-next.5

12 months ago

20.0.8-next.4

12 months ago

20.0.8-next.7

12 months ago

20.0.8-next.6

12 months ago

20.1.0

11 months ago

20.1.1

11 months ago

20.0.5

1 year ago

20.0.4

1 year ago

20.0.3

1 year ago

20.0.2

1 year ago

20.0.1

1 year ago

20.0.0

1 year ago

19.0.1

1 year ago

19.0.0

1 year ago

19.0.3

1 year ago

19.0.2

1 year ago

19.0.4

1 year ago

18.0.0

2 years ago

16.1.1

2 years ago

16.1.0

2 years ago

17.0.0

2 years ago

16.1.0-test.0

2 years ago

16.1.0-next.2

2 years ago

16.1.0-next.0

2 years ago

16.1.0-next.1

2 years ago

16.0.2

2 years ago

16.0.1

2 years ago

16.0.3

2 years ago

16.0.0

2 years ago

15.0.2

2 years ago

15.0.1

2 years ago

15.0.0

2 years ago

14.0.0

2 years ago

13.0.1

2 years ago

13.0.0

2 years ago

12.0.5

3 years ago

12.0.4

3 years ago

12.0.3

3 years ago

12.0.0

3 years ago

12.0.1

3 years ago

12.0.2

3 years ago

11.0.4

3 years ago

11.0.3

3 years ago

11.0.2

3 years ago

11.0.0

3 years ago

11.0.1

3 years ago

10.0.2

3 years ago

10.0.1

3 years ago

10.0.0

3 years ago

9.0.0

3 years ago

8.0.1

4 years ago

8.0.0

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.2.5

4 years ago

4.2.4

4 years ago

4.2.3

4 years ago

4.2.2

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

5 years ago

2.3.7

5 years ago

2.3.6

5 years ago

2.3.5

5 years ago

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago