Skip to content
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

Button - Loading #4502

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/eight-dolls-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/core": minor
---

Added `loading` prop/functionality to the Button component. This prop will display a spinner to indicate that the action is in progress. This is useful for actions that take some time to process, such as submitting a form or loading data.
16 changes: 15 additions & 1 deletion packages/core/src/__tests__/__e2e__/button/Button.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { composeStories } from "@storybook/react";
import { checkAccessibility } from "../../../../../../cypress/tests/checkAccessibility";

const composedStories = composeStories(buttonStories);
const { Default, FocusableWhenDisabled } = composedStories;
const { Default, FocusableWhenDisabled, LoadingSingle } = composedStories;

describe("Given a Button", () => {
checkAccessibility(composedStories);
Expand Down Expand Up @@ -51,6 +51,20 @@ describe("Given a Button", () => {
cy.get("@clickSpy").should("not.be.called");
});

it("should be focusable when loading", () => {
const clickSpy = cy.stub().as("clickSpy");
cy.mount(<LoadingSingle onClick={clickSpy} />);
cy.findByRole("button").should("have.attr", "data-loading", "true");
cy.realPress("Tab");
cy.findByRole("button").should("be.focused");
cy.realPress("Enter");
cy.get("@clickSpy").should("not.be.called");
cy.realPress("Space");
cy.get("@clickSpy").should("not.be.called");
cy.findByRole("button").realClick();
cy.get("@clickSpy").should("not.be.called");
});

it("should apply type prop to button element", () => {
cy.mount(<Default type="submit" />);
cy.findByRole("button").should("have.attr", "type", "submit");
Expand Down
95 changes: 95 additions & 0 deletions packages/core/src/button/Button.css
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@
--button-borderColor-disabled: var(--salt-actionable-accented-subtle-borderColor-disabled);
}

.saltButton-accented.saltButton-loading {
--button-text-color: var(--salt-actionable-accented-background);
--button-text-color-hover: var(--salt-actionable-accented-background);
--button-text-color-active: var(--salt-actionable-accented-background);
--button-text-color-disabled: var(--salt-actionable-accented-background);
--button-background: var(--salt-actionable-accented-background);
--button-background-hover: var(--salt-actionable-accented-background);
--button-background-active: var(--salt-actionable-accented-background);
--button-background-disabled: var(--salt-actionable-accented-background);
--button-borderColor: var(--salt-actionable-accented-borderColor);
--button-borderColor-hover: var(--salt-actionable-accented-borderColor);
--button-borderColor-active: var(--salt-actionable-accented-borderColor);
--button-borderColor-disabled: var(--salt-actionable-accented-borderColor);
cursor: progress;
}

.saltButton-neutral.saltButton-solid {
--button-text-color: var(--salt-actionable-bold-foreground);
--button-text-color-hover: var(--salt-actionable-bold-foreground-hover);
Expand Down Expand Up @@ -176,6 +192,22 @@
--button-borderColor-disabled: var(--salt-actionable-subtle-borderColor-disabled);
}

.saltButton-neutral.saltButton-loading {
--button-text-color: var(--salt-actionable-background);
--button-text-color-hover: var(--salt-actionable-background);
--button-text-color-active: var(--salt-actionable-background);
--button-text-color-disabled: var(--salt-actionable-background);
--button-background: var(--salt-actionable-background);
--button-background-hover: var(--salt-actionable-background);
--button-background-active: var(--salt-actionable-background);
--button-background-disabled: var(--salt-actionable-background);
--button-borderColor: var(--salt-actionable-borderColor);
--button-borderColor-hover: var(--salt-actionable-borderColor);
--button-borderColor-active: var(--salt-actionable-borderColor);
--button-borderColor-disabled: var(--salt-actionable-borderColor);
cursor: progress;
}

.saltButton-negative.saltButton-solid {
--button-text-color: var(--salt-actionable-negative-bold-foreground);
--button-text-color-hover: var(--salt-actionable-negative-bold-foreground-hover);
Expand Down Expand Up @@ -221,6 +253,22 @@
--button-borderColor-disabled: var(--salt-actionable-negative-subtle-borderColor-disabled);
}

.saltButton-negative.saltButton-loading {
--button-text-color: var(--salt-actionable-negative-background);
--button-text-color-hover: var(--salt-actionable-negative-background);
--button-text-color-active: var(--salt-actionable-negative-background);
--button-text-color-disabled: var(--salt-actionable-negative-background);
--button-background: var(--salt-actionable-negative-background);
--button-background-hover: var(--salt-actionable-negative-background);
--button-background-active: var(--salt-actionable-negative-background);
--button-background-disabled: var(--salt-actionable-negative-background);
--button-borderColor: var(--salt-actionable-negative-borderColor);
--button-borderColor-hover: var(--salt-actionable-negative-borderColor);
--button-borderColor-active: var(--salt-actionable-negative-borderColor);
--button-borderColor-disabled: var(--salt-actionable-negative-borderColor);
cursor: progress;
}

.saltButton-positive.saltButton-solid {
--button-text-color: var(--salt-actionable-positive-bold-foreground);
--button-text-color-hover: var(--salt-actionable-positive-bold-foreground-hover);
Expand Down Expand Up @@ -266,6 +314,22 @@
--button-borderColor-disabled: var(--salt-actionable-positive-subtle-borderColor-disabled);
}

.saltButton-positive.saltButton-loading {
--button-text-color: var(--salt-actionable-positive-background);
--button-text-color-hover: var(--salt-actionable-positive-background);
--button-text-color-active: var(--salt-actionable-positive-background);
--button-text-color-disabled: var(--salt-actionable-positive-background);
--button-background: var(--salt-actionable-positive-background);
--button-background-hover: var(--salt-actionable-positive-background);
--button-background-active: var(--salt-actionable-positive-background);
--button-background-disabled: var(--salt-actionable-positive-background);
--button-borderColor: var(--salt-actionable-positive-borderColor);
--button-borderColor-hover: var(--salt-actionable-positive-borderColor);
--button-borderColor-active: var(--salt-actionable-positive-borderColor);
--button-borderColor-disabled: var(--salt-actionable-positive-borderColor);
cursor: progress;
}

.saltButton-caution.saltButton-solid {
--button-text-color: var(--salt-actionable-caution-bold-foreground);
--button-text-color-hover: var(--salt-actionable-caution-bold-foreground-hover);
Expand Down Expand Up @@ -310,3 +374,34 @@
--button-borderColor-active: var(--salt-actionable-caution-subtle-borderColor-active);
--button-borderColor-disabled: var(--salt-actionable-caution-subtle-borderColor-disabled);
}

.saltButton-caution.saltButton-loading {
--button-text-color: var(--salt-actionable-caution-background);
--button-text-color-hover: var(--salt-actionable-caution-background);
--button-text-color-active: var(--salt-actionable-caution-background);
--button-text-color-disabled: var(--salt-actionable-caution-background);
--button-background: var(--salt-actionable-caution-background);
--button-background-hover: var(--salt-actionable-caution-background);
--button-background-active: var(--salt-actionable-caution-background);
--button-background-disabled: var(--salt-actionable-caution-background);
--button-borderColor: var(--salt-actionable-caution-borderColor);
--button-borderColor-hover: var(--salt-actionable-caution-borderColor);
--button-borderColor-active: var(--salt-actionable-caution-borderColor);
--button-borderColor-disabled: var(--salt-actionable-caution-borderColor);
cursor: progress;
}

.saltButton-spinner {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
}

.saltButton-sr-only {
position: fixed;
top: 0;
left: 0;
transform: translate(-100%, -100%);
}
17 changes: 17 additions & 0 deletions packages/core/src/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Spinner } from "@salt-ds/core";
import { useComponentCssInjection } from "@salt-ds/styles";
import { useWindow } from "@salt-ds/window";
import { clsx } from "clsx";
Expand Down Expand Up @@ -66,6 +67,13 @@ export interface ButtonProps extends ComponentPropsWithoutRef<"button"> {
* @since 1.36.0.
*/
sentiment?: ButtonSentiment;

/**
* If `true`, the button will be in a loading state. This allows a spinner to be nested inside the button.
*
* @since 1.38.0.
*/
loading?: boolean;
}

function variantToAppearanceAndColor(
Expand All @@ -87,6 +95,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
children,
className,
disabled,
loading,
focusableWhenDisabled,
onKeyUp,
onKeyDown,
Expand All @@ -101,6 +110,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
ref?,
): ReactElement<ButtonProps> {
const { active, buttonProps } = useButton({
loading,
disabled,
focusableWhenDisabled,
onKeyUp,
Expand All @@ -119,6 +129,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
const mapped = variantToAppearanceAndColor(variant);
const appearance: ButtonAppearance =
appearanceProp ?? mapped.appearance ?? "solid";

const sentiment: ButtonSentiment =
sentimentProp ?? mapped.sentiment ?? "neutral";

Expand All @@ -132,6 +143,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
withBaseName(),
withBaseName(variant),
{
[withBaseName("loading")]: loading,
[withBaseName("disabled")]: disabled,
[withBaseName("active")]: active,
[withBaseName(appearance)]: appearance,
Expand All @@ -143,6 +155,11 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
ref={ref}
type={type}
>
{loading && (
<div className={withBaseName("spinner")} aria-hidden>
<Spinner size="small" aria-hidden disableAnnouncer />
</div>
)}
{children}
</button>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/button/useButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "react";

export interface ButtonHookProps<T extends Element> {
loading?: boolean;
disabled?: boolean;
focusableWhenDisabled?: boolean;
onKeyUp?: (event: KeyboardEvent<T>) => void;
Expand All @@ -19,6 +20,7 @@ export interface ButtonHookResult<T extends Element> {
active: boolean;
buttonProps: {
"aria-disabled"?: boolean;
"data-loading"?: boolean;
disabled?: boolean;
tabIndex: number;
onBlur: (event: FocusEvent<T>) => void;
Expand All @@ -29,6 +31,7 @@ export interface ButtonHookResult<T extends Element> {
}

export const useButton = <T extends Element>({
loading,
disabled,
focusableWhenDisabled,
onKeyUp,
Expand Down Expand Up @@ -83,10 +86,11 @@ export const useButton = <T extends Element>({

const buttonProps = {
"aria-disabled": disabled && focusableWhenDisabled ? true : undefined,
"data-loading": loading,
disabled: disabled && !focusableWhenDisabled,
tabIndex: disabled && !focusableWhenDisabled ? -1 : 0,
onBlur: handleBlur,
onClick: !disabled ? handleClick : undefined,
onClick: !loading && !disabled ? handleClick : undefined,
onKeyDown: handleKeyDown,
onKeyUp: handleKeyUp,
};
Expand Down
3 changes: 3 additions & 0 deletions packages/core/stories/button/button.qa.stories.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.saltButton.noSpin .saltSpinner-spinner {
animation: none;
}
Loading
Loading