Skip to content

Commit

Permalink
Merge pull request #2442 from WTTJ/feat-button-loading-state
Browse files Browse the repository at this point in the history
feat(button): add loading state
  • Loading branch information
mleralec authored Apr 25, 2024
2 parents 81ec13a + 97b5a6b commit 60c067a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 43 deletions.
8 changes: 8 additions & 0 deletions docs/pages/components/button.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ All disabled buttons have the same style.
</Button>
```

## Loading

```jsx
<Button variant="primary" isLoading>
Loading
</Button>
```

## With icons

```jsx row
Expand Down
2 changes: 2 additions & 0 deletions packages/Button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
},
"dependencies": {
"@ariakit/react": "0.4.3",
"@welcome-ui/box": "^5.12.0",
"@welcome-ui/loader": "^5.12.0",
"@welcome-ui/system": "^5.12.0",
"@welcome-ui/utils": "^5.12.0"
},
Expand Down
53 changes: 41 additions & 12 deletions packages/Button/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react'
import { CreateWuiProps, forwardRef } from '@welcome-ui/system'
import { Box } from '@welcome-ui/box'
import { Loader } from '@welcome-ui/loader'

import * as S from './styles'

Expand All @@ -22,6 +24,7 @@ export type Variant =

export interface ButtonOptions {
disabled?: boolean
isLoading?: boolean
size?: Size
variant?: Variant
shape?: Shape
Expand All @@ -33,18 +36,44 @@ export type ButtonProps = CreateWuiProps<'button', ButtonOptions>
* @tag button
*/
export const Button = forwardRef<'button', ButtonProps>(
({ children, dataTestId, disabled, size = 'md', variant = 'primary', ...rest }, ref) => (
<S.Button
data-testid={dataTestId}
disabled={disabled}
ref={ref}
size={size}
variant={disabled ? 'disabled' : variant}
{...rest}
>
{children}
</S.Button>
)
(
{ children, dataTestId, disabled, isLoading, size = 'md', variant = 'primary', ...rest },
ref
) => {
const isDisabled = disabled || isLoading

return (
<S.Button
data-loading={isLoading}
data-testid={dataTestId}
disabled={isDisabled}
ref={ref}
size={size}
variant={isDisabled ? 'disabled' : variant}
{...rest}
>
{isLoading && (
<div>
<Box
alignItems="center"
bottom={0}
display="flex"
justifyContent="center"
left={0}
m={0}
position="absolute"
right={0}
top={0}
>
<Loader size="xs" />
</Box>
<Box opacity="0">{children}</Box>
</div>
)}
{!isLoading && children}
</S.Button>
)
}
)

Button.displayName = 'Button'
Expand Down
12 changes: 12 additions & 0 deletions packages/Button/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,16 @@ describe('<Button />', () => {
'font-size': theme.buttons.icon.only.md,
})
})

it('should have data-loading attribute', () => {
render(
<Button dataTestId="button" isLoading size="md">
{content}
</Button>
)

const button = screen.getByTestId('button')

expect(button).toHaveAttribute('data-loading', 'true')
})
})
63 changes: 32 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9472,9 +9472,9 @@ [email protected]:
from2 "^2.1.0"

follow-redirects@^1.0.0, follow-redirects@^1.15.0:
version "1.15.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf"
integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

for-each@^0.3.3:
version "0.3.3"
Expand Down Expand Up @@ -10491,12 +10491,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-3.8.1.tgz#39b0e16add9b605bf0a9ef3d9daaf4843b4cacd2"
integrity sha512-5ai2iksyV8ZXmnZhHH4rWPoxxistEexSi5936zIQ1bnNTW5VnA85B6P/VpXiRM017IgRvb2kKo1a//y+0wSp3w==

http-cache-semantics@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==

http-cache-semantics@^4.1.1:
http-cache-semantics@^4.1.0, http-cache-semantics@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a"
integrity sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ==
Expand Down Expand Up @@ -13436,6 +13431,11 @@ minipass@^4.0.2, minipass@^4.2.4:
resolved "https://registry.yarnpkg.com/minipass/-/minipass-4.2.4.tgz#7d0d97434b6a19f59c5c3221698b48bbf3b2cd06"
integrity sha512-lwycX3cBMTvcejsHITUgYj6Gy6A7Nh4Q6h9NP4sTHY1ccJlC7yKzDmiShEHsJ16Jf1nKGDEaiHxiltsJEvk0nQ==

minipass@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-5.0.0.tgz#3e9788ffb90b694a5d0ec94479a45b5d8738133d"
integrity sha512-3FnjYuehv9k6ovOEbyOswadCDPX1piCfhV8ncmYtHOjuPwylVWsghTLo7rabjC3Rx5xD4HDx8Wm1xnMF7S5qFQ==

"minipass@^5.0.0 || ^6.0.2 || ^7.0.0":
version "7.0.4"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.0.4.tgz#dbce03740f50a4786ba994c1fb908844d27b038c"
Expand Down Expand Up @@ -13580,6 +13580,11 @@ nanoid@^3.3.6:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==

nanoid@^3.3.7:
version "3.3.7"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.7.tgz#d0c301a691bc8d54efa0a2226ccf3fe2fd656bd8"
integrity sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
Expand Down Expand Up @@ -15235,23 +15240,14 @@ postcss@^8.1.10:
picocolors "^1.0.0"
source-map-js "^1.0.2"

postcss@^8.4.19:
version "8.4.21"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.21.tgz#c639b719a57efc3187b13a1d765675485f4134f4"
integrity sha512-tP7u/Sn/dVxK2NnruI4H9BG+x+Wxz6oeZ1cJ8P6G/PZY0IKk4k/63TDsQf2kQq3+qoJeLm2kIBUNlZe3zgb4Zg==
dependencies:
nanoid "^3.3.4"
picocolors "^1.0.0"
source-map-js "^1.0.2"

postcss@^8.4.23:
version "8.4.24"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.24.tgz#f714dba9b2284be3cc07dbd2fc57ee4dc972d2df"
integrity sha512-M0RzbcI0sO/XJNucsGjvWU9ERWxb/ytp1w6dKtxTKgixdtQDq4rmx/g8W1hnaheq9jgwL/oyEdH5Bc4WwJKMqg==
postcss@^8.4.19, postcss@^8.4.23:
version "8.4.38"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.38.tgz#b387d533baf2054288e337066d81c6bee9db9e0e"
integrity sha512-Wglpdk03BSfXkHoQa3b/oulrotAkwrlLDRSOb9D0bN86FdRyE9lppSp33aHNPgBa0JKCoB+drFLZkQoRRYae5A==
dependencies:
nanoid "^3.3.6"
nanoid "^3.3.7"
picocolors "^1.0.0"
source-map-js "^1.0.2"
source-map-js "^1.2.0"

precinct@^11.0.0:
version "11.0.5"
Expand Down Expand Up @@ -15450,9 +15446,9 @@ proto-list@~1.2.1:
integrity sha512-vtK/94akxsTMhe0/cbfpR+syPuszcuwhqVjJq26CuNDgFGj682oRBXOP5MJpv2r7JtE8MsiepGIqvvOTBwn2vA==

protobufjs@^7.1.2, protobufjs@^7.2.4:
version "7.2.4"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-7.2.4.tgz#3fc1ec0cdc89dd91aef9ba6037ba07408485c3ae"
integrity sha512-AT+RJgD2sH8phPmCf7OUZR8xGdcJRga4+1cOaXJ64hvcSkVhNcRHOwIxUatPH15+nj59WAGTDv3LSGZPEQbJaQ==
version "7.2.6"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-7.2.6.tgz#4a0ccd79eb292717aacf07530a07e0ed20278215"
integrity "sha1-SgzNeespJxeqzwdTCgfg7SAnghU= sha512-dgJaEDDL6x8ASUZ1YqWciTRrdOuYNzoOf27oHNfdyvKqHr5i0FV7FSLU+aIeFjyFgVxrpTOtQUi0BLLBymZaBw=="
dependencies:
"@protobufjs/aspromise" "^1.1.2"
"@protobufjs/base64" "^1.1.2"
Expand Down Expand Up @@ -16806,6 +16802,11 @@ source-map-js@^1.0.1, source-map-js@^1.0.2:
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c"
integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==

source-map-js@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.0.tgz#16b809c162517b5b8c3e7dcd315a2a5c2612b2af"
integrity sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==

source-map-resolve@^0.5.0:
version "0.5.3"
resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a"
Expand Down Expand Up @@ -17561,13 +17562,13 @@ [email protected]:
yallist "^4.0.0"

tar@^6.1.11, tar@^6.1.2:
version "6.1.13"
resolved "https://registry.yarnpkg.com/tar/-/tar-6.1.13.tgz#46e22529000f612180601a6fe0680e7da508847b"
integrity sha512-jdIBIN6LTIe2jqzay/2vtYLlBHa3JF42ot3h1dW8Q0PaAG4v8rm0cvpVePtau5C6OKXGGcgO9q2AMNSWxiLqKw==
version "6.2.1"
resolved "https://registry.yarnpkg.com/tar/-/tar-6.2.1.tgz#717549c541bc3c2af15751bea94b1dd068d4b03a"
integrity sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==
dependencies:
chownr "^2.0.0"
fs-minipass "^2.0.0"
minipass "^4.0.0"
minipass "^5.0.0"
minizlib "^2.1.1"
mkdirp "^1.0.3"
yallist "^4.0.0"
Expand Down

0 comments on commit 60c067a

Please sign in to comment.