Thanks. @threadi . Made some progress with a more barebone <hr>
. So now save.js
with
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.www.ads-software.com/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';
/**
* The save function defines the way in which the different attributes should
* be combined into the final markup, which is then serialized by the block
* editor into
post_content
.
*
* @see https://developer.www.ads-software.com/block-editor/reference-guides/block-api/block-edit-save/#save
*
* @return {Element} Element to render.
*/
export default function save({ attributes }) {
const { width, borderColor, borderStyle, borderWidth } = attributes;
return (
<hr
{...useBlockProps.save({
style: {
width,
borderTopColor: borderColor,
borderTopStyle: borderStyle,
borderTopWidth: borderWidth
}
})}
/>
);
}
and now all styling only gets applied to that main block selector. For edit.js
I have this now though not quite done:
/**
* Retrieves the translation of text.
*
* @see https://developer.www.ads-software.com/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.www.ads-software.com/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { PanelBody, ColorPalette, SelectControl, RangeControl } from '@wordpress/components';
/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';
/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.www.ads-software.com/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {Element} Element to render.
*/
export default function Edit({ attributes, setAttributes }) {
const { width, borderColor, borderStyle, borderWidth } = attributes;
const blockProps = useBlockProps({
style: {
width,
borderTopColor: borderColor,
borderTopStyle: borderStyle,
borderTopWidth: borderWidth
}
});
return (
<>
<InspectorControls>
<PanelBody title={__("Line Settings", "hr-line")} initialOpen={true}>
<RangeControl
label={__("Width (%)", "hr-line")}
value={parseInt(width)}
onChange={(newWidth) => setAttributes({ width:
${newWidth}%
})}
min={10}
max={100}
/>
<RangeControl
label={__("Border Width (px)", "hr-line")}
value={parseInt(borderWidth)}
onChange={(newBorderWidth) => setAttributes({ borderWidth: ${newBorderWidth}px
})}
min={1}
max={10}
/>
<SelectControl
label={__("Border Style", "hr-line")}
value={borderStyle}
options={[
{ label: __("Solid", "hr-line"), value: "solid" },
{ label: __("Dashed", "hr-line"), value: "dashed" },
{ label: __("Dotted", "hr-line"), value: "dotted" }
]}
onChange={(newBorderStyle) => setAttributes({ borderStyle: newBorderStyle })}
/>
<ColorPalette
label={__("Border Color", "hr-line")}
value={borderColor}
onChange={(newColor) => setAttributes({ borderColor: newColor })}
/>
</PanelBody>
</InspectorControls>
<hr {...blockProps} />
</>
);
}
Only briefly had some issues with caching and error that html was loaded in with <div>
and not with <hr>
as in the save method. Hard to empty out all the cache and it seems the WordPress package has no real solution for that as far as I can see. But perhaps I missed something.
Issue from before with Block names must be strings.
was perhaps because Bud only registers on the client side and not the server side. Was now working on it in WordPress @wordpress/create-block
plugin so that loads all automatically. Not sure. Working on that later on. Now as separate plugin.
issue width the width or the line only now:
<RangeControl
label={__("Width (%)", "hr-line")}
value={parseInt(width)}
onChange={(newWidth) => setAttributes({ width:
${newWidth}%
})}
min={10}
max={100}
/>
It currently is limited by the stack and does not cover the full width of the stack due to some CSS settings, padding in particular. Padding dded to keep stack just as wide as other stacks. Perhaps I need to work with minimum width or something.. to keep the line at a certain minimal size. Or stack min width… But working on that.
Will also work on renewed integration in theme and Sage width Bud, but only when I can avoid registration issues as I had earlier. Will keep this thread open for now.