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, BorderBox, BorderBoxControl: polish and DRY prop types, add default values #45463

Merged
merged 15 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- `UnitControl`: Remove outer wrapper to normalize className placement ([#41860](https://github.com/WordPress/gutenberg/pull/41860)).
- `ColorPalette`: Fix transparent checkered background pattern ([#45295](https://github.com/WordPress/gutenberg/pull/45295)).
- `ToggleGroupControl`: Add `isDeselectable` prop to allow deselecting the selected option ([#45123](https://github.com/WordPress/gutenberg/pull/45123)).
- `ColorPalette`, `BorderBox`, `BorderBoxControl`: polish and DRY prop types, add default values ([#45463](https://github.com/WordPress/gutenberg/pull/45463)).

### Bug Fix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export function useBorderBoxControlSplitControls(
) {
const {
className,
colors = [],
enableAlpha = false,
enableStyle = true,
size = 'default',
__experimentalHasMultipleOrigins = false,
__experimentalIsRenderedInSidebar = false,
...otherProps
} = useContextSystem( props, 'BorderBoxControlSplitControls' );

Expand All @@ -39,7 +44,12 @@ export function useBorderBoxControlSplitControls(
...otherProps,
centeredClassName,
className: classes,
colors,
enableAlpha,
enableStyle,
rightAlignedClassName,
size,
__experimentalHasMultipleOrigins,
__experimentalIsRenderedInSidebar,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ a `SlotFillProvider` overall.

## Props

### `colors`: `Array`
### `colors`: `( PaletteObject | ColorObject )[]`

An array of color definitions. This may also be a multi-dimensional array where
colors are organized by multiple origins.

Each color may be an object containing a `name` and `color` value.

- Required: No
- Default: `[]`

### `disableCustomColors`: `boolean`

Expand All @@ -89,6 +90,7 @@ This controls whether the alpha channel will be offered when selecting
custom colors.

- Required: No
- Default: `false`

### `enableStyle`: `boolean`

Expand Down Expand Up @@ -166,17 +168,3 @@ const splitBorders = {
```

- Required: No

### `__experimentalHasMultipleOrigins`: `boolean`

This is passed on to the color related sub-components which need to be made
aware of whether the colors prop contains multiple origins.

- Required: No

### `__experimentalIsRenderedInSidebar`: `boolean`

This is passed on to the color related sub-components so they may render more
effectively when used within a sidebar.

- Required: No
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const UnconnectedBorderBoxControl = (
onSplitChange,
popoverPlacement,
popoverOffset,
size = 'default',
size,
splitValue,
toggleLinked,
wrapperClassName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ import type { Borders, BorderSide, BorderBoxControlProps } from '../types';
export function useBorderBoxControl(
props: WordPressComponentProps< BorderBoxControlProps, 'div' >
) {
const { className, onChange, value, ...otherProps } = useContextSystem(
props,
'BorderBoxControl'
);
const {
className,
colors = [],
onChange,
enableAlpha = false,
enableStyle = true,
size = 'default',
value,
__experimentalHasMultipleOrigins = false,
__experimentalIsRenderedInSidebar = false,
...otherProps
} = useContextSystem( props, 'BorderBoxControl' );

const mixedBorders = hasMixedBorders( value );
const splitBorders = hasSplitBorders( value );
Expand Down Expand Up @@ -114,15 +122,21 @@ export function useBorderBoxControl(
return {
...otherProps,
className: classes,
colors,
disableUnits: mixedBorders && ! hasWidthValue,
enableAlpha,
enableStyle,
hasMixedBorders: mixedBorders,
isLinked,
linkedControlClassName,
onLinkedChange,
onSplitChange,
toggleLinked,
linkedValue,
size,
splitValue,
wrapperClassName,
__experimentalHasMultipleOrigins,
__experimentalIsRenderedInSidebar,
};
}
90 changes: 32 additions & 58 deletions packages/components/src/border-box-control/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* Internal dependencies
*/
import type { Border, ColorProps, LabelProps } from '../border-control/types';
import type {
Border,
ColorProps,
LabelProps,
BorderControlProps,
} from '../border-control/types';
import type { PopoverProps } from '../popover/types';

export type Borders = {
Expand All @@ -16,11 +21,8 @@ export type BorderProp = keyof Border;
export type BorderSide = keyof Borders;

export type BorderBoxControlProps = ColorProps &
LabelProps & {
/**
* This controls whether to support border style selections.
*/
enableStyle?: boolean;
LabelProps &
Pick< BorderControlProps, 'enableStyle' | 'size' > & {
/**
* A callback function invoked when any border value is changed. The value
* received may be a "flat" border object, one that has properties defining
Expand All @@ -43,15 +45,9 @@ export type BorderBoxControlProps = ColorProps &
* properties but for each side; `top`, `right`, `bottom`, and `left`.
*/
value: AnyBorder;
/**
* Size of the control.
*
* @default 'default'
*/
size?: 'default' | '__unstable-large';
};

export type LinkedButtonProps = {
export type LinkedButtonProps = Pick< BorderBoxControlProps, 'size' > & {
/**
* This prop allows the `LinkedButton` to reflect whether the parent
* `BorderBoxControl` is currently displaying "linked" or "unlinked"
Expand All @@ -64,58 +60,36 @@ export type LinkedButtonProps = {
* `BorderBoxControl`.
*/
onClick: () => void;
/**
* Size of the control.
*
* @default 'default'
*/
size?: 'default' | '__unstable-large';
};

export type VisualizerProps = {
export type VisualizerProps = Pick< BorderBoxControlProps, 'size' > & {
/**
* An object representing the current border configuration. It contains
* properties for each side, with each side an object reflecting the border
* color, style, and width.
*/
value?: Borders;
/**
* Size of the control.
*
* @default 'default'
*/
size?: 'default' | '__unstable-large';
};

export type SplitControlsProps = ColorProps & {
/**
* This controls whether to include border style options within the
* individual `BorderControl` components.
*/
enableStyle?: boolean;
/**
* A callback that is invoked whenever an individual side's border has
* changed.
*/
onChange: ( value: Border | undefined, side: BorderSide ) => void;
/**
* The position of the color popovers compared to the control wrapper.
*/
popoverPlacement?: PopoverProps[ 'placement' ];
/**
* The space between the popover and the control wrapper.
*/
popoverOffset?: PopoverProps[ 'offset' ];
/**
* An object representing the current border configuration. It contains
* properties for each side, with each side an object reflecting the border
* color, style, and width.
*/
value?: Borders;
/**
* Size of the control.
*
* @default 'default'
*/
size?: 'default' | '__unstable-large';
};
export type SplitControlsProps = ColorProps &
Pick< BorderBoxControlProps, 'enableStyle' | 'size' > & {
/**
* A callback that is invoked whenever an individual side's border has
* changed.
*/
onChange: ( value: Border | undefined, side: BorderSide ) => void;
/**
* The position of the color popovers compared to the control wrapper.
*/
popoverPlacement?: PopoverProps[ 'placement' ];
/**
* The space between the popover and the control wrapper.
*/
popoverOffset?: PopoverProps[ 'offset' ];
/**
* An object representing the current border configuration. It contains
* properties for each side, with each side an object reflecting the border
* color, style, and width.
*/
value?: Borders;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import { useBorderControlDropdown } from './hook';
import { StyledLabel } from '../../base-control/styles/base-control-styles';
import DropdownContentWrapper from '../../dropdown/dropdown-content-wrapper';

import type { Color, ColorOrigin, Colors, DropdownProps } from '../types';
import type { ColorObject, PaletteObject } from '../../color-palette/types';
import type { ColorProps, DropdownProps } from '../types';

const noop = () => undefined;
const getColorObject = (
colorValue: CSSProperties[ 'borderColor' ],
colors: Colors | undefined,
colors: ColorProps[ 'colors' ] | undefined,
hasMultipleColorOrigins: boolean
) => {
if ( ! colorValue || ! colors ) {
Expand All @@ -39,7 +40,7 @@ const getColorObject = (
if ( hasMultipleColorOrigins ) {
let matchedColor;

( colors as ColorOrigin[] ).some( ( origin ) =>
( colors as PaletteObject[] ).some( ( origin ) =>
origin.colors.some( ( color ) => {
if ( color.color === colorValue ) {
matchedColor = color;
Expand All @@ -53,14 +54,14 @@ const getColorObject = (
return matchedColor;
}

return ( colors as Color[] ).find(
return ( colors as ColorObject[] ).find(
( color ) => color.color === colorValue
);
};

const getToggleAriaLabel = (
colorValue: CSSProperties[ 'borderColor' ],
colorObject: Color | undefined,
colorObject: ColorObject | undefined,
style: CSSProperties[ 'borderStyle' ],
isStyleEnabled: boolean
) => {
Expand Down Expand Up @@ -131,6 +132,7 @@ const BorderControlDropdown = (
colors,
disableCustomColors,
enableAlpha,
enableStyle,
indicatorClassName,
indicatorWrapperClassName,
onReset,
Expand All @@ -140,7 +142,6 @@ const BorderControlDropdown = (
popoverControlsClassName,
resetButtonClassName,
showDropdownHeader,
enableStyle = true,
__unstablePopoverProps,
...otherProps
} = useBorderControlDropdown( props );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ export function useBorderControlDropdown(
const {
border,
className,
colors,
colors = [],
enableAlpha = false,
enableStyle = true,
onChange,
previousStyleSelection,
size = 'default',
__experimentalHasMultipleOrigins = false,
__experimentalIsRenderedInSidebar = false,
...otherProps
} = useContextSystem( props, 'BorderControlDropdown' );

Expand Down Expand Up @@ -81,6 +85,8 @@ export function useBorderControlDropdown(
border,
className: classes,
colors,
enableAlpha,
enableStyle,
indicatorClassName,
indicatorWrapperClassName,
onColorChange,
Expand All @@ -89,5 +95,7 @@ export function useBorderControlDropdown(
popoverContentClassName,
popoverControlsClassName,
resetButtonClassName,
__experimentalHasMultipleOrigins,
__experimentalIsRenderedInSidebar,
};
}
18 changes: 3 additions & 15 deletions packages/components/src/border-control/border-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ for the `BorderControl`'s color and style options, by rendering your

## Props

### `colors`: `Array`
### `colors`: `( PaletteObject | ColorObject )[]`

An array of color definitions. This may also be a multi-dimensional array where
colors are organized by multiple origins.

Each color may be an object containing a `name` and `color` value.

- Required: No
- Default: `[]`

### `disableCustomColors`: `boolean`

Expand All @@ -78,6 +79,7 @@ This controls whether the alpha channel will be offered when selecting
custom colors.

- Required: No
- Default: `false`

### `enableStyle`: `boolean`

Expand Down Expand Up @@ -172,17 +174,3 @@ Flags whether this `BorderControl` should also render a `RangeControl` for
additional control over a border's width.

- Required: No

### `__experimentalHasMultipleOrigins`: `boolean`

This is passed on to the color related sub-components which need to be made
aware of whether the colors prop contains multiple origins.

- Required: No

### `__experimentalIsRenderedInSidebar`: `boolean`

This is passed on to the color related sub-components so they may render more
effectively when used within a sidebar.

- Required: No
Loading