-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Merge the Button and IconButton into a single component #19193
Changes from all commits
82a286e
c2a8dfa
248c498
d8dac0b
d0a9819
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Basic rendering should display with required props 1`] = `"<span><div tabindex=\\"-1\\"><div><div><div class=\\"components-popover block-editor-link-control\\"><div class=\\"components-popover__content\\" tabindex=\\"-1\\"><div class=\\"block-editor-link-control__popover-inner\\"><div class=\\"block-editor-link-control__search\\"><form><div class=\\"components-base-control block-editor-url-input block-editor-link-control__search-input\\"><div class=\\"components-base-control__field\\"><input type=\\"text\\" aria-label=\\"URL\\" required=\\"\\" placeholder=\\"Search or type url\\" role=\\"combobox\\" aria-expanded=\\"false\\" aria-autocomplete=\\"list\\" aria-owns=\\"block-editor-url-input-suggestions-0\\" value=\\"\\"></div></div><button type=\\"reset\\" disabled=\\"\\" aria-label=\\"Reset\\" class=\\"components-button components-icon-button block-editor-link-control__search-reset\\"><svg aria-hidden=\\"true\\" role=\\"img\\" focusable=\\"false\\" xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"20\\" height=\\"20\\" viewBox=\\"0 0 20 20\\" class=\\"dashicon dashicons-no-alt\\"><path d=\\"M14.95 6.46L11.41 10l3.54 3.54-1.41 1.41L10 11.42l-3.53 3.53-1.42-1.42L8.58 10 5.05 6.47l1.42-1.42L10 8.58l3.54-3.53z\\"></path></svg></button></form></div></div></div></div></div></div></div></span>"`; | ||
exports[`Basic rendering should display with required props 1`] = `"<span><div tabindex=\\"-1\\"><div><div><div class=\\"components-popover block-editor-link-control\\"><div class=\\"components-popover__content\\" tabindex=\\"-1\\"><div class=\\"block-editor-link-control__popover-inner\\"><div class=\\"block-editor-link-control__search\\"><form><div class=\\"components-base-control block-editor-url-input block-editor-link-control__search-input\\"><div class=\\"components-base-control__field\\"><input type=\\"text\\" aria-label=\\"URL\\" required=\\"\\" placeholder=\\"Search or type url\\" role=\\"combobox\\" aria-expanded=\\"false\\" aria-autocomplete=\\"list\\" aria-owns=\\"block-editor-url-input-suggestions-0\\" value=\\"\\"></div></div><button type=\\"reset\\" disabled=\\"\\" class=\\"components-button block-editor-link-control__search-reset components-icon-button\\" aria-label=\\"Reset\\"><svg aria-hidden=\\"true\\" role=\\"img\\" focusable=\\"false\\" xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"20\\" height=\\"20\\" viewBox=\\"0 0 20 20\\" class=\\"dashicon dashicons-no-alt\\"><path d=\\"M14.95 6.46L11.41 10l3.54 3.54-1.41 1.41L10 11.42l-3.53 3.53-1.42-1.42L8.58 10 5.05 6.47l1.42-1.42L10 8.58l3.54-3.53z\\"></path></svg></button></form></div></div></div></div></div></div></div></span>"`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,19 @@ | |
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { isArray } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import deprecated from '@wordpress/deprecated'; | ||
import { createElement, forwardRef } from '@wordpress/element'; | ||
import { forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Tooltip from '../tooltip'; | ||
import Icon from '../icon'; | ||
|
||
export function Button( props, ref ) { | ||
const { | ||
|
@@ -25,6 +32,13 @@ export function Button( props, ref ) { | |
isDestructive, | ||
className, | ||
disabled, | ||
icon, | ||
iconSize, | ||
showTooltip, | ||
tooltipPosition, | ||
shortcut, | ||
label, | ||
children, | ||
...additionalProps | ||
} = props; | ||
|
||
|
@@ -44,19 +58,54 @@ export function Button( props, ref ) { | |
'is-busy': isBusy, | ||
'is-link': isLink, | ||
'is-destructive': isDestructive, | ||
'has-text': !! icon && !! children, | ||
// Ideally should be has-icon but this is named this way for BC | ||
'components-icon-button': !! icon, | ||
} ); | ||
|
||
const tag = href !== undefined && ! disabled ? 'a' : 'button'; | ||
const tagProps = tag === 'a' ? | ||
const Tag = href !== undefined && ! disabled ? 'a' : 'button'; | ||
const tagProps = Tag === 'a' ? | ||
{ href, target } : | ||
{ type: 'button', disabled, 'aria-pressed': isPressed }; | ||
|
||
return createElement( tag, { | ||
...tagProps, | ||
...additionalProps, | ||
className: classes, | ||
ref, | ||
} ); | ||
// Should show the tooltip if... | ||
const shouldShowTooltip = ! disabled && ( | ||
// an explicit tooltip is passed or... | ||
( showTooltip && label ) || | ||
// there's a shortcut or... | ||
shortcut || | ||
( | ||
// there's a label and... | ||
!! label && | ||
// the children are empty and... | ||
( ! children || ( isArray( children ) && ! children.length ) ) && | ||
// the tooltip is not explicitly disabled. | ||
false !== showTooltip | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this condition is now very similar to
we can refactor it later as it's quite hard to read :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a difference between explicit disabling of tooltips and "undefined" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured it out after 2 additional minutes spend on contemplating on it 🤣 |
||
) | ||
); | ||
|
||
const element = ( | ||
<Tag | ||
{ ...tagProps } | ||
{ ...additionalProps } | ||
className={ classes } | ||
aria-label={ additionalProps[ 'aria-label' ] || label } | ||
ref={ ref } | ||
> | ||
{ icon && <Icon icon={ icon } size={ iconSize } /> } | ||
{ children } | ||
</Tag> | ||
); | ||
|
||
if ( ! shouldShowTooltip ) { | ||
return element; | ||
} | ||
|
||
return ( | ||
<Tooltip text={ label } shortcut={ shortcut } position={ tooltipPosition }> | ||
{ element } | ||
</Tooltip> | ||
); | ||
} | ||
|
||
export default forwardRef( Button ); |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought classes didn't qualify?
https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/backward-compatibility/README.md#class-names-and-dom-updates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can do it, just trying to keep the changes small for now. Ideally, we also deprecate the component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at this and there are a few components that rely on this for styling. I'm going to leave it for a follow-up PR to ensure we test these properly and ease review of the current PR.