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

UnderlineNav fixes #134

Merged
merged 6 commits into from Jul 19, 2018
Merged

UnderlineNav fixes #134

merged 6 commits into from Jul 19, 2018

Conversation

shawnbot
Copy link
Contributor

This addresses a couple of points in #128:

  1. Removes a circular import between UnderlineNav and UnderlineNavLink by using duck typing to detect UnderlineNavLink instances in UnderlineNav.
  2. Moves the rendersClass() testing helper into utils/testing.

I also got a change to package-lock.json when I ran npm install, so I've included that in this PR. I can remove that commit if there's a problem with it, though.

@shawnbot shawnbot requested a review from emplums July 16, 2018 22:48
@shawnbot shawnbot mentioned this pull request Jul 16, 2018
4 tasks
// if this is a react-router NavLink (duck typing!),
// set activeClassName={SELECTED_CLASS}
if (child.type.name === 'NavLink') {
newProps.activeClassName = SELECTED_CLASS
}
return React.cloneElement(child, newProps)
return React.cloneElement(child, {className, ...newProps})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could probably just be:

newProps.className = className
return React.cloneElement(child, newProps)

Copy link

Choose a reason for hiding this comment

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

I think the shorthand here is fine!

@shawnbot
Copy link
Contributor Author

I have one minor concern about the duck typing approach of testing child.type.name, which should be the name of the component function or class. It's possible that code minifying tools like Uglify can rename these functions to have shorter names unless you tell them not to "mangle" them. For instance, this:

export default function MyComponent(props) {
  return <div>{props.children}</div>
}

might be minified with mangled function names into:

exports=function z(b){return A.createElement('div',{},b.children)}

...in which case the export's name will be z, not MyComponent. Various React tools understand this and the conventional solution is to set the displayName property on a component (function or class), which minifiers won't touch. Unfortunately, it looks like NavLink doesn't set displayName, so we can't use that to duck type.

Anyway, it works for now, and you can see in the live docs that it works in our production bundle, but it's something to be aware of.

Copy link

@emplums emplums left a comment

Choose a reason for hiding this comment

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

Looks good to me! I will keep the class name duck typing in mind when looking through the emotion/system-classname stuff. Not sure if emotion minifies the class names or not!

@shawnbot
Copy link
Contributor Author

Good call, @emplums. We might want to set displayName on our components just to be safe.

@shawnbot shawnbot merged commit 358885d into master Jul 19, 2018
@shawnbot shawnbot deleted the underline-nav-fixes branch July 19, 2018 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants