Skip to content

Commit

Permalink
Allows the actions of speed dial to be placed in any direction
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/material-ui-lab/src/SpeedDial/SpeedDial.js
#	pages/lab/api/speed-dial.md
  • Loading branch information
Sean Chamberlain committed Aug 1, 2018
1 parent f970ad4 commit a5775fe
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/material-ui-lab/src/SpeedDial/SpeedDial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SpeedDialProps
> {
ariaLabel: string;
ButtonProps?: Partial<ButtonProps>;
direction?: 'up' | 'down' | 'left' | 'right';
hidden?: boolean;
icon: React.ReactNode;
onClose?: React.ReactEventHandler<{}>;
Expand Down
40 changes: 36 additions & 4 deletions packages/material-ui-lab/src/SpeedDial/SpeedDial.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@ export const styles = {
root: {
zIndex: 1050,
display: 'flex',
flexDirection: 'column-reverse', // Place the Actions above the FAB.
pointerEvents: 'none',
},
/* Styles applied to the Button component. */
fab: {
pointerEvents: 'auto',
},
/* Styles applied to the root and action container elements when direction="up" */
directionUp: {
flexDirection: 'column-reverse',
},
/* Styles applied to the root and action container elements when direction="down" */
directionDown: {
flexDirection: 'column',
},
/* Styles applied to the root and action container elements when direction="left" */
directionLeft: {
flexDirection: 'row-reverse',
},
/* Styles applied to the root and action container elements when direction="right" */
directionRight: {
flexDirection: 'row',
},
/* Styles applied to the actions (`children` wrapper) element. */
actions: {
display: 'flex',
flexDirection: 'column-reverse', // Display the first action at the bottom.
paddingBottom: 16,
pointerEvents: 'auto',
marginBottom: 16,
},
/* Styles applied to the actions (`children` wrapper) element if `open={false}`. */
actionsClosed: {
Expand Down Expand Up @@ -109,6 +124,7 @@ class SpeedDial extends React.Component {
onClose,
onKeyDown,
open,
direction,
openIcon,
TransitionComponent,
transitionDuration,
Expand Down Expand Up @@ -158,8 +174,15 @@ class SpeedDial extends React.Component {
return icon;
};

const actionsPlacementClass = {
[classes.directionUp]: direction === 'up',
[classes.directionDown]: direction === 'down',
[classes.directionLeft]: direction === 'left',
[classes.directionRight]: direction === 'right',
};

return (
<div className={classNames(classes.root, classNameProp)} {...other}>
<div className={classNames(classes.root, actionsPlacementClass, classNameProp)} {...other}>
<TransitionComponent
in={!hidden}
timeout={transitionDuration}
Expand All @@ -186,7 +209,11 @@ class SpeedDial extends React.Component {
</TransitionComponent>
<div
id={`${id}-actions`}
className={classNames(classes.actions, { [classes.actionsClosed]: !open })}
className={classNames(
classes.actions,
{ [classes.actionsClosed]: !open },
actionsPlacementClass,
)}
ref={ref => {
this.actionsRef = ref;
}}
Expand Down Expand Up @@ -221,6 +248,10 @@ SpeedDial.propTypes = {
* @ignore
*/
className: PropTypes.string,
/**
* The direction the actions open relative to the floating action button.
*/
direction: PropTypes.oneOf(['up', 'down', 'left', 'right']),
/**
* If `true`, the SpeedDial will be hidden.
*/
Expand Down Expand Up @@ -273,6 +304,7 @@ SpeedDial.propTypes = {

SpeedDial.defaultProps = {
hidden: false,
direction: 'top',
TransitionComponent: Zoom,
transitionDuration: {
enter: duration.enteringScreen,
Expand Down
22 changes: 22 additions & 0 deletions packages/material-ui-lab/src/SpeedDial/SpeedDial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,26 @@ describe('<SpeedDial />', () => {
assert.strictEqual(handleKeyDown.args[0][0], event);
});
});

describe('prop: direction', () => {
const testDirection = direction => {
const className = `direction${direction}`;
const wrapper = shallow(
<SpeedDial {...defaultProps} direction={direction.toLowerCase()} icon={icon}>
<SpeedDialAction icon={icon} />
<SpeedDialAction icon={icon} />
</SpeedDial>,
);
const actionsWrapper = wrapper.childAt(1);
assert.strictEqual(wrapper.hasClass(classes[className]), true);
assert.strictEqual(actionsWrapper.hasClass(classes[className]), true);
};

it('should place actions in correct position', () => {
testDirection('Up');
testDirection('Down');
testDirection('Left');
testDirection('Right');
});
});
});
5 changes: 5 additions & 0 deletions pages/lab/api/speed-dial.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ title: SpeedDial API
| <span class="prop-name">ButtonProps</span> | <span class="prop-type">object |   | Properties applied to the [`Button`](/api/button) element. |
| <span class="prop-name required">children *</span> | <span class="prop-type">node |   | SpeedDialActions to display when the SpeedDial is `open`. |
| <span class="prop-name">classes</span> | <span class="prop-type">object |   | Override or extend the styles applied to the component. See [CSS API](#css-api) below for more details. |
| <span class="prop-name">direction</span> | <span class="prop-type">enum:&nbsp;'up'&nbsp;&#124;<br>&nbsp;'down'&nbsp;&#124;<br>&nbsp;'left'&nbsp;&#124;<br>&nbsp;'right'<br> | <span class="prop-default">'top'</span> | The direction the actions open relative to the floating action button. |
| <span class="prop-name">hidden</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the SpeedDial will be hidden. |
| <span class="prop-name required">icon *</span> | <span class="prop-type">element |   | The icon to display in the SpeedDial Floating Action Button. The `SpeedDialIcon` component provides a default Icon with animation. |
| <span class="prop-name">onClose</span> | <span class="prop-type">func |   | Callback fired when the component requests to be closed.<br><br>**Signature:**<br>`function(event: object, key: string) => void`<br>*event:* The event source of the callback<br>*key:* The key pressed |
Expand All @@ -39,6 +40,10 @@ This property accepts the following keys:
| Name | Description |
|:-----|:------------|
| <span class="prop-name">root</span> | Styles applied to the root element.
| <span class="prop-name">directionUp</span> | Styles applied to the root and action container elements when direction="up"
| <span class="prop-name">directionDown</span> | Styles applied to the root and action container elements when direction="down"
| <span class="prop-name">directionLeft</span> | Styles applied to the root and action container elements when direction="left"
| <span class="prop-name">directionRight</span> | Styles applied to the root and action container elements when direction="right"
| <span class="prop-name">fab</span> | Styles applied to the Button component.
| <span class="prop-name">actions</span> | Styles applied to the actions (`children` wrapper) element.
| <span class="prop-name">actionsClosed</span> | Styles applied to the actions (`children` wrapper) element if `open={false}`.
Expand Down

0 comments on commit a5775fe

Please sign in to comment.