Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
345 changes: 345 additions & 0 deletions docs/block-components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
# Tools

To simplify block customization and ensure a consistent experience for users, there are a number of built-in UI patterns to help generate the editor preview. These common tools are offered under the `wp.blocks` global. In this section, we'll explore `editable`, `toolbars`, and the block `inspector`.

## Editable

At the core of rich text formatting in Gutenberg lies the `Editable` component. It's a wrapper and controller around the TinyMCE library, and can be used by any block to power any rich text editing features. It us used for captions under images or embeds, for the separate fields in a quote, etc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo, It is used for captions rather than It us used for captions


<ul>
<li><strong>Type:</strong> <code>Function</code></li>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Technically a function, but more specifically a Component class.

</ul>

{% codetabs %}
{% ES5 %}
```js
var Editable = wp.blocks.Editable;

// Within registerBlockType
edit: function( props ) {
var content = props.attributes.content;

function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}

return el(
<Editable,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove leading <

{
tagName: 'p',
value: content,
onChange: onChangeContent,
}
);
}
```
{% ESNext %}
```js
const Editable = wp.blocks.Editable;

// Within registerBlockType
edit( { attributes } ) {
const { content } = attributes;

return (
<Editable
tagName="p"
value={ attributes.content }
onChange={ ( value ) => setAttributes( { content: value } ) }
/>
);
}
```
{% end %}

This is a very straighforward usage,

```js
import Editable from '../../editable';

edit( { attributes, className, focus } ) {
return (
<div className={ className }>
<p>{ attributes.content }</p>
<Editable
tagName="figcaption"
placeholder={ __( 'Write caption…' ) }
value={ caption }
focus={ focus && focus.editable === 'caption' ? focus : undefined }
onFocus={ focusCaption }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

straightforward 😬

onChange={ ( value ) => setAttributes( { caption: value } ) }
inlineToolbar
/>
</div>
);
}
```

`Editable` is quite powerful and can be configured through different props and events.

### tagName (optional)

...

### value

...

### placeholder (optional)

You can use this property to render placeholder text in the field.

### inlineToolbar

This is a `boolean` property.

### focus

...

### onChange

...

### onFocus

...

## Toolbar

<img src="https://cldup.com/jUslj672CK.png" width="391" height="79" alt="toolbar">

When the user selects a block, a number of control buttons may be shown in a toolbar above the selected block. Some of these block-level controls are included automatically if the editor is able to transform the block to another type, or if the focused element is an Editable component.

### BlockControls

You can also customize the toolbar to include controls specific to your block type. If the return value of your block type's `edit` function includes a `BlockControls` element, those controls will be shown in the selected block's toolbar.

{% codetabs %}
{% ES5 %}
```js
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
Editable = wp.blocks.Editable,
BlockControls = wp.blocks.BlockControls,
AlignmentToolbar = wp.blocks.AlignmentToolbar,
children = wp.blocks.source.children;

registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-04', {
title: 'Hello World (Step 4)',

icon: 'universal-access-alt',

category: 'layout',

attributes: {
content: {
type: 'array',
source: children( 'p' )
}
},

edit: function( props ) {
var content = props.attributes.content,
alignment = props.attributes.alignment,
focus = props.focus;

function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}

function onChangeAlignment( newAlignment ) {
props.setAttributes( { alignment: newAlignment } );
}

return [
!! focus && el(
BlockControls,
{ key: 'controls' },
el(
AlignmentToolbar,
{
value: alignment,
onChange: onChangeAlignment
}
)
),
el(
Editable,
{
key: 'editable',
tagName: 'p',
className: props.className,
style: { textAlign: alignment },
onChange: onChangeContent,
value: content,
focus: focus,
onFocus: props.setFocus
}
)
];
},

save: function( props ) {
var content = props.attributes.content;

return el( 'p', { className: props.className }, content );
},
} );
```
{% ESNext %}
```js
const {
registerBlockType,
Editable,
BlockControls,
AlignmentToolbar,
source
} = wp.blocks;
const { children } = source;

registerBlockType( 'gutenberg-boilerplate-esnext/hello-world-step-04', {
title: 'Hello World (Step 4)',

icon: 'universal-access-alt',

category: 'layout',

attributes: {
content: {
type: 'array',
source: children( 'p' ),
},
},

edit( { attributes, setAttributes, focus } ) {
const { content, alignment } = attributes;

function onChangeContent( newContent ) {
setAttributes( { content: newContent } );
}

function onChangeAlignment( newAlignment ) {
setAttributes( { alignment: newAlignment } );
}

return [
!! focus && (
<BlockControls key="controls">
<AlignmentToolbar
value={ alignment }
onChange={ onChangeAlignment }
/>
</BlockControls>
),
<Editable
key="editable"
tagName="p"
className={ props.className }
style={ { textAlign: alignment } }
onChange={ onChangeContent }
value={ content }
focus={ focus }
onFocus={ props.setFocus }
/>
];
},

save( { attributes, className } ) {
const { content } = attributes;

return <p className={ className }>{ content }</p>;
},
} );
```
{% end %}

Note that you should only include `BlockControls` if the block is currently selected. We must test that the `focus` value is truthy before rendering the element, otherwise you will inadvertently cause controls to be shown for the incorrect block type.

### BlockAlignmentToolbar

...

## Inspector

While the toolbar area is useful for displaying controls to toggle attributes of a block, sometimes a block needs to include extra functionality and settings that are not appropriate to show on the block itself or that require more screen space. Gutenberg includes a block inspector region in the sidebar for this purpose.

### InspectorControls

Similar to rendering a toolbar, if you include an `InspectorControls` element in the return value of your block type's `edit` function, those controls will be shown in the inspector region. All the children passed to this component will appear in the sidebar when the tab and the corresponding block are selected.

### BlockDescription

A block can display a short text describing what its purpose is. It is recommended to always include one when using the inspector.

# Components

In order to make it easier to create sophisticated blocks quickly, reducing the amount of work you have to do to develop common functionality, several components have been created which can be accessed through the `wp.components` global. This section covers the available UI components.

## Placeholder

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just an idea, should we maybe keep the component related docs in their own README files and only cross-link them in here with a very brief description?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd prefer to have them here as this will easier to navigate and refer to.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A middle-ground could be to create a separate page for each component using its README


Certain blocks, like media ones, have a state where they don't yet have user data filled but could be rendering something.

## CheckboxControl

...

## RadioControl

...

## RangeControl

...

## SelectControl

...

## TextControl

...

## TextareaControl

...

## ToggleControl

...

## MediaUploadButton

...

## FormFileUpload

...

## ColorPalette

...

## DropdownMenu

...

## Button

...

## IconButton

...

## Popover

...

## Tooltip

...

## Dashicon

...
6 changes: 6 additions & 0 deletions docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ addStory( {
markdown: require( './block-edit-save.md' ),
} );

addStory( {
name: 'components',
title: 'Components',
markdown: require( './block-components.md' ),
} );

addStory( {
name: 'blocks',
title: 'Creating Block Types',
Expand Down