-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use dropdown menus for nested color controls #37053
Changes from all commits
ecce34c
57de133
8a0b5e2
79d75e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,15 @@ import { getBlockSupport } from '@wordpress/blocks'; | |
import { __ } from '@wordpress/i18n'; | ||
import { useRef, useEffect, useMemo, Platform } from '@wordpress/element'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { | ||
__experimentalItemGroup as ItemGroup, | ||
__experimentalItem as Item, | ||
__experimentalHStack as HStack, | ||
FlexItem, | ||
ColorIndicator, | ||
PanelBody, | ||
Dropdown, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -28,6 +37,7 @@ import { | |
} from '../components/gradients'; | ||
import { cleanEmptyObject } from './utils'; | ||
import ColorPanel from './color-panel'; | ||
import InspectorControls from '../components/inspector-controls'; | ||
import useSetting from '../components/use-setting'; | ||
|
||
export const COLOR_SUPPORT_KEY = 'color'; | ||
|
@@ -294,6 +304,24 @@ export function ColorEdit( props ) { | |
} else if ( hasGradientColor ) { | ||
gradientValue = style?.color?.gradient; | ||
} | ||
const backgroundValue = hasBackgroundColor | ||
? getColorObjectByAttributeValues( | ||
allSolids, | ||
backgroundColor, | ||
style?.color?.background | ||
).color | ||
: undefined; | ||
const textColorValue = getColorObjectByAttributeValues( | ||
allSolids, | ||
textColor, | ||
style?.color?.text | ||
).color; | ||
const linkColorValue = hasLinkColor | ||
? getLinkColorFromAttributeValue( | ||
allSolids, | ||
style?.elements?.link?.color?.text | ||
) | ||
: undefined; | ||
|
||
const onChangeColor = ( name ) => ( value ) => { | ||
const colorObject = getColorObjectByColorValue( allSolids, value ); | ||
|
@@ -371,61 +399,132 @@ export function ColorEdit( props ) { | |
}; | ||
|
||
return ( | ||
<ColorPanel | ||
enableContrastChecking={ | ||
// Turn on contrast checker for web only since it's not supported on mobile yet. | ||
Platform.OS === 'web' && ! gradient && ! style?.color?.gradient | ||
} | ||
clientId={ props.clientId } | ||
settings={ [ | ||
...( hasTextColor | ||
? [ | ||
{ | ||
label: __( 'Text color' ), | ||
onColorChange: onChangeColor( 'text' ), | ||
colorValue: getColorObjectByAttributeValues( | ||
allSolids, | ||
textColor, | ||
style?.color?.text | ||
).color, | ||
}, | ||
] | ||
: [] ), | ||
...( hasBackgroundColor || hasGradientColor | ||
? [ | ||
{ | ||
label: __( 'Background color' ), | ||
onColorChange: hasBackgroundColor | ||
? onChangeColor( 'background' ) | ||
: undefined, | ||
colorValue: getColorObjectByAttributeValues( | ||
allSolids, | ||
backgroundColor, | ||
style?.color?.background | ||
).color, | ||
gradientValue, | ||
onGradientChange: hasGradientColor | ||
? onChangeGradient | ||
: undefined, | ||
}, | ||
] | ||
: [] ), | ||
...( hasLinkColor | ||
? [ | ||
{ | ||
label: __( 'Link Color' ), | ||
onColorChange: onChangeLinkColor, | ||
colorValue: getLinkColorFromAttributeValue( | ||
allSolids, | ||
style?.elements?.link?.color?.text | ||
), | ||
clearable: !! style?.elements?.link?.color | ||
?.text, | ||
}, | ||
] | ||
: [] ), | ||
] } | ||
/> | ||
<InspectorControls> | ||
<PanelBody | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the component PanelColorGradientSettings should be updated to be dropdown-based. The component API:
Can easily accommodate this UI where each color setting starts to be rendered on a new dropdown. As an advantage blocks like navigation and social links (and third-party blocks with custom color implementation) would automatically take advantage of this new UI. As part of this component UI change, we would also need to remove the usage of the component from the Global Styles UI e.g: from packages/edit-site/src/components/global-styles/screen-background-color.js. We can use the ColorPalette Component directly the only thing we would lose is the tabbing between gradient and color but we can easily add that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can keep them but they should be circular. Also keep in mind these panels should evolve to ToolsPanel and have resets, not be collapsible (yet), etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're making them circular in #37028 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI #37028 just got merged |
||
title={ __( 'Colors' ) } | ||
className="block-editor__hooks-colors-panel" | ||
> | ||
<ItemGroup isBordered isSeparated> | ||
{ ( hasBackgroundColor || hasGradientColor ) && ( | ||
<Dropdown | ||
contentClassName="block-editor__hooks-colors-panel__dropdown" | ||
renderToggle={ ( { onToggle } ) => { | ||
return ( | ||
<Item onClick={ onToggle }> | ||
<HStack justify="flex-start"> | ||
<FlexItem> | ||
<ColorIndicator | ||
colorValue={ | ||
gradientValue ?? | ||
backgroundValue | ||
} | ||
/> | ||
</FlexItem> | ||
<FlexItem> | ||
{ __( 'Background' ) } | ||
</FlexItem> | ||
</HStack> | ||
</Item> | ||
); | ||
} } | ||
renderContent={ () => ( | ||
<ColorPanel | ||
enableContrastChecking={ | ||
// Turn on contrast checker for web only since it's not supported on mobile yet. | ||
Platform.OS === 'web' && | ||
! gradient && | ||
! style?.color?.gradient | ||
} | ||
clientId={ props.clientId } | ||
setting={ { | ||
label: __( 'Background color' ), | ||
onColorChange: hasBackgroundColor | ||
? onChangeColor( 'background' ) | ||
: undefined, | ||
colorValue: backgroundValue, | ||
gradientValue, | ||
onGradientChange: hasGradientColor | ||
? onChangeGradient | ||
: undefined, | ||
} } | ||
/> | ||
) } | ||
/> | ||
) } | ||
{ hasTextColor && ( | ||
<Dropdown | ||
contentClassName="block-editor__hooks-colors-panel__dropdown" | ||
renderToggle={ ( { onToggle } ) => ( | ||
<Item onClick={ onToggle }> | ||
<HStack justify="flex-start"> | ||
<FlexItem> | ||
<ColorIndicator | ||
colorValue={ textColorValue } | ||
/> | ||
</FlexItem> | ||
<FlexItem>{ __( 'Text' ) }</FlexItem> | ||
</HStack> | ||
</Item> | ||
) } | ||
renderContent={ () => ( | ||
<ColorPanel | ||
enableContrastChecking={ | ||
// Turn on contrast checker for web only since it's not supported on mobile yet. | ||
Platform.OS === 'web' && | ||
! gradient && | ||
! style?.color?.gradient | ||
} | ||
clientId={ props.clientId } | ||
setting={ { | ||
label: __( 'Text color' ), | ||
onColorChange: onChangeColor( 'text' ), | ||
colorValue: textColorValue, | ||
} } | ||
/> | ||
) } | ||
/> | ||
) } | ||
{ hasLinkColor && ( | ||
<Dropdown | ||
contentClassName="block-editor__hooks-colors-panel__dropdown" | ||
renderToggle={ ( { onToggle } ) => ( | ||
<Item onClick={ onToggle }> | ||
<HStack justify="flex-start"> | ||
<FlexItem> | ||
<ColorIndicator | ||
colorValue={ linkColorValue } | ||
/> | ||
</FlexItem> | ||
<FlexItem>{ __( 'Links' ) }</FlexItem> | ||
</HStack> | ||
</Item> | ||
) } | ||
renderContent={ () => ( | ||
<ColorPanel | ||
enableContrastChecking={ | ||
// Turn on contrast checker for web only since it's not supported on mobile yet. | ||
Platform.OS === 'web' && | ||
! gradient && | ||
! style?.color?.gradient | ||
} | ||
clientId={ props.clientId } | ||
setting={ { | ||
label: __( 'Link Color' ), | ||
onColorChange: onChangeLinkColor, | ||
colorValue: getLinkColorFromAttributeValue( | ||
allSolids, | ||
style?.elements?.link?.color?.text | ||
), | ||
clearable: !! style?.elements?.link | ||
?.color?.text, | ||
} } | ||
/> | ||
) } | ||
/> | ||
) } | ||
</ItemGroup> | ||
</PanelBody> | ||
</InspectorControls> | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.block-editor__hooks-colors-panel { | ||
.components-dropdown { | ||
display: block; | ||
} | ||
|
||
// Allow horizontal overflow so the size-increasing color indicators don't cause a scrollbar. | ||
.components-navigator-screen { | ||
overflow-x: visible; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That rule can be removed. I suspect it's a remnant as this PR was forked from the one that included the navigator. I didn't feel very comfortable with targetting those classes either. |
||
} | ||
Comment on lines
+2
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not rely on internal components classnames, as they are not part of the public APIs and could change in the future. A better approach would be to set a custom className in the specific context of the block editor's color panel, and then use that new custom classname to apply styles (both for dropdown and navigator screen) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: Posted in context of the wrong comment. |
||
|
||
// @todo: this can be removed when https://github.com/WordPress/gutenberg/pull/37028 lands. | ||
.component-color-indicator { | ||
margin-left: 0; | ||
display: block; | ||
border-radius: 50%; | ||
border: 0; | ||
height: 24px; | ||
width: 24px; | ||
padding: 0; | ||
background-image: | ||
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200), | ||
repeating-linear-gradient(45deg, $gray-200 25%, transparent 25%, transparent 75%, $gray-200 75%, $gray-200); | ||
background-position: 0 0, 25px 25px; | ||
background-size: calc(2 * 5px) calc(2 * 5px); | ||
} | ||
Comment on lines
+11
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With #37028 merged, I believe these styles can be removed |
||
} | ||
|
||
@include break-medium() { | ||
.block-editor__hooks-colors-panel__dropdown .components-popover__content { | ||
margin-right: #{ math.div($sidebar-width, 2) + $grid-unit-20 } !important; | ||
margin-top: #{ -($grid-unit-60 + $grid-unit-15) } !important; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,11 @@ | |
&.is-opened { | ||
padding: $grid-unit-20; | ||
} | ||
|
||
// Don't cascade the padding into nested panels. | ||
.components-panel__body { | ||
padding: 0; | ||
} | ||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a CHANGELOG entry for this change to the |
||
} | ||
|
||
.components-panel__header { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were showing multiple color palettes default, theme, and user, and now we are just showing a single palette. I guess we can use the hook useMultipleOriginColorsAndGradients to get all the palettes.