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

Show active viewport dimensions & button to rotate #6045

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion addons/viewport/src/Tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import memoize from 'memoizerific';
import deprecate from 'util-deprecate';

import { Global } from '@storybook/theming';
import { styled, Global } from '@storybook/theming';

import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/components';
import { SET_STORIES } from '@storybook/core-events';
Expand Down Expand Up @@ -88,6 +88,24 @@ const getState = memoize(10)((props, state, change) => {
};
});

const ActiveViewportSize = styled.div(() => ({
display: 'inline-flex',
}));

const ActiveViewportLabel = styled.div(({ theme }) => ({
display: 'inline-block',
textDecoration: 'none',
padding: '10px',
fontWeight: theme.typography.weight.bold,
fontSize: theme.typography.size.s2 - 1,
lineHeight: 1,
height: 40,
border: 'none',
borderTop: '3px solid transparent',
borderBottom: '3px solid transparent',
background: 'transparent',
}));

export default class ViewportTool extends Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -118,11 +136,24 @@ export default class ViewportTool extends Component {

change = (...args) => this.setState(...args);

flipViewport = () =>
this.setState(({ isRotated }) => ({ isRotated: !isRotated, expanded: false }));

render() {
const { expanded } = this.state;
const { items, selected, isRotated } = getState(this.props, this.state, this.change);
const item = items.find(i => i.id === selected);

let viewportX = 0;
let viewportY = 0;
if (item) {
const height = item.value.height.replace('px', '');
const width = item.value.width.replace('px', '');

viewportX = isRotated ? height : width;
viewportY = isRotated ? width : height;
}

return items.length ? (
<Fragment>
{item ? (
Expand Down Expand Up @@ -150,6 +181,15 @@ export default class ViewportTool extends Component {
<Icons icon="grow" />
</IconButton>
</WithTooltip>
{item ? (
<ActiveViewportSize>
<ActiveViewportLabel title="Viewport width">{viewportX}</ActiveViewportLabel>
<IconButton key="viewport-rotate" title="Rotate viewport" onClick={this.flipViewport}>
<Icons icon="transfer" />
</IconButton>
<ActiveViewportLabel title="Viewport height">{viewportY}</ActiveViewportLabel>
</ActiveViewportSize>
) : null}
</Fragment>
) : null;
}
Expand Down