Skip to content

Commit

Permalink
Attempts to add interactivity API to show tab on click
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed Jul 29, 2024
1 parent 3a27615 commit a5b02b9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/tab/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function save( { attributes } ) {
<div
{ ...innerBlocksProps }
aria-labelledby={ tabLabelId }
data-wp-class--is-active={ `${ tabIndex } === context.activeTab` }
id={ tabPanelId }
role="tabpanel"
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/tabs/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
}
},
"editorStyle": "wp-block-tabs-editor",
"style": "wp-block-tabs"
"style": "wp-block-tabs",
"viewScriptModule": "file:./view.min.js"
}
10 changes: 8 additions & 2 deletions packages/block-library/src/tabs/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export default function save( { attributes } ) {
} );

return (
<div { ...blockProps }>
<div
{ ...blockProps }
data-wp-interactive="core/tabs"
data-wp-context='{ "activeTab": 0 }'
>
<ul className="wp-block-tabs__list" role="tablist">
{ innerTabs.map( ( tab, index ) => {
const isActive = index === 0;
Expand All @@ -34,7 +38,9 @@ export default function save( { attributes } ) {
>
<RichText.Content
aria-controls={ tab.id }
aria-selected={ isActive }
data-wp-bind--aria-selected={ `${ index } === context.activeTab` }
data-wp-class--is-active={ `${ index } === context.activeTab` }
data-wp-on--click={ `actions.setActiveTab.bind( null, ${ index } )` }
className={ clsx( 'wp-block-tabs__tab-label', {
'is-active': isActive,
} ) }
Expand Down
13 changes: 13 additions & 0 deletions packages/block-library/src/tabs/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* WordPress dependencies
*/
import { store, getContext } from '@wordpress/interactivity';

store( 'core/tabs', {
actions: {
setActiveTab: ( index ) => {
const context = getContext();
context.activeTab = index;
},
},
} );

0 comments on commit a5b02b9

Please sign in to comment.