Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Components - ToggleGroupControl]: Add icons support
  • Loading branch information
ntsekouras committed Mar 31, 2022
commit 74676391c7834d0400cf987a391fb28494f6ca22
40 changes: 34 additions & 6 deletions packages/components/src/toggle-group-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { boolean, text } from '@storybook/addon-knobs';
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import {
formatCapitalize,
formatLowercase,
formatUppercase,
} from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -57,25 +62,26 @@ const _default = ( { options } ) => {
KNOBS_GROUPS.ToggleGroupControl
);

const controlOptions = options.map( ( opt, index ) => (
const controlOptions = options.map( ( option, index ) => (
<ToggleGroupControlOption
key={ opt.value }
value={ opt.value }
key={ option.value }
value={ option.value }
label={ text(
`${ KNOBS_GROUPS.ToggleGroupControlOption }: label`,
opt.label,
option.label,
`${ KNOBS_GROUPS.ToggleGroupControlOption }-${ index + 1 }`
) }
aria-label={ text(
`${ KNOBS_GROUPS.ToggleGroupControlOption }: aria-label`,
opt[ 'aria-label' ],
option[ 'aria-label' ],
`${ KNOBS_GROUPS.ToggleGroupControlOption }-${ index + 1 }`
) }
showTooltip={ boolean(
`${ KNOBS_GROUPS.ToggleGroupControlOption }: showTooltip`,
opt.showTooltip,
option.showTooltip,
`${ KNOBS_GROUPS.ToggleGroupControlOption }-${ index + 1 }`
) }
icon={ option.icon ?? null }
/>
) );

Expand Down Expand Up @@ -125,6 +131,28 @@ WithAriaLabel.args = {
],
};

export const WithIcons = _default.bind( {} );
WithIcons.args = {
...Default.args,
options: [
{
label: 'Uppercase',
value: 'uppercase',
icon: formatUppercase,
},
{
label: 'Lowercase',
value: 'lowercase',
icon: formatLowercase,
},
{
label: 'Capitalize',
value: 'capitalize',
icon: formatCapitalize,
},
],
};

export const WithReset = () => {
const [ alignState, setAlignState ] = useState();
const aligns = [ 'Left', 'Center', 'Right' ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Radio } from 'reakit';
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { Icon } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -56,6 +57,7 @@ function ToggleGroupControlOption(
isBlock = false,
label,
value,
icon,
showTooltip = false,
...radioProps
} = {
Expand All @@ -75,9 +77,13 @@ function ToggleGroupControlOption(
? radioProps[ 'aria-label' ]
: label;

const content = !! icon ? <Icon icon={ icon } /> : label;
return (
<LabelView className={ labelViewClasses } data-active={ isActive }>
<WithToolTip showTooltip={ showTooltip } text={ optionLabel }>
<WithToolTip
showTooltip={ !! icon ? true : showTooltip }
text={ optionLabel }
>
<Radio
{ ...radioProps }
as="button"
Expand All @@ -87,7 +93,7 @@ function ToggleGroupControlOption(
ref={ forwardedRef }
value={ value }
>
<ButtonContentView>{ label }</ButtonContentView>
<ButtonContentView>{ content }</ButtonContentView>
</Radio>
</WithToolTip>
</LabelView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const buttonView = css`

export const buttonActive = css`
color: ${ COLORS.white };
fill: ${ COLORS.white };
background-color: ${ COLORS.gray[ 900 ] };
`;

Expand Down
5 changes: 5 additions & 0 deletions packages/components/src/toggle-group-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export type ToggleGroupControlOptionProps = {
* to specify a different label for assistive technologies.
*/
label: string;
/**
* Icon for the option. If `icon` is provided it will be used instead of the `label`
* and will show a tooltip automatically.
*/
icon?: JSX.Element;
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'm not sure about the best type here... --cc @ciampo

Copy link
Member

Choose a reason for hiding this comment

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

JSX.Element is the type I get back from the compiler for components in icons/src/library/*

Screen Shot 2022-03-28 at 11 54 23 am

/**
* Whether to display a Tooltip for the control option. If set to `true`, the tooltip will
* show the aria-label or the label prop text.
Expand Down