-
Notifications
You must be signed in to change notification settings - Fork 549
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
Add loading
prop for Button
and IconButton
#3582
Changes from 10 commits
a1c03e2
cfd0944
9408220
18d7954
1cc0465
a9a7771
5c901d4
fae28dd
72a0247
71b747d
556aff9
03ce69f
f7b316d
6f98511
5080989
f3e6d07
ee21a63
5956393
9687486
2fc7c4d
7f981b0
49d8853
3aa7225
7f7f326
73b59c7
fc15b72
c277957
d556bc9
ccaa4ae
30fb01d
22c7c65
787bfaa
6dcf5b3
09a3199
0e276de
e8fcd21
3eee19a
e63e61c
8a901da
39f2d4e
24abbcd
ffdff03
9d133a0
e069e04
611eea0
7cf14ca
1a4a08d
a091125
e4a6f41
c9a0743
4d9128e
3c38084
9408882
ceb5ba2
c7501e2
ad02496
db094c5
fe2dcee
e300738
fb6d639
0911586
1416fcb
e245fe9
5e8877e
f2ccc8e
f301ffe
e7a25ef
71de776
2e5ed47
6c425f9
01e07a1
20d81f4
b3cdce6
c38523c
9ad96f2
7e5bc66
f781434
6f9f6ed
8809ba5
9649e4e
87fa84b
5acdcb9
c0e2a3d
a7b114b
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/react": minor | ||
--- | ||
|
||
Add `loading` state to `Button` and `IconButton` |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,8 @@ import {ButtonProps, StyledButton} from './types' | |||||
import {getVariantStyles, getButtonStyles, getAlignContentSize} from './styles' | ||||||
import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef' | ||||||
import {defaultSxProp} from '../utils/defaultSxProp' | ||||||
import VisuallyHidden from '../_VisuallyHidden' | ||||||
import Spinner from '../Spinner' | ||||||
|
||||||
const ButtonBase = forwardRef( | ||||||
({children, as: Component = 'button', sx: sxProp = defaultSxProp, ...props}, forwardedRef): JSX.Element => { | ||||||
|
@@ -21,6 +23,8 @@ const ButtonBase = forwardRef( | |||||
size = 'medium', | ||||||
alignContent = 'center', | ||||||
block = false, | ||||||
loading = false, | ||||||
loadingMessage = 'Loading', | ||||||
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. Hi! Here's the new place to add props to the documentation: https://github.com/primer/react/blob/main/src/Button/Button.docs.json |
||||||
...rest | ||||||
} = props | ||||||
const LeadingVisual = leadingVisual ?? leadingIcon | ||||||
|
@@ -62,40 +66,60 @@ const ButtonBase = forwardRef( | |||||
} | ||||||
|
||||||
return ( | ||||||
<StyledButton | ||||||
as={Component} | ||||||
sx={sxStyles} | ||||||
{...rest} | ||||||
ref={innerRef} | ||||||
data-block={block ? 'block' : null} | ||||||
data-size={size === 'small' || size === 'large' ? size : undefined} | ||||||
data-no-visuals={!LeadingVisual && !TrailingVisual && !TrailingAction ? true : undefined} | ||||||
> | ||||||
{Icon ? ( | ||||||
<Icon /> | ||||||
) : ( | ||||||
<> | ||||||
<Box as="span" data-component="buttonContent" sx={getAlignContentSize(alignContent)}> | ||||||
{LeadingVisual && ( | ||||||
<Box as="span" data-component="leadingVisual" sx={{...iconWrapStyles}}> | ||||||
<LeadingVisual /> | ||||||
</Box> | ||||||
)} | ||||||
{children && <span data-component="text">{children}</span>} | ||||||
{TrailingVisual && ( | ||||||
<Box as="span" data-component="trailingVisual" sx={{...iconWrapStyles}}> | ||||||
<TrailingVisual /> | ||||||
<> | ||||||
<StyledButton | ||||||
as={Component} | ||||||
sx={sxStyles} | ||||||
{...rest} | ||||||
ref={innerRef} | ||||||
data-block={block ? 'block' : null} | ||||||
data-size={size === 'small' || size === 'large' ? size : undefined} | ||||||
data-no-visuals={!LeadingVisual && !TrailingVisual && !TrailingAction ? true : undefined} | ||||||
aria-disabled={loading ? true : undefined} | ||||||
aria-describedby={loading ? 'loading-message' : 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. Should this be?
Suggested change
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'm pretty sure 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. Ohhh sorry yes 100% 😄 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 think we need a unique loading message ID for each button. We could have a page with multiple buttons in a loading state, and we can't have duplicate IDs. I've picked up this PR, so I'll take care of this |
||||||
> | ||||||
{Icon ? ( | ||||||
loading ? ( | ||||||
<Spinner size="small" /> | ||||||
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. Is the spinner size always small or should it depend on the size of the button? 🤔 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. Yeah, I think it looks good in the large button as well. I had the same thought 😆 I'll check again 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. Bonus of not changing the size: it always matches the size of leadingVisual for this satisfying interaction: CleanShot.2023-10-11.at.14.55.13.mp4 |
||||||
) : ( | ||||||
<Icon /> | ||||||
) | ||||||
) : ( | ||||||
<> | ||||||
<Box as="span" data-component="buttonContent" sx={getAlignContentSize(alignContent)}> | ||||||
{loading && ( | ||||||
<Box as="span" data-component="leadingVisual" sx={{...iconWrapStyles}}> | ||||||
<Spinner size="small" /> | ||||||
</Box> | ||||||
)} | ||||||
{LeadingVisual && !loading && ( | ||||||
<Box as="span" data-component="leadingVisual" sx={{...iconWrapStyles}}> | ||||||
<LeadingVisual /> | ||||||
</Box> | ||||||
)} | ||||||
{children && <span data-component="text">{children}</span>} | ||||||
{TrailingVisual && ( | ||||||
<Box as="span" data-component="trailingVisual" sx={{...iconWrapStyles}}> | ||||||
<TrailingVisual /> | ||||||
</Box> | ||||||
)} | ||||||
</Box> | ||||||
{TrailingAction && ( | ||||||
<Box as="span" data-component="trailingAction" sx={{...iconWrapStyles}}> | ||||||
<TrailingAction /> | ||||||
</Box> | ||||||
)} | ||||||
</Box> | ||||||
{TrailingAction && ( | ||||||
<Box as="span" data-component="trailingAction" sx={{...iconWrapStyles}}> | ||||||
<TrailingAction /> | ||||||
</Box> | ||||||
)} | ||||||
</> | ||||||
</> | ||||||
)} | ||||||
</StyledButton> | ||||||
{loading && ( | ||||||
<VisuallyHidden> | ||||||
<span aria-live="polite" aria-busy="true" id="loading-message"> | ||||||
{loadingMessage} | ||||||
</span> | ||||||
</VisuallyHidden> | ||||||
)} | ||||||
</StyledButton> | ||||||
</> | ||||||
) | ||||||
}, | ||||||
) as PolymorphicForwardRefComponent<'button' | 'a', ButtonProps> | ||||||
|
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.
Is it an sr-only message?
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, visually hidden. Should the name of this prop be more specific like
hiddenLoadingMessage
?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.
Hmm good question. I think it is a good idea to indicate it in the name because I searched the code to see where we display the message and when we add this to the prop data it might not be obvious that it is sr only.
hiddenLoadingMessage
sounds like an option.visuallyHiddenLoadingMessage
is too long. Issr-only
wording out-dated? 🤔 Could it besrOnlyLoadingMessage
?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.
Hi!
The prop should ideally hint at what purpose it serves, but I think it doesn't have to describe implementation details.
If
loadingMessage
feels misguiding as the message is not shown in the button, how do you feel about something along the lines ofloadingAnnouncement
? It signals a live region for screen readers without getting into what it doesn't do (visuallyHidden / srOnly)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.
Love that @siddharthkp! Thank you 😄
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.
Love it too 😍