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

Mobile BottomSheet: Adding max-width and centering. #13882

Merged
merged 8 commits into from
Feb 19, 2019
76 changes: 47 additions & 29 deletions packages/editor/src/components/mobile/bottom-sheet/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { Text, View, KeyboardAvoidingView, Platform, PanResponder } from 'react-native';
import { Text, View, KeyboardAvoidingView, Platform, PanResponder, Dimensions } from 'react-native';
import Modal from 'react-native-modal';
import SafeArea from 'react-native-safe-area';

Expand Down Expand Up @@ -30,17 +30,20 @@ class BottomSheet extends Component {
}

componentDidMount() {
this.eventSubscription = SafeArea.addEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
this.safeAreaEventSubscription = SafeArea.addEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
}

componentWillUnmount() {
this.eventSubscription.remove();
this.eventSubscription = null;
if ( this.safeAreaEventSubscription === null ) {
return;
}
this.safeAreaEventSubscription.remove();
this.safeAreaEventSubscription = null;
SafeArea.removeEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
}

onSafeAreaInsetsUpdate( result ) {
if ( this.eventSubscription === null ) {
if ( this.safeAreaEventSubscription === null ) {
return;
}
const { safeAreaInsets } = result;
Expand All @@ -50,7 +53,15 @@ class BottomSheet extends Component {
}

render() {
const { title = '', isVisible, leftButton, rightButton, hideHeader, style = {} } = this.props;
const {
title = '',
isVisible,
leftButton,
rightButton,
hideHeader,
style = {},
contentStyle = {},
} = this.props;

const panResponder = PanResponder.create( {
onMoveShouldSetPanResponder: ( evt, gestureState ) => {
Expand All @@ -63,6 +74,25 @@ class BottomSheet extends Component {
},
} );

const getHeader = () => (
<View>
<View style={ styles.head }>
<View style={ { flex: 1 } }>
{ leftButton }
</View>
<View style={ styles.titleContainer }>
<Text style={ styles.title }>
{ title }
</Text>
</View>
<View style={ { flex: 1 } }>
{ rightButton }
</View>
</View>
<View style={ styles.separator } />
</View>
);

return (
<Modal
isVisible={ isVisible }
Expand All @@ -81,32 +111,15 @@ class BottomSheet extends Component {
>
<KeyboardAvoidingView
behavior={ Platform.OS === 'ios' && 'padding' }
style={ { ...styles.content, borderColor: 'rgba(0, 0, 0, 0.1)', ...style } }
style={ { ...styles.background, borderColor: 'rgba(0, 0, 0, 0.1)', ...style } }
keyboardVerticalOffset={ -this.state.safeAreaBottomInset }
>
<View style={ styles.dragIndicator } />
{ hideHeader ? (
<View style={ styles.emptyHeaderSpace } />
) : (
<View>
<View style={ styles.head }>
<View style={ { flex: 1 } }>
{ leftButton }
</View>
<View style={ styles.titleContainer }>
<Text style={ styles.title }>
{ title }
</Text>
</View>
<View style={ { flex: 1 } }>
{ rightButton }
</View>
</View>
<View style={ styles.separator } />
</View>
) }
{ this.props.children }
<View style={ { flexGrow: 1 } }></View>
{ hideHeader && ( <View style={ styles.emptyHeaderSpace } /> ) }
{ ! hideHeader && getHeader() }
<View style={ [ styles.content, contentStyle ] }>
{ this.props.children }
</View>
<View style={ { height: this.state.safeAreaBottomInset } } />
</KeyboardAvoidingView>
</Modal>
Expand All @@ -115,6 +128,11 @@ class BottomSheet extends Component {
}
}

function getWidth() {
return Math.min( Dimensions.get( 'window' ).width, styles.background.maxWidth );
}

BottomSheet.getWidth = getWidth;
BottomSheet.Button = Button;
BottomSheet.Cell = Cell;
BottomSheet.PickerCell = PickerCell;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
.bottomModal {
justify-content: flex-end;
margin: 0;
align-items: center;
}

.dragIndicator {
background-color: $light-gray-400;
height: 4px;
width: 36px;
margin: auto;
margin-top: 6px;
border-radius: 2px;
}

Expand All @@ -23,11 +25,16 @@
height: 14;
}

.content {
padding: 6px 16px 0 16px;
.background {
background-color: $white;
border-top-right-radius: 8px;
border-top-left-radius: 8px;
width: 100%;
max-width: 512;
}

.content {
padding: 0 16px 0 16px;
}

.head {
Expand Down