Skip to content

Commit

Permalink
Abstract logic to compute the common props.
Browse files Browse the repository at this point in the history
  • Loading branch information
afercia committed Feb 10, 2025
1 parent e68f676 commit 551b6cd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 145 deletions.
13 changes: 13 additions & 0 deletions packages/components/src/circular-option-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ Prevents keyboard interaction from wrapping around. Only used when `asButtons` i
- Required: No
- Default: `true`

### `aria-labelledby`: `string`

The ID reference list of one or more elements that label the wrapper element.

- Required: No

### `aria-label`: `string`

The label for the wrapper element. Not used if an 'aria-labelledby' is provided.

- Required: No
- Default: `Custom color picker`

## Subcomponents

### `CircularOptionPicker.ButtonAction`
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/circular-option-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export {
ButtonAction,
DropdownLinkAction,
} from './circular-option-picker-actions';
export { useComputeCircularOptionPickerCommonProps } from './utils';

export default CircularOptionPicker;
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe( 'CircularOptionPicker', () => {

expect( screen.queryByRole( 'listbox' ) ).not.toBeInTheDocument();
expect( screen.queryByRole( 'option' ) ).not.toBeInTheDocument();
expect( screen.getByRole( 'group' ) ).toBeInTheDocument();
expect( screen.getByRole( 'button' ) ).toBeInTheDocument();
} );
} );
Expand Down
21 changes: 11 additions & 10 deletions packages/components/src/circular-option-picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ type CommonCircularOptionPickerProps = {
* The child elements.
*/
children?: ReactNode;
} & (
| {
'aria-label': string;
'aria-labelledby'?: never;
}
| {
'aria-label'?: never;
'aria-labelledby': string;
}
);
/**
* The ID reference list of one or more elements that label the wrapper
* element.
*/
'aria-labelledby'?: string;
/**
* The label for the wrapper element. Defaults to 'Custom color picker'. Not
* used if an 'aria-labelledby' is provided.
*/
'aria-label'?: string;
};

type WithBaseId = {
baseId: string;
Expand Down
27 changes: 27 additions & 0 deletions packages/components/src/circular-option-picker/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Computes the common props for the CircularOptionPicker.
*/
export function useComputeCircularOptionPickerCommonProps(
asButtons?: boolean,
loop?: boolean,
ariaLabel?: string,
ariaLabelledby?: string
) {
const metaProps = asButtons
? { asButtons: true }
: { asButtons: false, loop };

const labelProps = {
'aria-labelledby': ariaLabelledby,
'aria-label': ariaLabelledby
? undefined
: ariaLabel || __( 'Custom color picker' ),
};

return { metaProps, labelProps };
}
55 changes: 10 additions & 45 deletions packages/components/src/color-palette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import { useCallback, useMemo, useState, forwardRef } from '@wordpress/element';
*/
import Dropdown from '../dropdown';
import { ColorPicker } from '../color-picker';
import CircularOptionPicker from '../circular-option-picker';
import CircularOptionPicker, {
useComputeCircularOptionPickerCommonProps,
} from '../circular-option-picker';
import { VStack } from '../v-stack';
import { Truncate } from '../truncate';
import { ColorHeading } from './styles';
Expand Down Expand Up @@ -251,50 +253,12 @@ function UnforwardedColorPalette(
</CircularOptionPicker.ButtonAction>
);

let metaProps:
| { asButtons: false; loop?: boolean; 'aria-label': string }
| { asButtons: false; loop?: boolean; 'aria-labelledby': string }
| { asButtons: true; 'aria-label': string }
| { asButtons: true; 'aria-labelledby': string };

if ( asButtons ) {
const _metaProps: { asButtons: true } = {
asButtons: true,
};

if ( ariaLabel ) {
metaProps = { ..._metaProps, 'aria-label': ariaLabel };
} else if ( ariaLabelledby ) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby,
};
} else {
metaProps = {
..._metaProps,
'aria-label': __( 'Custom color picker.' ),
};
}
} else {
const _metaProps: { asButtons: false; loop?: boolean } = {
asButtons: false,
loop,
};

if ( ariaLabel ) {
metaProps = { ..._metaProps, 'aria-label': ariaLabel };
} else if ( ariaLabelledby ) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby,
};
} else {
metaProps = {
..._metaProps,
'aria-label': __( 'Custom color picker.' ),
};
}
}
const { metaProps, labelProps } = useComputeCircularOptionPickerCommonProps(
asButtons,
loop,
ariaLabel,
ariaLabelledby
);

return (
<VStack spacing={ 3 } ref={ forwardedRef } { ...additionalProps }>
Expand Down Expand Up @@ -352,6 +316,7 @@ function UnforwardedColorPalette(
{ ( colors.length > 0 || actions ) && (
<CircularOptionPicker
{ ...metaProps }
{ ...labelProps }
actions={ actions }
options={
hasMultipleColorOrigins ? (
Expand Down
55 changes: 10 additions & 45 deletions packages/components/src/duotone-picker/duotone-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { __, sprintf } from '@wordpress/i18n';
* Internal dependencies
*/
import ColorListPicker from './color-list-picker';
import CircularOptionPicker from '../circular-option-picker';
import CircularOptionPicker, {
useComputeCircularOptionPickerCommonProps,
} from '../circular-option-picker';
import { VStack } from '../v-stack';

import CustomDuotoneBar from './custom-duotone-bar';
Expand Down Expand Up @@ -127,50 +129,12 @@ function DuotonePicker( {
);
} );

let metaProps:
| { asButtons: false; loop?: boolean; 'aria-label': string }
| { asButtons: false; loop?: boolean; 'aria-labelledby': string }
| { asButtons: true; 'aria-label': string }
| { asButtons: true; 'aria-labelledby': string };

if ( asButtons ) {
const _metaProps: { asButtons: true } = {
asButtons: true,
};

if ( ariaLabel ) {
metaProps = { ..._metaProps, 'aria-label': ariaLabel };
} else if ( ariaLabelledby ) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby,
};
} else {
metaProps = {
..._metaProps,
'aria-label': __( 'Custom color picker.' ),
};
}
} else {
const _metaProps: { asButtons: false; loop?: boolean } = {
asButtons: false,
loop,
};

if ( ariaLabel ) {
metaProps = { ..._metaProps, 'aria-label': ariaLabel };
} else if ( ariaLabelledby ) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby,
};
} else {
metaProps = {
..._metaProps,
'aria-label': __( 'Custom color picker.' ),
};
}
}
const { metaProps, labelProps } = useComputeCircularOptionPickerCommonProps(
asButtons,
loop,
ariaLabel,
ariaLabelledby
);

const options = unsetable
? [ unsetOption, ...duotoneOptions ]
Expand All @@ -180,6 +144,7 @@ function DuotonePicker( {
<CircularOptionPicker
{ ...otherProps }
{ ...metaProps }
{ ...labelProps }
options={ options }
actions={
!! clearable && (
Expand Down
55 changes: 10 additions & 45 deletions packages/components/src/gradient-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { useCallback, useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import CircularOptionPicker from '../circular-option-picker';
import CircularOptionPicker, {
useComputeCircularOptionPickerCommonProps,
} from '../circular-option-picker';
import CustomGradientPicker from '../custom-gradient-picker';
import { VStack } from '../v-stack';
import { ColorHeading } from '../color-palette/styles';
Expand Down Expand Up @@ -128,54 +130,17 @@ function Component( props: PickerProps< any > ) {
<SingleOrigin { ...additionalProps } />
);

let metaProps:
| { asButtons: false; loop?: boolean; 'aria-label': string }
| { asButtons: false; loop?: boolean; 'aria-labelledby': string }
| { asButtons: true; 'aria-label': string }
| { asButtons: true; 'aria-labelledby': string };

if ( asButtons ) {
const _metaProps: { asButtons: true } = {
asButtons: true,
};

if ( ariaLabel ) {
metaProps = { ..._metaProps, 'aria-label': ariaLabel };
} else if ( ariaLabelledby ) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby,
};
} else {
metaProps = {
..._metaProps,
'aria-label': __( 'Custom color picker.' ),
};
}
} else {
const _metaProps: { asButtons: false; loop?: boolean } = {
asButtons: false,
loop,
};

if ( ariaLabel ) {
metaProps = { ..._metaProps, 'aria-label': ariaLabel };
} else if ( ariaLabelledby ) {
metaProps = {
..._metaProps,
'aria-labelledby': ariaLabelledby,
};
} else {
metaProps = {
..._metaProps,
'aria-label': __( 'Custom color picker.' ),
};
}
}
const { metaProps, labelProps } = useComputeCircularOptionPickerCommonProps(
asButtons,
loop,
ariaLabel,
ariaLabelledby
);

return (
<CircularOptionPicker
{ ...metaProps }
{ ...labelProps }
actions={ actions }
options={ options }
/>
Expand Down

0 comments on commit 551b6cd

Please sign in to comment.