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
2 changes: 1 addition & 1 deletion build/toggle-block/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n'), 'version' => '258f20c46a12ec82d3d1');
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-hooks', 'wp-i18n'), 'version' => '1b8472592ef953dd9320');
2 changes: 1 addition & 1 deletion build/toggle-block/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/toggle-block/extend-navigation-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { addFilter } from '@wordpress/hooks';

/**
* Add the toggle block to the list of allowed blocks in the navigation block.
*
* @param {object} settings Settings for the navigation block.
* @param {string} name Name of the block.
* @returns {object} Settings for the navigation block.
*/
function addToggleBlockToNavigation(settings, name) {
if (name !== 'core/navigation') {
return settings;
}

const allowedBlocks = settings.allowedBlocks || [];

return {
...settings,
allowedBlocks: [...allowedBlocks, 'happyprime/toggle-block'],
};
}

addFilter(
'blocks.registerBlockType',
'happyprime/add-toggle-to-navigation',
addToggleBlockToNavigation
);
39 changes: 38 additions & 1 deletion src/toggle-block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import {
RichText,
useBlockProps,
} from '@wordpress/block-editor';
import { registerBlockType } from '@wordpress/blocks';
import { createBlock, registerBlockType } from '@wordpress/blocks';
import { PanelBody, TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

// Internal dependencies.
import metadata from './block.json';

// Extend the navigation block to allow the toggle block.
import './extend-navigation-block';

const Edit = (props) => {
const {
attributes: { bodyClass, buttonText, controlsId, labelText },
Expand Down Expand Up @@ -87,7 +90,41 @@ const Save = (props) => {
);
};

const Transforms = {
from: [
{
type: 'block',
blocks: ['core/navigation-link'],
transform: (attributes, innerBlocks) => {
return createBlock(
'happyprime/toggle-block',
{
buttonText: attributes.label || attributes.title || '',
},
innerBlocks
);
},
},
],
to: [
{
type: 'block',
blocks: ['core/navigation-link'],
transform: (attributes, innerBlocks) => {
return createBlock(
'core/navigation-link',
{
label: attributes.buttonText || '',
url: '#',
},
innerBlocks
);
},
},
],
};
registerBlockType(metadata, {
edit: Edit,
save: Save,
transforms: Transforms,
});