Skip to content

Commit

Permalink
Add explanatory comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Apr 12, 2022
1 parent 142ae74 commit 4efe4b4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/block-editor/src/components/colors-gradients/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import {
*/
import ColorGradientControl from './control';

// Conditionally wraps the `ColorGradientSettingsDropdown` color controls in an
// `ItemGroup` allowing for a standalone group of controls to be
// rendered semantically.
const WithItemGroup = ( { isItemGroup, children } ) => {
if ( ! isItemGroup ) {
return children;
Expand All @@ -38,6 +41,8 @@ const WithItemGroup = ( { isItemGroup, children } ) => {
);
};

// When the `ColorGradientSettingsDropdown` controls are being rendered to a
// `ToolsPanel` they must be wrapped in a `ToolsPanelItem`.
const WithToolsPanelItem = ( {
isItemGroup,
settings,
Expand Down Expand Up @@ -73,9 +78,13 @@ const LabeledColorIndicator = ( { colorValue, label } ) => (
</HStack>
);

// Renders a color dropdown's toggle as an `Item` if it is within an `ItemGroup`
// or as a `Button` if it isn't e.g. the controls are being rendered in
// a `ToolsPanel`.
const renderToggle = ( settings ) => ( { onToggle, isOpen } ) => {
const { isItemGroup, colorValue, label } = settings;

// Determine component, `Item` or `Button`, to wrap color indicator with.
const ToggleComponent = isItemGroup ? Item : Button;
const toggleClassName = isItemGroup
? 'block-editor-panel-color-gradient-settings__item'
Expand All @@ -93,6 +102,13 @@ const renderToggle = ( settings ) => ( { onToggle, isOpen } ) => {
);
};

// Renders a collection of color controls as dropdowns. Depending upon the
// context in which these dropdowns are being rendered, they may be wrapped
// in an `ItemGroup` with each dropdown's toggle as an `Item`, or alternatively,
// the may be individually wrapped in a `ToolsPanelItem` with the toggle as
// a regular `Button`.
//
// For more context see: https://github.com/WordPress/gutenberg/pull/40084
export default function ColorGradientSettingsDropdown( {
colors,
disableCustomColors,
Expand All @@ -114,6 +130,8 @@ export default function ColorGradientSettingsDropdown( {
: 'block-editor-tools-panel-color-gradient-settings__dropdown';

return (
// Only wrap with `ItemGroup` if these controls are being rendered
// semantically.
<WithItemGroup isItemGroup={ isItemGroup }>
{ settings.map( ( setting, index ) => {
const controlProps = {
Expand Down Expand Up @@ -141,6 +159,8 @@ export default function ColorGradientSettingsDropdown( {

return (
setting && (
// If not in an `ItemGroup` wrap the dropdown in a
// `ToolsPanelItem`
<WithToolsPanelItem
key={ index }
isItemGroup={ isItemGroup }
Expand Down

0 comments on commit 4efe4b4

Please sign in to comment.