Skip to content

Commit

Permalink
Fix scroll to top in single column UI (mastodon#11463)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored and hiyuki2578 committed Oct 2, 2019
1 parent 50cab53 commit 11abcc9
Show file tree
Hide file tree
Showing 26 changed files with 55 additions and 30 deletions.
15 changes: 12 additions & 3 deletions app/javascript/mastodon/components/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export default class Column extends React.PureComponent {
static propTypes = {
children: PropTypes.node,
label: PropTypes.string,
bindToDocument: PropTypes.bool,
};

scrollTop () {
const scrollable = this.node.querySelector('.scrollable');
const scrollable = this.props.bindToDocument ? document.scrollingElement : this.node.querySelector('.scrollable');

if (!scrollable) {
return;
Expand All @@ -33,11 +34,19 @@ export default class Column extends React.PureComponent {
}

componentDidMount () {
this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
if (this.props.bindToDocument) {
document.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
} else {
this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
}
}

componentWillUnmount () {
this.node.removeEventListener('wheel', this.handleWheel);
if (this.props.bindToDocument) {
document.removeEventListener('wheel', this.handleWheel);
} else {
this.node.removeEventListener('wheel', this.handleWheel);
}
}

render () {
Expand Down
15 changes: 14 additions & 1 deletion app/javascript/mastodon/components/column_back_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import React from 'react';
import { FormattedMessage } from 'react-intl';
import PropTypes from 'prop-types';
import Icon from 'mastodon/components/icon';
import { createPortal } from 'react-dom';

export default class ColumnBackButton extends React.PureComponent {

static contextTypes = {
router: PropTypes.object,
};

static propTypes = {
multiColumn: PropTypes.bool,
};

handleClick = () => {
if (window.history && window.history.length === 1) {
this.context.router.history.push('/');
Expand All @@ -18,12 +23,20 @@ export default class ColumnBackButton extends React.PureComponent {
}

render () {
return (
const { multiColumn } = this.props;

const component = (
<button onClick={this.handleClick} className='column-back-button'>
<Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
<FormattedMessage id='column_back_button.label' defaultMessage='Back' />
</button>
);

if (multiColumn) {
return component;
} else {
return createPortal(component, document.getElementById('tabs-bar__portal'));
}
}

}
5 changes: 3 additions & 2 deletions app/javascript/mastodon/features/account_gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class AccountGallery extends ImmutablePureComponent {
isLoading: PropTypes.bool,
hasMore: PropTypes.bool,
isAccount: PropTypes.bool,
multiColumn: PropTypes.bool,
};

state = {
Expand Down Expand Up @@ -116,7 +117,7 @@ class AccountGallery extends ImmutablePureComponent {
}

render () {
const { attachments, shouldUpdateScroll, isLoading, hasMore, isAccount } = this.props;
const { attachments, shouldUpdateScroll, isLoading, hasMore, isAccount, multiColumn } = this.props;
const { width } = this.state;

if (!isAccount) {
Expand All @@ -143,7 +144,7 @@ class AccountGallery extends ImmutablePureComponent {

return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />

<ScrollContainer scrollKey='account_gallery' shouldUpdateScroll={shouldUpdateScroll}>
<div className='scrollable scrollable--flex' onScroll={this.handleScroll}>
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/account_timeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AccountTimeline extends ImmutablePureComponent {

return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />

<StatusList
prepend={<HeaderContainer accountId={this.props.params.accountId} />}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Blocks extends ImmutablePureComponent {
const emptyMessage = <FormattedMessage id='empty_column.blocks' defaultMessage="You haven't blocked any users yet." />;

return (
<Column icon='ban' heading={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} icon='ban' heading={intl.formatMessage(messages.heading)}>
<ColumnBackButtonSlim />
<ScrollableList
scrollKey='blocks'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class CommunityTimeline extends React.PureComponent {
const pinned = !!columnId;

return (
<Column ref={this.setRef} label={intl.formatMessage(messages.title)}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
<ColumnHeader
icon='users'
active={hasUnread}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/direct_timeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class DirectTimeline extends React.PureComponent {
const pinned = !!columnId;

return (
<Column ref={this.setRef} label={intl.formatMessage(messages.title)}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
<ColumnHeader
icon='envelope'
active={hasUnread}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/domain_blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Blocks extends ImmutablePureComponent {
const emptyMessage = <FormattedMessage id='empty_column.domain_blocks' defaultMessage='There are no hidden domains yet.' />;

return (
<Column icon='minus-circle' heading={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} icon='minus-circle' heading={intl.formatMessage(messages.heading)}>
<ColumnBackButtonSlim />
<ScrollableList
scrollKey='domain_blocks'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Favourites extends ImmutablePureComponent {
const emptyMessage = <FormattedMessage id='empty_column.favourited_statuses' defaultMessage="You don't have any favourite toots yet. When you favourite one, it will show up here." />;

return (
<Column ref={this.setRef} label={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.heading)}>
<ColumnHeader
icon='star'
title={intl.formatMessage(messages.heading)}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/favourites/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Favourites extends ImmutablePureComponent {

return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />

<ScrollableList
scrollKey='favourites'
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/follow_requests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FollowRequests extends ImmutablePureComponent {
const emptyMessage = <FormattedMessage id='empty_column.follow_requests' defaultMessage="You don't have any follow requests yet. When you receive one, it will show up here." />;

return (
<Column icon='user-plus' heading={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} icon='user-plus' heading={intl.formatMessage(messages.heading)}>
<ColumnBackButtonSlim />
<ScrollableList
scrollKey='follow_requests'
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/followers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Followers extends ImmutablePureComponent {

return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />

<ScrollableList
scrollKey='followers'
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/following/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Following extends ImmutablePureComponent {

return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />

<ScrollableList
scrollKey='following'
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/getting_started/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class GettingStarted extends ImmutablePureComponent {
}

return (
<Column label={intl.formatMessage(messages.menu)}>
<Column bindToDocument={!multiColumn} label={intl.formatMessage(messages.menu)}>
{multiColumn && <div className='column-header__wrapper'>
<h1 className='column-header'>
<button>
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/hashtag_timeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class HashtagTimeline extends React.PureComponent {
const pinned = !!columnId;

return (
<Column ref={this.setRef} label={`#${id}`}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={`#${id}`}>
<ColumnHeader
icon='hashtag'
active={hasUnread}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/home_timeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class HomeTimeline extends React.PureComponent {
const pinned = !!columnId;

return (
<Column ref={this.setRef} label={intl.formatMessage(messages.title)}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
<ColumnHeader
icon='home'
active={hasUnread}
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/mastodon/features/keyboard_shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class KeyboardShortcuts extends ImmutablePureComponent {
};

render () {
const { intl } = this.props;
const { intl, multiColumn } = this.props;

return (
<Column icon='question' heading={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} icon='question' heading={intl.formatMessage(messages.heading)}>
<ColumnBackButtonSlim />
<div className='keyboard-shortcuts scrollable optionally-scrollable'>
<table>
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/mastodon/features/list_timeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ class ListTimeline extends React.PureComponent {
} else if (list === false) {
return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />
<MissingIndicator />
</Column>
);
}

return (
<Column ref={this.setRef} label={title}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={title}>
<ColumnHeader
icon='list-ul'
active={hasUnread}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/lists/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Lists extends ImmutablePureComponent {
const emptyMessage = <FormattedMessage id='empty_column.lists' defaultMessage="You don't have any lists yet. When you create one, it will show up here." />;

return (
<Column icon='list-ul' heading={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} icon='list-ul' heading={intl.formatMessage(messages.heading)}>
<ColumnBackButtonSlim />

<NewListForm />
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/mutes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Mutes extends ImmutablePureComponent {
const emptyMessage = <FormattedMessage id='empty_column.mutes' defaultMessage="You haven't muted any users yet." />;

return (
<Column icon='volume-off' heading={intl.formatMessage(messages.heading)}>
<Column bindToDocument={!multiColumn} icon='volume-off' heading={intl.formatMessage(messages.heading)}>
<ColumnBackButtonSlim />
<ScrollableList
scrollKey='mutes'
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Notifications extends React.PureComponent {
);

return (
<Column ref={this.setColumnRef} label={intl.formatMessage(messages.title)}>
<Column bindToDocument={!multiColumn} ref={this.setColumnRef} label={intl.formatMessage(messages.title)}>
<ColumnHeader
icon='bell'
active={isUnread}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/pinned_statuses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PinnedStatuses extends ImmutablePureComponent {
const { intl, shouldUpdateScroll, statusIds, hasMore, multiColumn } = this.props;

return (
<Column icon='thumb-tack' heading={intl.formatMessage(messages.heading)} ref={this.setRef}>
<Column bindToDocument={!multiColumn} icon='thumb-tack' heading={intl.formatMessage(messages.heading)} ref={this.setRef}>
<ColumnBackButtonSlim />
<StatusList
statusIds={statusIds}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/public_timeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class PublicTimeline extends React.PureComponent {
const pinned = !!columnId;

return (
<Column ref={this.setRef} label={intl.formatMessage(messages.title)}>
<Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
<ColumnHeader
icon='globe'
active={hasUnread}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/reblogs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Reblogs extends ImmutablePureComponent {

return (
<Column>
<ColumnBackButton />
<ColumnBackButton multiColumn={multiColumn} />

<ScrollableList
scrollKey='reblogs'
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/features/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class Status extends ImmutablePureComponent {
};

return (
<Column label={intl.formatMessage(messages.detailedStatus)}>
<Column bindToDocument={!multiColumn} label={intl.formatMessage(messages.detailedStatus)}>
<ColumnHeader
showBackButton
multiColumn={multiColumn}
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/styles/mastodon/components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2411,6 +2411,8 @@ a.account__display-name {
}

.column-back-button {
box-sizing: border-box;
width: 100%;
background: lighten($ui-base-color, 4%);
color: $highlight-text-color;
cursor: pointer;
Expand Down

0 comments on commit 11abcc9

Please sign in to comment.