Skip to content

Commit

Permalink
ToggleGroupControl: Add size variants (#42008)
Browse files Browse the repository at this point in the history
* Remove unused export

* ToggleGroupControl: Add size variants

* Add stories

* Remove 'medium' hardcoded class name

* Remove small size

* Update snapshot

* Add changelog
  • Loading branch information
mirka authored Jul 14, 2022
1 parent c7c4858 commit 9789ed0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
- `Popover`: pass missing anchor ref to the `getAnchorRect` callback prop. ([#42076](https://github.com/WordPress/gutenberg/pull/42076)).
- `Popover`: call `getAnchorRect` callback prop even if `anchorRefFallback` has no value. ([#42329](https://github.com/WordPress/gutenberg/pull/42329)).

### Enhancement

- `ToggleGroupControl`: Add large size variant ([#42008](https://github.com/WordPress/gutenberg/pull/42008/)).

### Internal

- `Grid`: Convert to TypeScript ([#41923](https://github.com/WordPress/gutenberg/pull/41923)).
Expand Down
22 changes: 19 additions & 3 deletions packages/components/src/toggle-group-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import Button from '../../button';
export default {
component: ToggleGroupControl,
title: 'Components (Experimental)/ToggleGroupControl',
argTypes: {
size: {
control: 'select',
options: [ 'default', '__unstable-large' ],
},
},
parameters: {
knobs: { disable: false },
},
Expand All @@ -33,7 +39,7 @@ const KNOBS_GROUPS = {
ToggleGroupControlOption: 'ToggleGroupControlOption',
};

const _default = ( { options } ) => {
const _default = ( { options, ...props } ) => {
const [ value, setValue ] = useState( options[ 0 ].value );
const label = text(
`${ KNOBS_GROUPS.ToggleGroupControl }: label`,
Expand Down Expand Up @@ -87,6 +93,7 @@ const _default = ( { options } ) => {
return (
<View>
<ToggleGroupControl
{ ...props }
onChange={ setValue }
value={ value }
label={ label }
Expand All @@ -109,6 +116,7 @@ Default.args = {
{ value: 'right', label: 'Right' },
{ value: 'justify', label: 'Justify' },
],
size: 'default',
};

export const WithTooltip = _default.bind( {} );
Expand All @@ -130,10 +138,11 @@ WithAriaLabel.args = {
],
};

export const WithIcons = () => {
export const WithIcons = ( props ) => {
const [ state, setState ] = useState();
return (
<ToggleGroupControl
{ ...props }
onChange={ setState }
value={ state }
label={ 'With icons' }
Expand All @@ -154,8 +163,11 @@ export const WithIcons = () => {
</ToggleGroupControl>
);
};
WithIcons.args = {
...Default.args,
};

export const WithReset = () => {
export const WithReset = ( props ) => {
const [ alignState, setAlignState ] = useState();
const aligns = [ 'Left', 'Center', 'Right' ];
const alignOptions = aligns.map( ( key, index ) => (
Expand All @@ -172,6 +184,7 @@ export const WithReset = () => {
return (
<View>
<ToggleGroupControl
{ ...props }
onChange={ setAlignState }
value={ alignState }
label={ 'Toggle Group Control' }
Expand All @@ -185,3 +198,6 @@ export const WithReset = () => {
</View>
);
};
WithReset.args = {
...Default.args,
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ exports[`ToggleGroupControl should render correctly with icons 1`] = `
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-flex;
min-height: 36px;
min-width: 0;
padding: 2px;
position: relative;
-webkit-transition: -webkit-transform 100ms linear;
transition: transform 100ms linear;
min-height: 36px;
}
@media ( prefers-reduced-motion: reduce ) {
Expand Down Expand Up @@ -167,7 +167,7 @@ exports[`ToggleGroupControl should render correctly with icons 1`] = `
</div>
<div
aria-label="Test Toggle Group Control"
class="medium components-toggle-group-control emotion-6 emotion-7"
class="components-toggle-group-control emotion-6 emotion-7"
data-wp-c16t="true"
data-wp-component="ToggleGroupControl"
id="toggle-group-control-1"
Expand Down Expand Up @@ -281,12 +281,12 @@ exports[`ToggleGroupControl should render correctly with text options 1`] = `
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-flex;
min-height: 36px;
min-width: 0;
padding: 2px;
position: relative;
-webkit-transition: -webkit-transform 100ms linear;
transition: transform 100ms linear;
min-height: 36px;
}
@media ( prefers-reduced-motion: reduce ) {
Expand Down Expand Up @@ -393,7 +393,7 @@ exports[`ToggleGroupControl should render correctly with text options 1`] = `
</div>
<div
aria-label="Test Toggle Group Control"
class="medium components-toggle-group-control emotion-6 emotion-7"
class="components-toggle-group-control emotion-6 emotion-7"
data-wp-c16t="true"
data-wp-component="ToggleGroupControl"
id="toggle-group-control-0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,3 @@ export const ButtonContentView = styled.div`
export const separatorActive = css`
background: transparent;
`;

export const medium = css`
min-height: ${ CONFIG.controlHeight };
`;
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function ToggleGroupControl(
hideLabelFromVision = false,
help,
onChange = noop,
size = 'default',
value,
children,
...otherProps
Expand Down Expand Up @@ -83,12 +84,11 @@ function ToggleGroupControl(
const classes = useMemo(
() =>
cx(
styles.ToggleGroupControl,
styles.ToggleGroupControl( { size } ),
isBlock && styles.block,
'medium',
className
),
[ className, cx, isBlock ]
[ className, cx, isBlock, size ]
);
return (
<BaseControl help={ help }>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ import styled from '@emotion/styled';
* Internal dependencies
*/
import { CONFIG, COLORS, reduceMotion } from '../../utils';
import type { ToggleGroupControlProps } from '../types';

export const ToggleGroupControl = css`
export const ToggleGroupControl = ( {
size,
}: {
size: NonNullable< ToggleGroupControlProps[ 'size' ] >;
} ) => css`
background: ${ COLORS.ui.background };
border: 1px solid;
border-color: ${ COLORS.ui.border };
border-radius: ${ CONFIG.controlBorderRadius };
display: inline-flex;
min-height: ${ CONFIG.controlHeight };
min-width: 0;
padding: 2px;
position: relative;
transition: transform ${ CONFIG.transitionDurationFastest } linear;
${ reduceMotion( 'transition' ) }
${ toggleGroupControlSize( size ) }
&:hover {
border-color: ${ COLORS.ui.borderHover };
}
Expand All @@ -33,6 +40,19 @@ export const ToggleGroupControl = css`
}
`;

export const toggleGroupControlSize = (
size: NonNullable< ToggleGroupControlProps[ 'size' ] >
) => {
const heights = {
default: '36px',
'__unstable-large': '40px',
};

return css`
min-height: ${ heights[ size ] };
`;
};

export const block = css`
display: flex;
width: 100%;
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/toggle-group-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export type ToggleGroupControlProps = Omit<
* using help property as the content.
*/
help?: ReactNode;
/**
* The size variant of the control.
*
* @default 'default'
*/
size?: 'default' | '__unstable-large';
};

export type ToggleGroupControlContextProps = RadioStateReturn & {
Expand Down

0 comments on commit 9789ed0

Please sign in to comment.