From ca64d9a1f57f711c93574c848195bd79e3ef8182 Mon Sep 17 00:00:00 2001 From: Jorge Date: Thu, 5 Sep 2019 22:45:35 +0100 Subject: [PATCH] Add: Buttons block --- .../block-alignment-toolbar/index.js | 1 + .../test/__snapshots__/index.js.snap | 1 + .../block-library/src/button/edit-settings.js | 11 ++ packages/block-library/src/button/edit.js | 108 ++++++++++++++---- packages/block-library/src/button/index.js | 1 + packages/block-library/src/buttons/block.json | 5 + packages/block-library/src/buttons/edit.js | 60 ++++++++++ .../block-library/src/buttons/editor.scss | 78 +++++++++++++ packages/block-library/src/buttons/icon.js | 8 ++ packages/block-library/src/buttons/index.js | 29 +++++ packages/block-library/src/buttons/save.js | 12 ++ packages/block-library/src/buttons/style.scss | 7 ++ packages/block-library/src/editor.scss | 1 + packages/block-library/src/index.js | 2 + packages/block-library/src/style.scss | 1 + .../e2e-tests/fixtures/block-transforms.js | 6 + .../fixtures/blocks/core__buttons.html | 11 ++ .../fixtures/blocks/core__buttons.json | 31 +++++ .../fixtures/blocks/core__buttons.parsed.json | 43 +++++++ .../blocks/core__buttons.serialized.html | 9 ++ .../blocks/__snapshots__/button.test.js.snap | 13 --- .../blocks/__snapshots__/buttons.test.js.snap | 21 ++++ .../{button.test.js => buttons.test.js} | 9 +- 23 files changed, 429 insertions(+), 39 deletions(-) create mode 100644 packages/block-library/src/button/edit-settings.js create mode 100644 packages/block-library/src/buttons/block.json create mode 100644 packages/block-library/src/buttons/edit.js create mode 100644 packages/block-library/src/buttons/editor.scss create mode 100644 packages/block-library/src/buttons/icon.js create mode 100644 packages/block-library/src/buttons/index.js create mode 100644 packages/block-library/src/buttons/save.js create mode 100644 packages/block-library/src/buttons/style.scss create mode 100644 packages/e2e-tests/fixtures/blocks/core__buttons.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__buttons.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__buttons.parsed.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__buttons.serialized.html delete mode 100644 packages/e2e-tests/specs/blocks/__snapshots__/button.test.js.snap create mode 100644 packages/e2e-tests/specs/blocks/__snapshots__/buttons.test.js.snap rename packages/e2e-tests/specs/blocks/{button.test.js => buttons.test.js} (75%) diff --git a/packages/block-editor/src/components/block-alignment-toolbar/index.js b/packages/block-editor/src/components/block-alignment-toolbar/index.js index e766504f0d9f4..53f003a2c34cc 100644 --- a/packages/block-editor/src/components/block-alignment-toolbar/index.js +++ b/packages/block-editor/src/components/block-alignment-toolbar/index.js @@ -52,6 +52,7 @@ export function BlockAlignmentToolbar( { value, onChange, controls = DEFAULT_CON return ( { @@ -72,6 +80,79 @@ function BorderPanel( { borderRadius = '', setAttributes } ) { ); } +const InlineURLPicker = withInstanceId( + function( { instanceId, isSelected, url, onChange } ) { + const linkId = `wp-block-button__inline-link-${ instanceId }`; + return ( + + + + ); + } +); + +function PopoverURLPicker( { url, onChange } ) { + const [ urlInput, setUrlInput ] = useState( url || '' ); + const [ isPopoverEnable, setIsPopoverEnable ] = useState( true ); + const onSubmit = useCallback( + () => { + onChange( urlInput ); + setIsPopoverEnable( false ); + }, + [ urlInput, onChange ] + ); + if ( ! isPopoverEnable ) { + return null; + } + return ( + + + + ); +} + +function URLPicker( { isSelected, url, setAttributes } ) { + const onChange = useCallback( + ( value ) => setAttributes( { url: value } ), + [ setAttributes ] + ); + const { urlInPopover } = useContext( ButtonEditSettings ); + if ( urlInPopover ) { + return isSelected ? ( + + ) : null; + } + return ( + + ); +} class ButtonEdit extends Component { constructor() { @@ -121,7 +202,6 @@ class ButtonEdit extends Component { fallbackTextColor, setAttributes, className, - instanceId, isSelected, } = this.props; @@ -135,8 +215,6 @@ class ButtonEdit extends Component { url, } = attributes; - const linkId = `wp-block-button__inline-link-${ instanceId }`; - return (
- - setAttributes( { url: value } ) } - disableSuggestions={ ! isSelected } - id={ linkId } - isFullWidth - hasBorder - /> - + { + const buttonBlock = createBlock( buttonBlockName ); + insertBlock( buttonBlock, undefined, clientId ); + }, + [ insertBlock, clientId ] + ); + return useCallback( + () => { + return ( + + ); + }, + [ insertButton ] + ); +} + +function ButtonsEdit( { clientId, className } ) { + const renderAppender = useInsertButton( clientId ); + return ( +
+ + + +
+ ); +} + +export default ButtonsEdit; diff --git a/packages/block-library/src/buttons/editor.scss b/packages/block-library/src/buttons/editor.scss new file mode 100644 index 0000000000000..bb4c7a7a5906c --- /dev/null +++ b/packages/block-library/src/buttons/editor.scss @@ -0,0 +1,78 @@ +.wp-block-buttons .block-editor-block-list__block[data-type="core/button"] { + display: inline-block; +} + + +.wp-block-buttons { + // 1. Reset margins on immediate innerblocks container. + > .block-editor-inner-blocks > .block-editor-block-list__layout { + margin-left: 0; + margin-right: 0; + } + + // 2. Remove paddings on subsequent immediate children. + > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block { + width: auto; + padding-left: 0; + padding-right: 0; + } + + // 3. Remove margins on subsequent Edit container. + > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block > .block-editor-block-list__block-edit { + margin-left: 0; + margin-right: 0; + } + + // 4. Hide the block outlines. + > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block > .block-editor-block-list__block-edit::before { + content: none; + } + + // 5. Remove vertical margins on subsequent block container. + > .block-editor-inner-blocks > .block-editor-block-list__layout > .wp-block > .block-editor-block-list__block-edit > [data-block] { + margin-top: 0; + margin-bottom: 0; + } + + // Hide the breadcrumb. + // Hide the mover. + .block-editor-block-list__breadcrumb, + .block-editor-block-mover.block-editor-block-mover { // Needs specificity. + display: none; + } + + .block-editor-inserter { + display: none; + } + + .block-editor-block-list__block[data-type="core/button"] .block-editor-block-alignment-toolbar { + display: none; + } + + .wp-block-button .wp-block-button__link { + margin-top: 0; + } +} + +.block-editor-block-list__block[data-type="core/buttons"] { + div[data-type="core/button"] div[data-block] { + display: block; + } + + &[data-align="center"] .block-editor-block-list__layout { + display: flex; + align-items: center; + flex-wrap: wrap; + justify-content: center; + } + + &[data-align="right"] .block-editor-block-list__layout { + display: flex; + justify-content: flex-end; + } + + .block-list-appender { + display: inline-block !important; + margin: 0; + } +} diff --git a/packages/block-library/src/buttons/icon.js b/packages/block-library/src/buttons/icon.js new file mode 100644 index 0000000000000..6e18a60a648fe --- /dev/null +++ b/packages/block-library/src/buttons/icon.js @@ -0,0 +1,8 @@ +/** + * WordPress dependencies + */ +import { G, Path, SVG } from '@wordpress/components'; + +export default ( + +); diff --git a/packages/block-library/src/buttons/index.js b/packages/block-library/src/buttons/index.js new file mode 100644 index 0000000000000..6a3ae48b99e56 --- /dev/null +++ b/packages/block-library/src/buttons/index.js @@ -0,0 +1,29 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import edit from './edit'; +import icon from './icon'; +import metadata from './block.json'; +import save from './save'; + +const { name } = metadata; + +export { metadata, name }; + +export const settings = { + title: __( 'Buttons' ), + description: __( 'Prompt visitors to take action with a group of button-style links.' ), + icon, + keywords: [ __( 'link' ) ], + supports: { + align: true, + alignWide: false, + }, + edit, + save, +}; diff --git a/packages/block-library/src/buttons/save.js b/packages/block-library/src/buttons/save.js new file mode 100644 index 0000000000000..91cb804231d05 --- /dev/null +++ b/packages/block-library/src/buttons/save.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks } from '@wordpress/block-editor'; + +export default function save() { + return ( +
+ +
+ ); +} diff --git a/packages/block-library/src/buttons/style.scss b/packages/block-library/src/buttons/style.scss new file mode 100644 index 0000000000000..47fb245ec0024 --- /dev/null +++ b/packages/block-library/src/buttons/style.scss @@ -0,0 +1,7 @@ +.wp-block-buttons .wp-block-button { + display: inline-block; + margin: $grid-size-small; +} +.wp-block-buttons.aligncenter { + text-align: center; +} diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index 3b38a4e136640..e6cd240f8482a 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -2,6 +2,7 @@ @import "./audio/editor.scss"; @import "./block/editor.scss"; @import "./button/editor.scss"; +@import "./buttons/editor.scss"; @import "./categories/editor.scss"; @import "./code/editor.scss"; @import "./columns/editor.scss"; diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index e05057a05ed10..b20fdf57f1a6d 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -23,6 +23,7 @@ import * as quote from './quote'; import * as gallery from './gallery'; import * as archives from './archives'; import * as audio from './audio'; +import * as buttons from './buttons'; import * as button from './button'; import * as calendar from './calendar'; import * as categories from './categories'; @@ -105,6 +106,7 @@ export const registerCoreBlocks = () => { archives, audio, button, + buttons, calendar, categories, code, diff --git a/packages/block-library/src/style.scss b/packages/block-library/src/style.scss index 433ef8cb3aa2f..06f337061f5ed 100644 --- a/packages/block-library/src/style.scss +++ b/packages/block-library/src/style.scss @@ -1,5 +1,6 @@ @import "./audio/style.scss"; @import "./button/style.scss"; +@import "./buttons/style.scss"; @import "./calendar/style.scss"; @import "./categories/style.scss"; @import "./columns/style.scss"; diff --git a/packages/e2e-tests/fixtures/block-transforms.js b/packages/e2e-tests/fixtures/block-transforms.js index d0cb4f59d3e27..3a0a0c2a03623 100644 --- a/packages/e2e-tests/fixtures/block-transforms.js +++ b/packages/e2e-tests/fixtures/block-transforms.js @@ -30,6 +30,12 @@ export const EXPECTED_TRANSFORMS = { 'Group', ], }, + core__buttons: { + originalBlock: 'Buttons', + availableTransforms: [ + 'Group', + ], + }, core__calendar: { originalBlock: 'Calendar', availableTransforms: [ diff --git a/packages/e2e-tests/fixtures/blocks/core__buttons.html b/packages/e2e-tests/fixtures/blocks/core__buttons.html new file mode 100644 index 0000000000000..e70af7acc72ad --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__buttons.html @@ -0,0 +1,11 @@ + +
+ + + + + + + +
+ diff --git a/packages/e2e-tests/fixtures/blocks/core__buttons.json b/packages/e2e-tests/fixtures/blocks/core__buttons.json new file mode 100644 index 0000000000000..044daeb82101a --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__buttons.json @@ -0,0 +1,31 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/buttons", + "isValid": true, + "attributes": {}, + "innerBlocks": [ + { + "clientId": "_clientId_0", + "name": "core/button", + "isValid": true, + "attributes": { + "text": "My button 1" + }, + "innerBlocks": [], + "originalContent": "" + }, + { + "clientId": "_clientId_1", + "name": "core/button", + "isValid": true, + "attributes": { + "text": "My button 2" + }, + "innerBlocks": [], + "originalContent": "" + } + ], + "originalContent": "
\n\t\n\n\t\n
" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__buttons.parsed.json b/packages/e2e-tests/fixtures/blocks/core__buttons.parsed.json new file mode 100644 index 0000000000000..b96b1f50db1fc --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__buttons.parsed.json @@ -0,0 +1,43 @@ +[ + { + "blockName": "core/buttons", + "attrs": {}, + "innerBlocks": [ + { + "blockName": "core/button", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n\t\n\t", + "innerContent": [ + "\n\t\n\t" + ] + }, + { + "blockName": "core/button", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n\t\n\t", + "innerContent": [ + "\n\t\n\t" + ] + } + ], + "innerHTML": "\n
\n\t\n\n\t\n
\n", + "innerContent": [ + "\n
\n\t", + null, + "\n\n\t", + null, + "\n
\n" + ] + }, + { + "blockName": null, + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n", + "innerContent": [ + "\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__buttons.serialized.html b/packages/e2e-tests/fixtures/blocks/core__buttons.serialized.html new file mode 100644 index 0000000000000..baf0a0226c066 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__buttons.serialized.html @@ -0,0 +1,9 @@ + + + diff --git a/packages/e2e-tests/specs/blocks/__snapshots__/button.test.js.snap b/packages/e2e-tests/specs/blocks/__snapshots__/button.test.js.snap deleted file mode 100644 index 82fca76a8fb04..0000000000000 --- a/packages/e2e-tests/specs/blocks/__snapshots__/button.test.js.snap +++ /dev/null @@ -1,13 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Button can jump focus back & forth 1`] = ` -" - -" -`; - -exports[`Button has focus on button content 1`] = ` -" - -" -`; diff --git a/packages/e2e-tests/specs/blocks/__snapshots__/buttons.test.js.snap b/packages/e2e-tests/specs/blocks/__snapshots__/buttons.test.js.snap new file mode 100644 index 0000000000000..8aae5ee57c83c --- /dev/null +++ b/packages/e2e-tests/specs/blocks/__snapshots__/buttons.test.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Buttons can jump focus back & forth 1`] = ` +" +
+ + + + + +
+" +`; + +exports[`Buttons has focus on button content 1`] = ` +" + +" +`; diff --git a/packages/e2e-tests/specs/blocks/button.test.js b/packages/e2e-tests/specs/blocks/buttons.test.js similarity index 75% rename from packages/e2e-tests/specs/blocks/button.test.js rename to packages/e2e-tests/specs/blocks/buttons.test.js index 69343633f4247..5ab4892bc44ca 100644 --- a/packages/e2e-tests/specs/blocks/button.test.js +++ b/packages/e2e-tests/specs/blocks/buttons.test.js @@ -7,23 +7,24 @@ import { createNewPost, } from '@wordpress/e2e-test-utils'; -describe( 'Button', () => { +describe( 'Buttons', () => { beforeEach( async () => { await createNewPost(); } ); it( 'has focus on button content', async () => { - await insertBlock( 'Button' ); + await insertBlock( 'Buttons' ); await page.keyboard.type( 'Content' ); expect( await getEditedPostContent() ).toMatchSnapshot(); } ); it( 'can jump focus back & forth', async () => { - await insertBlock( 'Button' ); + await insertBlock( 'Buttons' ); await page.keyboard.type( 'WordPress' ); await page.keyboard.press( 'Tab' ); - await page.keyboard.type( 'https://wordpress.org' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( 'Button!' ); expect( await getEditedPostContent() ).toMatchSnapshot(); } );