{"id":6012,"date":"2020-08-06T11:16:10","date_gmt":"2020-08-06T09:16:10","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=6012"},"modified":"2020-08-20T13:19:27","modified_gmt":"2020-08-20T11:19:27","slug":"block-full-width-gutenberg","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/block-full-width-gutenberg-6012\/","title":{"rendered":"Make custom block full-width in Gutenberg by default"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">By default, all blocks are inserted with the same width in the Gutenberg editor. Depending on the block and theme support, you get the option to set blocks to wide or full width, to let the block go beyond the normal content width on the website.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now I had the case that a custom block should be displayed full-width by default, without the option to change that. On the frontend, I just modified the styling, but in the editor, there are a few more adjustments necessary because the block controls for a full-width block are above the block, not on the left side.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"update-box\">\n\n<p><strong>Update from August 20, 2020:<\/strong> <a href=\"https:\/\/florianbrinkmann.com\/en\/block-full-width-gutenberg-6012\/#comment-13724\">Fabian K\u00e4gy posted a code snippet in the comments<\/a> that shows a way to make this also work for core or third-party blocks \ud83c\udf89 I updated the second part of the post accordingly.<\/p>\n\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">To recreate that behavior, I set the <code>align<\/code> attribute to <code>full<\/code> and add <code>data-align=\"full\"<\/code> to the block wrapper. That is done by the following code in the <code>registerBlockType()<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">attributes: {\n\t<span class=\"hljs-attr\">align<\/span>: {\n\t\t<span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">'string'<\/span>,\n\t\t<span class=\"hljs-attr\">default<\/span>: <span class=\"hljs-string\">'full'<\/span>,\n\t}\n},\ngetEditWrapperProps() {\n\t<span class=\"hljs-keyword\">return<\/span> {\n\t\t<span class=\"hljs-string\">'data-align'<\/span>: <span class=\"hljs-string\">'full'<\/span>,\n\t};\n},<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Of course, that only works for own blocks that you created (the <code>alignfull<\/code> class name is not added automatically here).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the same behavior for core or third-party blocks, where we cannot modify the <code>registerBlockType()<\/code> object, we can use the <code>blocks.registerBlockType<\/code> filter, like in the following code example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> modifyDefaultAlignment = <span class=\"hljs-function\">(<span class=\"hljs-params\"> settings, name <\/span>) =&gt;<\/span> {\n\t<span class=\"hljs-keyword\">if<\/span> ( name !== <span class=\"hljs-string\">'core\/media-text'<\/span> ) {\n\t\t<span class=\"hljs-keyword\">return<\/span> settings;\n\t}\n\t<span class=\"hljs-keyword\">const<\/span> newSettings = {\n\t\t...settings,\n\t\t<span class=\"hljs-attr\">attributes<\/span>: {\n\t\t\t...settings.attributes,\n\t\t\t<span class=\"hljs-attr\">align<\/span>: { <span class=\"hljs-attr\">type<\/span>: <span class=\"hljs-string\">'string'<\/span>, <span class=\"hljs-attr\">default<\/span>: <span class=\"hljs-string\">'full'<\/span> },\n\t\t},\n\t};\n\t<span class=\"hljs-keyword\">return<\/span> newSettings;\n};\nwp.hooks.addFilter(\n\t<span class=\"hljs-string\">'blocks.registerBlockType'<\/span>,\n\t<span class=\"hljs-string\">'textdomain\/change-media-text-default-alignment'<\/span>,\n\tmodifyDefaultAlignment,\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The code checks for the <code>core\/media-text<\/code> block and, if we have one, adds the <code>align<\/code> attribute to the settings with <code>full<\/code> as the default value. That works only if the theme has support for wide and full alignment enabled.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, all blocks are inserted with the same width in the Gutenberg editor. Depending on the block and theme support, you get the option to set blocks to wide or full width, to let the block go beyond the normal content width on the website. Now I had the case that a custom block [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-6012","post","type-post","status-publish","format-standard","hentry","category-web-development"],"wp-worthy-pixel":{"ignored":false,"public":"4c0b98c394e34561976427e4d180f690","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/4c0b98c394e34561976427e4d180f690"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/6012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=6012"}],"version-history":[{"count":10,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/6012\/revisions"}],"predecessor-version":[{"id":6031,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/6012\/revisions\/6031"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=6012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=6012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=6012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}