-
-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[WIP] create PopupState component #12330
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0df3ecf
feat: create MenuState component
jedwards1211 59a1b3b
fix(MenuState): fix some issues and test MenuState
jedwards1211 0db78ec
docs: try and fail to add all MenuState docs
jedwards1211 9a20eeb
fix(MenuState): drop render prop
jedwards1211 b6bae4d
refactor(MenuState): rename event handlers
jedwards1211 a0da12f
refactor: rename MenuState to PopupState
jedwards1211 d0a1d5a
docs(PopupState): fix children function docs
jedwards1211 0b33c2c
fix: add variant property to PopupState
jedwards1211 15203ee
docs(api): regen docs
jedwards1211 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import renderProps from '../utils/renderProps'; | ||
|
|
||
| export default class MenuState extends React.Component { | ||
| state = { anchorEl: null }; | ||
|
|
||
| open = eventOrAnchorEl => | ||
| this.setState({ | ||
| anchorEl: eventOrAnchorEl instanceof Event ? eventOrAnchorEl.target : eventOrAnchorEl, | ||
| }); | ||
|
|
||
| close = () => this.setState({ anchorEl: null }); | ||
|
|
||
| render() { | ||
| const { open, close } = this; | ||
| const { menuId, children, render } = this.props; | ||
| const { anchorEl } = this.state; | ||
|
|
||
| const isOpen = Boolean(anchorEl); | ||
|
|
||
| return renderProps( | ||
| { children, render }, | ||
| { | ||
| open, | ||
| close, | ||
| isOpen, | ||
| bindTrigger: { | ||
| 'aria-owns': isOpen ? menuId : null, | ||
| 'aria-haspopup': true, | ||
| onClick: open, | ||
| }, | ||
| bindMenu: { | ||
| id: menuId, | ||
| anchorEl, | ||
| open: isOpen, | ||
| onClose: close, | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| MenuState.propTypes = { | ||
| /** | ||
| * The render function. It will be called with a single object with the | ||
| * following properties: | ||
| * * `open(eventOrAnchorEl)` - calling this will open the menu | ||
| * * `close()` - calling this will close the menu | ||
| * * `isOpen` - `true`/`false` if the menu is open/closed | ||
| * * `bindTrigger` - a set of properties to pass to the menu trigger, including | ||
| * an `onClick` property that will open the menu | ||
| * * `bindMenu` - a set of properties to pass to the `Menu` component | ||
| */ | ||
| children: PropTypes.func, | ||
| /** | ||
| * The `id` property to use for the `Menu`. Will be passed to the render | ||
| * function as `bindMenu.id`, and also used for the `aria-owns` property | ||
| * passed to the trigger component via `bindTrigger`. | ||
| */ | ||
| menuId: PropTypes.string, | ||
| /** | ||
| * If you don't want to pass a `children` function, you can pass it as | ||
| * `render` instead. | ||
| */ | ||
| render: PropTypes.func, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // this is basically copied from react-powerplug: | ||
| // https://github.com/renatorib/react-powerplug/blob/master/src/utils/renderProps.js | ||
|
|
||
| import warning from 'warning'; | ||
|
|
||
| const isFn = prop => typeof prop === 'function'; | ||
|
|
||
| /** | ||
| * renderProps | ||
| * is a render/children props interop. | ||
| * will pick up the prop that was used, | ||
| * or children if both are used | ||
| */ | ||
|
|
||
| const renderProps = ({ children, render }, ...props) => { | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| warning( | ||
| isFn(children) && isFn(render), | ||
| 'You are using the children and render props together.\n' + | ||
| 'This is impossible, therefore, only the children will be used.', | ||
| ); | ||
| } | ||
|
|
||
| const fn = isFn(children) ? children : render; | ||
|
|
||
| return fn ? fn(...props) : null; | ||
| }; | ||
|
|
||
| export default renderProps; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the libraries I have seen seems to abandon the
renderproperty. What about not supporting it? So we save people asking themselves the question, "What's better between a and b?". Answering the question only provides a marginal gain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sure wouldn't mind removing it, I only put it in there because some libs like
react-powerplugseem to include it to appease people who think passing a child function is some kind of blasphemyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I'm happy about being as opinionated as React.createContext API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh that's true. I'm glad the React folks decided to do that, because there were some really dogmatic people complaining that
childrenshould always be aReact.Node.It's about the same as how in all of the Angular vs. React vs. Vue type videos I've seen, people said they didn't like React because something about putting HTML in JavaScript didn't feel right to them. They didn't have any more substantive criticism than that.