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] Add Reusable blocks to the inserter menu #28495

Merged
merged 101 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 98 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
178648e
Add custom header to BottomSheet
fluiddot Sep 10, 2020
bf5f8aa
SegmentedControl mobile component exported
fluiddot Sep 10, 2020
463c257
Add Reusable blocks to inserter menu with tabs
fluiddot Sep 10, 2020
0e164f5
Add native version of inserter-list-item
fluiddot Sep 14, 2020
c081f76
Add mobile version of block-types-list component
fluiddot Sep 15, 2020
8efbfbb
Add getWidth to mobile version of inserter-list-item component
fluiddot Sep 15, 2020
1010667
Add blocks-tab mobile version to inserter component
fluiddot Sep 15, 2020
2507e95
Add reusable-blocks-tab mobile version to inserter component
fluiddot Sep 15, 2020
9cc6776
Inserter menu mobile version refactor to use new tabs
fluiddot Sep 15, 2020
4ca23d7
Lint fixes in inserter menu components
fluiddot Sep 18, 2020
b9d9cb9
README of block-types-list component updated with mobile changes
fluiddot Sep 21, 2020
d29b5c5
modal prefix removed from inserter-list-item style names
fluiddot Sep 21, 2020
11c59bb
blocks-tab renamed to block-types-tab in inserter menu
fluiddot Sep 21, 2020
420dbb6
add block-types-tab UI test
fluiddot Sep 21, 2020
37ea46a
Add reusable-blocks-tab UI test
fluiddot Sep 22, 2020
83bac66
Remove unused import from block types list
fluiddot Oct 2, 2020
3a5e7e5
Change key calculation for block types list component
fluiddot Jan 4, 2021
68ccca7
Fix style format issue in inserter list item
fluiddot Jan 4, 2021
cc6efdc
Add header prop to BottomSheet
fluiddot Mar 26, 2021
17ae9a4
Add header height to max height calculation
fluiddot Mar 26, 2021
32e66c2
Move inserter search form to bottom sheet header
fluiddot Mar 26, 2021
71eeab7
Merge branch 'trunk' into rnmobile/add/2575-reusable-blocks-inserter
fluiddot Mar 26, 2021
57a0b71
Merge branch 'rnmobile/add/custom-header-bottom-sheet' into rnmobile/…
fluiddot Mar 26, 2021
ad76855
Update BlockTypesList to functional component
fluiddot Mar 26, 2021
005dba9
Add use clipboard block hook
fluiddot Mar 26, 2021
899141f
Use block types list in search results
fluiddot Mar 26, 2021
4d3013f
Remove inserter list item native files
fluiddot Mar 26, 2021
a412058
Cleanup inserter native styles
fluiddot Mar 26, 2021
a3c3ca8
Update platform specific styles
jhnstn Apr 8, 2021
a5a6beb
Fix styles import
jhnstn Apr 9, 2021
6aa2a11
Update android input
jhnstn Apr 9, 2021
b28f276
Replace search icone with Gridicon
jhnstn Apr 9, 2021
ed09983
Remove touchable highlight component
jhnstn Apr 10, 2021
f63dd63
Refactor input form
jhnstn Apr 13, 2021
851079f
Clean up style generation
jhnstn Apr 13, 2021
e18274a
Rename input container class
jhnstn Apr 13, 2021
7456ddf
Fix input container active styling
jhnstn Apr 14, 2021
844184e
Update input button styles
jhnstn Apr 14, 2021
cccf2b4
Fix input button on Android
jhnstn Apr 14, 2021
1aea172
Fix iOS active search styles
jhnstn Apr 15, 2021
c883fd8
Tighten up iOS input buttons
jhnstn Apr 15, 2021
14ad9d6
Align items in icon button
jhnstn Apr 15, 2021
360f5d4
Use elevation for input shadow
jhnstn Apr 16, 2021
1145225
Updates for dark mode
jhnstn Apr 19, 2021
fb37e84
Merge branch 'trunk' into rnmobile/add/2575-reusable-blocks-inserter
fluiddot Apr 21, 2021
7f64173
Fix icon fill color
jhnstn Apr 21, 2021
8e817e1
Clean up styles
jhnstn Apr 22, 2021
eacd5bc
Fix key calculation in BlockTypesList
fluiddot Apr 23, 2021
8b7b5c3
Add block-editor store definition in ReusableBlocksTab
fluiddot Apr 23, 2021
fd97494
Limit the number of lines of block title
fluiddot Apr 23, 2021
e7b2676
Add inserter tabs component
fluiddot Apr 23, 2021
f2e0c79
Add inserter tabs in inserter menu
fluiddot Apr 23, 2021
cadfaa6
Add search focus to inserter menu
fluiddot Apr 23, 2021
8d0f3ae
Unify keyboard listeners in bottom sheet component
fluiddot Apr 23, 2021
dcd4fa9
Merge branch 'trunk' into rnmobile/add/2575-reusable-blocks-inserter
fluiddot Apr 23, 2021
4ccd706
Update useClipboardBlock hook
fluiddot Apr 23, 2021
44c0537
Add visual improvements to inserter menu
fluiddot Apr 23, 2021
575c58f
Set maximum items per page of reusable blocks
fluiddot Apr 23, 2021
1ad3268
Add new color scheme hook
jhnstn Apr 26, 2021
873e389
Refactor style generation
jhnstn Apr 26, 2021
d1d3f12
Fix style selectors
jhnstn Apr 26, 2021
c16c5bc
Fix fill color on icons
jhnstn Apr 26, 2021
4df39cd
Merge branch 'update/inserter-block-search' into rnmobile/add/2575-re…
fluiddot Apr 27, 2021
60e461b
Refactor native inserter tabs
fluiddot Apr 29, 2021
2e02624
Add layout animations in inserter menu
fluiddot Apr 29, 2021
00527a5
Add layout animation fixes in bottom sheet
fluiddot Apr 29, 2021
110a513
Increase max listeners on iOS to prevent warning
fluiddot Apr 29, 2021
fa4c0ec
Add root client id to search results
fluiddot Apr 29, 2021
853d45f
Hide tabs when is not main inserter
fluiddot Apr 29, 2021
5d23d10
Force layout calculation on block types list mount
fluiddot Apr 29, 2021
663cc8c
Improve inserter menu layout animations for Android
fluiddot Apr 29, 2021
c93082a
Add layout animations to bottom sheet component
fluiddot Apr 30, 2021
2a70a38
Update keyboard layout animation
fluiddot Apr 30, 2021
736fab5
Add keyboard show/hide callbacks to bottom sheet
fluiddot Apr 30, 2021
e35bff5
Remove search form height
fluiddot Apr 30, 2021
1d6e507
Merge branch 'rnmobile/add/custom-header-bottom-sheet' into rnmobile/…
fluiddot May 4, 2021
9c4dbfe
Prevent multiple layout animations on Android
fluiddot May 4, 2021
7eaa61c
Use keyboard events instead focus for display tabs
fluiddot May 4, 2021
49ea69c
Enable layout animation of empty search for Android
fluiddot May 4, 2021
8a4179c
Update block-types-list README
fluiddot May 5, 2021
0b1ad2f
Add last scroll events to inserter tabs
fluiddot May 5, 2021
4455515
Fix block types tab unit test
fluiddot May 5, 2021
e12c54e
Add landscape mode support in inserter tabs
fluiddot May 5, 2021
f060f9c
Revert "Merge branch 'update/inserter-block-search' into rnmobile/add…
fluiddot May 5, 2021
622313d
Revert "Add search focus to inserter menu"
fluiddot May 5, 2021
8b4c462
Revert "Increase max listeners on iOS to prevent warning"
fluiddot May 5, 2021
fcf1ccf
Limit layout animation to header changes in bottom sheet
fluiddot May 6, 2021
76be478
Enable reusable tab when inserting in inner blocks
fluiddot May 6, 2021
24fc1f4
Merge branch 'trunk' into rnmobile/add/2575-reusable-blocks-inserter
fluiddot May 6, 2021
e25217d
Remove hasReusableBlocks from inserter menu
fluiddot May 6, 2021
958dd9d
Apply BEM naming to block types list styles
fluiddot May 10, 2021
08f4549
Rename block types tab to match web version
fluiddot May 10, 2021
6153e92
Move tabs container style to a constant
fluiddot May 10, 2021
8ebca15
Rename block types tab function to match web version
fluiddot May 10, 2021
60e020b
Rename import of block types list in inserter unit tests
fluiddot May 10, 2021
89a2e43
Move bottom-sheet keyboard handlers to constants
fluiddot May 10, 2021
444386a
Update inserter tabs to use wrapper width for animations
fluiddot May 11, 2021
c95db83
Merge branch 'trunk' into rnmobile/add/2575-reusable-blocks-inserter
fluiddot May 12, 2021
00289fc
Include safe area inset on the block types list
fluiddot May 13, 2021
0ca8b43
Merge branch 'trunk' into rnmobile/add/2575-reusable-blocks-inserter
fluiddot May 14, 2021
d9f3257
Update react-native-editor changelog
fluiddot May 14, 2021
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
19 changes: 19 additions & 0 deletions packages/block-editor/src/components/block-types-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ The blocks that will be displayed in the block list.

- Type: `Array<Block>`
- Required: Yes
- Platform: Web | Mobile

#### name

Name of the list to be used as part of component's key.

- Type: `String`
- Required: Yes
- Platform: Mobile

#### listProps

Extra `FlatList` props for customizing the list.

On Mobile usually this component is rendered inside `BottomSheet` component, which already [generates these props](<(https://github.com/WordPress/gutenberg/blob/1ca1fe0c64dfe1a385221399fc94b0fb14f34199/packages/components/src/mobile/bottom-sheet/index.native.js#L355-L372)>) for this component.

- Type: `String`
- Required: No
- Platform: Mobile

## Related components

Expand Down
100 changes: 100 additions & 0 deletions packages/block-editor/src/components/block-types-list/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* External dependencies
*/
import {
FlatList,
View,
TouchableWithoutFeedback,
Dimensions,
} from 'react-native';

/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { BottomSheet, InserterButton } from '@wordpress/components';

/**
* Internal dependencies
*/
import styles from './style.scss';

const MIN_COL_NUM = 3;

export default function BlockTypesList( { name, items, onSelect, listProps } ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The logic for this component has been extracted from InserterSearchResults component. This component is now used for rendering the block types in the different tabs of the inserter menu as well as in the search results.

const [ numberOfColumns, setNumberOfColumns ] = useState( MIN_COL_NUM );
const [ itemWidth, setItemWidth ] = useState();
const [ maxWidth, setMaxWidth ] = useState();

useEffect( () => {
Dimensions.addEventListener( 'change', onLayout );
onLayout();
return () => {
Dimensions.removeEventListener( 'change', onLayout );
};
}, [] );

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

function onLayout() {
const columnStyle = styles[ 'block-types-list__column' ];
const sumLeftRightPadding =
columnStyle.paddingLeft + columnStyle.paddingRight;

const bottomSheetWidth = BottomSheet.getWidth();
const containerTotalWidth = bottomSheetWidth - sumLeftRightPadding;
const itemTotalWidth = calculateItemWidth();

const columnsFitToWidth = Math.floor(
containerTotalWidth / itemTotalWidth
);

const numColumns = Math.max( MIN_COL_NUM, columnsFitToWidth );

setNumberOfColumns( numColumns );
setMaxWidth( containerTotalWidth / numColumns );

if ( columnsFitToWidth < MIN_COL_NUM ) {
const updatedItemWidth =
( bottomSheetWidth - 2 * sumLeftRightPadding ) / MIN_COL_NUM;
setItemWidth( updatedItemWidth );
}
}

return (
<FlatList
onLayout={ onLayout }
key={ `InserterUI-${ name }-${ numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ numberOfColumns }
data={ items }
initialNumToRender={ 3 }
ItemSeparatorComponent={ () => (
<TouchableWithoutFeedback accessible={ false }>
<View
style={ styles[ 'block-types-list__row-separator' ] }
/>
</TouchableWithoutFeedback>
) }
keyExtractor={ ( item ) => item.id }
renderItem={ ( { item } ) => (
<InserterButton
{ ...{
item,
itemWidth,
maxWidth,
onSelect,
} }
/>
) }
{ ...listProps }
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.block-types-list__row-separator {
height: 12px;
}

.block-types-list__column {
padding: $grid-unit-20;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import BlockTypesList from '../block-types-list';
import useClipboardBlock from './hooks/use-clipboard-block';
import { store as blockEditorStore } from '../../store';

const NON_BLOCK_CATEGORIES = [ 'reusable' ];

function BlockTypesTab( { onSelect, rootClientId, listProps } ) {
const clipboardBlock = useClipboardBlock( rootClientId );

const { items } = useSelect(
( select ) => {
const { getInserterItems } = select( blockEditorStore );

const allItems = getInserterItems( rootClientId );
const blockItems = allItems.filter(
( { category } ) => ! NON_BLOCK_CATEGORIES.includes( category )
);

return {
items: clipboardBlock
? [ clipboardBlock, ...blockItems ]
: blockItems,
};
},
[ rootClientId ]
);

return (
<BlockTypesList
name="Blocks"
items={ items }
onSelect={ onSelect }
listProps={ listProps }
/>
);
}

export default BlockTypesTab;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { rawHandler, store as blocksStore } from '@wordpress/blocks';
import { getClipboard } from '@wordpress/components';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';

export default function useClipboardBlock( destinationRootClientId ) {
geriux marked this conversation as resolved.
Show resolved Hide resolved
const { canInsertBlockType } = useSelect( blockEditorStore );
const { getBlockType } = useSelect( blocksStore );

const clipboard = getClipboard();
const clipboardBlock = rawHandler( { HTML: clipboard } )[ 0 ];

const canAddClipboardBlock = canInsertBlockType(
clipboardBlock?.name,
destinationRootClientId
);

if ( ! canAddClipboardBlock ) {
return undefined;
}

const { icon, name } = getBlockType( clipboardBlock.name );
const { attributes: initialAttributes, innerBlocks } = clipboardBlock;

return {
id: 'clipboard',
name,
icon,
initialAttributes,
innerBlocks,
};
}
Loading