Skip to content

Commit

Permalink
WIP Show only active tab
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed Jul 18, 2024
1 parent eb97843 commit 5ba5162
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/tab/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"inserter": false,
"reusable": false
},
"usesContext": [ "tabs/activeTab" ],
"editorScript": "file:./build/index.js",
"style": "file:./build/style.css"
}
29 changes: 14 additions & 15 deletions packages/block-library/src/tab/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div { ...useBlockProps() }>
<RichText
className="wp-block-tab__title"
tagName="div"
value={ title }
onChange={ ( newTitle ) =>
setAttributes( { title: newTitle } )
}
placeholder={ __( 'Add text…' ) }
<div
{ ...useBlockProps( {
className: isActive ? 'is-active' : '',
} ) }
>
<InnerBlocks
defaultBlock={ [
'core/paragraph',
{ placeholder: __( 'Enter tab content…' ) },
] }
directInsert
/>
<div className="wp-block-tab__content">
<InnerBlocks />
</div>
</div>
);
}
1 change: 0 additions & 1 deletion packages/block-library/src/tab/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default function save( { attributes } ) {

return (
<div { ...useBlockProps.save() }>
<div className="wp-block-tab__title">{ title }</div>
<div className="wp-block-tab__content">
<InnerBlocks.Content />
</div>
Expand Down
15 changes: 12 additions & 3 deletions packages/block-library/src/tabs/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
}
42 changes: 38 additions & 4 deletions packages/block-library/src/tabs/edit.js
Original file line number Diff line number Diff line change
@@ -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 (
<div { ...useBlockProps() }>
<InnerBlocks />
<div { ...useBlockProps() } data-wp-interactive="core/tabs">
<div className="wp-block-tabs__tab-titles">
{ innerBlocks.map( ( block, index ) => (
<button
key={ block.clientId }
className={ `wp-block-tabs__tab-title ${
index === activeTab ? 'is-active' : ''
}` }
onClick={ () => setActiveTab( index ) }
>
{ block.attributes.title }
</button>
) ) }
</div>
<div className="wp-block-tabs__tab-content">
<InnerBlocks />
</div>
</div>
);
}

0 comments on commit 5ba5162

Please sign in to comment.