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

Add custom nav link props to Navigation component #24570

Merged
merged 3 commits into from
Aug 20, 2020

Conversation

joshuatf
Copy link
Contributor

Description

NOTE: Branched from add/nav-badges and will need a rebase pending its merge.

Allows passing the link tag name as well as additional props to this component. Allows consumers to bring their own routing components and link tags.

Screenshots

Screen Shot 2020-08-14 at 7 10 18 AM

Testing

  1. Run npm run storybook:dev.
  2. Visit the Navigation component.
  3. Check that the newly added "Custom link" works by opening a new browser tab.

@psealock psealock added the [Feature] Navigation Component A navigational waterfall component for hierarchy of items. label Aug 17, 2020
Copy link
Contributor

@psealock psealock left a comment

Choose a reason for hiding this comment

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

Very nice. I noted an opportunity to simplify logic

{
title: 'Custom link',
id: 'item-4',
linkTag: 'a',
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make an assumption here that if an href is found, use a anchor tag?

Copy link
Contributor

Choose a reason for hiding this comment

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

Now that I think about it, Button already has this logic built in.

const Tag = href !== undefined && ! trulyDisabled ? 'a' : 'button';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya, button already includes this logic; the use of a here is purely for demonstration, though we could easily swap this with another tag if you think it would be more appropriate.

Using linkTag allows us to pass a prop like the Link component or some other custom component to integrate with various routers.

@psealock
Copy link
Contributor

There are 3 types of nav actions:

  • Button with a callback
  • External link
  • Internal link

To differentiate between the first two can be as simple as the presence of an href in the config data. Should the onClick callback be disabled in this case? Does the External Link component offer any functionality here?

The 3rd type provides navigation in whatever context of the consuming app's routing. A callback could be sufficient as you can do something like getHistory().push( /** something */ );. But what about passing a component like <Link /> from wc-admin? Maybe NavigationMenuItem could have link prop or something. What do you think?

@joshuatf
Copy link
Contributor Author

To differentiate between the first two can be as simple as the presence of an href in the config data.

I think the Button component adequately covers this.

Should the onClick callback be disabled in this case?

I'm in favor of allowing both. The use case here might be a link click, but also wanting to fire an event, such as tracking.

Does the External Link component offer any functionality here?

Seems like it does offer some a11y support, but I would let consumers use this in the linkTag if they feel inclined to and just defer to default Button behavior where possible.

But what about passing a component like from wc-admin? Maybe NavigationMenuItem could have link prop or something. What do you think?

Agreed- that's the intention of the linkTag 😄 linkTagProps allows passing additional parameters like to or anything necessary for a passed component's routing.

@psealock
Copy link
Contributor

Can you rebase this one now that the two previous PRs are merged?

@psealock psealock force-pushed the add/nav-custom-links branch from d8506c6 to 52cf7ef Compare August 19, 2020 00:45
@psealock
Copy link
Contributor

To differentiate between the first two can be as simple as the presence of an href in the config data.

I think the Button component adequately covers this.

Indeed it does. If you let Button handle all the logic with this change, not only doe Button apply the correct presentational styles, but it respects the target: '_blank', and renders sematically with an <a /> tag.

        return (
                <MenuItemUI className={ classes }>
-                       <LinkTagName
+                       <Button
                                className={ classes }
                                href={ href }
                                onClick={ handleClick }
@@ -56,7 +56,7 @@ const NavigationMenuItem = ( props ) => {
                                </Text>
                                { badge && <BadgeUI>{ badge }</BadgeUI> }
                                { hasChildren ? <Icon icon={ chevronRight } /> : null }
-                       </LinkTagName>
+                       </Button>
                </MenuItemUI>
        );
 };

Screen Shot 2020-08-19 at 12 57 32 PM

Screen Shot 2020-08-19 at 1 00 10 PM

In 8864e06 I take it one step further by adding a LinkComponent prop for passing in a custom component. I went ahead and pushed to the branch because it creating a new PR for just that change hides it under the psealock namespace. What do you think? Feel free to revert the commit or push new changes.

@joshuatf
Copy link
Contributor Author

Indeed it does. If you let Button handle all the logic with this change, not only doe Button apply the correct presentational styles, but it respects the target: '_blank', and renders sematically with an tag.

Just for visibility from above:

Ya, button already includes this logic; the use of a here is purely for demonstration, though we could easily swap this with another tag if you think it would be more appropriate.

In 8864e06 I take it one step further by adding a LinkComponent prop for passing in a custom component.

The linkTag I previously added allowed passing in a custom component, so it looks like 8864e06 renames linkTag to LinkComponent. I think the renaming here is good, but we should keep my old implementation for the tag instead of separating the two and having to pass props twice.

{ LinkComponent ? (
<LinkComponent
className={ classes }
onClick={ handleClick }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missing href in here. I think the old implementation simplifies the props logic here a bit.

{
title: 'Internal link',
id: 'item-5',
LinkComponent: CustomRouterLink,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice addition 👍

@joshuatf
Copy link
Contributor Author

@psealock Added back in the link component tag in 2b71528

Copy link
Contributor

@psealock psealock left a comment

Choose a reason for hiding this comment

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

I like the composite approach here. I did realize after the fact that your approach did indeed allow passing of a custom component. I think we will need some good documentation around LinkComponent, and the entire config for that matter.

@psealock psealock merged commit 1c2111b into WordPress:feature/navigation Aug 20, 2020
psealock added a commit that referenced this pull request Aug 25, 2020
* Allow custom link tag and props for menu items

* Let Button handle logic and add LinkComponent

* Add back in link tag const to allow sharing of props

Co-authored-by: Paul Sealock <psealock@gmail.com>
psealock added a commit that referenced this pull request Aug 26, 2020
* Allow custom link tag and props for menu items

* Let Button handle logic and add LinkComponent

* Add back in link tag const to allow sharing of props

Co-authored-by: Paul Sealock <psealock@gmail.com>
psealock added a commit that referenced this pull request Aug 31, 2020
* Allow custom link tag and props for menu items

* Let Button handle logic and add LinkComponent

* Add back in link tag const to allow sharing of props

Co-authored-by: Paul Sealock <psealock@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Navigation Component A navigational waterfall component for hierarchy of items.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants