import classnames from 'classnames/dedupe'; import { InspectorControls, RichText, useBlockProps, } from '@wordpress/block-editor'; import { PanelBody, SelectControl, TabPanel, ToggleControl, } from '@wordpress/components'; import { useEffect, useState } from '@wordpress/element'; import { applyFilters } from '@wordpress/hooks'; import { __ } from '@wordpress/i18n'; import ApplyFilters from '../../components/apply-filters'; import ColorIndicator from '../../components/color-indicator'; import ColorPicker from '../../components/color-picker'; import IconPicker from '../../components/icon-picker'; import RangeControl from '../../components/range-control'; import ToggleGroup from '../../components/toggle-group'; import URLPicker from '../../components/url-picker'; const SIZES = [ { label: 'XS', value: 'xs', }, { label: 'S', value: 'sm', }, { label: 'M', value: 'md', }, { label: 'L', value: 'lg', }, { label: 'XL', value: 'xl', }, ]; /** * Block Edit Class. * * @param props */ export default function BlockEdit(props) { const { attributes, setAttributes, isSelected } = props; const { tagName, text, icon, iconPosition, hideText, url, ariaLabel, target, rel, size, color, textColor, borderRadius, borderWeight, borderColor, focusOutlineWeight, focusOutlineColor, hoverColor, hoverTextColor, hoverBorderColor, } = attributes; let { className = '' } = attributes; const [selectedColorState, setSelectedColorState] = useState('normal'); // Reset selected color state when block is not selected. useEffect(() => { if (!isSelected) { setSelectedColorState('normal'); } }, [isSelected]); let isNormalState = false; let isHoveredState = false; let isFocusedState = false; if (isSelected) { isNormalState = true; if (selectedColorState === 'hover') { isNormalState = false; isHoveredState = true; } else if (selectedColorState === 'focus') { isNormalState = false; isFocusedState = true; } } className = classnames( 'ghostkit-button', size && `ghostkit-button-${size}`, hideText && 'ghostkit-button-icon-only', isNormalState && 'ghostkit-button-is-normal-state', isHoveredState && 'ghostkit-button-is-hover-state', isFocusedState && 'ghostkit-button-is-focus-state', className ); // focus outline if (focusOutlineWeight && focusOutlineColor) { className = classnames(className, 'ghostkit-button-with-outline'); } className = applyFilters('ghostkit.editor.className', className, props); const colorsTabs = [ { name: 'normal', title: __('Normal', 'ghostkit'), className: 'ghostkit-control-tabs-tab', }, { name: 'hover', title: __('Hover', 'ghostkit'), className: 'ghostkit-control-tabs-tab', }, ]; if (focusOutlineWeight && focusOutlineColor) { colorsTabs.push({ name: 'focus', title: __('Focus', 'ghostkit'), className: 'ghostkit-control-tabs-tab', }); } const blockProps = useBlockProps({ className, }); return (
{ setAttributes({ size: value }); }} />
setAttributes({ borderRadius: value }) } allowCustomMax /> setAttributes({ borderWeight: value }) } allowCustomMax /> setAttributes({ focusOutlineWeight: value }) } allowCustomMax /> setAttributes({ icon: value })} insideInspector /> {icon ? ( setAttributes({ hideText: val })} /> ) : null} {icon && !hideText ? ( setAttributes({ iconPosition: value }) } /> ) : null} {__('Colors', 'ghostkit')} {borderWeight ? ( ) : null} {focusOutlineWeight && focusOutlineColor ? ( ) : null} } initialOpen={false} > { setSelectedColorState(state); }} > {(tabData) => { const isHover = tabData.name === 'hover'; // focus tab if (tabData.name === 'focus') { return ( setAttributes({ focusOutlineColor: val, }) } alpha /> ); } return ( <> setAttributes( isHover ? { hoverColor: val } : { color: val } ) } alpha gradient /> setAttributes( isHover ? { hoverTextColor: val, } : { textColor: val } ) } alpha /> {borderWeight ? ( setAttributes( isHover ? { hoverBorderColor: val, } : { borderColor: val, } ) } alpha /> ) : null} ); }}
{!tagName || tagName === 'a' ? ( { setAttributes(data); }} isSelected={isSelected} toolbarSettings inspectorSettings /> ) : null} {icon ? (
setAttributes({ icon: value })} value={icon} renderToggle={({ onToggle }) => ( )} />
) : null} {!hideText ? ( setAttributes({ text: value })} isSelected={isSelected} withoutInteractiveFormatting /> ) : null}
); }