Skip to content
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

ColorPalette, GradientPicker: fix color picker popover positioning #42989

Merged
merged 14 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- `ColorPicker`: fix layout overflow [#42992](https://github.com/WordPress/gutenberg/pull/42992)).
- `ToolsPanel`: Constrain grid columns to 50% max-width ([#42795](https://github.com/WordPress/gutenberg/pull/42795)).
- `Popover`: anchor correctly to parent node when no explicit anchor is passed ([#42971](https://github.com/WordPress/gutenberg/pull/42971)).
- `ColorPalette`: forward correctly `popoverProps` in the `CustomColorPickerDropdown` component [#42989](https://github.com/WordPress/gutenberg/pull/42989)).
- `ColorPalette`, `CustomGradientBar`: restore correct color picker popover position [#42989](https://github.com/WordPress/gutenberg/pull/42989)).

### Enhancements

Expand Down
36 changes: 26 additions & 10 deletions packages/components/src/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,35 @@ function MultiplePalettes( {
);
}

export function CustomColorPickerDropdown( { isRenderedInSidebar, ...props } ) {
export function CustomColorPickerDropdown( {
isRenderedInSidebar,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced here, but this is the first time I've seen it. isRenderedInSidebar doesn't seem like it should be in the components package. The prop is very specific to the editor's right-hand sidebar. What if there's a sidebar on the left of the screen?

It looks like it was a late fix for a WordPress release - #37115. @jorgefilipecosta, as you introduced this would you be willing to revisit it?

My feeling is that it should be more generic, like a childPopoverPlacement prop that's passed through to any popovers that should cascade in a given direction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 100% on this one, and coincidentally I have just opened an issue with a few changes that I'd like to see for GradientPicker:

My feeling is that it should be more generic, like a childPopoverPlacement prop that's passed through to any popovers that should cascade in a given direction.

For the specific prop, it will largely depend on what we decide design-wise — let's see how this conversation evolves — but in general, I'd love to get rid of that isRenderedInSidebar prop (also considering how many times it's forwarded through the various component chain!)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be good to get rid of isRenderedInSidebar it was added as a short fix for a visual issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorgefilipecosta , would you have any capacity to help with the work on this component, also since you are its main author ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ciampo, I will try to find some availability to work on some enhancements to this component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's great, thank you! I've assigned the issue with the list of enhancements — feel free to ping for any help and for reviews :)

popoverProps: receivedPopoverProps,
...props
} ) {
const popoverProps = useMemo(
() => ( {
__unstableShift: true,
...( isRenderedInSidebar
? {
// When in the sidebar: open to the left (stacking),
// leaving the same gap as the parent popover.
placement: 'left-start',
offset: 34,
}
: {
// Default behavior: open below the anchor
placement: 'bottom',
offset: 8,
} ),
...receivedPopoverProps,
} ),
[ isRenderedInSidebar, receivedPopoverProps ]
);

return (
<Dropdown
contentClassName="components-color-palette__custom-color-dropdown-content"
popoverProps={
isRenderedInSidebar
? {
placement: 'left-start',
offset: 20,
__unstableShift: true,
}
: undefined
}
popoverProps={ popoverProps }
{ ...props }
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ exports[`ColorPalette Dropdown should render it correctly 1`] = `

<Dropdown
contentClassName="components-color-palette__custom-color-dropdown-content"
popoverProps={
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that this snapshot change is due to a change in CustomColorPickerDropdown (in packages/components/src/color-palette/index.js), where previously the __unstableShift prop was set only when isRenderedInSidebar was true — while, with the changes in this PR, __unstableShift is always set to true

Object {
"__unstableShift": true,
"offset": 8,
"placement": "bottom",
}
}
renderContent={[Function]}
renderToggle={[Function]}
>
Expand Down Expand Up @@ -728,6 +735,13 @@ exports[`ColorPalette should render a dynamic toolbar of colors 1`] = `
>
<Dropdown
contentClassName="components-color-palette__custom-color-dropdown-content"
popoverProps={
Object {
"__unstableShift": true,
"offset": 8,
"placement": "bottom",
}
}
renderContent={[Function]}
renderToggle={[Function]}
>
Expand Down
63 changes: 29 additions & 34 deletions packages/components/src/custom-gradient-bar/control-points.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { LEFT, RIGHT } from '@wordpress/keycodes';
* Internal dependencies
*/
import Button from '../button';
import { HStack } from '../h-stack';
import { ColorPicker } from '../color-picker';
import { VisuallyHidden } from '../visually-hidden';
import { CustomColorPickerDropdown } from '../color-palette';
Expand Down Expand Up @@ -73,23 +74,16 @@ function ControlPointButton( { isOpen, position, color, ...additionalProps } ) {
);
}

function GradientColorPickerDropdown( {
isRenderedInSidebar,
gradientPickerDomRef,
...props
} ) {
const popoverProps = useMemo( () => {
const result = {
className:
'components-custom-gradient-picker__color-picker-popover',
position: 'top',
};
if ( isRenderedInSidebar ) {
result.anchorRef = gradientPickerDomRef.current;
result.position = 'bottom left';
}
return result;
}, [ gradientPickerDomRef, isRenderedInSidebar ] );
function GradientColorPickerDropdown( { isRenderedInSidebar, ...props } ) {
// Open the popover below the gradient control/insertion point
const popoverProps = useMemo(
() => ( {
placement: 'bottom',
offset: 8,
} ),
[]
);

return (
<CustomColorPickerDropdown
isRenderedInSidebar={ isRenderedInSidebar }
Expand Down Expand Up @@ -163,7 +157,6 @@ function ControlPoints( {
return (
ignoreMarkerPosition !== initialPosition && (
<GradientColorPickerDropdown
gradientPickerDomRef={ gradientPickerDomRef }
isRenderedInSidebar={ __experimentalIsRenderedInSidebar }
key={ index }
onClose={ onStopControlPointChange }
Expand Down Expand Up @@ -256,21 +249,25 @@ function ControlPoints( {
} }
/>
{ ! disableRemove && controlPoints.length > 2 && (
<Button
className="components-custom-gradient-picker__remove-control-point"
onClick={ () => {
onChange(
removeControlPoint(
controlPoints,
index
)
);
onClose();
} }
variant="link"
<HStack
className="components-custom-gradient-picker__remove-control-point-wrapper"
alignment="center"
>
{ __( 'Remove Control Point' ) }
</Button>
<Button
onClick={ () => {
onChange(
removeControlPoint(
controlPoints,
index
)
);
onClose();
} }
variant="link"
>
{ __( 'Remove Control Point' ) }
</Button>
</HStack>
) }
</>
) }
Expand All @@ -288,12 +285,10 @@ function InsertPoint( {
insertPosition,
disableAlpha,
__experimentalIsRenderedInSidebar,
gradientPickerDomRef,
} ) {
const [ alreadyInsertedPoint, setAlreadyInsertedPoint ] = useState( false );
return (
<GradientColorPickerDropdown
gradientPickerDomRef={ gradientPickerDomRef }
isRenderedInSidebar={ __experimentalIsRenderedInSidebar }
className="components-custom-gradient-picker__inserter"
onClose={ () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/components/src/custom-gradient-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ export default function CustomGradientBar( {
__experimentalIsRenderedInSidebar={
__experimentalIsRenderedInSidebar
}
gradientPickerDomRef={
gradientMarkersContainerDomRef
}
disableAlpha={ disableAlpha }
insertPosition={ gradientBarState.insertPosition }
value={ controlPoints }
Expand Down
7 changes: 2 additions & 5 deletions packages/components/src/custom-gradient-picker/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ $components-custom-gradient-picker__padding: $grid-unit-20; // 48px container, 1
}
}

.components-custom-gradient-picker__color-picker-popover .components-custom-gradient-picker__remove-control-point {
margin-left: auto;
margin-right: auto;
display: block;
margin-bottom: $grid-unit-10;
.components-custom-gradient-picker__remove-control-point-wrapper {
padding-bottom: $grid-unit-10;
}

.components-custom-gradient-picker__inserter {
Expand Down