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

[docs] Use stable context API #13477

Merged
merged 3 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 23 additions & 23 deletions docs/src/modules/components/AppDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Hidden from '@material-ui/core/Hidden';
import AppDrawerNavItem from 'docs/src/modules/components/AppDrawerNavItem';
import Link from 'docs/src/modules/components/Link';
import { pageToTitle } from 'docs/src/modules/utils/helpers';
import PageContext from 'docs/src/modules/components/PageContext';

const styles = theme => ({
paper: {
Expand Down Expand Up @@ -92,28 +93,32 @@ function reduceChildRoutes({ props, activePage, items, page, depth }) {
// So: <SwipeableDrawer disableBackdropTransition={false} />
const iOS = process.browser && /iPad|iPhone|iPod/.test(navigator.userAgent);

function AppDrawer(props, context) {
function AppDrawer(props) {
const { classes, className, disablePermanent, mobileOpen, onClose, onOpen } = props;

const drawer = (
<div className={classes.nav}>
<div className={classes.toolbarIe11}>
<div className={classes.toolbar}>
<Link className={classes.title} href="/" onClick={onClose}>
<Typography variant="h6" color="inherit">
Material-UI
</Typography>
</Link>
{process.env.LIB_VERSION ? (
<Link className={classes.anchor} href={_rewriteUrlForNextExport('/versions')}>
<Typography variant="caption">{`v${process.env.LIB_VERSION}`}</Typography>
</Link>
) : null}
<PageContext.Consumer>
{({ activePage, pages }) => (
<div className={classes.nav}>
<div className={classes.toolbarIe11}>
<div className={classes.toolbar}>
<Link className={classes.title} href="/" onClick={onClose}>
<Typography variant="h6" color="inherit">
Material-UI
</Typography>
</Link>
{process.env.LIB_VERSION ? (
<Link className={classes.anchor} href={_rewriteUrlForNextExport('/versions')}>
<Typography variant="caption">{`v${process.env.LIB_VERSION}`}</Typography>
</Link>
) : null}
</div>
</div>
<Divider />
{renderNavItems({ props, pages, activePage, depth: 0 })}
</div>
</div>
<Divider />
{renderNavItems({ props, pages: context.pages, activePage: context.activePage, depth: 0 })}
</div>
)}
</PageContext.Consumer>
);

return (
Expand Down Expand Up @@ -161,9 +166,4 @@ AppDrawer.propTypes = {
onOpen: PropTypes.func.isRequired,
};

AppDrawer.contextTypes = {
activePage: PropTypes.object.isRequired,
pages: PropTypes.array.isRequired,
};

export default withStyles(styles)(AppDrawer);
199 changes: 98 additions & 101 deletions docs/src/modules/components/AppFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Link from 'docs/src/modules/components/Link';
import AppDrawer from 'docs/src/modules/components/AppDrawer';
import AppSearch from 'docs/src/modules/components/AppSearch';
import Notifications from 'docs/src/modules/components/Notifications';
import { pageToTitle } from 'docs/src/modules/utils/helpers';
import PageTitle from 'docs/src/modules/components/PageTitle';
import actionTypes from 'docs/src/modules/redux/actionTypes';

Router.onRouteChangeStart = () => {
Expand Down Expand Up @@ -109,101 +109,104 @@ class AppFrame extends React.Component {
render() {
const { children, classes, uiTheme } = this.props;

if (!this.context.activePage) {
throw new Error('Missing activePage.');
}

const title =
this.context.activePage.title !== false ? pageToTitle(this.context.activePage) : null;

let disablePermanent = false;
let navIconClassName = '';
let appBarClassName = classes.appBar;

if (title === null) {
// home route, don't shift app bar or dock drawer
disablePermanent = true;
appBarClassName += ` ${classes.appBarHome}`;
} else {
navIconClassName = classes.navIconHide;
appBarClassName += ` ${classes.appBarShift}`;
}

return (
<div className={classes.root}>
<NProgressBar />
<CssBaseline />
<AppBar className={appBarClassName}>
<Toolbar>
<IconButton
color="inherit"
aria-label="Open drawer"
onClick={this.handleDrawerOpen}
className={navIconClassName}
>
<MenuIcon />
</IconButton>
{title !== null && (
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
{title}
</Typography>
)}
<div className={classes.grow} />
<AppSearch />
<Tooltip title="Edit docs colors" enterDelay={300}>
<IconButton
color="inherit"
aria-label="Edit docs colors"
component={Link}
href="/style/color/#color-tool"
>
<ColorsIcon />
</IconButton>
</Tooltip>
<Tooltip title="Toggle light/dark theme" enterDelay={300}>
<IconButton
color="inherit"
onClick={this.handleTogglePaletteType}
aria-label="Toggle light/dark theme"
>
{uiTheme.paletteType === 'light' ? <LightbulbOutlineIcon /> : <LightbulbFullIcon />}
</IconButton>
</Tooltip>
<Tooltip title="Toggle right-to-left/left-to-right" enterDelay={300}>
<IconButton
color="inherit"
onClick={this.handleToggleDirection}
aria-label="Toggle right-to-left/left-to-right"
>
{uiTheme.direction === 'rtl' ? (
<FormatTextdirectionLToR />
) : (
<FormatTextdirectionRToL />
)}
</IconButton>
</Tooltip>
<Tooltip title="GitHub repository" enterDelay={300}>
<IconButton
component="a"
color="inherit"
href="https://github.com/mui-org/material-ui"
aria-label="GitHub repository"
>
<GithubIcon />
</IconButton>
</Tooltip>
</Toolbar>
</AppBar>
<Notifications />
<AppDrawer
className={classes.drawer}
disablePermanent={disablePermanent}
onClose={this.handleDrawerClose}
onOpen={this.handleDrawerOpen}
mobileOpen={this.state.mobileOpen}
/>
{children}
</div>
<PageTitle>
{title => {
let disablePermanent = false;
let navIconClassName = '';
let appBarClassName = classes.appBar;

if (title === null) {
// home route, don't shift app bar or dock drawer
disablePermanent = true;
appBarClassName += ` ${classes.appBarHome}`;
} else {
navIconClassName = classes.navIconHide;
appBarClassName += ` ${classes.appBarShift}`;
}

return (
<div className={classes.root}>
<NProgressBar />
<CssBaseline />
<AppBar className={appBarClassName}>
<Toolbar>
<IconButton
color="inherit"
aria-label="Open drawer"
onClick={this.handleDrawerOpen}
className={navIconClassName}
>
<MenuIcon />
</IconButton>
{title !== null && (
<Typography className={classes.title} variant="h6" color="inherit" noWrap>
{title}
</Typography>
)}
<div className={classes.grow} />
<AppSearch />
<Tooltip title="Edit docs colors" enterDelay={300}>
<IconButton
color="inherit"
aria-label="Edit docs colors"
component={Link}
href="/style/color/#color-tool"
>
<ColorsIcon />
</IconButton>
</Tooltip>
<Tooltip title="Toggle light/dark theme" enterDelay={300}>
<IconButton
color="inherit"
onClick={this.handleTogglePaletteType}
aria-label="Toggle light/dark theme"
>
{uiTheme.paletteType === 'light' ? (
<LightbulbOutlineIcon />
) : (
<LightbulbFullIcon />
)}
</IconButton>
</Tooltip>
<Tooltip title="Toggle right-to-left/left-to-right" enterDelay={300}>
<IconButton
color="inherit"
onClick={this.handleToggleDirection}
aria-label="Toggle right-to-left/left-to-right"
>
{uiTheme.direction === 'rtl' ? (
<FormatTextdirectionLToR />
) : (
<FormatTextdirectionRToL />
)}
</IconButton>
</Tooltip>
<Tooltip title="GitHub repository" enterDelay={300}>
<IconButton
component="a"
color="inherit"
href="https://github.com/mui-org/material-ui"
aria-label="GitHub repository"
>
<GithubIcon />
</IconButton>
</Tooltip>
</Toolbar>
</AppBar>
<Notifications />
<AppDrawer
className={classes.drawer}
disablePermanent={disablePermanent}
onClose={this.handleDrawerClose}
onOpen={this.handleDrawerOpen}
mobileOpen={this.state.mobileOpen}
/>
{children}
</div>
);
}}
</PageTitle>
);
}
}
Expand All @@ -215,12 +218,6 @@ AppFrame.propTypes = {
uiTheme: PropTypes.object.isRequired,
};

AppFrame.contextTypes = {
activePage: PropTypes.shape({
pathname: PropTypes.string.isRequired,
}).isRequired,
};

export default compose(
connect(state => ({
uiTheme: state.theme,
Expand Down
35 changes: 35 additions & 0 deletions docs/src/modules/components/EditPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react';
import PropTypes from 'prop-types';
import Button from '@material-ui/core/Button';
import PageContext from 'docs/src/modules/components/PageContext';

function EditPage(props) {
const { markdownLocation, sourceCodeRootUrl } = props;

return (
<PageContext.Consumer>
{({ userLanguage }) => {
if (userLanguage === 'zh') {
return (
<Button component="a" href="https://translate.material-ui.com/project/material-ui-docs">
{'将此页面翻译成中文'}
</Button>
);
}

return (
<Button component="a" href={`${sourceCodeRootUrl}${markdownLocation}`}>
{'Edit this page'}
</Button>
);
}}
</PageContext.Consumer>
);
}

EditPage.propTypes = {
markdownLocation: PropTypes.string.isRequired,
sourceCodeRootUrl: PropTypes.string.isRequired,
};

export default EditPage;
Copy link
Member Author

Choose a reason for hiding this comment

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

"Fun" fact: If you don't export anything eslint will not warn you, the runtime will not throw because read of undefined. React will just throw with "expected component but got object".