-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Components: Remove unexpected has-text
class when empty children are passed to Button
#44198
Changes from all commits
5c80024
c7bd31a
7ede583
eaa2dd1
39737e1
12fe7db
6d75c4a
a9e383d
02ee824
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ import { plusCircle } from '@wordpress/icons'; | |
* Internal dependencies | ||
*/ | ||
import Button from '../'; | ||
import { Tooltip } from '../../'; | ||
|
||
jest.mock( '../../icon', () => () => <div data-testid="test-icon" /> ); | ||
|
||
|
@@ -75,6 +76,41 @@ describe( 'Button', () => { | |
expect( screen.getByRole( 'button' ) ).toHaveClass( 'is-pressed' ); | ||
} ); | ||
|
||
it( 'should render a button element with has-text when children are passed', async () => { | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
render( <Button icon={ plusCircle }>Children</Button> ); | ||
await screen.getByRole( 'button' ).focus(); | ||
expect( screen.getByRole( 'button' ) ).toHaveClass( 'has-text' ); | ||
} ); | ||
|
||
it( 'should render a button element without has-text when children are not passed', async () => { | ||
render( <Button icon={ plusCircle }></Button> ); | ||
expect( screen.getByRole( 'button' ) ).not.toHaveClass( | ||
'has-text' | ||
); | ||
} ); | ||
|
||
it( 'should render a button element without has-text when children are empty fragment', async () => { | ||
render( | ||
<Button icon={ plusCircle }> | ||
<></> | ||
</Button> | ||
); | ||
expect( screen.getByRole( 'button' ) ).not.toHaveClass( | ||
'has-text' | ||
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. Eventually I'm hoping we can move these kinds of tests to visual regression testing because they are more robust than checking for class names ✨ |
||
); | ||
} ); | ||
|
||
it( 'should render a button element without has-text when a button wrapped in Tooltip', async () => { | ||
render( | ||
<Tooltip text="Help text"> | ||
<Button icon={ plusCircle } /> | ||
</Tooltip> | ||
); | ||
expect( screen.getByRole( 'button' ) ).not.toHaveClass( | ||
'has-text' | ||
); | ||
} ); | ||
|
||
it( 'should add a disabled prop to the button', () => { | ||
render( <Button disabled /> ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ exports[`PostSavedState returns a disabled button if the post is not saveable 1` | |
<button | ||
aria-disabled="true" | ||
aria-label="Save draft" | ||
class="components-button has-text has-icon" | ||
class="components-button has-icon" | ||
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. Thanks to the failure of this unit test I have found another problem. When the screen width is narrow, only an icon is shown on the PostSavedState_trunk.mp4This PR will solve this problem as well. Therefore, this snapshot update is intended. PostSavedState_pr.mp4 |
||
type="button" | ||
> | ||
<svg | ||
|
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.
If
Tooltip
is not excluded as a child, it will be considered to have children when moused over andhas-text
class will be given.I couldn't find a good approach to not regard
Tooltip
as a child.Is there a better way?
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.
Oof, I can't think of a better way either. I think it's good enough for a temporary measure though.
Long term, I'm hoping it could be resolved by the
Tooltip
rewrite. Maybe that will remove the need to inject direct children?Otherwise, we could perhaps add an official API to
Button
that allows consumers to set some kind of "ignore me as a visible child" flag in CSS. So like if you add a designated CSS class, e.g.<Button><div className="ignore-as-components-button-child" /></Button>
, Button could exclude it from itshasChildren
check.Another way might be to only apply
.has-text
when the child is actually aReactText
. I'm not sure, but I'm guessing that there are not a lot of use cases that involve aicon
prop and an element child with text.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.
Perhaps I think this new API should be considered in the overall Button component in #44042 for consideration.
Is it correct that a use case with a icon prop and an element child with text is something like the following?
I've seen such usage many times in my experience, so I think that the
has-text
class should be added.Yes, that's the challenge I must address in #42753 😅
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.
Sorry, this was unclear. By "icon prop and an element child with text" I meant something like:
in contrast to a text child:
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.
Thank you, I understand.
From what I have found on GitHub Code Search, it appears that "an element child with text" is almost never used.
For now, however, I think it would have less impact to consider both "element child with text" and "text child" as "having text".
This specification may need to be reconsidered in relation to #44042, but I think it is reasonable in this PR.
If it is not a problem, I would like to merge it 🚀
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.
Yep, sounds good! 🚢