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

Fix: Prevent ScrollToTop from interrupting initial load scrolling #6519

Closed
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
33 changes: 12 additions & 21 deletions packages/volto/src/helpers/ScrollToTop/ScrollToTop.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,42 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import config from '@plone/volto/registry';

/**
*
*
* @class ScrollToTop
* @extends {Component}
*/
class ScrollToTop extends React.Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
location: PropTypes.shape({
pathname: PropTypes.string,
}),
children: PropTypes.node.isRequired,
};

/**
* @param {*} prevProps Previous Props
* @returns {null} Null
* @memberof ScrollToTop
*/
constructor(props) {
super(props);
this.isInitialLoad = true; // Add a flag for the initial load
}

componentDidUpdate(prevProps) {
const { location } = this.props;
const noInitialBlocksFocus = // Do not scroll on /edit
const noInitialBlocksFocus =
config.blocks?.initialBlocksFocus === null
? this.props.location?.pathname.slice(-5) !== '/edit'
: true;

const isHash = location?.hash || location?.pathname.hash;

// Scroll only if it's not the initial load and other conditions are met
if (
!this.isInitialLoad && // Prevent scrolling on the initial load
!isHash &&
noInitialBlocksFocus &&
location?.pathname !== prevProps.location?.pathname
) {
window.scrollTo(0, 0);
}

// Reset the initial load flag after the first render
this.isInitialLoad = false;
}

/**
* @returns {node} Children
* @memberof ScrollToTop
*/
render() {
return this.props.children;
}
Expand Down