ワードプレスの始め方
~稼ぐためのしくみづくりまでを
徹底解説!!

好評セミナー”ワードプレス100分でブログサイトを制作”を書籍化しました

amazonでたった1コインで手に入れる

ワードプレスの始め方
~稼げるしくみづくりまで
書籍で徹底解説!

詳細はこちら

amazonでたった1コインで手に入れる
  • ブログ型サイト
  • 企業ブランディング・集客ページ
  • 飲食店や医院用サイト
  • 各種LP制作

・・・などなど
ワードプレスを使ったサイト制作や
コーディングのみも承ります。

お気軽にお問い合わせください。

※本サイトで紹介する商品は提携先アフィリエイトリンク付き(PR)の場合がございます。

ブロック作成③【BlockControlを使ってツールバーにAlignmentToolbarを設置】

ワードプレスのブロックの作成方法③として、BlockControlコンポーネントを使って、ブロックツールバーにテキスト配置調整用のAlignmentToolbarを設置します。

本記事で作成するブロック

前回までで作成したRichTextコンポーネントによるブロックのツールバーにアライメントボタンを実装します。

AlignmentToolbarによるブロックツールバーへのアライメントボタン実装

コードの変更点

srcフォルダのblock.json、edit.js、save.jsを変更します。

block.json

変更前のblock.json

{
	"$schema": "https://json.schemastore.org/block.json",
	"apiVersion": 2,
	"name": "create-block/richtext",
	"version": "0.1.0",
	"title": "Richtext",
	"category": "text",
	"icon": "welcome-write-blog",
	"description": "RichTextを使ってブロックを実装",
	"supports": {
		"className": false,
		"html": false
	},
	"styles": [
		{
			"name": "default",
			"label": "デフォルト",
			"isDefault": true
		},
		{
			"name": "red-border",
			"label": "赤枠"
		},
		{
			"name": "blue-border",
			"label": "青枠"
		}
	],
	"attributes": {
		"content": {
			"type": "string",
			"source": "html",
			"selector": ".my-richtext"
		},
		"color": {
			"type": "string",
			"default": ""
		}
	},
	"textdomain": "richtext",
	"editorScript": "file:./index.js",
	"editorStyle": "file:./index.css",
	"style": "file:./style-index.css"
}

変更後のblock.json

AlignmentToolbar用のプロパティとしてattributes.alignmentを追加。

{
	"$schema": "https://json.schemastore.org/block.json",
	"apiVersion": 2,
	"name": "create-block/richtext",
	"version": "0.1.0",
	"title": "Richtext",
	"category": "text",
	"icon": "welcome-write-blog",
	"description": "RichTextを使ってブロックを実装",
	"supports": {
		"className": false
	},
	"styles": [
		{
			"name": "default",
			"label": "デフォルト",
			"isDefault": true
		},
		{
			"name": "red-border",
			"label": "赤枠"
		},
		{
			"name": "blue-border",
			"label": "青枠"
		}
	],
	"attributes": {
		"content": {
			"type": "string",
			"source": "html",
			"selector": ".my-richtext"
		},
		"color": {
			"type": "string",
			"default": ""
		},
		"alignment": {
			"type": "string",
			"default": "none"
		}
	},
	"textdomain": "richtext",
	"editorScript": "file:./index.js",
	"editorStyle": "file:./index.css",
	"style": "file:./style-index.css"
}

edit.js

変更前のedit.js

import { __ } from '@wordpress/i18n';
import { InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor';
import { PanelBody, SelectControl } from '@wordpress/components';
import './editor.scss';

export default function Edit(props) {
	const { attributes, setAttributes } = props;
	const blockProps = useBlockProps({
		tagName: "div",
		className: "my-richtext",
		value: attributes.content,
		placeholder: "ここにテキストを入力・・・",
		style: { color: attributes.color || null },
		onChange: (newContent) => {
			setAttributes({ content: newContent });
		}
	});
	return (
		<>
			<RichText {...blockProps} />
			<InspectorControls>
				<PanelBody title="色の選択">
					<SelectControl
						value = { attributes.color }
						options = { [
							{value: '', label: 'カラーを選択'},
							{value: 'red', label: '赤色'},
							{value: 'blue', label: '青色'}
						] }
						onChange = { (newColor) => {
							setAttributes({ color: newColor });
						}}
					/>
				</PanelBody>
			</InspectorControls>
		</>
	);
}

変更後のedit.js

  • <BlockControl>とその下位に<AlignmentToolbar>を配置
  • <RichText>のstyle属性にtextAlignを追加してAlignmentToolbarから渡されたスタイルを反映させる
import { __ } from '@wordpress/i18n';
import { 
	AlignmentToolbar, 
	BlockControls, 
	InspectorControls, 
	RichText, 
	useBlockProps 
} from '@wordpress/block-editor';
import { 
	PanelBody, 
	SelectControl 
} from '@wordpress/components';
import './editor.scss';

export default function Edit(props) {
	const { attributes, setAttributes } = props;
	const blockProps = useBlockProps({
		tagName: "div",
		className: "my-richtext",
		value: attributes.content,
		placeholder: "ここにテキストを入力・・・",
		style: {
			color: attributes.color || null,
			textAlign: attributes.alignment || null
		},
		onChange: (newContent) => {
			setAttributes({ content: newContent });
		}
	});
	return (
		<>
			<RichText {...blockProps} />
			<BlockControls>
				<AlignmentToolbar
					value = { attributes.alignment }
					onChange = { ( newAlignment ) => {
						setAttributes({ alignment: newAlignment });
					} }
				/>
			</BlockControls>
			<InspectorControls>
				<PanelBody title="色の選択">
					<SelectControl
						value = { attributes.color }
						options = { [
							{value: '', label: 'カラーを選択'},
							{value: 'red', label: '赤色'},
							{value: 'blue', label: '青色'}
						] }
						onChange = { (newColor) => {
							setAttributes({ color: newColor });
						} }
					/>
				</PanelBody>
			</InspectorControls>
		</>
	);
}

save.js

変更前のsave.js

import { __ } from '@wordpress/i18n';
import { RichText, useBlockProps } from '@wordpress/block-editor';

export default function save(props) {
	const{ attributes } = props;
	const blockProps = useBlockProps.save({
		tagName: "div",
		className: "my-richtext",
		value: attributes.content,
		style: { color: attributes.color || null }
	});
	return (
		<RichText.Content {...blockProps} />
	);
}

変更後のsave.js

<RichText.Content />のstyle属性にtextAlignを追加してAlignmentToolbarから渡されたスタイルを反映させる

import { __ } from '@wordpress/i18n';
import { RichText, useBlockProps } from '@wordpress/block-editor';

export default function save(props) {
	const{ attributes } = props;
	const blockProps = useBlockProps.save({
		tagName: "div",
		className: "my-richtext",
		value: attributes.content,
		style: {
			color: attributes.color || null,
			textAlign: attributes.alignment || null
		}
	});
	return (
		<RichText.Content {...blockProps} />
	);
}

以上でブロックツールバー内にアラインメント用ボタンが実装され、選んだアライメントスタイルがフロント側にも反映されます。

ワードプレスの始め方
~稼ぐためのしくみづくりまでを
徹底解説!!

好評セミナー”ワードプレス100分でブログサイトを制作”を書籍化しました

amazonでたった1コインで手に入れる