I am working to develop a custom block that allows the user to add tabs, and then populate each tab with as much content as desired using other gutenberg blocks. Its my understanding that there can only be a single instance of InnerBlocks in the edit / save function - even though that seems at first like how you would implement the functionality I am looking for (a single instance of InnerBlocks.Content for each tab.
So far, I have been unable to find a good resource on the high level methodology for implementing tabs or columns as a custom gutenberg block. I have reviewed some of the wordpress block library column code, however I find it to be pretty complex and abstract for beginner development of custom blocks.
I am hoping to receive guidance from a high level on how one would achieve the functionality I am looking for. Below I have included my very simple tabs block code for reference.
const { __ } = window.wp.i18n;
const { registerBlockType } = window.wp.blocks;
const { InnerBlocks, RichText } = window.wp.blockEditor;
registerBlockType('myplugin/tabs', {
title: __('Tabs', 'myplugin'),
category: 'widgets',
attributes: {
tabs: {
type: 'array',
default: []
}
},
edit: function ({
className,
attributes,
setAttributes,
}) {
let { tabs } = attributes;
return (
<div className={className}>
<button onClick={() => setAttributes({ tabs: [...tabs, { title: 'New Tab' }] })}>Add a tab</button>
{tabs.map((tab, i) => {
return (
<div key={i}>
<div>
<RichText
tagName="div"
value={tab.title}
onChange={(value) => {
tabs[i].title = value;
setAttributes({ tabs })
}}
/>
</div>
<div>
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
/>
</div>
</div>
)
})}
</div>
)
},
save: function ({
attributes,
}) {
const { tabs } = attributes;
return (
<div>
{tabs.map((tab, i) => {
return (
<div key={i}>
<div>
{tab.title}
</div>
<div>
<InnerBlocks.Content />
</div>
</div>
)
})}
</div>
)
}
});
The pattern for creating blocks with multiple nested children is as follows. First define 2 block types, a parent and a child. Gutenberg core Block Columns is an example of this, there is "Columns Block" as the parent and then there is "Column Block" as the child block.
The use of the parent and child block enable us as developers to work around the restriction of only 1 InnerBlock region per block. It enables the structure using tabs as an example where we have:
/tabs-parent-block
/tab-child-block-1
/tab-child-block-2
/tab-child-block-3
These child blocks could still be within 1 InnerBlocks section which enables the user to add more child blocks. You can restrict the block types for child block to your custom tab child block using the <InnerBlocks allowedBlockType={ ALLOWED_BLOCK_TYPES } />.
What about a situation where putting all the child blocks in one area isn't practical? That is something I'm still experimenting with myself. It doesn't seem to matter for things like tabs because the parent/child approach should work. But what if it doesn't and you really need to have a parent block that has entirely different regions like for a page layout?
The reference shared in an earlier answer is relevant, this script registers 2 blocks which work together in the parent/child block pattern: https://github.com/Ultimate-Blocks/Ultimate-Blocks/blob/master/src/blocks/tabbed-content/components/tab.js
Notice that the first block registered is a singular tab (child block) ub/tab, the second block registered is the parent block ub/tab-block. In the parent block the save() function saves only the child tabs using <InnerBlocks.Content />.
The way to do this is to define another block which then to include in the parent block.
You can check out this plugin: https://github.com/Ultimate-Blocks/Ultimate-Blocks/tree/master/src/blocks/tabbed-content
Related
I am currently building a form builder with vue3 composition API. The user can add in different types of inputs like text, radio buttons etc into the form before saving the form. The saved form will then render with the appropriate HTML inputs. The user can edit the name of the question, eg Company Name <HTML textInput.
Currently, when the user adds an input type eg,text, the type is saved into an ordered array. I run a v-for through the ordered array and creating a custom component formComponent, passing in the type.
My formComponent renders out a basic text input for the user to edit the name of the question, and a place holder string for where the text input will be displayed. My issue is in trying to save the question text from the parent.
<div v-if="type=='text'">
<input type="text" placeholder="Key in title"/>
<span>Input field here</span>
</div>
I have an exportForm button in the parent file that when pressed should ideally return an ordered array of toString representations of all child components. I have tried playing with $emit but I have issue triggering the $emit on all child components from the parent; if I understand, $emit was designed for a parent component to listen to child events.
I have also tried using $refs in the forLoop. However, when I log the $refs they give me the div elements.
<div v-for="item in formItems" ref="formComponents">
<FormComponent :type="item" />
</div>
The ideal solution would be to define a method toString() inside each of the child components and have a forLoop running through the array of components to call toString() and append it to a string but I am unable to do that.
Any suggestions will be greatly appreciated!
At first:
You don't really need to access the child components, to get their values. You can bind them dynamically on your data. I would prefer this way, since it is more Vue conform way to work with reactive data.
But I have also implemented the other way you wanted to achieve, with accessing the child component's methods getValue().
I would not suggest to use toString() since it can be confused with internal JS toString() function.
In short:
the wrapping <div> is not necessary
the refs should be applied to the <FormComponents> (see Refs inside v-for)
this.$refs.formComponents returns the Array of your components
FormComponent is used here as <form-components> (see DOM Template Parsing Caveats)
The values are two-way bound with Component v-model
Here is the working playground with the both ways of achieving your goal.
Pay attention how the values are automatically changing in the FormItems data array.
const { createApp } = Vue;
const FormComponent = {
props: ['type', 'modelValue'],
emits: ['update:modelValue'],
template: '#form-component',
data() {
return { value: this.modelValue }
},
methods: {
getValue() {
return this.value;
}
}
}
const App = {
components: { FormComponent },
data() {
return {
formItems: [
{ type: 'text', value: null },
{ type: 'checkbox', value: false }
]
}
},
methods: {
getAllValues() {
let components = this.$refs.formComponents;
let values = [];
for(var i = 0; i < components.length; i++) {
values.push(components[i].getValue())
}
console.log(`values: ${values}`);
}
}
}
const app = createApp(App)
app.mount('#app')
#app { line-height: 2; }
[v-cloak] { display: none; }
label { font-weight: bold; }
th, td { padding: 0px 8px 0px 8px; }
<div id="app">
<label>FormItems:</label><br/>
<table border=1>
<thead><tr><th>#</th><th>Item Type:</th><th>Item Value</th></tr></thead>
<tbody><tr v-for="(item, index) in formItems" :key="index">
<td>{{index}}</td><td>{{item.type}}</td><td>{{item.value}}</td>
</tr></tbody>
</table>
<hr/>
<label>FormComponents:</label>
<form-component
v-for="(item, index) in formItems"
:type="item.type" v-model="item.value" :key="index" ref="formComponents">
</form-component>
<button type="button" #click="getAllValues">Get all values</button>
</div>
<script src="https://unpkg.com/vue#3/dist/vue.global.prod.js"></script>
<script type="text/x-template" id="form-component">
<div>
<label>type:</label> {{type}},
<label>value:</label> <input :type='type' v-model="value" #input="$emit('update:modelValue', this.type=='checkbox' ? $event.target.checked : $event.target.value)" />
</div>
</script>
I'm using scoped CSS with https://github.com/gaoxiaoliangz/react-scoped-css and am trying to follow the following rules (besides others):
Scoped component CSS should only include styles that manipulate the "inside" of the component. E.g. manipulating padding, background-color etc. is fine whilst I try to stay away from manipulating stuff like margin, width, flex etc. from within the component CSS
Manipulating the "outside" of a component (margin, width, flex etc.) should only be done by "consuming" or parent components
This is rule is somewhat derived from some of the ideas behind BEM (and probably other CSS methodologies as well) and allows for a rather modular system where components can be used without "touching their outside" but letting the parent decide how their internal layouts etc. works.
Whilst this is all fine in theory, I don't really know how to best manipulate the "outside styles" of a component from the consuming code which is best shown with an example:
search-field.scoped.css (the component)
.input-field {
background: lightcoral;
}
search-field.tsx (the component)
import './search-field.scoped.css';
type SearchFieldProps = {
className: string;
};
export const SearchField = (props: SearchFieldProps) => {
return <input className={`input-field ${props.className}`} placeholder="Search text" />;
};
sidebar.scoped.css (the consumer)
.sidebar-search-field {
margin: 16px;
}
sidebar.tsx (the consumer)
import './sidebar.scoped.css';
// ...
export const Sidebar = () => {
return (
<SearchField className="sidebar-search-field" />
(/* ... */)
);
};
In the above example, the CSS from the class sidebar-search-field in sidebar.scoped.css is not applied because the class passed to SearchField is scoped to the Sidebar and the final selector .sidebar-search-field[data-sidebarhash] simply doesn't match as the input element of the SearchField (obviously) doesn't have the data attribute data-sidebarhash but data-searchfieldhash.
ATM, I tend to create wrapper elements in situations like this which works but is rather cumbersome & clutters the markdown unnecessarily:
// ...
export const Sidebar = () => {
return (
<div className="sidebar-search-field">
<SearchField />
</div>
(/* ... */)
);
};
Question
Is there any way to "style scoped CSS component from the outside"?
Ps.: I'm not sure if all the above also applies to scoped styles in Vue. If not, please let me know how it works there so that I can create a feature request in https://github.com/gaoxiaoliangz/react-scoped-css.
When Gutenberg creates a class, it seems to be of the format
div.wp-block
div.editor-block-list__insertion-point
div.editor-block-list__block-edit
div.editor-block-contextual-toolbar
div
<your actual block html goes here>
I'd like to be able to add a class to that top div.wp-block element so I can properly style my block in the editor. The class is dynamically generated based on an attribute so I can't just use the block name class. Is there a clean way of doing this? I can hack it using javascript DOM, but it gets overwritten quickly enough.
https://wordpress.org/gutenberg/handbook/designers-developers/developers/filters/block-filters/#editor-blocklistblock
const { createHigherOrderComponent } = wp.compose
const withCustomClassName = createHigherOrderComponent((BlockListBlock) => {
return props => {
return <BlockListBlock { ...props } className={ 'my-custom-class' } />
}
}, 'withCustomClassName')
wp.hooks.addFilter('editor.BlockListBlock', 'my-plugin/with-custom-class-name', withCustomClassName)
You can add class in your block edit view by using className that is present in this.props, className will print class in following format wp-blocks-[block_name]
edit( { className } ) { // using destructing from JavaScript ES-6
return <div className={ className }></div>
}
Suggestion
Always try to look for manipulating DOM via React instead of manipulating DOM directly because React manages it's own state and issues can occur by manipulating DOM directly.
I like how the columns block works because it supports nesting. What I'm trying to do is similar to making the column block support 1 column (which you can force it to do, but it doesn't size correctly. This way, the entire block of multiple blocks and block types can be saved as a single reusable block instance.
Is this an instance where I need to start working on my own custom block type? Or will this be supported at some point?
What you need is InnerBlock. You can basically, group couple of blocks into a single custom block.
import { registerBlockType } from '#wordpress/blocks';
import { InnerBlocks } from '#wordpress/editor';
registerBlockType( 'my-plugin/my-block', {
// ...
edit( { className } ) {
return (
<div className={ className }>
<InnerBlocks />
</div>
);
},
save() {
return (
<div>
<InnerBlocks.Content />
</div>
);
}
} );
Btw, this is the main focus of Gutenberg Phase 2 development. And, Gutenberg will slowly take over into template building process.
I'm using the following markup to mark the clicked component as active.
<div *ngFor="let menu of menus;"
(click)="onClick($event,menu.link)"
[ngClass]="{'active':menu.active}">
{{menu.title}}
</div>
The method handling the click is as follows.
onClick(target, link) {
target.active = !target.active;
this.router.navigate([{ outlets: { primary: [""], menus: [link] } }]);
}
It seems that the value of target.active goes from undefined to true to false to true etc. but the style doesn't get set. (I'm printing out the whole component to the console and can't see the addition of the class' name.)
Question: What am I missing in this approach?
NB, I know how to resolve it by approaching it from a different angle. I set up a local variable keeping the index and setting it like shown here. The aim of my question is to learn to achieve the requested behavior in a more like-a-bossy way.
target here:
onClick(target, link) {
target.active = !target.active; <------------
this.router.navigate([{ outlets: { primary: [""], menus: [link] } }]);
}
doesn't refer to menu, it refers to the event. But based on your ngClass directive:
[ngClass]="{'active':menu.active}">
You need to set active to menu variable and so it can be done like this:
<div *ngFor="let menu of menus;"
(click)="onClick(menu,menu.link)"
[ngClass]="{'active':menu.active}">
{{menu.title}}
</div>
Instead of passing in the $event, send it the actual menu object. Like this:
<div *ngFor="let menu of menus;"
(click)="onClick(menu)"
[ngClass]="{'active':menu.active}">
{{menu.title}}
</div>
And in the component:
onClick(menu) {
menu.active = !menu.active;
this.router.navigate([{ outlets: { primary: [""], menus: [menu.link] } }]);
}