Skip to content

Commit

Permalink
[SwipeableDrawer] Fix SSR issue on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jul 28, 2018
1 parent a2c6937 commit becf3d0
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = [
name: 'The size of all the modules of material-ui.',
webpack: true,
path: 'packages/material-ui/build/index.js',
limit: '95.2 KB',
limit: '95.4 KB',
},
{
name: 'The main bundle of the docs',
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/AppDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function AppDrawer(props, context) {

return (
<div className={className}>
<Hidden lgUp={!disablePermanent}>
<Hidden lgUp={!disablePermanent} implementation="js">
<SwipeableDrawer
classes={{
paper: classNames(classes.paper, 'algolia-drawer'),
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/HomeBackers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import NoSSR from '@material-ui/docs/NoSSR';
import NoSSR from '@material-ui/core/NoSSR';
import MarkdownElement from '@material-ui/docs/MarkdownElement';

const styles = theme => ({
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/HomeSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FileDownloadIcon from '@material-ui/docs/svgIcons/FileDownload';
import BuildIcon from '@material-ui/icons/Build'; // eslint-disable-line import/no-unresolved
import WhatshotIcon from '@material-ui/icons/Whatshot';
import MarkdownElement from '@material-ui/docs/MarkdownElement';
import NoSSR from '@material-ui/docs/NoSSR';
import NoSSR from '@material-ui/core/NoSSR';
import Link from 'docs/src/modules/components/Link';

const styles = theme => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classNames from 'classnames';
import Select from 'react-select';
import { withStyles } from '@material-ui/core/styles';
import Typography from '@material-ui/core/Typography';
import NoSSR from '@material-ui/docs/NoSSR';
import NoSSR from '@material-ui/core/NoSSR';
import TextField from '@material-ui/core/TextField';
import Chip from '@material-ui/core/Chip';
import MenuItem from '@material-ui/core/MenuItem';
Expand Down
1 change: 0 additions & 1 deletion packages/material-ui-docs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as MarkdownElement } from './MarkdownElement';
export { default as NoSSR } from './NoSSR';
export { default as NProgressBar } from './NProgressBar';
10 changes: 10 additions & 0 deletions packages/material-ui/src/NoSSR/NoSSR.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from 'react';

export interface NoSSRProps {
children: React.ReactNode;
fallback?: React.ReactNode;
}

declare const NoSSR: React.ComponentType<NoSSRProps>;

export default NoSSR;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import exactProp from '../utils/exactProp';

const Fallback = () => null;

Expand Down Expand Up @@ -32,6 +33,8 @@ NoSSR.propTypes = {
fallback: PropTypes.node,
};

NoSSR.propTypes = exactProp(NoSSR.propTypes);

NoSSR.defaultProps = {
fallback: <Fallback />,
};
Expand Down
53 changes: 53 additions & 0 deletions packages/material-ui/src/NoSSR/NoSSR.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @flow

import React from 'react';
import { assert } from 'chai';
import { createMount, createShallow } from '../test-utils';
import NoSSR from './NoSSR';

describe('<NoSSR />', () => {
let mount;
let shallow;

before(() => {
mount = createMount();
shallow = createShallow({ disableLifecycleMethods: true });
});

after(() => {
mount.cleanUp();
});

describe('server side rendering', () => {
it('should not render the children as the width is unknown', () => {
const wrapper = shallow(
<NoSSR>
<span>Hello</span>
</NoSSR>,
);
assert.strictEqual(wrapper.name(), 'Fallback');
});
});

describe('mounted', () => {
it('should render the children', () => {
const wrapper = mount(
<NoSSR>
<span>Hello</span>
</NoSSR>,
);
assert.strictEqual(wrapper.find('span').length, 1);
});
});

describe('prop: fallback', () => {
it('should render the fallback', () => {
const wrapper = shallow(
<NoSSR fallback="fallback">
<span>Hello</span>
</NoSSR>,
);
assert.strictEqual(wrapper.text(), 'fallback');
});
});
});
2 changes: 2 additions & 0 deletions packages/material-ui/src/NoSSR/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './NoSSR';
export * from './NoSSR';
File renamed without changes.
18 changes: 9 additions & 9 deletions packages/material-ui/src/SwipeableDrawer/SwipeArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ export const styles = theme => ({
position: 'fixed',
top: 0,
left: 0,
height: '100vh',
bottom: 0,
zIndex: theme.zIndex.drawer - 1,
},
discoveryAnchorLeft: {
anchorLeft: {
right: 'auto',
},
discoveryAnchorRight: {
anchorRight: {
left: 'auto',
right: 0,
},
discoveryAnchorTop: {
anchorTop: {
bottom: 'auto',
right: 0,
},
discoveryAnchorBottom: {
anchorBottom: {
top: 'auto',
bottom: 0,
right: 0,
Expand All @@ -36,13 +36,13 @@ export const styles = theme => ({
* @ignore - internal component.
*/
function SwipeArea(props) {
const { anchor, classes, swipeAreaWidth, ...other } = props;
const { anchor, classes, width, ...other } = props;

return (
<div
className={classNames(classes.root, classes[`discoveryAnchor${capitalize(anchor)}`])}
className={classNames(classes.root, classes[`anchor${capitalize(anchor)}`])}
style={{
[isHorizontal(props) ? 'width' : 'height']: swipeAreaWidth,
[isHorizontal(props) ? 'width' : 'height']: width,
}}
{...other}
/>
Expand All @@ -62,7 +62,7 @@ SwipeArea.propTypes = {
* The width of the left most (or right most) area in pixels where the
* drawer can be swiped open from.
*/
swipeAreaWidth: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
};

export default withStyles(styles)(SwipeArea);
7 changes: 6 additions & 1 deletion packages/material-ui/src/SwipeableDrawer/SwipeableDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Drawer, { getAnchor, isHorizontal } from '../Drawer/Drawer';
import { duration } from '../styles/transitions';
import withTheme from '../styles/withTheme';
import { getTransitionProps } from '../transitions/utils';
import NoSSR from '../NoSSR';
import SwipeArea from './SwipeArea';

// This value is closed to what browsers are using internally to
Expand Down Expand Up @@ -332,6 +333,7 @@ class SwipeableDrawer extends React.Component {

render() {
const {
anchor,
disableBackdropTransition,
disableDiscovery,
disableSwipeToOpen,
Expand Down Expand Up @@ -365,12 +367,15 @@ class SwipeableDrawer extends React.Component {
},
ref: this.handlePaperRef,
}}
anchor={anchor}
{...other}
/>
{!disableDiscovery &&
!disableSwipeToOpen &&
variant === 'temporary' && (
<SwipeArea anchor={other.anchor} swipeAreaWidth={swipeAreaWidth} />
<NoSSR>
<SwipeArea anchor={anchor} width={swipeAreaWidth} />
</NoSSR>
)}
</React.Fragment>
);
Expand Down
27 changes: 8 additions & 19 deletions packages/material-ui/src/SwipeableDrawer/SwipeableDrawer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,14 @@ describe('<SwipeableDrawer />', () => {
theme={createMuiTheme()}
/>,
);
if (React.Fragment) {
assert.strictEqual(wrapper.childAt(0).type(), Drawer);
assert.strictEqual(wrapper.childAt(1).type(), SwipeArea);
} else {
assert.strictEqual(
wrapper
.childAt(0)
.childAt(0)
.type(),
Drawer,
);
assert.strictEqual(
wrapper
.childAt(0)
.childAt(1)
.type(),
SwipeArea,
);
}
assert.strictEqual(wrapper.childAt(0).type(), Drawer);
assert.strictEqual(
wrapper
.childAt(1)
.childAt(0)
.type(),
SwipeArea,
);
wrapper.unmount();
});

Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export { default as MenuList } from './MenuList';
export { default as MobileStepper } from './MobileStepper';
export { default as Modal, ModalManager } from './Modal';
export { default as NativeSelect } from './NativeSelect';
export { default as NoSSR } from './NoSSR';
export { default as Paper } from './Paper';
export { default as Popover } from './Popover';
export { default as Popper } from './Popper';
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export { default as MenuList } from './MenuList';
export { default as MobileStepper } from './MobileStepper';
export { default as Modal, ModalManager } from './Modal';
export { default as NativeSelect } from './NativeSelect';
export { default as NoSSR } from './NoSSR';
export { default as Paper } from './Paper';
export { default as Popover } from './Popover';
export { default as Popper } from './Popper';
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/withWidth/withWidth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('withWidth', () => {
describe('server side rendering', () => {
it('should not render the children as the width is unknown', () => {
const wrapper = shallow(<EmptyWithWidth />);
assert.strictEqual(wrapper.type(), null, 'should render nothing');
assert.strictEqual(wrapper.type(), null);
});
});

Expand Down

0 comments on commit becf3d0

Please sign in to comment.