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

Dashboard: Prevent scroll to top on pagination #2883

Merged
merged 2 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
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
26 changes: 2 additions & 24 deletions assets/src/dashboard/app/views/myStories/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { __ } from '@wordpress/i18n';
/**
* External dependencies
*/
import { useEffect, useRef } from 'react';
import { memo } from 'react';
import PropTypes from 'prop-types';

/**
Expand All @@ -34,7 +34,6 @@ import {
InfiniteScroller,
Layout,
StandardViewContentGutter,
useLayoutContext,
} from '../../../../components';
import {
UsersPropType,
Expand Down Expand Up @@ -64,27 +63,6 @@ function Content({
view,
dateFormat,
}) {
const {
actions: { scrollToTop },
} = useLayoutContext();

const previousFilter = useRef(filter);
const previousViewStyle = useRef(view.style);

useEffect(() => {
/**
* Ensure we only scroll back to top when the filter or view style change.
*/
if (
previousFilter.current !== filter ||
previousViewStyle.current !== view.style
) {
previousFilter.current = filter;
previousViewStyle.current = view.style;
scrollToTop();
}
}, [filter, scrollToTop, view]);

return (
<Layout.Scrollable>
<FontProvider>
Expand Down Expand Up @@ -136,4 +114,4 @@ Content.propTypes = {
dateFormat: PropTypes.string,
};

export default Content;
export default memo(Content);
29 changes: 23 additions & 6 deletions assets/src/dashboard/app/views/myStories/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import { __ } from '@wordpress/i18n';
/**
* External dependencies
*/
import { useMemo } from 'react';
import { useMemo, memo, useCallback } from 'react';
import PropTypes from 'prop-types';
/**
* Internal dependencies
*/
import { Layout, ToggleButtonGroup } from '../../../../components';
import {
Layout,
ToggleButtonGroup,
useLayoutContext,
} from '../../../../components';
import {
DASHBOARD_VIEWS,
STORY_STATUSES,
Expand Down Expand Up @@ -59,6 +63,10 @@ function Header({
view,
wpListURL,
}) {
const {
actions: { scrollToTop },
} = useLayoutContext();

const resultsLabel = useDashboardResultsLabel({
currentFilter: filter.value,
isActiveSearch: Boolean(search.keyword),
Expand All @@ -79,7 +87,10 @@ function Header({
<ToggleButtonGroup
buttons={STORY_STATUSES.map((storyStatus) => {
return {
handleClick: () => filter.set(storyStatus.value),
handleClick: () => {
filter.set(storyStatus.value);
scrollToTop();
},
key: storyStatus.value,
isActive: filter.value === storyStatus.value,
disabled: totalStoriesByStatus?.[storyStatus.status] <= 0,
Expand All @@ -93,7 +104,7 @@ function Header({
/>
</HeaderToggleButtonContainer>
);
}, [filter, totalStoriesByStatus]);
}, [filter, scrollToTop, totalStoriesByStatus]);

return (
<Layout.Squishable>
Expand All @@ -114,7 +125,13 @@ function Header({
handleLayoutSelect={view.toggleStyle}
currentSort={sort.value}
pageSortOptions={STORY_SORT_MENU_ITEMS}
handleSortChange={sort.set}
handleSortChange={useCallback(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I'd have put this into a variable, e.g. const onHandleSortChange = useCallback(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'll move this in my next pr - thanks!

(newSort) => {
sort.set(newSort);
scrollToTop();
},
[scrollToTop, sort]
)}
wpListURL={wpListURL}
sortDropdownAriaLabel={__(
'Choose sort option for display',
Expand All @@ -135,4 +152,4 @@ Header.propTypes = {
wpListURL: PropTypes.string,
};

export default Header;
export default memo(Header);
5 changes: 4 additions & 1 deletion assets/src/dashboard/app/views/shared/pageHeading.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* External dependencies
*/
import { useMemo } from 'react';
import styled from 'styled-components';
import PropTypes from 'prop-types';

Expand Down Expand Up @@ -105,6 +106,8 @@ const PageHeading = ({
handleTypeaheadChange,
typeaheadValue = '',
}) => {
const typeaheadResults = useMemo(() => stories.slice(0, 5), [stories]);

return (
<Container>
<HeadingBodyWrapper>
Expand All @@ -119,7 +122,7 @@ const PageHeading = ({
<TypeaheadSearch
placeholder={searchPlaceholder}
currentValue={typeaheadValue}
stories={stories}
stories={typeaheadResults}
handleChange={handleTypeaheadChange}
/>
</SearchInner>
Expand Down