Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Example to adds two colour palettes to the sidebar in wordpress

import { registerBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
 
import {
    useBlockProps,
    ColorPalette,
    InspectorControls,
} from '@wordpress/block-editor';
 
registerBlockType( 'softhunt/inspector-example', {

    apiVersion: 2,
    attributes: {
        message: {
            type: 'string',
            source: 'text',
            selector: 'div',
            default: '', // empty default
        },
        bg_color: { type: 'string', default: '#000000' },
        text_color: { type: 'string', default: '#ffffff' },
    },
    edit: ( { attributes, setAttributes } ) => {
        const onChangeBGColor = ( hexColor ) => {
            setAttributes( { bg_color: hexColor } );
        };
 
        const onChangeTextColor = ( hexColor ) => {
            setAttributes( { text_color: hexColor } );
        };
 
        return (
            <div { ...useBlockProps() }>
                <InspectorControls key="setting">
                    <div id="softhunt-inspector-controls">
                        <fieldset>
                            <legend className="blocks-base-control__label">
                                { __( 'Background color', 'softhunt' ) }
                            </legend>
                            <ColorPalette // Element Tag for Gutenberg standard                                                       colour selector
                                onChange={ onChangeBGColor } // onChange event callback
                            />
                        </fieldset>
                        <fieldset>
                            <legend className="blocks-base-control__label">
                                { __( 'Text color', 'softhunt' ) }
                            </legend>
                            <ColorPalette // Element Tag for Gutenberg standard colour selector
                                onChange={ onChangeTextColor } // onChange event callback
                            />
                        </fieldset>
                    </div>
                </InspectorControls>
                <TextControl
                    value={ attributes.message }
                    onChange={ ( val ) => setAttributes( { message: val } ) }
                    style={ {
                        backgroundColor: attributes.bg_color,
                        color: attributes.text_color,
                    } }
                />
            </div>
        );
    },
    save: ( { attributes } ) => {
        return (
            <div
                { ...useBlockProps.save() }
                style={ {
                    backgroundColor: attributes.bg_color,
                    color: attributes.text_color,
                } }
            >
                { attributes.message }
            </div>
        );
    },
} );
Comment

PREVIOUS NEXT
Code Example
Javascript :: Example code of using inner blocks in Wordpress with ESNext 
Javascript :: Adding Notices in the Block Editor Wordpress 
Javascript :: Constant declaration in ES6 
Javascript :: javascript on enter keyup select button 
Javascript :: how add element at beginning of array in javascript using splice 
Javascript :: js date add days daylight saving 
Javascript :: material ui table row onclick 
Javascript :: Imports should be sorted alphabetically sort-imports 
Javascript :: isFinite(): returns true if the number is not Infinity or -Infinity 
Javascript :: is enabled 
Javascript :: regular expression for twitter embedded tweets 
Javascript :: get src vanilla js 
Javascript :: tailwind intenseness react 
Javascript :: he "slide" event cannot be bound because Hammer.JS is not loaded and no custom loader has been specified 
Javascript :: jshack130mhrl 
Javascript :: how to remove comma from toString function javascript 
Javascript :: react document documentMode not found 
Javascript :: react native image path in vriable 
Javascript :: react Update a label when rate moves "quietly" 
Javascript :: .datepicker make modal exit 
Javascript :: gsheet calculate next tuesday date 
Javascript :: async loop with mongoose 
Javascript :: triangle sum of odds numbers formula 
Javascript :: Failed: Template parse errors: There is no directive with "exportAs" set to "ngModel" karma 
Javascript :: VueJS - getting the last element of a split string array 
Javascript :: alpiee js hide amother button click 
Javascript :: ex:password 
Javascript :: how to trigger on Blur only when clicked outside parent component and not child component in react js 
Javascript :: please run 
Javascript :: pdf extract text nodejs 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =