Skip to content
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

Media: Refactor the MediaUploadButton to be agnostic to its rendered UI #4513

Merged
merged 1 commit into from
Jan 16, 2018

Conversation

youknowriad
Copy link
Contributor

We're using the MediaUploadButton in several places and the button triggering the modal is very different from a place to another:

  • We use regular buttons sometimes
  • We use icon buttons with tooltips and labels
  • We use simple links
  • We use images

To address all the use-cases we were slowly adding complexity to the render method of the MediaUploadButton. This PR solves this issue by relying on a render prop instead. That way we can reuse any other component we use regularly to render these buttons.

This PR also adds a README to document this component and deprecates the old component.

@youknowriad youknowriad added the [Feature] Media Anything that impacts the experience of managing media label Jan 16, 2018
@youknowriad youknowriad self-assigned this Jan 16, 2018
>
{ __( 'Add from Media Library' ) }
</MediaUploadButton>
render={ ( { open } ) => (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Children as function" would also work but I personally prefer "render props". I prefer if children would have been always used as "arrays" to avoid confusion in the shape of Children in general.

Copy link
Member

@gziolo gziolo Jan 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I heard in Egghead.io podcast that render props is easier to follow for the majority of developers. I prefer it over this awkward this.props.children(), too.

@youknowriad youknowriad force-pushed the update/media-upload-button-render-prop branch from 17b42d1 to 410a06e Compare January 16, 2018 12:57
const { children, buttonProps, tooltip } = this.props;

return (
<MediaUpload
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, nice! :)

MediaUpload
===========

Dropdown is a React component used to render a button that opens a the WordPress media modal.
Copy link
Member

@gziolo gziolo Jan 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and paste here :)

@gziolo
Copy link
Member

gziolo commented Jan 16, 2018

Love this refactoring. In general, I'm always happy seeing when if statements can be replaced with the higher level logic to make the code more explicit about what is happening. This adds a few lines of code to the components using <MediaModal />, but it also makes them much more flexible and at the same time easier to understand! Nice work @youknowriad.

I will do some tests before accepting this PR.

@youknowriad youknowriad force-pushed the update/media-upload-button-render-prop branch from 410a06e to 1ba726b Compare January 16, 2018 13:46
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests also well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Media Anything that impacts the experience of managing media
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants