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 all 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
151 changes: 137 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,147 @@
import React, { PropTypes } from 'react';
import { View } from 'react-native';
import React, { Component, PropTypes } from 'react';
import {
Animated,
Easing,
View,
TouchableWithoutFeedback,
Image,
Text,
StatusBar,
} 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(...args) {
super(...args);

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,
menuWidth: 0,
};

this.storyChangedHandler = this.handleStoryChanged.bind(this);
this.menuToggledHandler = this.handleToggleMenu.bind(this);
this.menuLayoutHandler = this.handleMenuLayout.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,
});
}

handleMenuLayout(e) {
this.setState({
menuWidth: e.nativeEvent.layout.width,
});
}

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 { menuAnimation, selectedKind, selectedStory, menuWidth } = this.state;

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

const menuSpacerStyles = [
{
width: menuAnimation.interpolate({
inputRange: [0, 1],
outputRange: [0, menuWidth],
}),
},
];

const headerStyles = [
style.headerContainer,
{
opacity: menuAnimation.interpolate({
inputRange: [0, 1],
outputRange: [1, 0],
}),
},
];

/* 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}>
<StatusBar hidden />
<Animated.View style={menuSpacerStyles} />
<View style={style.previewContainer}>
<Animated.View style={headerStyles}>
<TouchableWithoutFeedback onPress={this.menuToggledHandler}>
<View>
<Image source={openMenuImage} style={style.icon} />
</View>
</TouchableWithoutFeedback>
<Text style={style.headerText} numberOfLines={1}>
{selectedKind} / {selectedStory}
</Text>
</Animated.View>
<View style={style.previewWrapper}>
<View style={style.preview}>
<StoryView url={url} events={events} />
</View>
</View>
</View>
<Animated.View style={menuStyles} onLayout={this.menuLayoutHandler}>
<TouchableWithoutFeedback onPress={this.menuToggledHandler}>
<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.
39 changes: 27 additions & 12 deletions app/react-native/src/preview/components/OnDeviceUI/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@ export default {
main: {
flex: 1,
flexDirection: 'row',
paddingTop: 20,
backgroundColor: 'rgba(247, 247, 247, 1)',
backgroundColor: 'rgba(255, 255, 255, 1)',
},
leftPanel: {
flex: 1,
maxWidth: 250,
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)',
},
menuContainer: {
...StyleSheet.absoluteFillObject,
right: null,
paddingHorizontal: 8,
paddingBottom: 8,
backgroundColor: 'rgba(247, 247, 247, 1)',
},
rightPanel: {
previewContainer: {
flex: 1,
backgroundColor: 'rgba(255, 255, 255, 1)',
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'rgba(236, 236, 236, 1)',
borderRadius: 4,
marginBottom: 8,
marginHorizontal: 8,
},
previewWrapper: {
flex: 1,
},
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