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

React Native collapsible OnDeviceUI navigation #1544

Merged
merged 8 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
120 changes: 106 additions & 14 deletions app/react-native/src/preview/components/OnDeviceUI/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,116 @@
import React, { PropTypes } from 'react';
import { View } from 'react-native';
import React, { Component, PropTypes } from 'react';
import { Animated, Easing, View, TouchableWithoutFeedback, Image, Text } from 'react-native';
import style from './style';
import StoryListView from '../StoryListView';
import StoryView from '../StoryView';

export default function OnDeviceUI(props) {
const { stories, events, url } = props;
export default class OnDeviceUI extends Component {
constructor(props, ...args) {
super(props, ...args);
Copy link
Member

Choose a reason for hiding this comment

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

Super nitty feedback - but since props isn't used, we could just spread all the arguments

constructor(...props) {
  super(...props)
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I had props in there before and just forgot to clean this up. I'll get it updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved.


return (
<View style={style.main}>
<View style={style.leftPanel}>
<StoryListView stories={stories} events={events} />
</View>
<View style={style.rightPanel}>
<View style={style.preview}>
<StoryView url={url} events={events} />
this.state = {
menuAnimation: new Animated.Value(0),
isMenuOpen: false,
selectedKind: null,
selectedStory: null,
};

this.storyChangedHandler = this.handleStoryChanged.bind(this);
this.handleToggleMenu = this.handleToggleMenu.bind(this);

this.props.events.on('story', this.storyChangedHandler);
}

componentWillUnmount() {
this.props.events.removeListener('story', this.storyChangedHandler);
}

handleStoryChanged(storyFn, selection) {
const { kind, story } = selection;
this.setState({
selectedKind: kind,
selectedStory: story,
});
}

handleToggleMenu() {
const isMenuOpen = !this.state.isMenuOpen;

Animated.timing(this.state.menuAnimation, {
toValue: isMenuOpen ? 1 : 0,
duration: 150,
easing: Easing.linear,
}).start();

this.setState({
isMenuOpen,
});
}

render() {
const { stories, events, url } = this.props;
Copy link
Member

@atticoos atticoos Jul 30, 2017

Choose a reason for hiding this comment

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

Do we need to subscribe for stories being added? Or will added stories become updated in props.stories and lead to a re-render?

Update: Deferred to #1547

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly not sure. The subscription setup was actually setup by someone else. This PR is just changing the UI to be more giving on space available to the preview section. I can work on cleaning this up in a follow up PR

Copy link
Member

Choose a reason for hiding this comment

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

The original component received added stories through event emissions -- I think we may want to subscribe to those here too, or otherwise the sidebar may not update as stories become added. cc @shilman or @ndelangen -- do Preview components receive additional stories on props.stories over time? Or are subscriptions needed

Copy link
Member

Choose a reason for hiding this comment

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

@ajwhite right now in examples/react-native-vanilla it appears the stories don't update in the browser OR in the OnDeviceUI when I modify the story code. I'm not sure if this is a bug in my setup or whether something is broken in @storybook/react-native or just the example...

Copy link
Member

Choose a reason for hiding this comment

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

Okay - sounds like something we can come back to and keep the scope of this PR narrow 👍

Opened #1547, can probably tackle once I'm back in Boston on Tuesday.

const { isMenuOpen, menuAnimation, selectedKind, selectedStory } = this.state;

const overlayStyles = [style.overlayContainer, { opacity: menuAnimation }];

const menuStyles = [
style.menuContainer,
{
transform: [
{
translateX: menuAnimation.interpolate({
inputRange: [0, 1],
outputRange: [0, 250],
}),
},
],
},
];

/* eslint-disable global-require */
const openMenuImage = require('./menu_open.png');
const closeMenuImage = require('./menu_close.png');
/* eslint-enable global-require */

return (
<View style={style.main}>
<View style={style.previewContainer}>
<View style={style.headerContainer}>
<TouchableWithoutFeedback onPress={this.handleToggleMenu}>
<View>
<Image source={openMenuImage} style={style.icon} />
</View>
</TouchableWithoutFeedback>
<Text style={style.headerText} numberOfLines={1}>
{selectedKind} / {selectedStory}
</Text>
</View>
<View style={style.previewWrapper}>
<View style={style.preview}>
<StoryView url={url} events={events} />
</View>
</View>
</View>
{isMenuOpen &&
<TouchableWithoutFeedback onPress={this.handleToggleMenu}>
<Animated.View style={overlayStyles} />
</TouchableWithoutFeedback>}
Copy link
Member

Choose a reason for hiding this comment

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

dropping the trailing } onto another line would make this more legible -- thought Animated.View was part of this condition block 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prettier is not a fan of that. I typically would have it on the next line but prettier complained at me. I'll look at options because I agree that this isn't as readable as it could be.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, if prettier did this, then nothing to see here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved by way of removing this code all together 😄

<Animated.View style={menuStyles}>
<TouchableWithoutFeedback onPress={this.handleToggleMenu}>
<View style={style.closeButton}>
<Image source={closeMenuImage} style={style.icon} />
</View>
</TouchableWithoutFeedback>
<StoryListView
stories={stories}
events={events}
selectedKind={selectedKind}
selectedStory={selectedStory}
/>
</Animated.View>
</View>
</View>
);
);
}
}

OnDeviceUI.propTypes = {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 38 additions & 11 deletions app/react-native/src/preview/components/OnDeviceUI/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,51 @@ import { StyleSheet } from 'react-native';
export default {
main: {
flex: 1,
paddingTop: 20,
backgroundColor: 'rgba(255, 255, 255, 1)',
},
icon: {
width: 20,
height: 20,
opacity: 0.5,
},
headerContainer: {
flexDirection: 'row',
alignItems: 'center',
margin: 5,
},
headerText: {
marginLeft: 5,
fontSize: 14,
color: 'rgba(0, 0, 0, 0.5)',
},
overlayContainer: {
position: 'absolute',
Copy link
Member

Choose a reason for hiding this comment

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

For consistency, you can use StyleSheet.absoluteFillObject like used below in the preview style 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I meant to switch them all to be the same but forgot. Will get this updated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved.

top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
},
menuContainer: {
position: 'absolute',
top: 0,
bottom: 0,
left: -250,
width: 250,
paddingHorizontal: 8,
paddingTop: 20,
paddingBottom: 8,
backgroundColor: 'rgba(247, 247, 247, 1)',
},
leftPanel: {
previewContainer: {
flex: 1,
maxWidth: 250,
paddingHorizontal: 8,
paddingBottom: 8,
},
rightPanel: {
previewWrapper: {
flex: 1,
backgroundColor: 'rgba(255, 255, 255, 1)',
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'rgba(236, 236, 236, 1)',
borderRadius: 4,
marginBottom: 8,
marginHorizontal: 8,
},
closeButton: {
marginVertical: 5,
},
preview: {
...StyleSheet.absoluteFillObject,
Expand Down
26 changes: 10 additions & 16 deletions app/react-native/src/preview/components/StoryListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ export default class StoryListView extends Component {
super(props, ...args);
this.state = {
sections: [],
selectedKind: null,
selectedStory: null,
};

this.storyAddedHandler = this.handleStoryAdded.bind(this);
this.storyChangedHandler = this.handleStoryChanged.bind(this);
this.changeStoryHandler = this.changeStory.bind(this);

this.props.stories.on('storyAdded', this.storyAddedHandler);
this.props.events.on('story', this.storyChangedHandler);
}

componentDidMount() {
Expand All @@ -54,8 +50,7 @@ export default class StoryListView extends Component {
}

componentWillUnmount() {
this.props.stories.removeListener('storyAdded', this.storiesHandler);
this.props.events.removeListener('story', this.storyChangedHandler);
this.props.stories.removeListener('storyAdded', this.storyAddedHandler);
}

handleStoryAdded() {
Expand All @@ -75,14 +70,6 @@ export default class StoryListView extends Component {
}
}

handleStoryChanged(storyFn, selection) {
const { kind, story } = selection;
this.setState({
selectedKind: kind,
selectedStory: story,
});
}

changeStory(kind, story) {
this.props.events.emit('setCurrentStory', { kind, story });
}
Expand All @@ -95,14 +82,14 @@ export default class StoryListView extends Component {
<ListItem
title={item.name}
selected={
item.kind === this.state.selectedKind && item.name === this.state.selectedStory
item.kind === this.props.selectedKind && item.name === this.props.selectedStory
}
onPress={() => this.changeStory(item.kind, item.name)}
/>}
renderSectionHeader={({ section }) =>
<SectionHeader
title={section.title}
selected={section.title === this.state.selectedKind}
selected={section.title === this.props.selectedKind}
/>}
sections={this.state.sections}
stickySectionHeadersEnabled={false}
Expand All @@ -123,4 +110,11 @@ StoryListView.propTypes = {
emit: PropTypes.func.isRequired,
removeListener: PropTypes.func.isRequired,
}).isRequired,
selectedKind: PropTypes.string,
selectedStory: PropTypes.string,
};

StoryListView.defaultProps = {
selectedKind: null,
selectedStory: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
maxWidth: 250,
},
header: {
paddingTop: 24,
paddingTop: 4,
paddingBottom: 4,
},
headerText: {
Expand Down