|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import clsx from 'clsx'; |
| 5 | + |
| 6 | +/** |
| 7 | + * WordPress dependencies |
| 8 | + */ |
| 9 | +import { __ } from '@wordpress/i18n'; |
| 10 | +import { |
| 11 | + InspectorControls, |
| 12 | + __experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown, |
| 13 | + __experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients, |
| 14 | + ContrastChecker, |
| 15 | +} from '@wordpress/block-editor'; |
| 16 | + |
| 17 | +function ContrastCheckerMatrix( { attributes, activeBackgroundColor, activeTextColor, hoverBackgroundColor, hoverTextColor } ) { |
| 18 | + const { |
| 19 | + customActiveBackgroundColor, |
| 20 | + customActiveTextColor, |
| 21 | + customHoverBackgroundColor, |
| 22 | + customHoverTextColor, |
| 23 | + backgroundColor, |
| 24 | + textColor, |
| 25 | + fontSize, |
| 26 | + } = attributes; |
| 27 | + |
| 28 | + const activeBackground = activeBackgroundColor?.color ?? customActiveBackgroundColor; |
| 29 | + const activeText = activeTextColor?.color ?? customActiveTextColor; |
| 30 | + const hoverBackground = hoverBackgroundColor?.color ?? customHoverBackgroundColor; |
| 31 | + const hoverText = hoverTextColor?.color ?? customHoverTextColor; |
| 32 | + |
| 33 | + return ( |
| 34 | + <> |
| 35 | + <ContrastChecker |
| 36 | + backgroundColor={ backgroundColor } |
| 37 | + fontSize={ fontSize } |
| 38 | + textColor={ textColor } |
| 39 | + /> |
| 40 | + <ContrastChecker |
| 41 | + backgroundColor={ activeBackground } |
| 42 | + fontSize={ fontSize } |
| 43 | + textColor={ activeText } |
| 44 | + /> |
| 45 | + <ContrastChecker |
| 46 | + backgroundColor={ hoverBackground } |
| 47 | + fontSize={ fontSize } |
| 48 | + textColor={ hoverText } |
| 49 | + /> |
| 50 | + </> |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +export default function Controls( { |
| 55 | + attributes, |
| 56 | + setAttributes, |
| 57 | + clientId, |
| 58 | + activeBackgroundColor, |
| 59 | + setActiveBackgroundColor, |
| 60 | + activeTextColor, |
| 61 | + setActiveTextColor, |
| 62 | + hoverBackgroundColor, |
| 63 | + setHoverBackgroundColor, |
| 64 | + hoverTextColor, |
| 65 | + setHoverTextColor, |
| 66 | +} ) { |
| 67 | + const { |
| 68 | + customActiveBackgroundColor, |
| 69 | + customActiveTextColor, |
| 70 | + customHoverBackgroundColor, |
| 71 | + customHoverTextColor, |
| 72 | + } = attributes; |
| 73 | + |
| 74 | + const colorSettings = useMultipleOriginColorsAndGradients(); |
| 75 | + |
| 76 | + return ( |
| 77 | + <InspectorControls group="color"> |
| 78 | + <ColorGradientSettingsDropdown |
| 79 | + settings={ [ |
| 80 | + { |
| 81 | + label: __( 'Active Background' ), |
| 82 | + colorValue: |
| 83 | + activeBackgroundColor?.color ?? customActiveBackgroundColor, |
| 84 | + onColorChange: ( value ) => { |
| 85 | + setActiveBackgroundColor( value ); |
| 86 | + setAttributes( { |
| 87 | + customActiveBackgroundColor: value, |
| 88 | + } ); |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + label: __( 'Active Text' ), |
| 93 | + colorValue: |
| 94 | + activeTextColor?.color ?? customActiveTextColor, |
| 95 | + onColorChange: ( value ) => { |
| 96 | + setActiveTextColor( value ); |
| 97 | + setAttributes( { |
| 98 | + customActiveTextColor: value, |
| 99 | + } ); |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + label: __( 'Hover Background' ), |
| 104 | + colorValue: |
| 105 | + hoverBackgroundColor?.color ?? customHoverBackgroundColor, |
| 106 | + onColorChange: ( value ) => { |
| 107 | + setHoverBackgroundColor( value ); |
| 108 | + setAttributes( { |
| 109 | + customHoverBackgroundColor: value, |
| 110 | + } ); |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + label: __( 'Hover Text' ), |
| 115 | + colorValue: |
| 116 | + hoverTextColor?.color ?? customHoverTextColor, |
| 117 | + onColorChange: ( value ) => { |
| 118 | + setHoverTextColor( value ); |
| 119 | + setAttributes( { |
| 120 | + customHoverTextColor: value, |
| 121 | + } ); |
| 122 | + }, |
| 123 | + }, |
| 124 | + ] } |
| 125 | + panelId={ clientId } |
| 126 | + disableCustomColors={ false } |
| 127 | + __experimentalIsRenderedInSidebar |
| 128 | + __next40pxDefaultSize |
| 129 | + { ...colorSettings } |
| 130 | + /> |
| 131 | + <ContrastCheckerMatrix |
| 132 | + attributes={ attributes } |
| 133 | + activeBackgroundColor={ activeBackgroundColor } |
| 134 | + activeTextColor={ activeTextColor } |
| 135 | + hoverBackgroundColor={ hoverBackgroundColor } |
| 136 | + hoverTextColor={ hoverTextColor } |
| 137 | + /> |
| 138 | + </InspectorControls> |
| 139 | + ); |
| 140 | +} |
0 commit comments