diff --git a/.changeset/block-button-loading.md b/.changeset/block-button-loading.md new file mode 100644 index 000000000..0cc1442b7 --- /dev/null +++ b/.changeset/block-button-loading.md @@ -0,0 +1,16 @@ +--- +"@jects/jds": minor +--- + +**BlockButton** + +`loading` prop을 추가하고, 버튼 내용을 감싸는 요소를 추가했습니다. 버튼의 직계 자식을 기준으로 스타일을 지정하던 소비처는 확인이 필요합니다. + +**추가** + +- `loading` — 기본값 `false`, 내용을 숨겨 너비를 유지한 채 아이콘 크기의 스피너를 가운데에 표시, `aria-busy`와 `aria-disabled`를 지정하고 클릭과 부모로의 클릭 전파, hover, pressed 오버레이를 막음, 포커스는 유지 +- `disabled`와 함께 지정하면 `disabled` 우선, `asChild`와는 함께 쓸 수 없음 + +**동작 변경 (코드 수정 불필요)** + +- 렌더 구조 — `asChild`가 아니면 아이콘과 레이블이 `button`의 직계 자식이 아니라 내부 `span`의 자식으로 렌더링, `button > [data-part='icon']` 같은 직계 자식 선택자나 루트의 `justify-content`로 아이콘과 레이블을 배치하던 스타일은 확인 필요 diff --git a/.changeset/icon-button-loading.md b/.changeset/icon-button-loading.md new file mode 100644 index 000000000..545285185 --- /dev/null +++ b/.changeset/icon-button-loading.md @@ -0,0 +1,10 @@ +--- +"@jects/jds": patch +--- + +**IconButton** + +`loading` prop을 추가했습니다. 기존 호출부는 수정할 필요가 없습니다. + +- `loading` — 기본값 `false`, 내용을 숨겨 너비를 유지한 채 아이콘 크기의 스피너를 가운데에 표시, `aria-busy`와 `aria-disabled`를 지정하고 클릭과 부모로의 클릭 전파, hover, pressed 오버레이를 막음, 포커스는 유지 +- `disabled`와 함께 지정하면 `disabled` 우선, `asChild`와는 함께 쓸 수 없음 diff --git a/.changeset/label-button-loading.md b/.changeset/label-button-loading.md new file mode 100644 index 000000000..0032d1f70 --- /dev/null +++ b/.changeset/label-button-loading.md @@ -0,0 +1,16 @@ +--- +"@jects/jds": minor +--- + +**LabelButton** + +`loading` prop을 추가하고, 버튼 내용을 감싸는 요소를 추가했습니다. 버튼의 직계 자식을 기준으로 스타일을 지정하던 소비처는 확인이 필요합니다. + +**추가** + +- `loading` — 기본값 `false`, 내용을 숨겨 너비를 유지한 채 아이콘 크기의 스피너를 가운데에 표시, `aria-busy`와 `aria-disabled`를 지정하고 클릭과 부모로의 클릭 전파, hover, pressed 오버레이를 막음, 포커스는 유지 +- `disabled`와 함께 지정하면 `disabled` 우선, `asChild`와는 함께 쓸 수 없음 + +**동작 변경 (코드 수정 불필요)** + +- 렌더 구조 — `asChild`가 아니면 아이콘과 레이블이 `button`의 직계 자식이 아니라 내부 `span`의 자식으로 렌더링, `button > [data-part='icon']` 같은 직계 자식 선택자나 루트의 `justify-content`로 아이콘과 레이블을 배치하던 스타일은 확인 필요 diff --git a/packages/jds/src/components/Button/BlockButton/BlockButton.stories.tsx b/packages/jds/src/components/Button/BlockButton/BlockButton.stories.tsx index 75896c081..95934f62f 100644 --- a/packages/jds/src/components/Button/BlockButton/BlockButton.stories.tsx +++ b/packages/jds/src/components/Button/BlockButton/BlockButton.stories.tsx @@ -52,6 +52,11 @@ const meta: Meta = { description: "비활성화 여부", table: { defaultValue: { summary: "false" } }, }, + loading: { + control: "boolean", + description: "로딩 여부", + table: { defaultValue: { summary: "false" } }, + }, }, } satisfies Meta; @@ -126,6 +131,26 @@ export const BlockButtonDisabled: Story = { ), }; +export const BlockButtonLoading: Story = { + render: () => ( + + {BLOCK_BUTTON_VARIANT_OPTIONS.map(variant => ( + + 레이블 + + ))} + + ), + parameters: { + docs: { + description: { + story: + "포커스는 유지하고 클릭은 막습니다. `disabled`와 함께 지정하면 `disabled`가 우선합니다.", + }, + }, + }, +}; + export const BlockButtonWithIcons: Story = { render: () => ( diff --git a/packages/jds/src/components/Button/BlockButton/BlockButton.tsx b/packages/jds/src/components/Button/BlockButton/BlockButton.tsx index 0bee0f986..b6f69e056 100644 --- a/packages/jds/src/components/Button/BlockButton/BlockButton.tsx +++ b/packages/jds/src/components/Button/BlockButton/BlockButton.tsx @@ -1,11 +1,12 @@ import { clsx } from "clsx"; import { Slot } from "radix-ui"; -import { forwardRef } from "react"; +import { forwardRef, type MouseEvent } from "react"; import { getLabelClassName } from "utils"; import { basicRoot, feedbackRoot, iconSizeMap } from "./blockButton.css"; import type { BlockButtonProps } from "./blockButton.types"; import { Icon } from "../../Icon"; +import { ButtonSpinner, buttonContent, loadingRoot } from "../ButtonSpinner"; export const BlockButton = forwardRef( ( @@ -19,7 +20,9 @@ export const BlockButton = forwardRef( prefixIcon, suffixIcon, disabled = false, + loading = false, className, + onClick, ...restProps }, forwardedRef, @@ -29,21 +32,45 @@ export const BlockButton = forwardRef( ? feedbackRoot({ feedback, size }) : basicRoot({ hierarchy: hierarchy ?? "primary", variant: variant ?? "solid", size }); + const isLoading = loading && !disabled && !asChild; + + const handleClick = (event: MouseEvent) => { + if (isLoading) { + event.preventDefault(); + event.stopPropagation(); + return; + } + onClick?.(event); + }; + const Component = asChild ? Slot.Root : "button"; const nativeButtonProps = asChild ? {} : { type: "button" as const, disabled }; + const content = [ + prefixIcon && , + {children}, + suffixIcon && , + ]; + return ( - {prefixIcon && } - {children} - {suffixIcon && } + {asChild ? content : {content}} + {isLoading && } ); }, diff --git a/packages/jds/src/components/Button/BlockButton/blockButton.types.ts b/packages/jds/src/components/Button/BlockButton/blockButton.types.ts index 44a438835..8fdf18958 100644 --- a/packages/jds/src/components/Button/BlockButton/blockButton.types.ts +++ b/packages/jds/src/components/Button/BlockButton/blockButton.types.ts @@ -37,20 +37,25 @@ interface BlockButtonFeedbackProps { interface BlockButtonNativeProps { asChild?: false; disabled?: boolean; + loading?: boolean; children: ReactNode; } interface BlockButtonCustomProps { asChild: true; disabled?: never; + loading?: never; children: ReactElement; } /** - * `asChild`와 `disabled`는 동시에 사용할 수 없다. + * `asChild`는 `disabled`, `loading`과 동시에 사용할 수 없다. * * asChild로 라우팅 컴포넌트를 전달하면 이동을 자식 요소가 제어하므로, * 비활성 상태는 asChild 없이 ``로 표현한다. + * + * `loading`은 내용을 숨기고 스피너를 표시하며 클릭을 막는다. 포커스는 유지한다. + * `disabled`와 함께 지정하면 `disabled`가 우선한다. */ export type BlockButtonProps = BaseBlockButtonProps & (BlockButtonBasicProps | BlockButtonFeedbackProps) & diff --git a/packages/jds/src/components/Button/ButtonSpinner/ButtonSpinner.tsx b/packages/jds/src/components/Button/ButtonSpinner/ButtonSpinner.tsx new file mode 100644 index 000000000..9133baf93 --- /dev/null +++ b/packages/jds/src/components/Button/ButtonSpinner/ButtonSpinner.tsx @@ -0,0 +1,34 @@ +import { clsx } from "clsx"; + +import * as styles from "./buttonSpinner.css"; +import type { IconSize } from "../../Icon"; +import { sizeMap } from "../../Icon/IconMap"; + +interface ButtonSpinnerProps { + size: IconSize; + className?: string; +} + +const ARC_PATH = "M8 2A6 6 0 0 1 13.638 5.948"; + +export const ButtonSpinner = ({ size, className }: ButtonSpinnerProps) => { + const pixelSize = sizeMap[size]; + + return ( + + + + + + ); +}; diff --git a/packages/jds/src/components/Button/ButtonSpinner/buttonSpinner.css.ts b/packages/jds/src/components/Button/ButtonSpinner/buttonSpinner.css.ts new file mode 100644 index 000000000..9dcc1dfbe --- /dev/null +++ b/packages/jds/src/components/Button/ButtonSpinner/buttonSpinner.css.ts @@ -0,0 +1,59 @@ +import { keyframes, style } from "@vanilla-extract/css"; +import { vars } from "tokens"; +import { overlayHoverOpacity, overlayPressedOpacity } from "utils"; + +export const loadingRoot = style({ + selectors: { + "&[data-loading]": { + cursor: "progress", + vars: { + [overlayHoverOpacity]: "0", + [overlayPressedOpacity]: "0", + }, + }, + }, +}); + +export const buttonContent = style({ + display: "inline-flex", + alignItems: "center", + gap: "inherit", + selectors: { + "[data-loading] > &": { opacity: 0 }, + }, +}); + +export const root = style({ + position: "absolute", + inset: 0, + margin: "auto", + pointerEvents: "none", +}); + +export const track = style({ + opacity: `calc(${vars.scheme.semantic.opacity["29"]} / 100)`, +}); + +const tailRotate = keyframes({ + "0%": { transform: "rotate(0deg)" }, + "50%": { transform: "rotate(360deg)" }, + "100%": { transform: "rotate(720deg)" }, +}); + +const headRotate = keyframes({ + "0%": { transform: "rotate(0deg)" }, + "50%": { transform: "rotate(430deg)" }, + "100%": { transform: "rotate(720deg)" }, +}); + +const arc = style({ + transformBox: "view-box", + transformOrigin: "50% 50%", + animationDuration: "2s", + animationTimingFunction: "ease", + animationIterationCount: "infinite", +}); + +export const tail = style([arc, { animationName: tailRotate }]); + +export const head = style([arc, { animationName: headRotate }]); diff --git a/packages/jds/src/components/Button/ButtonSpinner/index.ts b/packages/jds/src/components/Button/ButtonSpinner/index.ts new file mode 100644 index 000000000..7cc658995 --- /dev/null +++ b/packages/jds/src/components/Button/ButtonSpinner/index.ts @@ -0,0 +1,2 @@ +export { ButtonSpinner } from "./ButtonSpinner"; +export { buttonContent, loadingRoot } from "./buttonSpinner.css"; diff --git a/packages/jds/src/components/Button/IconButton/IconButton.stories.tsx b/packages/jds/src/components/Button/IconButton/IconButton.stories.tsx index 045f0fb26..fbf988afe 100644 --- a/packages/jds/src/components/Button/IconButton/IconButton.stories.tsx +++ b/packages/jds/src/components/Button/IconButton/IconButton.stories.tsx @@ -46,6 +46,11 @@ const meta: Meta = { description: "비활성화 여부", table: { defaultValue: { summary: "false" } }, }, + loading: { + control: "boolean", + description: "로딩 여부", + table: { defaultValue: { summary: "false" } }, + }, "aria-label": { control: "text", description: @@ -118,6 +123,30 @@ export const IconButtonDisabled: Story = { ), }; +export const IconButtonLoading: Story = { + render: () => ( + + {ICON_BUTTON_HIERARCHY_OPTIONS.map(hierarchy => ( + + ))} + + ), + parameters: { + docs: { + description: { + story: + "포커스는 유지하고 클릭은 막습니다. `disabled`와 함께 지정하면 `disabled`가 우선합니다.", + }, + }, + }, +}; + export const IconButtonCondensed: Story = { render: () => ( diff --git a/packages/jds/src/components/Button/IconButton/IconButton.tsx b/packages/jds/src/components/Button/IconButton/IconButton.tsx index 275843d71..ec79df553 100644 --- a/packages/jds/src/components/Button/IconButton/IconButton.tsx +++ b/packages/jds/src/components/Button/IconButton/IconButton.tsx @@ -1,11 +1,19 @@ import { assignInlineVars } from "@vanilla-extract/dynamic"; import { clsx } from "clsx"; import { Slot } from "radix-ui"; -import { cloneElement, forwardRef, isValidElement, useEffect, type ReactNode } from "react"; +import { + cloneElement, + forwardRef, + isValidElement, + useEffect, + type MouseEvent, + type ReactNode, +} from "react"; import * as styles from "./iconButton.css"; import type { IconButtonProps } from "./iconButton.types"; import { Icon } from "../../Icon"; +import { ButtonSpinner, loadingRoot } from "../ButtonSpinner"; export const IconButton = forwardRef( ( @@ -17,9 +25,11 @@ export const IconButton = forwardRef( hierarchy = "primary", condensed = true, disabled = false, + loading = false, accentColor, className, style, + onClick, ...restProps }, forwardedRef, @@ -45,6 +55,17 @@ export const IconButton = forwardRef( ); }, [hasIgnoredChildren]); + const isLoading = loading && !disabled && !asChild; + + const handleClick = (event: MouseEvent) => { + if (isLoading) { + event.preventDefault(); + event.stopPropagation(); + return; + } + onClick?.(event); + }; + const Component = asChild ? Slot.Root : "button"; const nativeButtonProps = asChild ? {} : { type: "button" as const, disabled }; @@ -53,9 +74,12 @@ export const IconButton = forwardRef( ref={forwardedRef} {...nativeButtonProps} {...restProps} + {...(isLoading && { "aria-disabled": true, "aria-busy": true })} + onClick={handleClick} data-disabled={disabled || undefined} + data-loading={isLoading || undefined} data-part='root' - className={clsx(styles.root({ hierarchy, size, condensed }), className)} + className={clsx(styles.root({ hierarchy, size, condensed }), loadingRoot, className)} style={{ ...accentStyle, ...style }} > {asChild && ( @@ -64,6 +88,7 @@ export const IconButton = forwardRef( )} + {isLoading && } ); }, diff --git a/packages/jds/src/components/Button/IconButton/iconButton.css.ts b/packages/jds/src/components/Button/IconButton/iconButton.css.ts index 3f745deea..327fd7c6e 100644 --- a/packages/jds/src/components/Button/IconButton/iconButton.css.ts +++ b/packages/jds/src/components/Button/IconButton/iconButton.css.ts @@ -179,4 +179,11 @@ export const root = recipe({ export const icon = style({ color: iconButtonIconColor, + selectors: { + "[data-loading] > &": { opacity: 0 }, + }, +}); + +export const spinner = style({ + color: iconButtonIconColor, }); diff --git a/packages/jds/src/components/Button/IconButton/iconButton.types.ts b/packages/jds/src/components/Button/IconButton/iconButton.types.ts index 62e7f962a..2e0e52827 100644 --- a/packages/jds/src/components/Button/IconButton/iconButton.types.ts +++ b/packages/jds/src/components/Button/IconButton/iconButton.types.ts @@ -46,21 +46,26 @@ interface IconButtonAccentProps { interface IconButtonNativeProps { asChild?: false; disabled?: boolean; + loading?: boolean; children?: never; } interface IconButtonCustomProps { asChild: true; disabled?: never; + loading?: never; children: ReactElement; } /** - * `asChild`와 `disabled`는 동시에 사용할 수 없다. + * `asChild`는 `disabled`, `loading`과 동시에 사용할 수 없다. * * asChild로 라우팅 컴포넌트를 전달하면 이동을 자식 요소가 제어하므로, * 비활성 상태는 asChild 없이 ``로 표현한다. * + * `loading`은 내용을 숨기고 스피너를 표시하며 클릭을 막는다. 포커스는 유지한다. + * `disabled`와 함께 지정하면 `disabled`가 우선한다. + * * asChild로 전달한 요소의 children은 무시하고 `icon`만 렌더링한다. * 요소는 비워서 전달하고, 접근 이름은 `aria-label`로 지정한다. * diff --git a/packages/jds/src/components/Button/LabelButton/LabelButton.stories.tsx b/packages/jds/src/components/Button/LabelButton/LabelButton.stories.tsx index a02ef9090..4bb4a114d 100644 --- a/packages/jds/src/components/Button/LabelButton/LabelButton.stories.tsx +++ b/packages/jds/src/components/Button/LabelButton/LabelButton.stories.tsx @@ -45,6 +45,11 @@ const meta: Meta = { description: "비활성화 여부", table: { defaultValue: { summary: "false" } }, }, + loading: { + control: "boolean", + description: "로딩 여부", + table: { defaultValue: { summary: "false" } }, + }, }, } satisfies Meta; @@ -106,6 +111,26 @@ export const LabelButtonDisabled: Story = { ), }; +export const LabelButtonLoading: Story = { + render: () => ( + + {LABEL_BUTTON_HIERARCHY_OPTIONS.map(hierarchy => ( + + 레이블 + + ))} + + ), + parameters: { + docs: { + description: { + story: + "포커스는 유지하고 클릭은 막습니다. `disabled`와 함께 지정하면 `disabled`가 우선합니다.", + }, + }, + }, +}; + export const LabelButtonWithIcons: Story = { render: () => ( diff --git a/packages/jds/src/components/Button/LabelButton/LabelButton.tsx b/packages/jds/src/components/Button/LabelButton/LabelButton.tsx index 5d4b3a4d8..fc50761fc 100644 --- a/packages/jds/src/components/Button/LabelButton/LabelButton.tsx +++ b/packages/jds/src/components/Button/LabelButton/LabelButton.tsx @@ -1,11 +1,12 @@ import { clsx } from "clsx"; import { Slot } from "radix-ui"; -import { forwardRef } from "react"; +import { forwardRef, type MouseEvent } from "react"; import { getLabelClassName } from "utils"; import { basicRoot, feedbackRoot, iconSizeMap } from "./labelButton.css"; import type { LabelButtonProps } from "./labelButton.types"; import { Icon } from "../../Icon"; +import { ButtonSpinner, buttonContent, loadingRoot } from "../ButtonSpinner"; export const LabelButton = forwardRef( ( @@ -18,7 +19,9 @@ export const LabelButton = forwardRef( prefixIcon, suffixIcon, disabled = false, + loading = false, className, + onClick, ...restProps }, forwardedRef, @@ -28,21 +31,45 @@ export const LabelButton = forwardRef( ? feedbackRoot({ feedback, size }) : basicRoot({ hierarchy: hierarchy ?? "primary", size }); + const isLoading = loading && !disabled && !asChild; + + const handleClick = (event: MouseEvent) => { + if (isLoading) { + event.preventDefault(); + event.stopPropagation(); + return; + } + onClick?.(event); + }; + const Component = asChild ? Slot.Root : "button"; const nativeButtonProps = asChild ? {} : { type: "button" as const, disabled }; + const content = [ + prefixIcon && , + {children}, + suffixIcon && , + ]; + return ( - {prefixIcon && } - {children} - {suffixIcon && } + {asChild ? content : {content}} + {isLoading && } ); }, diff --git a/packages/jds/src/components/Button/LabelButton/labelButton.types.ts b/packages/jds/src/components/Button/LabelButton/labelButton.types.ts index 143fb9939..d1050766f 100644 --- a/packages/jds/src/components/Button/LabelButton/labelButton.types.ts +++ b/packages/jds/src/components/Button/LabelButton/labelButton.types.ts @@ -38,20 +38,25 @@ interface LabelButtonFeedbackProps { interface LabelButtonNativeProps { asChild?: false; disabled?: boolean; + loading?: boolean; children: ReactNode; } interface LabelButtonCustomProps { asChild: true; disabled?: never; + loading?: never; children: ReactElement; } /** - * `asChild`와 `disabled`는 동시에 사용할 수 없다. + * `asChild`는 `disabled`, `loading`과 동시에 사용할 수 없다. * * asChild로 라우팅 컴포넌트를 전달하면 이동을 자식 요소가 제어하므로, * 비활성 상태는 asChild 없이 ``로 표현한다. + * + * `loading`은 내용을 숨기고 스피너를 표시하며 클릭을 막는다. 포커스는 유지한다. + * `disabled`와 함께 지정하면 `disabled`가 우선한다. */ export type LabelButtonProps = BaseLabelButtonProps & (LabelButtonBasicProps | LabelButtonFeedbackProps) & diff --git a/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-blockbutton--block-button-loading_0_storybook-root_0_desktop.png b/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-blockbutton--block-button-loading_0_storybook-root_0_desktop.png new file mode 100644 index 000000000..01ed54673 Binary files /dev/null and b/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-blockbutton--block-button-loading_0_storybook-root_0_desktop.png differ diff --git a/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-iconbutton--icon-button-loading_0_storybook-root_0_desktop.png b/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-iconbutton--icon-button-loading_0_storybook-root_0_desktop.png new file mode 100644 index 000000000..332ac7ef0 Binary files /dev/null and b/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-iconbutton--icon-button-loading_0_storybook-root_0_desktop.png differ diff --git a/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-labelbutton--label-button-loading_0_storybook-root_0_desktop.png b/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-labelbutton--label-button-loading_0_storybook-root_0_desktop.png new file mode 100644 index 000000000..29ef4bd2e Binary files /dev/null and b/packages/vrt/backstop_data/bitmaps_reference/jds-vrt_components-labelbutton--label-button-loading_0_storybook-root_0_desktop.png differ