Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ interface ScrollAndFocusHandlerProps {
}
class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerProps> {
handlePotentialScroll = () => {
// Handle scroll and focus, it's only applied once in the first useEffect that triggers that changed.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

componentDidMount and componentDidUpdate run sync after commit and are much closer to useLayoutEffect than useEffect

// Handle scroll and focus, it's only applied once.
const { focusAndScrollRef, segmentPath } = this.props

if (focusAndScrollRef.apply) {
Expand Down Expand Up @@ -192,7 +192,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
() => {
// In case of hash scroll, we only need to scroll the element into view
if (hashFragment) {
;(domNode as HTMLElement).scrollIntoView()
domNode.scrollIntoView()

return
}
Expand All @@ -202,7 +202,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
const viewportHeight = htmlElement.clientHeight

// If the element's top edge is already in the viewport, exit early.
if (topOfElementInViewport(domNode as HTMLElement, viewportHeight)) {
if (topOfElementInViewport(domNode, viewportHeight)) {
return
}

Expand All @@ -213,9 +213,9 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
htmlElement.scrollTop = 0

// Scroll to domNode if domNode is not in viewport when scrolled to top of document
if (!topOfElementInViewport(domNode as HTMLElement, viewportHeight)) {
if (!topOfElementInViewport(domNode, viewportHeight)) {
// Scroll into view doesn't scroll horizontally by default when not needed
;(domNode as HTMLElement).scrollIntoView()
domNode.scrollIntoView()
}
},
{
Expand All @@ -238,10 +238,7 @@ class InnerScrollAndFocusHandler extends React.Component<ScrollAndFocusHandlerPr
}

componentDidUpdate() {
// Because this property is overwritten in handlePotentialScroll it's fine to always run it when true as it'll be set to false for subsequent renders.
if (this.props.focusAndScrollRef.apply) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We already check this within handlePotentialScroll. New factoring makes it clearer that we can convert this to useLayoutEffect

this.handlePotentialScroll()
}
this.handlePotentialScroll()
}

render() {
Expand Down
Loading