Skip to content

Commit

Permalink
Add initial quick inserter
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 1, 2020
1 parent ccb3ed6 commit 1a6636e
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ function BlockPopover( {
<Inserter
clientId={ clientId }
rootClientId={ rootClientId }
__experimentalIsQuick
/>
</div>
) }
Expand All @@ -214,6 +215,7 @@ function BlockPopover( {
position="top right"
rootClientId={ rootClientId }
clientId={ clientId }
__experimentalIsQuick
/>
</div>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export default function InsertionPoint( {
<Inserter
position="bottom center"
clientId={ inserterClientId }
__experimentalIsQuick
/>
</div>
</div>
Expand Down
59 changes: 59 additions & 0 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { parse } from '@wordpress/blocks';
import { ENTER, SPACE } from '@wordpress/keycodes';

/**
* Internal dependencies
*/
import BlockPreview from '../block-preview';

function BlockPattern( { pattern, onClick } ) {
const { content, viewportWidth } = pattern;
const blocks = useMemo( () => parse( content ), [ content ] );

return (
<div
className="block-editor-block-patterns-list__item"
role="button"
onClick={ () => onClick( pattern, blocks ) }
onKeyDown={ ( event ) => {
if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
onClick( pattern, blocks );
}
} }
tabIndex={ 0 }
aria-label={ pattern.title }
>
<BlockPreview blocks={ blocks } viewportWidth={ viewportWidth } />
<div className="block-editor-block-patterns-list__item-title">
{ pattern.title }
</div>
</div>
);
}

function BlockPatternPlaceholder() {
return (
<div className="block-editor-block-patterns-list__item is-placeholder" />
);
}

function BlockPatternList( { blockPatterns, shownPatterns, onClickPattern } ) {
return blockPatterns.map( ( pattern ) => {
const isShown = shownPatterns.includes( pattern );
return isShown ? (
<BlockPattern
key={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
);
} );
}

export default BlockPatternList;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.block-editor-block-patterns-list__item {
border-radius: $radius-block-ui;
cursor: pointer;
margin-top: $grid-unit-20;
transition: all 0.05s ease-in-out;
position: relative;
border: $border-width solid transparent;

&:hover {
border: $border-width solid $theme-color;
}

&:focus {
box-shadow: inset 0 0 0 1px $white, 0 0 0 $border-width-focus $theme-color;

// Windows High Contrast mode will show this outline, but not the box-shadow.
outline: 2px solid transparent;
}

&.is-placeholder {
min-height: 100px;
}
}

.block-editor-block-patterns-list__item-title {
padding: $grid-unit-05;
font-size: 12px;
text-align: center;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function ButtonBlockAppender(
position="bottom center"
rootClientId={ rootClientId }
__experimentalSelectBlockOnInsert={ selectBlockOnInsert }
__experimentalIsQuick
renderToggle={ ( {
onToggle,
disabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function DefaultBlockAppender( {
rootClientId={ rootClientId }
position="top right"
isAppender
__experimentalIsQuick
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,17 @@ import { fromPairs } from 'lodash';
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { parse } from '@wordpress/blocks';
import { ENTER, SPACE } from '@wordpress/keycodes';
import { __, _x } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import BlockPreview from '../block-preview';
import useAsyncList from './use-async-list';
import InserterPanel from './panel';
import { searchItems } from './search-items';
import InserterNoResults from './no-results';
import usePatternsState from './hooks/use-patterns-state';

function BlockPattern( { pattern, onClick } ) {
const { content, viewportWidth } = pattern;
const blocks = useMemo( () => parse( content ), [ content ] );

return (
<div
className="block-editor-inserter__patterns-item"
role="button"
onClick={ () => onClick( pattern, blocks ) }
onKeyDown={ ( event ) => {
if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
onClick( pattern, blocks );
}
} }
tabIndex={ 0 }
aria-label={ pattern.title }
>
<BlockPreview blocks={ blocks } viewportWidth={ viewportWidth } />
<div className="block-editor-inserter__patterns-item-title">
{ pattern.title }
</div>
</div>
);
}

function BlockPatternPlaceholder() {
return (
<div className="block-editor-inserter__patterns-item is-placeholder" />
);
}

function BlockPatternList( { patterns, shownPatterns, onClickPattern } ) {
return patterns.map( ( pattern ) => {
const isShown = shownPatterns.includes( pattern );
return isShown ? (
<BlockPattern
key={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
);
} );
}
import useAsyncList from './hooks/use-async-list';
import BlockPatternList from '../block-patterns-list';

function BlockPatternsSearchResults( { filterValue, onInsert } ) {
const [ patterns, , onClick ] = usePatternsState( onInsert );
Expand All @@ -81,7 +33,7 @@ function BlockPatternsSearchResults( { filterValue, onInsert } ) {
<InserterPanel title={ __( 'Search Results' ) }>
<BlockPatternList
shownPatterns={ currentShownPatterns }
patterns={ filteredPatterns }
blockPatterns={ filteredPatterns }
onClickPattern={ onClick }
/>
</InserterPanel>
Expand Down Expand Up @@ -147,7 +99,7 @@ function BlockPatternsPerCategories( { onInsert } ) {
>
<BlockPatternList
shownPatterns={ currentShownPatterns }
patterns={ categoryPatterns }
blockPatterns={ categoryPatterns }
onClickPattern={ onClick }
/>
</InserterPanel>
Expand All @@ -159,7 +111,7 @@ function BlockPatternsPerCategories( { onInsert } ) {
<InserterPanel title={ _x( 'Uncategorized' ) }>
<BlockPatternList
shownPatterns={ currentShownPatterns }
patterns={ uncategorizedPatterns }
blockPatterns={ uncategorizedPatterns }
onClickPattern={ onClick }
/>
</InserterPanel>
Expand All @@ -168,7 +120,7 @@ function BlockPatternsPerCategories( { onInsert } ) {
);
}

function BlockPatterns( { onInsert, filterValue } ) {
function BlockPatternsTabs( { onInsert, filterValue } ) {
return filterValue ? (
<BlockPatternsSearchResults
onInsert={ onInsert }
Expand All @@ -179,4 +131,4 @@ function BlockPatterns( { onInsert, filterValue } ) {
);
}

export default BlockPatterns;
export default BlockPatternsTabs;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const getBlockNamespace = ( item ) => item.name.split( '/' )[ 0 ];

const MAX_SUGGESTED_ITEMS = 6;

export function InserterBlockList( {
export function BlockTypesTab( {
rootClientId,
onInsert,
onHover,
Expand Down Expand Up @@ -248,4 +248,4 @@ export function InserterBlockList( {
);
}

export default compose( withSpokenMessages )( InserterBlockList );
export default compose( withSpokenMessages )( BlockTypesTab );
20 changes: 19 additions & 1 deletion packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* External dependencies
*/
import { size } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand All @@ -18,6 +20,7 @@ import { plus } from '@wordpress/icons';
* Internal dependencies
*/
import InserterMenu from './menu';
import QuickInserter from './quick-inserter';

const defaultRenderToggle = ( {
onToggle,
Expand Down Expand Up @@ -116,8 +119,19 @@ class Inserter extends Component {
isAppender,
showInserterHelpPanel,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
__experimentalIsQuick: isQuick,
} = this.props;

if ( isQuick ) {
return (
<QuickInserter
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
selectBlockOnInsert={ selectBlockOnInsert }
/>
);
}
return (
<InserterMenu
onSelect={ onClose }
Expand All @@ -135,6 +149,7 @@ class Inserter extends Component {
position,
hasSingleBlockType,
insertOnlyAllowedBlock,
__experimentalIsQuick: isQuick,
} = this.props;

if ( hasSingleBlockType ) {
Expand All @@ -144,7 +159,10 @@ class Inserter extends Component {
return (
<Dropdown
className="block-editor-inserter"
contentClassName="block-editor-inserter__popover"
contentClassName={ classnames(
'block-editor-inserter__popover',
{ 'is-quick': isQuick }
) }
position={ position }
onToggle={ this.onToggle }
expandOnMobile
Expand Down
32 changes: 10 additions & 22 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { includes } from 'lodash';
*/
import { useState } from '@wordpress/element';
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { TabPanel } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';

/**
Expand All @@ -18,9 +16,10 @@ import { useSelect } from '@wordpress/data';
import Tips from './tips';
import InserterSearchForm from './search-form';
import InserterPreviewPanel from './preview-panel';
import InserterBlockList from './block-list';
import BlockPatterns from './block-patterns';
import BlockTypesTab from './block-types-tab';
import BlockPatternsTabs from './block-patterns-tab';
import useInsertionPoint from './hooks/use-insertion-point';
import InserterTabs from './tabs';

const stopKeyPropagation = ( event ) => event.stopPropagation();

Expand Down Expand Up @@ -82,7 +81,7 @@ function InserterMenu( {
<>
<div className="block-editor-inserter__block-list">
<div className="block-editor-inserter__scrollable">
<InserterBlockList
<BlockTypesTab
rootClientId={ destinationRootClientId }
onInsert={ onInsert }
onHover={ onHover }
Expand All @@ -100,7 +99,10 @@ function InserterMenu( {

const patternsTab = (
<div className="block-editor-inserter__scrollable">
<BlockPatterns onInsert={ onInsert } filterValue={ filterValue } />
<BlockPatternsTabs
onInsert={ onInsert }
filterValue={ filterValue }
/>
</div>
);

Expand All @@ -119,28 +121,14 @@ function InserterMenu( {
<div className="block-editor-inserter__main-area">
<InserterSearchForm onChange={ setFilterValue } />
{ showPatterns && (
<TabPanel
className="block-editor-inserter__tabs"
tabs={ [
{
name: 'blocks',
/* translators: Blocks tab title in the block inserter. */
title: __( 'Blocks' ),
},
{
name: 'patterns',
/* translators: Patterns tab title in the block inserter. */
title: __( 'Patterns' ),
},
] }
>
<InserterTabs>
{ ( tab ) => {
if ( tab.name === 'blocks' ) {
return blocksTab;
}
return patternsTab;
} }
</TabPanel>
</InserterTabs>
) }
{ ! showPatterns && blocksTab }
</div>
Expand Down
Loading

0 comments on commit 1a6636e

Please sign in to comment.