Skip to content
Merged
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
6 changes: 0 additions & 6 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1145,12 +1145,6 @@
"markdown_source": "../packages/block-library/src/tab-panels/README.md",
"parent": "core-blocks-design"
},
{
"title": "Tab",
"slug": "core-block-tab",
"markdown_source": "../packages/block-library/src/tab/README.md",
"parent": "core-blocks-design"
},
{
"title": "Table of Contents",
"slug": "core-block-table-of-contents",
Expand Down
12 changes: 1 addition & 11 deletions docs/reference-guides/core-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -998,16 +998,6 @@ Add white space between blocks and customize its height.
- **Supports:** anchor, interactivity (clientNavigation), spacing (margin)
- **Attributes:** height, width

## Tab

A single tab button in the tab list.

- **Name:** [core/tab](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tab/)
- **Experimental:** true
- **Category:** [design](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/)
- **Parent:** core/tab-list
- **Supports:** color (background, text), spacing (padding), typography (fontSize, textAlign), ~~html~~, ~~lock~~, ~~reusable~~, ~~visibility~~

## Tab List

Display the tab buttons for a tabbed interface.
Expand All @@ -1016,8 +1006,8 @@ Display the tab buttons for a tabbed interface.
- **Experimental:** true
- **Category:** [design](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/)
- **Parent:** core/tabs
- **Allowed Blocks:** core/tab
- **Supports:** color (background, text), dimensions (~~aspectRatio~~, ~~height~~, ~~minHeight~~, ~~width~~), layout (allowJustification, allowOrientation, allowVerticalAlignment, default, ~~allowSwitching~~), spacing (blockGap, margin, padding), typography (fontSize), ~~html~~, ~~lock~~, ~~reusable~~, ~~visibility~~
- **Attributes:** tabs

## Tab Panel

Expand Down
1 change: 0 additions & 1 deletion docs/reference-guides/core-blocks/category-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
- [Page Break](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-nextpage/) — `core/nextpage`: Separate your content into a multi-page experience.
- [Separator](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-separator/) — `core/separator`: Create a break between ideas or sections with a horizontal separator.
- [Spacer](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-spacer/) — `core/spacer`: Add white space between blocks and customize its height.
- [Tab](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tab/) — `core/tab`: A single tab button in the tab list.
- [Tab List](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tab-list/) — `core/tab-list`: Display the tab buttons for a tabbed interface.
- [Tab Panel](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tab-panel/) — `core/tab-panel`: Content for a tab in a tabbed interface.
- [Tab Panels](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tab-panels/) — `core/tab-panels`: Container for tab panel content in a tabbed interface.
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
@use "./spacer/editor.scss" as *;
@use "./table/editor.scss" as *;
@use "./tab-list/editor.scss" as *;
@use "./tab/editor.scss" as *;
@use "./template-part/editor.scss" as *;
@use "./term-template/editor.scss" as *;
@use "./text-columns/editor.scss" as *;
Expand Down
2 changes: 0 additions & 2 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ import * as siteTitle from './site-title';
import * as socialLink from './social-link';
import * as socialLinks from './social-links';
import * as spacer from './spacer';
import * as tab from './tab';
import * as tabPanel from './tab-panel';
import * as tabPanels from './tab-panels';
import * as table from './table';
Expand Down Expand Up @@ -287,7 +286,6 @@ const getAllBlocks = () => {
}

if ( window?.__experimentalEnableBlockExperiments ) {
blocks.push( tab );
blocks.push( tabList );
blocks.push( tabs );
blocks.push( tabPanel );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
@use "./social-links/style.scss" as *;
@use "./spacer/style.scss" as *;
@use "./tag-cloud/style.scss" as *;
@use "./tab/style.scss" as *;
@use "./tab-list/style.scss" as *;
@use "./tab-panel/style.scss" as *;
@use "./tab-panels/style.scss" as *;
@use "./table/style.scss" as *;
Expand Down
20 changes: 15 additions & 5 deletions packages/block-library/src/tab-list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ Display the tab buttons for a tabbed interface.
**[Parent](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#parent) blocks (direct):**
- [`core/tabs`](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tabs/)

**[Allowed](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#allowed-blocks) inner blocks:**
- [`core/tab`](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-design/core-block-tab/)

## Attributes

_Defined via the [`attributes`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/) property in block.json._

_This block has no custom attributes._
| Attribute | [Type](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#type-validation) | [Default](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#default-value) | Description |
|-----------|------|---------|-------------|
| `tabs` | `array` | `[]` | [Source](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#value-source): `query`. [Selector](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#value-source): `button` |

## Supports

Expand Down Expand Up @@ -61,12 +60,23 @@ _Defined via the [`usesContext` and `providesContext`](https://developer.wordpre

- `core/tabs-list`

## CSS Selectors

_Defined via the [`selectors`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-selectors/) property in block.json._

- **border**: `.wp-block-tab-list button`
- **color**:
- background: `.wp-block-tab-list button`
- text: `.wp-block-tab-list button`
- **spacing**:
- padding: `.wp-block-tab-list button`

## Block Markup

This is a [**hybrid block**](https://developer.wordpress.org/block-editor/getting-started/fundamentals/static-dynamic-rendering/). It saves static markup that the server may enhance during rendering.

```html
<!-- wp:tab-list -->
<!-- wp:tab-list {"tabs":[]} -->
<!-- Content... -->
<!-- /wp:tab-list -->
```
Expand Down
34 changes: 30 additions & 4 deletions packages/block-library/src/tab-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@
"category": "design",
"textdomain": "default",
"parent": [ "core/tabs" ],
"allowedBlocks": [ "core/tab" ],
"usesContext": [ "core/tabs-list" ],
"attributes": {},
"attributes": {
"tabs": {
"type": "array",
"source": "query",
"selector": "button",
"query": {
"label": {
"type": "string",
"source": "html"
}
},
"default": []
}
},
"supports": {
"html": false,
"reusable": false,
Expand All @@ -25,6 +37,7 @@
"color": {
"background": true,
"text": true,
"__experimentalSkipSerialization": true,
"__experimentalDefaultControls": {
"background": true,
"text": true
Expand All @@ -38,7 +51,8 @@
"color": true,
"radius": true,
"style": true,
"width": true
"width": true,
"__experimentalSkipSerialization": true
},
"layout": {
"default": {
Expand All @@ -55,13 +69,25 @@
"padding": true,
"margin": true,
"blockGap": true,
"__experimentalSkipSerialization": [ "padding" ],
"__experimentalDefaultControls": {
"padding": true,
"margin": true,
"blockGap": true
}
}
},
"selectors": {
"border": ".wp-block-tab-list button",
"color": {
"background": ".wp-block-tab-list button",
"text": ".wp-block-tab-list button"
},
"spacing": {
"padding": ".wp-block-tab-list button"
}
},
"editorScript": "file:./index.js",
"editorStyle": "file:./editor.css"
"editorStyle": "file:./editor.css",
"style": "file:./style-index.css"
}
162 changes: 147 additions & 15 deletions packages/block-library/src/tab-list/edit.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,175 @@
/**
* External dependencies
*/
import clsx from 'clsx';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
RichText,
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps,
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo, useCallback, useEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import AddTabToolbarControl from '../tab-panel/add-tab-toolbar-control';
import RemoveTabToolbarControl from '../tab-panel/remove-tab-toolbar-control';

const TAB_LIST_TEMPLATE = [ [ 'core/tab' ], [ 'core/tab' ] ];
const EMPTY_ARRAY = [];

function Edit( { attributes, clientId, context } ) {
const tabsList = context[ 'core/tabs-list' ] || EMPTY_ARRAY;

const colorProps = useColorProps( attributes );
const borderProps = useBorderProps( attributes );
const spacingProps = getSpacingClassesAndStyles( attributes );

function Edit( { clientId } ) {
const tabsClientId = useSelect(
( select ) =>
select( blockEditorStore ).getBlockRootClientId( clientId ),
const { tabsClientId, editorActiveTabIndex, activeTabIndex } = useSelect(
( select ) => {
const { getBlockRootClientId, getBlockAttributes } =
select( blockEditorStore );

const _tabsClientId = getBlockRootClientId( clientId );
const tabsAttributes = _tabsClientId
? getBlockAttributes( _tabsClientId )
: {};

return {
tabsClientId: _tabsClientId,
editorActiveTabIndex: tabsAttributes?.editorActiveTabIndex,
activeTabIndex: tabsAttributes?.activeTabIndex ?? 0,
};
},
[ clientId ]
);

const blockProps = useBlockProps();
const effectiveActiveIndex = useMemo( () => {
return editorActiveTabIndex ?? activeTabIndex;
}, [ editorActiveTabIndex, activeTabIndex ] );

const { __unstableMarkNextChangeAsNotPersistent, updateBlockAttributes } =
useDispatch( blockEditorStore );

const handleTabClick = useCallback(
( tabIndex ) => {
if ( tabsClientId && tabIndex !== effectiveActiveIndex ) {
__unstableMarkNextChangeAsNotPersistent();
updateBlockAttributes( tabsClientId, {
editorActiveTabIndex: tabIndex,
} );
}
},
[
tabsClientId,
effectiveActiveIndex,
updateBlockAttributes,
__unstableMarkNextChangeAsNotPersistent,
]
);

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/tab' ],
orientation: 'horizontal',
template: TAB_LIST_TEMPLATE,
templateLock: false,
renderAppender: false,
const handleLabelChange = useCallback(
( tabIndex, newLabel ) => {
const tab = tabsList[ tabIndex ];
if ( tab?.clientId ) {
updateBlockAttributes( tab.clientId, { label: newLabel } );
}
},
[ tabsList, updateBlockAttributes ]
);

const menuRef = useRef();
const prevTabCountRef = useRef( tabsList.length );

// When tabs are added or removed, focus the appropriate button.
useEffect( () => {
const prevCount = prevTabCountRef.current;
prevTabCountRef.current = tabsList.length;

if ( ! menuRef.current || tabsList.length === prevCount ) {
return;
}

const focusButtonAt = ( index ) => {
window.requestAnimationFrame( () => {
const buttons = menuRef.current?.querySelectorAll( 'button' );
const target = buttons?.[ index ];
if ( ! target ) {
return;
}
const richText = target.querySelector( '[contenteditable]' );
if ( richText ) {
richText.focus();
} else {
target.focus();
}
} );
};

if ( tabsList.length > prevCount ) {
// Tab added — focus the last (newly added) button.
focusButtonAt( tabsList.length - 1 );
} else {
// Tab removed — focus the new active button.
focusButtonAt( effectiveActiveIndex );
}
}, [ tabsList.length, effectiveActiveIndex ] );

const blockProps = useBlockProps( {
role: 'tablist',
ref: menuRef,
} );

const buttonClassName = clsx( colorProps.className, borderProps.className );

const buttonStyle = {
...colorProps.style,
...borderProps.style,
...spacingProps.style,
};

return (
<>
<AddTabToolbarControl tabsClientId={ tabsClientId } />
<RemoveTabToolbarControl tabsClientId={ tabsClientId } />
<div { ...innerBlocksProps } />
<div { ...blockProps }>
{ tabsList.map( ( tab, index ) => {
const isActive = index === effectiveActiveIndex;
return (
<button
key={ tab.clientId || index }
type="button"
className={ clsx( buttonClassName, {
'is-active': isActive,
} ) }
style={ buttonStyle }
tabIndex={ -1 }
onClick={ ( event ) => {
event.preventDefault();
handleTabClick( index );
} }
>
<RichText
tagName="span"
withoutInteractiveFormatting
placeholder={ __( 'Tab title' ) }
value={ tab.label }
onChange={ ( newLabel ) =>
handleLabelChange( index, newLabel )
}
/>
</button>
);
} ) }
</div>
</>
);
}
Expand Down
Loading
Loading