调色板样式添加不正确

时间:2018-11-26 05:39:23

标签: wordpress-gutenberg gutenberg-blocks

出于我无法理解的原因,自定义CSS颜色名称未正确添加到html元素中。例如-我的自定义块具有文本颜色红色的调色板,并且显示得很完美。当我选择颜色时,相应的HTML元素将添加text-color-#F44336 CSS类,而不是text-color-red。因此,颜色不适用于该元素。任何帮助将不胜感激

Function.php代码

function mytheme_setup_theme_supported_features() {
    add_theme_support( 'editor-color-palette', array(
        array(
            'name' => __( 'red', 'tar' ),
            'slug' => 'red',
            'color' => '#F44336',
        ),
    ) );
}
add_action( 'after_setup_theme', 'mytheme_setup_theme_supported_features' );

阻止码

/**
 * BLOCK:  Accordion Image Block
 *
 * Registering a basic block with Gutenberg.
 * Simple block, renders and saves the same content without any interactivity.
 */

//  Import CSS.
import './style.scss';
import './editor.scss';




// let's us call registerBlockType() directly from the wp.blocks library
const { 
    registerBlockType, 
} = wp.blocks;


// let's us use the withAPIData() function from the wp.components library to access the Rest API
const {
    PanelBody,
    PanelColor,
    FormToggle,
    RangeControl,
    SelectControl,
    ToggleControl,
    Button 
} = wp.components;

// let's us use the __() function from the wp.i18n library to translate strings
const { __ } = wp.i18n;


const { Fragment } = wp.element;
const {
    RichText,
    BlockControls,
    InspectorControls,
    MediaUpload,
    ColorPalette,
    BlockDescription,
    BlockAlignmentToolbar,
    PanelColorSettings,
} = wp.editor;


const accordionImageEdit = ( props ) => {

    const { isSelected, className, setAttributes } = props;

    const { 
        textColor,
        imgOneID,
        imgOneURL,
        imgOneTxt,

     } = props.attributes;

    //Handle Image changes
    const onSelectImageOne = ( media ) => {
        setAttributes( {
            imgOneURL: media.url,
            imgOneID: media.id,
        } );
    };



     return [
         isSelected && (
            <InspectorControls key= { 'inspector' } >

                <PanelBody title={__('Image Accordion Block Settings', 'text-domain')}>

                    <PanelColorSettings 
                        title={ __('Color Settings', 'text-domain') }
                        colorSettings= { [ 
                            {
                            value: textColor,
                            onChange: (colorValue) => setAttributes ( { textColor: colorValue } ),
                            label: __('Text Color', 'text-domain'),
                            },
                         ] }
                    />
                </PanelBody>

            </InspectorControls>
         ),

         <div class="accordimg">
            <ul>
            <li>
                <MediaUpload
                        onSelect={ onSelectImageOne }
                        type="image"
                        value={ imgOneID }
                        render={ ( { open } ) => (
                            <Button className={ imgOneID ? 'image-button' : 'button button-large' } onClick={ open }>
                                { ! imgOneID ? __( 'Upload Image', 'text-domain' ) :  <img src={ imgOneURL } alt={ __( 'Upload Image', 'text-domain' ) } /> }
                            </Button>
                        ) }
                    />


                <div className="text--container">
                <RichText
                    tagName="h3"
                    placeholder={ __( 'Nature', 'text-domain' ) }
                    value={ imgOneTxt }
                    onChange={  (onChangeImgOneTxt) => setAttributes({ imgOneTxt: onChangeImgOneTxt}) }
                    className={ `font-size-${textSize} text-color-${textColor}` }

                />
                </div>


                </li>


            </ul>
            </div>

     ]

}

const accordionImageSave = ( props ) => {
    const { isSelected, className, setAttributes } = props;

    const { 
        textColor,
        imgOneID,
        imgOneURL,
        imgOneTxt,

     } = props.attributes;

     return (
         <div className="accordimg">
             <ul>
                <li 
                 style= { { backgroundImage: 'url(' + imgOneURL + ')',} }
                >

                    <div className="text--container">
                        <RichText.Content
                        tagName="h3"
                        value={ imgOneTxt }
                        className={ `font-size-${textSize} text-color-${textColor}` }
                        />
                    </div>
                </li>


             </ul>
         </div>
     )
}

 registerBlockType('theme/accordion-image-block',{
     title: __( 'Image accordion', 'text-domain' ),
     icon: 'megaphone',
     description: __('Image accordion block for  Theme', 'text-domain'),
     category: __('blocks', 'text-domain'),
     keywords: [
         __( 'Accordion Block', 'text-domain' ),
     ],
     attributes: {

         textColor: {
            type: 'string',
            default: '#111'
         },


        imgOneID: {
             type: 'number',
        },
        imgOneURL: {
            type: 'string',
        },
        imgOneTxt: {
            type: 'string',
            selector: 'h3',
            default: __('Nature', 'text-domain') ,
        },

     },

     edit: accordionImageEdit,

     save: accordionImageSave,

 })

editor.css和style.css

  .text-color-red {
    color:#F44336;
  }

0 个答案:

没有答案