Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Web/navbar hide #41

Merged
merged 18 commits into from
Aug 13, 2018
Merged

Web/navbar hide #41

merged 18 commits into from
Aug 13, 2018

Conversation

bobheadxi
Copy link
Contributor

@bobheadxi bobheadxi commented Aug 9, 2018

Closes #35

👷 Changes

navbardemo-2

💭 Notes

Targeted at #39 for now for a clearer diff, do not merge! Will point to master once that is in.

🔦 Testing Instructions

make web go to /ui_demo

To Do

  • Update tests 😢

@bobheadxi bobheadxi changed the base branch from web/flexbox-utils to master August 9, 2018 19:45
Copy link

@sherryyx sherryyx left a comment

Choose a reason for hiding this comment

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

The transition from the background-fill state to the transparent background state looks rlly good!

After looking at it some more, I think we should just make the background fill 100% opaque. The transparency doesn't really add anything & makes it kind of distracting to read the nav bar. I've updated it in the figma document just now.

Other than that, it looks good to go 💯

Thanks!

@bobheadxi bobheadxi added status: finalized and removed status: DO NOT MERGE!!1! just for @mingyokim ❤️ labels Aug 11, 2018
@bobheadxi bobheadxi requested review from chamkank and mingyokim and removed request for chamkank and mingyokim August 11, 2018 00:49
Copy link
Contributor

@mingyokim mingyokim left a comment

Choose a reason for hiding this comment

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

found a bug, will review the rest later

<nav>
<div>
<div>{getLogo()}</div>
class Navbar extends React.Component {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you need to add a componentShouldUpdate lifecycle method to update the state when new props are passed into this component.

Right now the component is not updating based on incoming props. Reproduction steps:

  • Make sure you're logged out
  • Go to /
  • Refresh and it should have logo, links, and login button
  • Click "login" button
  • The navbar still has logo, links, and login button
  • Refresh
  • The navbar is updated to just have logo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't repro anymore after f66d3b0 - let me know if it works when you get the chance!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I can't reproduce it anymore. But wouldn't it be better to have displayType and buttonType in the state? The only reason why this component re-renders properly is because its container is re-rendering itself. If for some reason the container does not re-render itself, but the props change, then this component wouldn't re-render.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, I’ll have a closer look at this tonight

Copy link
Contributor Author

@bobheadxi bobheadxi Aug 12, 2018

Choose a reason for hiding this comment

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

@mingyokim sorry, I'm not sure I understand - doesn't a prop change force the container to re-render? From what I understand, displayType and buttonType are only manipulated from here, and the navbar itself does not change them:

https://github.com/nwhacks/nwhacks2019/blob/master/web/containers/navbar/index.js#L57

It also seems like setting props that a component doesn't manipulate into state is somewhat bad practice (https://stackoverflow.com/a/47341539), but there also seems to be some other APIs for doing this kind of stuff (https://stackoverflow.com/a/49839457, https://stackoverflow.com/a/32414771)... though to be honest this seems to be kind of murky territory, since from what I understand, this type of stuff should just be kept in props (https://stackoverflow.com/a/40330195). I could be missing something though...

Copy link
Contributor Author

@bobheadxi bobheadxi Aug 13, 2018

Choose a reason for hiding this comment

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

Officially, react only distinguishes between class and function (potentially stateful and stateless respectively), and:

The above two components are equivalent from React’s point of view.

https://reactjs.org/docs/components-and-props.html#functional-and-class-components

Copy link
Contributor

Choose a reason for hiding this comment

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

so props do cause a render on stateful components? oops i was mistaken, it's all good then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar question: https://www.reddit.com/r/reactjs/comments/7tn79g/state_and_componentwillreceiveprops/

class Foo extends Component {
  state = { value: this.props.value }

  // If the props change, update state
  componentWillReceiveProps (nextProps) {
    if (nextProps.value !== this.props.value) {
      this.setState({ value: nextProps.value })
    }  
  } 

  render () {
    // render based on state.value
  }
}

Top response:

Why you don't render props directly?

Copy link
Contributor

Choose a reason for hiding this comment

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

An update can be caused by changes to props or state.

https://reactjs.org/docs/react-component.html

Okay, so clearly change in props cause a re-render, reactjs says so. My bad lol

If this is the case, definitely use props rather than state because the component is not modifying the buttonType or displayType, they're just displaying it.

Approving

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll just do one last sanity check

@bobheadxi bobheadxi force-pushed the web/navbar-hide branch 4 times, most recently from a13c8e1 to ca0bf1a Compare August 11, 2018 20:25
Copy link
Contributor

@mingyokim mingyokim left a comment

Choose a reason for hiding this comment

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

minor comments - looks good!

@@ -1,43 +1,21 @@
nav
display: flex
z-index: 500
Copy link
Contributor

Choose a reason for hiding this comment

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

curious, why is this needed?

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 keeps the navbar above all other elements, so long as we avoid using z-index elsewhere

img
margin-top: 3px
Copy link
Contributor

Choose a reason for hiding this comment

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

spaces should always be multiples of 4 - can you change this to 4px? If the design said 3px that should be an error in design. @sherryyx can you confirm?

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 is a hack because the logo looked slightly off :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

4px looks fine too though - I'll make this change

<nav>
<div>
<div>{getLogo()}</div>
class Navbar extends React.Component {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I can't reproduce it anymore. But wouldn't it be better to have displayType and buttonType in the state? The only reason why this component re-renders properly is because its container is re-rendering itself. If for some reason the container does not re-render itself, but the props change, then this component wouldn't re-render.


// Calculate position
const offset = window.pageYOffset || document.documentElement.scrollTop;
const atTop = offset < 96;
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 store 96 into some kind of variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure - 96 is in the CSS as navbar height, but I’ll replicate it here

const atTop = offset < 96;

// Calculate transparency
const transparent = offset < 256;
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

const { hidden, transparent } = this.state;
const { displayType, buttonType } = this.props;
const button = getButton(buttonType);
const links = [
Copy link
Contributor

Choose a reason for hiding this comment

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

cleaann but can we name this something like linksAndButton? it doesn't just have links

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True!


// repeating horizontal elements
&-horizontal
&-divs
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

Copy link
Contributor

@mingyokim mingyokim 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, and thanks for correcting my misunderstanding on component rendering!

@bobheadxi bobheadxi merged commit 9f3835f into master Aug 13, 2018
@bobheadxi bobheadxi deleted the web/navbar-hide branch August 13, 2018 01:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants