24.1.0 • Published 15 days ago

@leafygreen-ui/menu v24.1.0

Weekly downloads
335
License
Apache-2.0
Repository
github
Last release
15 days ago

Menu

npm (scoped)

View on MongoDB.design

Installation

Yarn

yarn add @leafygreen-ui/menu

NPM

npm install @leafygreen-ui/menu

Peer Dependencies

PackageVersion
@leafygreen-ui/leafygreen-provider^1.1.0

Example

import { Menu, MenuSeparator, MenuItem } from '@leafygreen-ui/menu';

// Trigger as an HTML Element
<Menu align="bottom" justify="start" trigger={<button>trigger</button>}>
  <MenuItem active>Active Menu Item</MenuItem>
  <MenuItem
    disabled={boolean('Disabled', true)}
    description="I am a description"
  >
    Disabled Menu Item
  </MenuItem>
  <MenuItem description="I am also a description">
    Menu Item With Description
  </MenuItem>
  <MenuItem href="http://mongodb.design">I am a link!</MenuItem>
</Menu>;

// Trigger as a function
<Menu
  align="bottom"
  justify="start"
  trigger={({ onClick, children }) => (
    <button onClick={onClick}>
      Example Trigger
      {children}
    </button>
  )}
>
  <MenuItem>Menu Item 1</MenuItem>
  <MenuItem>Menu Item 2</MenuItem>
  <MenuItem>Menu Item 3</MenuItem>
</Menu>;

Output HTML

<!-- Trigger as an HTML Element -->
<button>
  trigger
  <div class="leafygreen-ui-1hyfx7x"></div>
</button>
<div class="leafygreen-ui-19z0mfw">
  <ul class="leafygreen-ui-1guv7w9" role="menu">
    <li role="none">
      <button
        data-leafygreen-ui="menu-item-container"
        class="leafygreen-ui-19xfwtd"
        role="menuitem"
        aria-disabled="false"
      >
        <div class="leafygreen-ui-14a8fex">Active Menu Item</div>
      </button>
    </li>
    <li role="none">
      <button
        data-leafygreen-ui="menu-item-container"
        class="leafygreen-ui-7pgwa0"
        role="menuitem"
        aria-disabled="true"
        tabindex="-1"
      >
        <div class="leafygreen-ui-10xqyru">Disabled Menu Item</div>
        <div class="leafygreen-ui-17sf9go">I am a description</div>
      </button>
    </li>
    <li role="none">
      <button
        data-leafygreen-ui="menu-item-container"
        class="leafygreen-ui-1sk3xcx"
        role="menuitem"
        aria-disabled="false"
      >
        <div class="leafygreen-ui-r0sqyc">Menu Item With Description</div>
        <div class="leafygreen-ui-1dm36mc">I am also a description</div>
      </button>
    </li>
    <li role="none">
      <a
        data-leafygreen-ui="menu-item-container"
        target="_blank"
        rel="noopener noreferrer"
        class="leafygreen-ui-1sk3xcx"
        role="menuitem"
        aria-disabled="false"
        href="http://mongodb.design"
        ><div class="leafygreen-ui-r0sqyc">I am a link!</div></a
      >
    </li>
  </ul>
</div>

<!-- Trigger as a function -->
<button>
  Example Trigger
  <div class="leafygreen-ui-1hyfx7x"></div>
</button>
<div class="leafygreen-ui-1td4qre">
  <ul class="leafygreen-ui-1guv7w9" role="menu">
    <li role="none">
      <button
        data-leafygreen-ui="menu-item-container"
        class="leafygreen-ui-1sk3xcx"
        role="menuitem"
        aria-disabled="false"
      >
        <div class="leafygreen-ui-r0sqyc">Menu Item 1</div>
      </button>
    </li>
    <li role="none">
      <button
        data-leafygreen-ui="menu-item-container"
        class="leafygreen-ui-1sk3xcx"
        role="menuitem"
        aria-disabled="false"
      >
        <div class="leafygreen-ui-r0sqyc">Menu Item 2</div>
      </button>
    </li>
    <li role="none">
      <button
        data-leafygreen-ui="menu-item-container"
        class="leafygreen-ui-1sk3xcx"
        role="menuitem"
        aria-disabled="false"
      >
        <div class="leafygreen-ui-r0sqyc">Menu Item 3</div>
      </button>
    </li>
  </ul>
</div>

Usage with NextJS Link components

We recommend using Menu with NextJS's links components in the following pattern:

import NextLink from 'next/link';

function CustomLink({ href, children, ...props }) {
  return (
    <NextLink href={href}>
      <a {...props}>
        {children}
      </a>
    </NextLink>
  );
}

<Menu trigger={<button onClick={handleClick}>trigger</button>} open={open}>
  <MenuItem as={CustomLink} href="/test">
    Test
  </MenuItem>
  <SubMenu
    title="Submenu"
    href="http://mongodb.design"
  >
    <MenuItem>SubMenu Item 1</MenuItem>
    <MenuItem as={CustomLink} href="/test-2">
      SubMenu Item 2
    </MenuItem>
  </SubMenu>
</Menu>

This pattern is recommended given the SubMenu component expects to pass styling through the className prop, which would not apply correctly if it was passed to NextLink.

In other words, defining a MenuItem as:

<MenuItem as={CustomLink} href="/test-2">
  SubMenu Item 2
  </MenuItem>

would render, but without the correct styles.

Properties

PropTypeDescriptionDefault
openbooleanDetermines whether or not the <Menu /> will appear as open or closedfalse
setOpenfunctionWhen controlling the component, use setOpen to keep track of the <Menu /> component's state so that clicks on the document's backdrop as well as a user pressing the Escape Key will close the Menu and update the consuming application's local state accordingly.
initialOpenbooleanPasses an initial value for "open" to an uncontrolled menufalse
shouldClosefunctionDetermines if the Menu should close when the backdrop or Escape keys are clicked. Defaults to true.() => true
align'top', 'bottom', 'left', 'right'Determines the alignment of the <Menu /> component relative to a reference element, or the element's nearest parent'bottom'
justify'start', 'middle', 'end'Determines the justification of the Menu component (based on the alignment) relative to a reference element or the element's nearest parent'end'
refElHTMLElementPass a reference to an element that the Menu component should be positioned against
triggerfunction, React.ReactNodeA ReactNode against which the Menu will be positioned. The trigger prop can also support being passed a function. To work as expected, the function must accept an argument of children, which should be rendered inside of the function passed to trigger.
usePortalbooleanWill position Menu's children relative to its parent without using a Portal if usePortal is set to false. NOTE: The parent element should be CSS position relative, fixed, or absolute if using this option.true
adjustOnMutationbooleanDetermines whether or not the <Menu/> should reposition itself based on changes to trigger or reference element position.false
usePortalbooleanDetermines if the Menu will be rendered within a portal.true
portalContainerHTMLElement, nullSets the container used for the popover's portal.
scrollContainerHTMLElement, nullIf the popover portal has a scrollable ancestor other than the window, this prop allows passing a reference to that lement to allow the portal to position properly.
portalClassNamestringPasses the given className to the popover's portal container if the default portal container is being used.
popoverZIndexnumberSets the z-index CSS property for the popover.
darkModebooleanDetermines whether or not the component will be rendered in dark theme.

Any other properties will be spread on the Menu ul container

MenuItem

Properties

PropTypeDescriptionDefault
hrefstringIf supplied, will render the <MenuItem /> inside of an <a> tag, rather than a <button> tag
childrennodeContent to appear inside of <MenuItem /> component
classNamestringClassname applied to li element
onClickfunctionFunction that will be called when a <MenuItem /> is clicked
activebooleanDetermines if the <MenuItem /> is activefalse
disabledbooleanDetermines if the <MenuItem /> is disabledfalse
descriptionnodeContent to appear below main text of the <MenuItem />
asReact.ElementTypeDetermines what the <MenuItem /> will be rendered as
sizedefault, largeSize of the <MenuItem /> component'default'
glyphReact.ReactElementSlot to pass in an Icon rendered to the left of <MenuItem /> text.
variant'default', 'destructive'Determines variant of <MenuItem /> component'default'
...native attributes of component passed to as propAny other props will be spread on the root element

SubMenu

Properties

PropTypeDescriptionDefault
openbooleanDetermines if <SubMenu /> item appears openfalse
setOpenfunctionFunction to set the value of open in <SubMenu />
classNamestringclassName applied to SubMenu root element
descriptionstring, React.ReactElementContent to appear below main text of SubMenu
activebooleanDetermines if <SubMenu /> appears activefalse
disabledbooleanDetermines if <SubMenu /> appears disabledfalse
glyphReact.ReactElementSlot to pass in an Icon rendered to the left of <SubMenu /> text.
titlestringMain text rendered in <SubMenu />
hrefstringIf supplied, will render the <SubMenu /> inside of an <a> tag, rather than a <button> tag
spacingnumberDistance between the content rendered inside of the Menu and the trigger15
...native anchor or button attributesAny other props will be spread on the root element
24.1.0

15 days ago

24.0.0

25 days ago

23.0.3

1 month ago

23.0.2

2 months ago

23.0.1

2 months ago

23.0.1-popover.0

4 months ago

23.0.0

7 months ago

22.0.3

10 months ago

22.0.9

7 months ago

22.0.8

8 months ago

22.0.7

9 months ago

22.0.6

9 months ago

22.0.5

9 months ago

22.0.4

9 months ago

23.0.0-alpha.0

10 months ago

23.0.0-alpha.1

10 months ago

21.0.1-next.2

12 months ago

21.0.1-next.3

12 months ago

21.0.1-next.4

12 months ago

21.0.1-next.5

12 months ago

21.0.1-next.6

12 months ago

21.0.1-next.7

12 months ago

21.0.0

12 months ago

21.0.1-next.0

12 months ago

21.0.1-next.1

12 months ago

22.0.2

11 months ago

22.0.1

11 months ago

22.0.0

11 months ago

20.0.1

1 year ago

20.0.3

1 year ago

20.0.2

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.0.0-next.1

2 years ago

15.0.1

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.0

2 years ago

16.0.0-next.0

2 years ago

15.0.0

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

14.0.2

2 years ago

15.0.1-next.0

2 years ago

13.1.1

2 years ago

13.1.0

2 years ago

13.0.1

2 years ago

12.2.0

2 years ago

13.0.0

2 years ago

12.1.0

2 years ago

12.0.0

2 years ago

11.0.1

3 years ago

11.0.0

3 years ago

10.0.3

3 years ago

10.0.0

3 years ago

10.0.1

3 years ago

10.0.2

3 years ago

9.1.4

3 years ago

9.1.3

3 years ago

9.1.2

3 years ago

9.1.1

3 years ago

9.1.0

3 years ago

9.0.2

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

8.0.0

4 years ago

7.0.10

4 years ago

7.0.9

4 years ago

7.0.8

4 years ago

7.0.7

4 years ago

7.0.6

4 years ago

7.0.5

4 years ago

7.0.4

4 years ago

7.0.3

4 years ago

7.0.2

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

6.0.14

4 years ago

6.0.13

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.8

4 years ago

6.0.7

4 years ago

6.0.6

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago