From 74676391c7834d0400cf987a391fb28494f6ca22 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 25 Mar 2022 14:28:18 +0200 Subject: [PATCH 1/8] [Components - ToggleGroupControl]: Add icons support --- .../src/toggle-group-control/stories/index.js | 40 ++++++++++++++++--- .../toggle-group-control-option/component.tsx | 10 ++++- .../toggle-group-control-option/styles.ts | 1 + .../src/toggle-group-control/types.ts | 5 +++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/packages/components/src/toggle-group-control/stories/index.js b/packages/components/src/toggle-group-control/stories/index.js index 99e6a391a7cef..5787d3d8c6e89 100644 --- a/packages/components/src/toggle-group-control/stories/index.js +++ b/packages/components/src/toggle-group-control/stories/index.js @@ -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 @@ -57,25 +62,26 @@ const _default = ( { options } ) => { KNOBS_GROUPS.ToggleGroupControl ); - const controlOptions = options.map( ( opt, index ) => ( + const controlOptions = options.map( ( option, index ) => ( ) ); @@ -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' ]; diff --git a/packages/components/src/toggle-group-control/toggle-group-control-option/component.tsx b/packages/components/src/toggle-group-control/toggle-group-control-option/component.tsx index 39f2cd98f3ea0..b17b6b4ff4d34 100644 --- a/packages/components/src/toggle-group-control/toggle-group-control-option/component.tsx +++ b/packages/components/src/toggle-group-control/toggle-group-control-option/component.tsx @@ -9,6 +9,7 @@ import { Radio } from 'reakit'; * WordPress dependencies */ import { useInstanceId } from '@wordpress/compose'; +import { Icon } from '@wordpress/icons'; /** * Internal dependencies @@ -56,6 +57,7 @@ function ToggleGroupControlOption( isBlock = false, label, value, + icon, showTooltip = false, ...radioProps } = { @@ -75,9 +77,13 @@ function ToggleGroupControlOption( ? radioProps[ 'aria-label' ] : label; + const content = !! icon ? : label; return ( - + - { label } + { content } diff --git a/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts b/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts index 3e49726cd39bb..167d9d2cb281c 100644 --- a/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts +++ b/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts @@ -55,6 +55,7 @@ export const buttonView = css` export const buttonActive = css` color: ${ COLORS.white }; + fill: ${ COLORS.white }; background-color: ${ COLORS.gray[ 900 ] }; `; diff --git a/packages/components/src/toggle-group-control/types.ts b/packages/components/src/toggle-group-control/types.ts index 47909c9c129a9..038ee26d1262d 100644 --- a/packages/components/src/toggle-group-control/types.ts +++ b/packages/components/src/toggle-group-control/types.ts @@ -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; /** * 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. From c6f18bd5caf8f82ba54526b104891876d6ff850f Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 25 Mar 2022 14:40:24 +0200 Subject: [PATCH 2/8] add changelog entry --- packages/components/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 3ef364d531813..6cc6ae1582f7d 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -5,6 +5,7 @@ ### Enhancements - Update `BorderControl` and `BorderBoxControl` to allow the passing of custom class names to popovers ([#39753](https://github.com/WordPress/gutenberg/pull/39753)). +- `ToggleGroupControlOption`: Add `icon` support ([#39760](https://github.com/WordPress/gutenberg/pull/39760)). ### Internal From 0204a7366e87709d2e5fac2a597d6df10e70b60b Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 28 Mar 2022 22:43:24 +0300 Subject: [PATCH 3/8] add prop to readme and fix styles --- .../toggle-group-control-option/README.md | 19 ++++++++++++++----- .../toggle-group-control-option/styles.ts | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/components/src/toggle-group-control/toggle-group-control-option/README.md b/packages/components/src/toggle-group-control/toggle-group-control-option/README.md index 3a4c0924902d7..ab72aecbf5461 100644 --- a/packages/components/src/toggle-group-control/toggle-group-control-option/README.md +++ b/packages/components/src/toggle-group-control/toggle-group-control-option/README.md @@ -18,7 +18,11 @@ import { function Example() { return ( - + ); @@ -31,17 +35,22 @@ function Example() { Label for the option. If needed, the `aria-label` prop can be used in addition to specify a different label for assistive technologies. -- Required: Yes +- Required: Yes ### `value`: `string | number` The value of the `ToggleGroupControlOption`. -- Required: Yes +- Required: Yes + +### `icon`: `WPComponent` + +Icon for the option. If `icon` is provided it will be used instead of the `label` and will show a tooltip automatically. + +- Required: No ### `showTooltip`: `boolean` Whether to show a tooltip when hovering over the option. The tooltip will attempt to use the `aria-label` prop text first, then the `label` prop text if no `aria-label` prop is found. -- Required: No - +- Required: No diff --git a/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts b/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts index 167d9d2cb281c..fe243c09cc512 100644 --- a/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts +++ b/packages/components/src/toggle-group-control/toggle-group-control-option/styles.ts @@ -27,6 +27,7 @@ export const buttonView = css` border: none; border-radius: ${ CONFIG.controlBorderRadius }; color: ${ COLORS.gray[ 700 ] }; + fill: currentColor; cursor: pointer; display: flex; font-family: inherit; @@ -55,7 +56,6 @@ export const buttonView = css` export const buttonActive = css` color: ${ COLORS.white }; - fill: ${ COLORS.white }; background-color: ${ COLORS.gray[ 900 ] }; `; From 086a901a52c96e6ebb26e528c41e7e316f791b8e Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 28 Mar 2022 22:55:11 +0300 Subject: [PATCH 4/8] update tests --- .../src/toggle-group-control/test/__snapshots__/index.js.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap b/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap index 51b4d08703550..b8b35e95a1043 100644 --- a/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap +++ b/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap @@ -79,6 +79,7 @@ exports[`ToggleGroupControl should render correctly 1`] = ` border: none; border-radius: 2px; color: #757575; + fill: currentColor; cursor: pointer; display: -webkit-box; display: -webkit-flex; From ae5004ebe1779a7207f14c877a148968cc1557cb Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 30 Mar 2022 12:51:01 +0300 Subject: [PATCH 5/8] refactor to use ToggleGroupControlOptionBase --- packages/components/src/index.js | 1 + .../src/toggle-group-control/index.ts | 1 + .../src/toggle-group-control/stories/index.js | 54 ++-- .../test/__snapshots__/index.js.snap | 230 +++++++++++++++++- .../src/toggle-group-control/test/index.js | 51 +++- .../README.md | 25 ++ .../component.tsx | 128 ++++++++++ .../toggle-group-control-option-base/index.ts | 1 + .../styles.ts | 0 .../README.md | 54 ++++ .../component.tsx | 55 +++++ .../toggle-group-control-option-icon/index.ts | 1 + .../toggle-group-control-option/README.md | 6 - .../toggle-group-control-option/component.tsx | 112 +-------- .../src/toggle-group-control/types.ts | 36 ++- 15 files changed, 596 insertions(+), 159 deletions(-) create mode 100644 packages/components/src/toggle-group-control/toggle-group-control-option-base/README.md create mode 100644 packages/components/src/toggle-group-control/toggle-group-control-option-base/component.tsx create mode 100644 packages/components/src/toggle-group-control/toggle-group-control-option-base/index.ts rename packages/components/src/toggle-group-control/{toggle-group-control-option => toggle-group-control-option-base}/styles.ts (100%) create mode 100644 packages/components/src/toggle-group-control/toggle-group-control-option-icon/README.md create mode 100644 packages/components/src/toggle-group-control/toggle-group-control-option-icon/component.tsx create mode 100644 packages/components/src/toggle-group-control/toggle-group-control-option-icon/index.ts diff --git a/packages/components/src/index.js b/packages/components/src/index.js index b3a820f05a38d..6b90c5a1142e3 100644 --- a/packages/components/src/index.js +++ b/packages/components/src/index.js @@ -146,6 +146,7 @@ export { default as ToggleControl } from './toggle-control'; export { ToggleGroupControl as __experimentalToggleGroupControl, ToggleGroupControlOption as __experimentalToggleGroupControlOption, + ToggleGroupControlOptionIcon as __experimentalToggleGroupControlOptionIcon, } from './toggle-group-control'; export { default as Toolbar } from './toolbar'; export { default as ToolbarButton } from './toolbar-button'; diff --git a/packages/components/src/toggle-group-control/index.ts b/packages/components/src/toggle-group-control/index.ts index 5b84f88a7ee0c..e5e40497d9220 100644 --- a/packages/components/src/toggle-group-control/index.ts +++ b/packages/components/src/toggle-group-control/index.ts @@ -1,2 +1,3 @@ export { ToggleGroupControl } from './toggle-group-control'; export { ToggleGroupControlOption } from './toggle-group-control-option'; +export { ToggleGroupControlOptionIcon } from './toggle-group-control-option-icon'; diff --git a/packages/components/src/toggle-group-control/stories/index.js b/packages/components/src/toggle-group-control/stories/index.js index 5787d3d8c6e89..ef784fc9688d4 100644 --- a/packages/components/src/toggle-group-control/stories/index.js +++ b/packages/components/src/toggle-group-control/stories/index.js @@ -7,16 +7,16 @@ import { boolean, text } from '@storybook/addon-knobs'; * WordPress dependencies */ import { useState } from '@wordpress/element'; -import { - formatCapitalize, - formatLowercase, - formatUppercase, -} from '@wordpress/icons'; +import { formatLowercase, formatUppercase } from '@wordpress/icons'; /** * Internal dependencies */ -import { ToggleGroupControl, ToggleGroupControlOption } from '../index'; +import { + ToggleGroupControl, + ToggleGroupControlOption, + ToggleGroupControlOptionIcon, +} from '../index'; import { View } from '../../view'; import Button from '../../button'; @@ -81,7 +81,6 @@ const _default = ( { options } ) => { option.showTooltip, `${ KNOBS_GROUPS.ToggleGroupControlOption }-${ index + 1 }` ) } - icon={ option.icon ?? null } /> ) ); @@ -131,26 +130,27 @@ 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 WithIcons = () => { + const [ state, setState ] = useState(); + return ( + + + + + ); }; export const WithReset = () => { diff --git a/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap b/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap index b8b35e95a1043..30816250f5453 100644 --- a/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap +++ b/packages/components/src/toggle-group-control/test/__snapshots__/index.js.snap @@ -1,6 +1,226 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`ToggleGroupControl should render correctly 1`] = ` +exports[`ToggleGroupControl should render correctly with icons 1`] = ` +.emotion-0 { + font-family: -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',sans-serif; + font-size: 13px; +} + +.emotion-2 { + margin-bottom: calc(4px * 2); +} + +.components-panel__row .emotion-2 { + margin-bottom: inherit; +} + +.emotion-4 { + display: inline-block; + margin-bottom: calc(4px * 2); +} + +.emotion-6 { + background: #fff; + border: 1px solid; + border-color: #757575; + border-radius: 2px; + display: -webkit-inline-box; + 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; +} + +@media ( prefers-reduced-motion: reduce ) { + .emotion-6 { + transition-duration: 0ms; + } +} + +.emotion-6:hover { + border-color: #757575; +} + +.emotion-6:focus-within { + border-color: var( --wp-admin-theme-color-darker-10, #007cba); + box-shadow: 0 0 0 0.5px var( --wp-admin-theme-color, #00669b); + outline: none; + z-index: 1; +} + +.emotion-8 { + display: -webkit-inline-box; + display: -webkit-inline-flex; + display: -ms-inline-flexbox; + display: inline-flex; + max-width: 100%; + min-width: 0; + position: relative; + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; +} + +.emotion-10 { + -webkit-align-items: center; + -webkit-box-align: center; + -ms-flex-align: center; + align-items: center; + -webkit-appearance: none; + -moz-appearance: none; + -ms-appearance: none; + appearance: none; + background: transparent; + border: none; + border-radius: 2px; + color: #757575; + fill: currentColor; + cursor: pointer; + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; + font-family: inherit; + height: 100%; + -webkit-box-pack: center; + -ms-flex-pack: center; + -webkit-justify-content: center; + justify-content: center; + line-height: 100%; + outline: none; + padding: 0 12px; + position: relative; + text-align: center; + -webkit-transition: background 160ms linear,color 160ms linear,font-weight 60ms linear; + transition: background 160ms linear,color 160ms linear,font-weight 60ms linear; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + width: 100%; + z-index: 2; +} + +@media ( prefers-reduced-motion: reduce ) { + .emotion-10 { + transition-duration: 0ms; + } +} + +.emotion-10::-moz-focus-inner { + border: 0; +} + +.emotion-10:active { + background: #fff; +} + +.emotion-11 { + color: #fff; + background-color: #1e1e1e; +} + +.emotion-12 { + font-size: 13px; + line-height: 1; +} + +
+
+
+ + Test Toggle Group Control + +
+
+
+ +
+
+ +
+
+
+
+`; + +exports[`ToggleGroupControl should render correctly with text options 1`] = ` .emotion-0 { font-family: -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen-Sans,Ubuntu,Cantarell,'Helvetica Neue',sans-serif; font-size: 13px; @@ -153,10 +373,10 @@ exports[`ToggleGroupControl should render correctly 1`] = `