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

[RNMobile] Adjust bottom-sheet #18574

Merged
merged 24 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f8fdeaf
Initial work on iOS Inserter
lukewalczak Nov 18, 2019
b190a91
Merge branch 'master' into rnmobile/inserter
lukewalczak Dec 19, 2019
a020ec0
Merge branch 'master' into rnmobile/inserter
lukewalczak Jan 29, 2020
7ef43f6
Disable swiping-to-close on Android
lukewalczak Jan 30, 2020
54196b2
Merge branch 'master' into rnmobile/inserter
lukewalczak Jan 30, 2020
c2b05b0
Add listener for device rotation and small styling changes
lukewalczak Jan 31, 2020
d311517
Merge branch 'master' into rnmobile/inserter
lukewalczak Jan 31, 2020
e897e67
Merge branch 'master' into rnmobile/inserter
lukewalczak Jan 31, 2020
edfc88c
Small cleanup
lukewalczak Jan 31, 2020
84fa9f8
More refactor
lukewalczak Jan 31, 2020
2a5d771
Refactor horizontal mode
lukewalczak Feb 3, 2020
d58567c
Merge branch 'master' into rnmobile/inserter
lukewalczak Feb 3, 2020
65c54d2
Adjust itemWidth for zoomed screen on Android
lukewalczak Feb 4, 2020
359f566
Set accessiblity false
lukewalczak Feb 5, 2020
01c18d4
Merge branch 'master' into rnmobile/inserter
lukewalczak Feb 5, 2020
89684ee
Add disableScrollViewPanResponder
lukewalczak Feb 5, 2020
1efdd26
Adjust BottomSheet height when a keyboard is opened
lukewalczak Feb 6, 2020
60daed9
Correct removing keyboard listeners
lukewalczak Feb 6, 2020
45279b5
Merge branch 'master' into rnmobile/inserter
lukewalczak Feb 11, 2020
b282411
Code clean up
lukewalczak Feb 11, 2020
4ed2acb
Revert change
lukewalczak Feb 11, 2020
fbb6a9f
Revert pushed file by mistake
lukewalczak Feb 11, 2020
b5a1c12
Code refactor after CR
lukewalczak Feb 11, 2020
fe5ec26
Remove dimensions listener on unmount
lukewalczak Feb 12, 2020
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
135 changes: 92 additions & 43 deletions packages/block-editor/src/components/inserter/menu.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* External dependencies
*/
import { FlatList, View, Text, TouchableHighlight } from 'react-native';
import {
FlatList,
View,
Text,
TouchableHighlight,
Dimensions,
} from 'react-native';

/**
* WordPress dependencies
Expand All @@ -21,15 +27,19 @@ import { BottomSheet, Icon } from '@wordpress/components';
*/
import styles from './style.scss';

const MIN_COL_NUM = 3;

export class InserterMenu extends Component {
constructor() {
super( ...arguments );

this.onClose = this.onClose.bind( this );
this.onLayout = this.onLayout.bind( this );
this.state = {
numberOfColumns: this.calculateNumberOfColumns(),
numberOfColumns: MIN_COL_NUM,
};

Dimensions.addEventListener( 'change', this.onLayout );
}

componentDidMount() {
Expand All @@ -38,23 +48,46 @@ export class InserterMenu extends Component {

componentWillUnmount() {
this.props.hideInsertionPoint();
Dimensions.removeEventListener( 'change', this.onLayout );
}

calculateNumberOfColumns() {
const bottomSheetWidth = BottomSheet.getWidth();
calculateMinItemWidth( bottomSheetWidth ) {
const { paddingLeft, paddingRight } = styles.columnPadding;
return (
( bottomSheetWidth - 2 * ( paddingLeft + paddingRight ) ) /
MIN_COL_NUM
);
}

calculateItemWidth() {
const {
paddingLeft: itemPaddingLeft,
paddingRight: itemPaddingRight,
} = styles.modalItem;
const { width: itemWidth } = styles.modalIconWrapper;
return itemWidth + itemPaddingLeft + itemPaddingRight;
}

calculateColumnsProperties() {
const bottomSheetWidth = BottomSheet.getWidth();
const {
paddingLeft: containerPaddingLeft,
paddingRight: containerPaddingRight,
} = styles.content;
const { width: itemWidth } = styles.modalIconWrapper;
const itemTotalWidth = itemWidth + itemPaddingLeft + itemPaddingRight;
const itemTotalWidth = this.calculateItemWidth();
const containerTotalWidth =
bottomSheetWidth - ( containerPaddingLeft + containerPaddingRight );
return Math.floor( containerTotalWidth / itemTotalWidth );
const numofColumns = Math.floor( containerTotalWidth / itemTotalWidth );

if ( numofColumns < MIN_COL_NUM ) {
return {
numOfColumns: MIN_COL_NUM,
itemWidth: this.calculateMinItemWidth( bottomSheetWidth ),
};
}
return {
numOfColumns: numofColumns,
};
}

onClose() {
Expand All @@ -67,12 +100,16 @@ export class InserterMenu extends Component {
}

onLayout() {
const numberOfColumns = this.calculateNumberOfColumns();
const columnProperties = this.calculateColumnsProperties();
const numberOfColumns = columnProperties.numOfColumns;

this.setState( { numberOfColumns } );
}

render() {
const { getStylesFromColorScheme } = this.props;
const { getStylesFromColorScheme, items, onSelect } = this.props;
const { numberOfColumns } = this.state;

const bottomPadding = styles.contentBottomPadding;
const modalIconWrapperStyle = getStylesFromColorScheme(
styles.modalIconWrapper,
Expand All @@ -87,49 +124,61 @@ export class InserterMenu extends Component {
styles.modalItemLabelDark
);

const columnProperties = this.calculateColumnsProperties();

return (
<BottomSheet
isVisible={ true }
onClose={ this.onClose }
contentStyle={ [ styles.content, bottomPadding ] }
hideHeader
>
<FlatList
onLayout={ this.onLayout }
scrollEnabled={ false }
key={ `InserterUI-${ this.state.numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ this.state.numberOfColumns }
data={ this.props.items }
ItemSeparatorComponent={ () => (
<View style={ styles.rowSeparator } />
) }
keyExtractor={ ( item ) => item.name }
renderItem={ ( { item } ) => (
<TouchableHighlight
style={ styles.touchableArea }
underlayColor="transparent"
activeOpacity={ 0.5 }
accessibilityLabel={ item.title }
onPress={ () => this.props.onSelect( item ) }
>
<View style={ styles.modalItem }>
<View style={ modalIconWrapperStyle }>
<View style={ modalIconStyle }>
<Icon
icon={ item.icon.src }
fill={ modalIconStyle.fill }
size={ modalIconStyle.width }
/>
<TouchableHighlight accessible={ false }>
<FlatList
onLayout={ this.onLayout }
scrollEnabled={ false }
key={ `InserterUI-${ numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ numberOfColumns }
data={ items }
ItemSeparatorComponent={ () => (
<View style={ styles.rowSeparator } />
) }
keyExtractor={ ( item ) => item.name }
renderItem={ ( { item } ) => (
<TouchableHighlight
style={ styles.touchableArea }
underlayColor="transparent"
activeOpacity={ 0.5 }
accessibilityLabel={ item.title }
onPress={ () => onSelect( item ) }
>
<View style={ styles.modalItem }>
<View
style={ [
modalIconWrapperStyle,
columnProperties.itemWidth && {
width:
columnProperties.itemWidth,
},
] }
>
<View style={ modalIconStyle }>
<Icon
icon={ item.icon.src }
fill={ modalIconStyle.fill }
size={ modalIconStyle.width }
/>
</View>
</View>
<Text style={ modalItemLabelStyle }>
{ item.title }
</Text>
</View>
<Text style={ modalItemLabelStyle }>
{ item.title }
</Text>
</View>
</TouchableHighlight>
) }
/>
</TouchableHighlight>
) }
/>
</TouchableHighlight>
</BottomSheet>
);
}
Expand Down
11 changes: 7 additions & 4 deletions packages/block-editor/src/components/inserter/style.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
.content {
padding: 0 0 0 0;
align-items: center;
justify-content: space-evenly;
}

.contentBottomPadding {
Expand All @@ -20,10 +19,10 @@

.modalItem {
flex-direction: column;
justify-content: center;
justify-content: flex-start;
align-items: center;
padding-left: 8;
padding-right: 8;
padding-left: $panel-padding / 2;
padding-right: $panel-padding / 2;
padding-top: 0;
padding-bottom: 0;
}
Expand Down Expand Up @@ -79,3 +78,7 @@
color: $blue-30;
border-color: $blue-30;
}

.columnPadding {
padding: $panel-padding;
}
Loading