generated from timelessco/react-components-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(button): ♻️ split button components & add announcer
- Loading branch information
1 parent
b3dd6fa
commit d5a449e
Showing
5 changed files
with
131 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as React from "react"; | ||
import { cx } from "@renderlesskit/react"; | ||
|
||
import { useTheme } from "../theme"; | ||
import { ButtonProps } from "./Button"; | ||
import { ButtonSpinner } from "./ButtonSpinner"; | ||
import { runIfFn, withIconA11y } from "../utils"; | ||
|
||
export interface ButtonChildrenProps | ||
extends Pick< | ||
ButtonProps, | ||
"suffix" | "prefix" | "spinner" | "loading" | "iconOnly" | ||
> { | ||
size: NonNullable<ButtonProps["size"]>; | ||
} | ||
|
||
export const ButtonChildren: React.FC<ButtonChildrenProps> = props => { | ||
const { children, iconOnly, suffix, prefix, size, loading, spinner } = props; | ||
|
||
if (loading && !prefix && !suffix) { | ||
return ( | ||
<> | ||
<ButtonSpinner spinner={spinner} iconOnly={iconOnly} size={size} /> | ||
<div className="opacity-0"> | ||
{iconOnly ? runIfFn(withIconA11y(iconOnly)) : children} | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<ChildrenWithPrefixSuffix | ||
suffix={suffix} | ||
prefix={prefix} | ||
size={size} | ||
loading={loading} | ||
spinner={spinner} | ||
> | ||
{children} | ||
</ChildrenWithPrefixSuffix> | ||
); | ||
}; | ||
|
||
export interface ChildrenWithPrefixSuffixProps | ||
extends Pick< | ||
ButtonChildrenProps, | ||
"suffix" | "prefix" | "size" | "loading" | "spinner" | ||
> {} | ||
|
||
const ChildrenWithPrefixSuffix: React.FC<ChildrenWithPrefixSuffixProps> = | ||
props => { | ||
const { suffix, prefix, children, size, loading, spinner } = props; | ||
const theme = useTheme(); | ||
const suffixStyles = cx(theme.newButton.suffix.size[size]); | ||
const prefixStyles = cx(theme.newButton.prefix.size[size]); | ||
|
||
return ( | ||
<> | ||
{prefix ? ( | ||
loading && !suffix ? ( | ||
<ButtonSpinner spinner={spinner} size={size} /> | ||
) : ( | ||
runIfFn(withIconA11y(prefix, { className: prefixStyles })) | ||
) | ||
) : null} | ||
<span>{children}</span> | ||
{suffix ? ( | ||
loading ? ( | ||
<ButtonSpinner spinner={spinner} size={size} /> | ||
) : ( | ||
runIfFn(withIconA11y(suffix, { className: suffixStyles })) | ||
) | ||
) : null} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as React from "react"; | ||
import { cx } from "@renderlesskit/react"; | ||
|
||
import { useTheme } from "../theme"; | ||
import { Spinner } from "../spinner"; | ||
import { ButtonChildrenProps } from "./ButtonChildren"; | ||
|
||
export interface ButtonSpinnerProps | ||
extends Pick<ButtonChildrenProps, "spinner" | "size" | "iconOnly"> {} | ||
|
||
export const ButtonSpinner: React.FC<ButtonSpinnerProps> = props => { | ||
const { spinner, iconOnly, size } = props; | ||
const theme = useTheme(); | ||
|
||
if (spinner) return <ButtonSpinnerWrapper>{spinner}</ButtonSpinnerWrapper>; | ||
|
||
const spinnerStyles = cx( | ||
!iconOnly | ||
? theme.newButton.spinner.size[size] | ||
: theme.newButton.spinner.iconOnly.size[size], | ||
); | ||
|
||
return ( | ||
<ButtonSpinnerWrapper> | ||
<Spinner className={spinnerStyles} size="em" /> | ||
</ButtonSpinnerWrapper> | ||
); | ||
}; | ||
|
||
export const ButtonSpinnerWrapper: React.FC = props => ( | ||
<div className="absolute flex items-center justify-center" {...props} /> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters