Skip to content

Commit

Permalink
[default-layout] Improve typings of NavbarContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent d6d91c8 commit 67c5525
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions packages/@sanity/default-layout/src/navbar/NavbarContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ interface State {
showToolMenuMinWidth: number
}

/* eslint-disable complexity */
/* eslint-disable max-depth */
/* eslint-disable no-lonely-if */
function getNextState(
state: {
showLabel: boolean
showLabelMinWidth: number
showToolMenu: boolean
showToolMenuMinWidth: number
},
mostRight: {},
winWidth: number
) {
function getNextState(state: State, mostRight: number, winWidth: number): NextState {
const {showLabel, showLabelMinWidth, showToolMenu, showToolMenuMinWidth} = state
const mostRightIsVisible = mostRight && mostRight <= winWidth
const nextState: NextState = {winWidth}
Expand All @@ -64,20 +52,19 @@ function getNextState(
} else if (showToolMenuMinWidth < winWidth) {
nextState.showToolMenu = true
}
} else {
// most-right element is NOT within viewport
if (showLabel) {
nextState.showLabel = false
} else if (showToolMenu) {
nextState.showToolMenu = false
}

return nextState
}

// most-right element is NOT within viewport
if (showLabel) {
nextState.showLabel = false
} else if (showToolMenu) {
nextState.showToolMenu = false
}

return nextState
}
/* eslint-enable complexity */
/* eslint-enable max-depth */
/* eslint-enable no-lonely-if */

class NavbarContainer extends React.PureComponent<Props, State> {
state = {
Expand All @@ -100,7 +87,6 @@ class NavbarContainer extends React.PureComponent<Props, State> {
this.tick()
}

/* eslint-disable complexity */
componentDidUpdate(prevProps: Props, prevState: State) {
const {showLabel, showLabelMinWidth, showToolMenu, showToolMenuMinWidth} = this.state
const didShowLabel = showLabelMinWidth === -1 && !prevState.showLabel && showLabel
Expand All @@ -111,7 +97,6 @@ class NavbarContainer extends React.PureComponent<Props, State> {
this.handleCustomResize(window.innerWidth)
}
}
/* eslint-enable complexity */

componentWillUnmount() {
if (this.io) {
Expand Down Expand Up @@ -143,10 +128,11 @@ class NavbarContainer extends React.PureComponent<Props, State> {
const mostRightRect = showToolMenu
? this.loginStatusElement.getBoundingClientRect()
: this.searchElement.getBoundingClientRect()
this.setState(state => {
const nextState = getNextState(state, mostRightRect.left + mostRightRect.width, winWidth)
return nextState as State
})

this.setState(
prevState =>
getNextState(prevState, mostRightRect.left + mostRightRect.width, winWidth) as State
)
}
}

Expand Down

0 comments on commit 67c5525

Please sign in to comment.