Make tabs be their own panels

This commit is contained in:
Travis Ralston 2019-01-18 15:07:11 -07:00
parent 5adfc09237
commit 15709040e7
5 changed files with 46 additions and 126 deletions

View file

@ -1,5 +1,6 @@
/*
Copyright 2017 Travis Ralston
Copyright 2019 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -20,40 +21,22 @@ limitations under the License.
display: flex;
width: 100%;
height: 100%;
background-color: $tab-panel-bg-color;
}
.mx_TabbedView_tabLabels {
width: 300px;
width: 136px;
height: 100%;
background-color: $tab-list-bg-color;
color: $tab-list-fg-color;
border-right: 1px solid $tab-border-color;
border-left: 1px solid $tab-border-color;
}
.mx_TabbedView_tabPanels {
width: calc(100% - 320px);
display: inline-block;
height: 100%;
padding-left: 20px;
scroll-snap-type: block;
color: $tab-label-fg-color;
}
.mx_TabbedView_tabLabel {
text-align: center;
vertical-align: middle;
text-transform: uppercase;
cursor: pointer;
display: block;
padding: 20px;
width: calc(100% - 40px);
border-bottom: 1px solid $tab-border-color;
}
.mx_TabbedView_exit {
padding-top: 10px;
padding-bottom: 10px;
border-radius: 3px;
font-size: 12px;
font-weight: 600;
height: 20px;
}
.mx_TabbedView_tabLabel:hover {
@ -61,16 +44,17 @@ limitations under the License.
}
.mx_TabbedView_tabLabel_active {
font-weight: 700;
background-color: $tab-list-active-bg-color;
color: $tab-list-active-fg-color;
background-color: $tab-label-active-bg-color;
color: $tab-label-active-fg-color;
}
.mx_TabbedView_tabPanel {
width: calc(100% - 320px);
display: inline-block;
height: 100vh; // 100% of viewport height
scroll-snap-align: start;
margin-left: 20px;
}
.mx_TabbedView_tabPanelContent {
width: 600px;
flex-grow: 1;
}

View file

@ -187,12 +187,10 @@ $lightbox-fg-color: #ffffff;
$lightbox-border-color: #ffffff;
// Tabbed views
$tab-list-bg-color: $secondary-accent-color;
$tab-list-fg-color: $accent-color;
$tab-list-active-bg-color: $tertiary-accent-color;
$tab-list-active-fg-color: $accent-color;
$tab-border-color: $tertiary-accent-color;
$tab-panel-bg-color: #fff;
$tab-label-fg-color: #45474a;
$tab-label-active-fg-color: #ffffff;
$tab-label-bg-color: transparent;
$tab-label-active-bg-color: #7ac9a1;
// unused?
$progressbar-color: #000;

View file

@ -183,12 +183,10 @@ $imagebody-giflabel-border: rgba(0, 0, 0, 0.2);
$imagebody-giflabel-color: rgba(255, 255, 255, 1);
// Tabbed views
$tab-list-bg-color: $secondary-accent-color;
$tab-list-fg-color: $accent-color;
$tab-list-active-bg-color: $tertiary-accent-color;
$tab-list-active-fg-color: $accent-color;
$tab-border-color: $tertiary-accent-color;
$tab-panel-bg-color: #fff;
$tab-label-fg-color: #45474a;
$tab-label-active-fg-color: #ffffff;
$tab-label-bg-color: transparent;
$tab-label-active-bg-color: #7ac9a1;
// unused?
$progressbar-color: #000;

View file

@ -16,45 +16,42 @@ limitations under the License.
*/
import * as React from "react";
import {_t, _td} from '../../languageHandler';
import GeminiScrollbar from 'react-gemini-scrollbar';
import {_t} from '../../languageHandler';
import PropTypes from "prop-types";
//import scrollSnapPolyfill from 'css-scroll-snap-polyfill';
const DEFAULT_EXIT_STRING = _td("Return to app");
/**
* Represents a tab for the TabbedView
* Represents a tab for the TabbedView.
*/
export class Tab {
/**
* Creates a new tab
* @param {string} tabLabel The untranslated tab label
* Creates a new tab.
* @param {string} tabLabel The untranslated tab label.
* @param {string} tabIconRef The relative path to the tab's icon.
* @param {string} tabJsx The JSX for the tab container.
*/
constructor(tabLabel, tabJsx) {
constructor(tabLabel, tabIconRef, tabJsx) {
this.label = tabLabel;
this.body = tabJsx;
}
}
export class TabbedView extends React.Component {
static propTypes = {
// The tabs to show
tabs: PropTypes.arrayOf(Tab).isRequired,
};
constructor() {
super();
// This is used to track when the user has scrolled all the way up or down so we
// don't immediately start flipping between tabs.
this._reachedEndAt = 0;
}
getInitialState() {
return {
this.state = {
activeTabIndex: 0,
};
}
_getActiveTabIndex() {
return this.state ? this.state.activeTabIndex : 0;
if (!this.state || !this.state.activeTabIndex) return 0;
return this.state.activeTabIndex;
}
/**
@ -66,28 +63,12 @@ export class TabbedView extends React.Component {
const idx = this.props.tabs.indexOf(tab);
if (idx !== -1) {
this.setState({activeTabIndex: idx});
this._reachedEndAt = 0; // reset scroll timer
}
else console.error("Could not find tab " + tab.label + " in tabs");
}
_nextTab() {
let targetIndex = this._getActiveTabIndex() + 1;
if (targetIndex < this.props.tabs.length) {
this.setState({activeTabIndex: targetIndex});
this._reachedEndAt = 0; // reset scroll timer
} else {
console.error("Could not find tab " + tab.label + " in tabs");
}
}
_previousTab() {
let targetIndex = this._getActiveTabIndex() - 1;
if (targetIndex >= 0) {
this.setState({activeTabIndex: targetIndex});
this._reachedEndAt = 0; // reset scroll timer
}
}
_getTabLabel(tab) {
_renderTabLabel(tab) {
let classes = "mx_TabbedView_tabLabel ";
const idx = this.props.tabs.indexOf(tab);
@ -101,7 +82,7 @@ export class TabbedView extends React.Component {
);
}
_getTabPanel(tab) {
_renderTabPanel(tab) {
return (
<div className="mx_TabbedView_tabPanel" key={"mx_tabpanel_" + tab.label}>
{tab.body}
@ -109,57 +90,17 @@ export class TabbedView extends React.Component {
);
}
componentDidUpdate() {
window.requestAnimationFrame(() => {
console.log("SCROLL SNAP POLYFILL: UPDATE");
//scrollSnapPolyfill();
});
}
componentDidMount() {
window.requestAnimationFrame(() => {
console.log("SCROLL SNAP POLYFILL: MOUNT");
//scrollSnapPolyfill();
});
}
render() {
const labels = [];
const tabs = [];
for (const tab of this.props.tabs) {
labels.push(this._getTabLabel(tab));
tabs.push(this._getTabPanel(tab));
}
const returnToApp = (
<span className="mx_TabbedView_tabLabel mx_TabbedView_exit" onClick={this.props.onExit}>
{_t(this.props.exitLabel || DEFAULT_EXIT_STRING)}
</span>
);
const labels = this.props.tabs.map(tab => this._renderTabLabel(tab));
const panel = this._renderTabPanel(this.props.tabs[this._getActiveTabIndex()]);
return (
<div className="mx_TabbedView">
<div className="mx_TabbedView_tabLabels">
{returnToApp}
{labels}
</div>
<div className="mx_TabbedView_tabPanels">
{tabs}
</div>
{panel}
</div>
);
}
}
TabbedView.PropTypes = {
// Called when the user clicks the "Exit" or "Return to app" button
onExit: PropTypes.func.isRequired,
// The untranslated label for the "Return to app" button.
// Default: "Return to app"
exitLabel: PropTypes.string,
// The tabs to show
tabs: PropTypes.arrayOf(Tab).isRequired,
};
}

View file

@ -16,7 +16,6 @@ limitations under the License.
import React from 'react';
import PropTypes from 'prop-types';
import sdk from '../../../index';
import {Tab, TabbedView} from "../../structures/TabbedView";
import {_td} from "../../../languageHandler";
@ -27,14 +26,14 @@ export default class UserSettingsDialog extends React.Component {
_getTabs() {
return [
new Tab(_td("General"), <div>General Test</div>),
new Tab(_td("Account"), <div>Account Test</div>),
new Tab(_td("General"), "", <div>General Test</div>),
new Tab(_td("Account"), "", <div>Account Test</div>),
];
}
render() {
return (
<TabbedView onExit={this.props.onFinished} tabs={this._getTabs()} />
<TabbedView tabs={this._getTabs()} />
// <UserSettings
// onClose={this.props.onFinished}
// brand={SdkConfig.get().brand}