block background colours and tint

This commit is contained in:
2026-01-02 01:15:56 +00:00
parent 0c6b74042e
commit ed045c16e1
6 changed files with 175 additions and 15 deletions

View File

@@ -6,8 +6,8 @@ class DisablePost
{
public function __construct()
{
add_filter('register_post_type_args', [$this, 'args'], 0, 2);
add_filter('register_taxonomy_args', [$this, 'args'], 0, 2);
// add_filter('register_post_type_args', [$this, 'args'], 0, 2);
// add_filter('register_taxonomy_args', [$this, 'args'], 0, 2);
}
public function args($args, $type)

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Admin;
use ourcodeworld\NameThatColor\ColorInterpreter as NameThatColor;
use App\Utilities;
class Theme
{
public function __construct()
{
add_action( 'after_setup_theme', [$this, 'DynamicPalette'] );
// add_action( 'init' , [$this, 'BackgroundTints']);
}
public function DynamicPalette()
{
$colour = new Utilities\Colour;
$NameThatColour = new NameThatColor;
$palette = [];
$colours = $colour->values();
foreach($colours as $slug => $hex) {
$palette[] = [
'name' => esc_html__(@$NameThatColour->name($hex)['name'], 'badegg'),
'slug' => $slug,
'color' => $hex,
];
}
if(!empty($colours)) {
add_theme_support('editor-color-palette', $palette);
}
}
public function BackgroundTints()
{
register_block_style(
'badegg/article',
[
'name' => 'badegg-colour-tint',
'label' => __('Tint', 'badegg'),
// 'inline_style' =>'.wp-block-image.is-style-badegg-colour-tint { }',
]
);
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace App\Utilities;
use ourcodeworld\NameThatColor\ColorInterpreter as NameThatColor;
class RestAPI
{
@@ -13,18 +14,27 @@ class RestAPI
{
$restBase = 'badegg/v1';
register_rest_route($restBase, '/blocks/container-widths', [
register_rest_route($restBase, '/blocks/config', [
'methods' => 'GET',
'callback' => [ $this, 'containerWidths'],
'callback' => [ $this, 'config'],
'permission_callback' => function(){
return true;
},
]);
}
public function config()
{
return rest_ensure_response([
'container' => $this->containerWidths(),
'colours' => $this->colours(),
'tints' => $this->tints(),
]);
}
public function containerWidths()
{
$containerWidths = [
return [
[ 'label' => __('Auto', 'badegg'), 'value' => 0 ],
[ 'label' => __('Narrow', 'badegg'), 'value' => 'narrow' ],
[ 'label' => __('Small', 'badegg'), 'value' => 'small' ],
@@ -32,7 +42,39 @@ class RestAPI
[ 'label' => __('Large', 'badegg'), 'value' => 'large' ],
[ 'label' => __('Edge to edge', 'badegg'), 'value' => 'full' ],
];
return rest_ensure_response($containerWidths);
}
public function colours()
{
$colour = new Colour;
$NameThatColour = new NameThatColor;
$palette = [];
$colours = $colour->values();
foreach($colours as $slug => $hex) {
$palette[] = [
'name' => esc_html__(@$NameThatColour->name($hex)['name'], 'badegg'),
'slug' => $slug,
'color' => $hex,
];
}
return $palette;
}
public function tints()
{
return [
['label' => __('Lightest', 'badegg'), 'value' => 'lightest'],
['label' => __('Lighter', 'badegg'), 'value' => 'lighter' ],
['label' => __('Light', 'badegg'), 'value' => 'light' ],
['label' => __('None', 'badegg'), 'value' => 0 ],
['label' => __('Dark', 'badegg'), 'value' => 'dark' ],
['label' => __('Darker', 'badegg'), 'value' => 'darker' ],
['label' => __('Darkest', 'badegg'), 'value' => 'darkest' ],
];
}
}

View File

@@ -28,6 +28,19 @@ export function sectionClassNames(attributes, defaultClasses, extraClasses = [])
if('padding_bottom'in attributes && !attributes.padding_bottom)
classNames.push('section-zero-bottom');
if('background_colour' in attributes && attributes.background_colour) {
let bg = `bg-${ attributes.background_colour }`;
if(
'background_tint' in attributes &&
attributes.background_tint != 0 &&
!['white', 'black'].includes(attributes.background_colour)
) {
bg += `-${ attributes.background_tint }`;
}
classNames.push(bg);
}
// combine arrays
classNames = classNames.concat(defaultClasses).concat(extraClasses);

View File

@@ -11,7 +11,7 @@
"attributes": {
"container_width": {
"type": "string",
"default": 0
"default": ""
},
"alignment": {
"type": "string"
@@ -23,12 +23,24 @@
"padding_bottom": {
"type": "boolean",
"default": true
},
"background_colour": {
"type": "string",
"default": ""
},
"background_hex": {
"type": "string",
"default": ""
},
"background_tint": {
"type": "string",
"default": "0"
}
},
"supports": {
"html": true,
"color": {
"background": true,
"background": false,
"text": false
}
}

View File

@@ -17,6 +17,8 @@ import {
PanelRow,
SelectControl,
ToggleControl,
ColorIndicator,
ColorPalette,
} from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
@@ -35,20 +37,23 @@ registerBlockType(metadata.name, {
alignment,
padding_top,
padding_bottom,
background_colour,
background_hex,
background_tint,
} = attributes;
const [
containerWidthOptions, setContainerWidthOptions,
configOptions, setConfigOptions,
] = useState( [] );
useEffect( () => {
apiFetch( { path: '/badegg/v1/blocks/container-widths' } )
apiFetch( { path: '/badegg/v1/blocks/config' } )
.then( ( data ) => {
setContainerWidthOptions( data );
setConfigOptions( data );
setIsLoading( false );
} )
.catch( () => {
setContainerWidthOptions( [] );
setConfigOptions( [] );
setIsLoading( false );
} );
}, [] );
@@ -60,7 +65,7 @@ registerBlockType(metadata.name, {
'wysiwyg'
]).join(' ');
console.log(blockProps.className);
// console.log(attributes);
return (
<div { ...blockProps }>
@@ -76,7 +81,7 @@ registerBlockType(metadata.name, {
<SelectControl
label={ __("Container Width", "badegg") }
value={ container_width }
options={ containerWidthOptions }
options={ configOptions.container }
onChange={ (value) => setAttributes({ container_width: value }) }
__next40pxDefaultSize={ true }
__nextHasNoMarginBottom={ true }
@@ -94,6 +99,46 @@ registerBlockType(metadata.name, {
__nextHasNoMarginBottom
/>
</PanelBody>
<PanelBody title={ __("Background Colour", "badegg") }>
<ColorPalette
colors={ configOptions.colours }
value={ background_hex }
disableCustomColors={ true }
onChange={ ( value ) => {
let slug, hex, selected = '';
if(value) {
selected = configOptions.colours.find(
( c ) => c.color === value
);
hex = value;
}
if(selected) {
slug = selected.slug;
}
setAttributes( {
background_colour: slug,
background_hex: hex,
});
} }
/>
{ 'background_colour' in attributes && attributes.background_colour ? (
<SelectControl
label={ __("Tint", "badegg") }
value={ background_tint }
options={ configOptions.tints }
onChange={ (value) => setAttributes({ background_tint: value }) }
__next40pxDefaultSize={ true }
__nextHasNoMarginBottom={ true }
/>
) : null }
</PanelBody>
</Panel>
</InspectorControls>
<div className={ containerClassNames(attributes).join(' ') }>