From 5ba516261a79b0720ed779f3ffbc16609ccdf229 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Wed, 17 Jul 2024 22:02:35 -0500 Subject: [PATCH] WIP Show only active tab --- packages/block-library/src/tab/block.json | 1 + packages/block-library/src/tab/edit.js | 29 ++++++++------- packages/block-library/src/tab/save.js | 1 - packages/block-library/src/tabs/block.json | 15 ++++++-- packages/block-library/src/tabs/edit.js | 42 +++++++++++++++++++--- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/packages/block-library/src/tab/block.json b/packages/block-library/src/tab/block.json index 402b6257247828..802c014d5579af 100644 --- a/packages/block-library/src/tab/block.json +++ b/packages/block-library/src/tab/block.json @@ -18,6 +18,7 @@ "inserter": false, "reusable": false }, + "usesContext": [ "tabs/activeTab" ], "editorScript": "file:./build/index.js", "style": "file:./build/style.css" } diff --git a/packages/block-library/src/tab/edit.js b/packages/block-library/src/tab/edit.js index cb06dc2045fa60..45b75750e156af 100644 --- a/packages/block-library/src/tab/edit.js +++ b/packages/block-library/src/tab/edit.js @@ -4,23 +4,22 @@ import { InnerBlocks, RichText, useBlockProps } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; -export default function Edit( { attributes, setAttributes } ) { - const { title } = attributes; - +export default function Edit( { context } ) { + console.log( context ); + const isActive = context[ 'tabs/activeTab' ]; return ( -
- - setAttributes( { title: newTitle } ) - } - placeholder={ __( 'Add text…' ) } +
+ -
- -
); } diff --git a/packages/block-library/src/tab/save.js b/packages/block-library/src/tab/save.js index e7fafd12668f98..b78ba5bb38bddf 100644 --- a/packages/block-library/src/tab/save.js +++ b/packages/block-library/src/tab/save.js @@ -8,7 +8,6 @@ export default function save( { attributes } ) { return (
-
{ title }
diff --git a/packages/block-library/src/tabs/block.json b/packages/block-library/src/tabs/block.json index b3bc49af3ea061..154ba0e9e6ada8 100644 --- a/packages/block-library/src/tabs/block.json +++ b/packages/block-library/src/tabs/block.json @@ -8,10 +8,19 @@ "textdomain": "default", "__experimental": true, "allowedBlocks": [ "core/tab" ], - "attributes": {}, + "attributes": { + "activeTab": { + "type": "number", + "default": 0 + } + }, + "providesContext": { + "tabs/activeTab": "activeTab" + }, "supports": { "align": [ "wide", "full" ], "html": false, + "interactivity": true, "layout": { "default": { "type": "flex", "orientation": "horizontal" }, "allowSwitching": false, @@ -23,6 +32,6 @@ "padding": true } }, - "editorScript": "file:./build/index.js", - "style": "file:./build/style.css" + "editorStyle": "wp-block-tabs-editor", + "style": "wp-block-tabs" } diff --git a/packages/block-library/src/tabs/edit.js b/packages/block-library/src/tabs/edit.js index 25fc9b40db6a1d..481479b15f0550 100644 --- a/packages/block-library/src/tabs/edit.js +++ b/packages/block-library/src/tabs/edit.js @@ -1,12 +1,46 @@ /** * WordPress dependencies */ -import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; +import { + InnerBlocks, + useBlockProps, + useInnerBlocksProps, + store as blockEditorStore, +} from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; +import { useState } from '@wordpress/element'; +import clsx from 'clsx'; + +export default function Edit( { attributes, clientId, setAttributes } ) { + const { activeTab } = attributes; + const innerBlocks = useSelect( + ( select ) => select( blockEditorStore ).getBlocks( clientId ), + [ clientId ] + ); + + const setActiveTab = ( index ) => { + setAttributes( { activeTab: index } ); + }; -export default function Edit() { return ( -
- +
+
+ { innerBlocks.map( ( block, index ) => ( + + ) ) } +
+
+ +
); }