question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: render props components for managing menu, popover, etc. state

See original GitHub issue
  • This is a v1.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

I find that using a render props component is almost always more convenient for managing local state, especially for cases like Menus and Popovers where the pattern is almost always going to be the same.

Not to mention components like this would help users avoid forgetting to write ARIA attributes.

For example, I just coded up the following MenuState component for myself. I’d be happy to make a PR including docs once you guys say you’re interested in this.

// @flow

import * as React from 'react'

export type Props = {
  menuId?: ?string,
  children: (props: ChildProps) => ?React.Node,
}

export type ChildProps = {
  open: (event: Event) => any,
  close: () => any,
  isOpen: boolean,
  bindTrigger: {
    'aria-owns': ?string,
    'aria-haspopup': true,
    onClick: (event: Event) => any,
  },
  bindMenu: {
    id: ?string,
    anchorEl: ?HTMLElement,
    open: boolean,
    onClose: () => any,
  },
}

export type State = {
  anchorEl: ?HTMLElement,
}

export default class MenuState extends React.Component<Props, State> {
  state: State = {anchorEl: null}

  open = (event: Event) => this.setState({anchorEl: (event: any).target})
  close = () => this.setState({anchorEl: null})

  render(): ?React.Node {
    const {open, close} = this
    const {menuId, children} = this.props
    const {anchorEl} = this.state

    const isOpen = Boolean(anchorEl)

    return children({
      open,
      close,
      isOpen,
      bindTrigger: {
        'aria-owns': isOpen ? menuId : null,
        'aria-haspopup': true,
        onClick: open,
      },
      bindMenu: {
        id: menuId,
        anchorEl,
        open: isOpen,
        onClose: close,
      },
    })
  }
}

Usage example:

const AddDeviceGroupButton = ({onConfirm}: Props) => (
  <MenuState menuId="add-device-group-menu">
    {({close, bindTrigger, bindMenu}) => (
      <React.Fragment>
        <Button {...bindTrigger}>
          <IconInButtonStyles>
            {({classes}) => <AddRectangle className={classes.leftIcon} />}
          </IconInButtonStyles>
          Add Topic
        </Button>
        <Menu {...bindMenu}>
          <MenuItem
            onClick={() => {
              close()
              if (onConfirm) onConfirm(FROM_DEVICE)
            }}
          >
            From MQTT
          </MenuItem>
          <MenuItem
            onClick={() => {
              close()
              if (onConfirm) onConfirm(TO_DEVICE)
            }}
          >
            To MQTT
          </MenuItem>
        </Menu>
      </React.Fragment>
    )}
  </MenuState>
)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
jedwards1211commented, Feb 13, 2019

Yeah, will do soon

0reactions
jedwards1211commented, Feb 16, 2019

@mattbrookes that’s a good idea, though i think there should be an option to choose between pressing enter or space to open a submenu, or opening it automatically when stepping to the menu item with arrow keys. I think i’ve seen menus before where the submenu opens automatically but one has to press the right arrow to move into it (and the left arrow to move back to the parent menu)

Read more comments on GitHub >

github_iconTop Results From Across the Web

render props components for managing menu, popover, etc ...
I find that using a render props component is almost always more convenient for managing local state, especially for cases like Menu s...
Read more >
Render Props - React
The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function. A...
Read more >
Popover | Components - BootstrapVue
The Popover feature provides a tooltip-like behavior, can be easily applied to any interactive element via the component or v-b-popover directive.
Read more >
Controlling tooltips & pop-up menus with components in React
Use compound components to control tooltips and pop-up menus in your React applications, helping you keep your user interface clean and ...
Read more >
Headless Components - fritz2
The pure functionality of such elements, in particular the user interaction and the corresponding data handling, such as navigating within a selection list ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found