UNPKG

@teamsparta/stack-button

Version:
757 lines (745 loc) 23 kB
"use client"; "use strict"; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __objRest = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop]; } return target; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Button: () => Button, IconButton: () => IconButton, TextButton: () => TextButton }); module.exports = __toCommonJS(src_exports); // src/Button/Button.tsx var import_stack_core = require("@teamsparta/stack-core"); var import_stack_spinner = require("@teamsparta/stack-spinner"); var import_stack_utils2 = require("@teamsparta/stack-utils"); var import_react2 = require("react"); // src/Button/Button.style.ts var import_react = require("@emotion/react"); var import_stack_font = require("@teamsparta/stack-font"); var import_stack_tokens = require("@teamsparta/stack-tokens"); var import_stack_utils = require("@teamsparta/stack-utils"); var bgColorVar = "--stack-button-bg-color"; var hoverBgColorVar = "--stack-button-hover-bg-color"; var textColorVar = "--stack-button-text-color"; var borderColorVar = "--stack-button-border-color"; var buttonSizeVariants = { xs: { minWidth: "52px", height: "36px", paddingInline: "12px", gap: "2px" }, sm: { minWidth: "56px", height: "40px", paddingInline: "12px" }, md: { minWidth: "70px", height: "48px", paddingInline: "14px" }, lg: { minWidth: "100px", height: "52px", gap: "2px", paddingInline: "16px" }, xl: { minWidth: "154px", height: "56px", gap: "4px", paddingInline: "16px" } }; var buttonTextVariants = { xs: { padding: "0 2px" }, sm: { padding: "0 4px" }, md: { padding: "0 4px" }, lg: { padding: "0 4px" }, xl: { padding: "0 4px" } }; var buttonTextFontVariants = { xs: import_stack_font.captionSb, sm: import_stack_font.captionSb, md: import_stack_font.bodyB, lg: import_stack_font.subTitle3, xl: import_stack_font.subTitle3 }; var buttonRadiusVariants = { xs: { borderRadius: "6px" }, sm: { borderRadius: "6px" }, md: { borderRadius: "8px" }, lg: { borderRadius: "8px" }, xl: { borderRadius: "8px" } }; var buttonIconVariants = { xs: { svg: { width: "16px", height: "16px" } }, sm: { svg: { width: "16px", height: "16px" } }, md: { svg: { width: "16px", height: "16px" } }, lg: { svg: { width: "20px", height: "20px" } }, xl: { svg: { width: "20px", height: "20px" } } }; var buttonVariants = { // todo "solid-primary": { [bgColorVar]: import_stack_tokens.vars.button.solidPrimary.background, [hoverBgColorVar]: import_stack_tokens.vars.button.solidPrimary.backgroundHover, [textColorVar]: import_stack_tokens.vars.button.solidPrimary.text }, "solid-secondary": { [bgColorVar]: import_stack_tokens.vars.button.solidSecondary.background, [hoverBgColorVar]: import_stack_tokens.vars.button.solidSecondary.backgroundHover, [textColorVar]: import_stack_tokens.vars.button.solidSecondary.text }, "solid-tertiary": { [bgColorVar]: import_stack_tokens.vars.button.solidTertiary.background, [hoverBgColorVar]: import_stack_tokens.vars.button.solidTertiary.backgroundHover, [textColorVar]: import_stack_tokens.vars.button.solidTertiary.text }, "solid-error": { [bgColorVar]: import_stack_tokens.vars.button.solidError.background, [hoverBgColorVar]: import_stack_tokens.vars.button.solidError.backgroundHover, [textColorVar]: import_stack_tokens.vars.button.solidError.text }, // done "outline-primary": { [bgColorVar]: import_stack_tokens.vars.button.outlinePrimary.background, [hoverBgColorVar]: import_stack_tokens.vars.button.outlinePrimary.backgroundHover, [textColorVar]: import_stack_tokens.vars.button.outlinePrimary.text, [borderColorVar]: import_stack_tokens.vars.button.outlinePrimary.border }, "outline-secondary": { [bgColorVar]: import_stack_tokens.vars.button.outlineSecondary.background, [hoverBgColorVar]: import_stack_tokens.vars.button.outlineSecondary.backgroundHover, [textColorVar]: import_stack_tokens.vars.button.outlineSecondary.text, [borderColorVar]: import_stack_tokens.vars.button.outlineSecondary.border }, "outline-tertiary": {}, "outline-error": {} }; var buttonBaseCss = (0, import_react.css)({ position: "relative", display: "inline-flex", alignItems: "center", justifyContent: "center", cursor: "pointer", outline: "none", transition: "background-color 0.08s ease-in-out", backgroundColor: (0, import_stack_utils.getVar)(bgColorVar), border: `1px solid ${(0, import_stack_utils.getVar)(borderColorVar)}`, color: (0, import_stack_utils.getVar)(textColorVar), userSelect: "none", "@media (hover: hover) and (pointer: fine)": { "&:hover:not(:disabled)": { backgroundColor: (0, import_stack_utils.getVar)(hoverBgColorVar) } }, "&:active:not(:disabled)": { backgroundColor: (0, import_stack_utils.getVar)(hoverBgColorVar) }, "&:focus": { outline: "revert" } }); var buttonIconSizeCss = (sizes) => sizes.map( ({ breakpoint, size }) => import_react.css` @media screen and (min-width: ${breakpoint}px) { ${buttonIconVariants[size]} } ` ); var buttonSizeCss = (sizes) => sizes.map( ({ breakpoint, size }) => import_react.css` @media screen and (min-width: ${breakpoint}px) { ${buttonSizeVariants[size]} } ` ); var buttonWidthCss = (sizes) => sizes.map( ({ breakpoint, value }) => value && import_react.css` @media screen and (min-width: ${breakpoint}px) { width: 100%; } ` ); var buttonTextCss = (sizes) => sizes.map( ({ breakpoint, size }) => import_react.css` @media screen and (min-width: ${breakpoint}px) { ${buttonTextVariants[size]} ${buttonTextFontVariants[size]} } &&& { color: inherit; } ` ); var buttonRadiusCss = (sizes) => sizes.map( ({ breakpoint, size }) => import_react.css` @media screen and (min-width: ${breakpoint}px) { ${buttonRadiusVariants[size]} } ` ); var buttonChildrenCss = (0, import_react.css)({ display: "flex", alignItems: "center", justifyContent: "center", whiteSpace: "nowrap", "&[data-loading='true']": { opacity: 0 } }); var buttonRoundedCss = (0, import_react.css)({ borderRadius: "999px" }); var addonCss = (0, import_react.css)({ display: "inline-flex", alignItems: "center", justifyContent: "center", "& > svg": { color: `${(0, import_stack_utils.getVar)(textColorVar)} !important` }, "&[data-loading='true']": { opacity: 0 } }); var loadingContainerCss = (0, import_react.css)({ cursor: "not-allowed" }); var spinnerContainerCss = (0, import_react.css)({ position: "absolute", top: 0, left: 0, width: "100%", height: "100%", display: "flex", alignItems: "center", justifyContent: "center" }); var adjustTouchableCss = (0, import_react.css)({ display: "block", position: "absolute", minWidth: "48px", minHeight: "48px", width: "100%", height: "100%" }); var disabledCss = (0, import_react.css)({ opacity: "0.35", cursor: "not-allowed" }); // src/Button/Button.tsx var import_jsx_runtime = require("@emotion/react/jsx-runtime"); var RESPONSIVE_SPINNER_SIZE_MAP = { xl: "lg", lg: "lg", md: "md", sm: "sm", xs: "sm" }; var Button = (0, import_react2.forwardRef)( function _Button(_a, ref) { var _b = _a, { variant = "solid", colorScheme = "primary", size: sizeFromProps = "md", radius = "square", children, style: styleFromProps, loading = false, disabled = false, leftAddon, rightAddon, fullWidth = false } = _b, restProps = __objRest(_b, [ "variant", "colorScheme", "size", "radius", "children", "style", "loading", "disabled", "leftAddon", "rightAddon", "fullWidth" ]); const breakpoints = (0, import_stack_core.useBreakpointContext)("Button"); const sizes = (0, import_stack_core.mapResponsive)( breakpoints, sizeFromProps, (breakpoint, size) => ({ breakpoint, size }) ); const fullWidths = (0, import_stack_core.mapResponsive)( breakpoints, fullWidth, (breakpoint, value) => ({ breakpoint, value }) ); const style = __spreadValues(__spreadValues({}, buttonVariants[`${variant}-${colorScheme}`]), styleFromProps); const buttonContainerCss = [ buttonBaseCss, buttonSizeCss(sizes), radius === "rounded" ? buttonRoundedCss : buttonRadiusCss(sizes), disabled && disabledCss, loading && loadingContainerCss, buttonWidthCss(fullWidths) ]; const buttonAddonCss = [addonCss, buttonIconSizeCss(sizes)]; const buttonTextContainerCss = [buttonTextCss(sizes), buttonChildrenCss]; const buttonLoadingContainerCss = [spinnerContainerCss]; const spinnerSize = (0, import_stack_core.mapResponsiveValue)( sizeFromProps, RESPONSIVE_SPINNER_SIZE_MAP ); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( "button", __spreadProps(__spreadValues({ ref, style, css: buttonContainerCss, disabled: disabled || loading }, restProps), { children: [ leftAddon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { css: buttonAddonCss, "data-loading": loading, children: leftAddon }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { css: buttonTextContainerCss, "data-loading": loading, children }), loading && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { css: buttonLoadingContainerCss, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_stack_spinner.Spinner, { size: spinnerSize, color: (0, import_stack_utils2.getVar)(textColorVar) }) }), rightAddon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { css: buttonAddonCss, "data-loading": loading, children: rightAddon }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { css: adjustTouchableCss }) ] }) ); } ); // src/IconButton/IconButton.tsx var import_stack_core2 = require("@teamsparta/stack-core"); var import_stack_utils4 = require("@teamsparta/stack-utils"); var import_react4 = require("react"); // src/IconButton/IconButton.style.ts var import_react3 = require("@emotion/react"); var import_stack_tokens2 = require("@teamsparta/stack-tokens"); var import_stack_utils3 = require("@teamsparta/stack-utils"); var iconButtonSizeVariants = { xs: { width: "36px", height: "36px", padding: "10px" }, sm: { width: "40px", height: "40px", padding: "12px" }, md: { width: "48px", height: "48px", padding: "14px" }, lg: { width: "52px", height: "52px", padding: "16px" }, xl: { width: "56px", height: "56px", padding: "16px" } }; var iconButtonRadiusVariants = { xs: { borderRadius: "6px" }, sm: { borderRadius: "6px" }, md: { borderRadius: "8px" }, lg: { borderRadius: "8px" }, xl: { borderRadius: "8px" } }; var iconButtonIconVariants = { xs: { svg: { width: "16px", height: "16px" } }, sm: { svg: { width: "16px", height: "16px" } }, md: { svg: { width: "20px", height: "20px" } }, lg: { svg: { width: "20px", height: "20px" } }, xl: { svg: { width: "24px", height: "24px" } } }; var iconButtonVariants = { "solid-primary": { [bgColorVar]: import_stack_tokens2.vars.button.solidPrimary.background, [hoverBgColorVar]: import_stack_tokens2.vars.button.solidPrimary.backgroundHover, [textColorVar]: import_stack_tokens2.vars.button.solidPrimary.text }, "solid-secondary": { [bgColorVar]: import_stack_tokens2.vars.button.solidSecondary.background, [hoverBgColorVar]: import_stack_tokens2.vars.button.solidSecondary.backgroundHover, [textColorVar]: import_stack_tokens2.vars.button.solidSecondary.text }, "solid-tertiary": { [bgColorVar]: import_stack_tokens2.vars.button.solidTertiary.background, [hoverBgColorVar]: import_stack_tokens2.vars.button.solidTertiary.backgroundHover, [textColorVar]: import_stack_tokens2.vars.button.solidTertiary.text }, "outline-primary": { [bgColorVar]: import_stack_tokens2.vars.button.outlinePrimary.background, [hoverBgColorVar]: import_stack_tokens2.vars.button.outlinePrimary.backgroundHover, [borderColorVar]: import_stack_tokens2.vars.button.outlinePrimary.border, [textColorVar]: import_stack_tokens2.vars.button.outlinePrimary.text }, "outline-secondary": { [bgColorVar]: import_stack_tokens2.vars.button.outlineSecondary.background, [hoverBgColorVar]: import_stack_tokens2.vars.button.outlineSecondary.backgroundHover, [borderColorVar]: import_stack_tokens2.vars.button.outlineSecondary.border, [textColorVar]: import_stack_tokens2.vars.button.outlineSecondary.text }, // NOTE: outline-tertiary 미사용 "outline-tertiary": {} }; var buttonBaseCss2 = (0, import_react3.css)({ position: "relative", display: "inline-flex", alignItems: "center", justifyContent: "center", cursor: "pointer", outline: "none", transition: "background-color 0.08s ease-in-out", backgroundColor: (0, import_stack_utils3.getVar)(bgColorVar), border: `1px solid ${(0, import_stack_utils3.getVar)(borderColorVar)}`, userSelect: "none", "@media (hover: hover) and (pointer: fine)": { "&:hover:not(:disabled)": { backgroundColor: (0, import_stack_utils3.getVar)(hoverBgColorVar) } }, "&:active:not(:disabled)": { backgroundColor: (0, import_stack_utils3.getVar)(hoverBgColorVar) }, "&:focus": { outline: "revert" } }); var buttonIconSizeCss2 = (sizes) => sizes.map( ({ breakpoint, size }) => import_react3.css` @media screen and (min-width: ${breakpoint}px) { ${iconButtonIconVariants[size]} } ` ); var buttonSizeCss2 = (sizes) => sizes.map( ({ breakpoint, size }) => import_react3.css` @media screen and (min-width: ${breakpoint}px) { ${iconButtonSizeVariants[size]} } ` ); var buttonRadiusCss2 = (sizes) => sizes.map( ({ breakpoint, size }) => import_react3.css` @media screen and (min-width: ${breakpoint}px) { ${iconButtonRadiusVariants[size]} } ` ); var buttonRoundedCss2 = (0, import_react3.css)({ borderRadius: "999px" }); var adjustTouchableCss2 = (0, import_react3.css)({ display: "block", position: "absolute", minWidth: "48px", minHeight: "48px", width: "100%", height: "100%" }); var disabledCss2 = (0, import_react3.css)({ opacity: "0.35", cursor: "not-allowed" }); // src/IconButton/IconButton.tsx var import_jsx_runtime2 = require("@emotion/react/jsx-runtime"); var IconButton = (0, import_react4.forwardRef)( function _IconButton(_a, ref) { var _b = _a, { variant = "solid", colorScheme = "primary", size: sizeFromProps = "md", radius = "square", style: styleFromProps, disabled = false, children } = _b, restProps = __objRest(_b, [ "variant", "colorScheme", "size", "radius", "style", "disabled", "children" ]); var _a2, _b2, _c; const breakpoints = (0, import_stack_core2.useBreakpointContext)("IconButton"); const sizes = (0, import_stack_core2.mapResponsive)( breakpoints, sizeFromProps, (breakpoint, size) => ({ breakpoint, size }) ); const style = __spreadValues(__spreadValues({}, iconButtonVariants[`${variant}-${colorScheme}`]), styleFromProps); const buttonContainerCss = [ buttonBaseCss2, buttonSizeCss2(sizes), radius === "rounded" ? buttonRoundedCss2 : buttonRadiusCss2(sizes), disabled && disabledCss2, buttonIconSizeCss2(sizes) ]; const enhancedIcon = (0, import_react4.isValidElement)(children) ? (0, import_react4.cloneElement)(children, __spreadProps(__spreadValues({}, (_a2 = children.props) != null ? _a2 : {}), { color: (_c = (_b2 = children.props) == null ? void 0 : _b2.color) != null ? _c : (0, import_stack_utils4.getVar)("--stack-button-text-color") })) : children; return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)( "button", __spreadProps(__spreadValues({ ref, style, css: buttonContainerCss, disabled }, restProps), { children: [ enhancedIcon, /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { css: adjustTouchableCss2 }) ] }) ); } ); // src/TextButton/TextButton.tsx var import_stack_core3 = require("@teamsparta/stack-core"); var import_stack_tokens4 = require("@teamsparta/stack-tokens"); var import_react6 = require("react"); // src/TextButton/TextButton.style.ts var import_react5 = require("@emotion/react"); var import_stack_font2 = require("@teamsparta/stack-font"); var import_stack_tokens3 = require("@teamsparta/stack-tokens"); var import_stack_utils5 = require("@teamsparta/stack-utils"); var textButtonIconVariants = { sm: { svg: { width: "16px", height: "16px" } }, md: { svg: { width: "16px", height: "16px" } }, lg: { svg: { width: "20px", height: "20px" } } }; var buttonTextFontVariants2 = { sm: import_stack_font2.captionSb, md: import_stack_font2.bodyB, lg: import_stack_font2.subTitle3 }; var textButtonTextColorVar = "--stack-text-button-text-color"; var buttonTextCss2 = (sizes) => sizes.map( ({ breakpoint, size }) => import_react5.css` @media screen and (min-width: ${breakpoint}px) { ${buttonTextFontVariants2[size]} } ` ); var buttonBaseCss3 = (0, import_react5.css)({ position: "relative", display: "inline-flex", gap: "4px", alignItems: "center", justifyContent: "center", cursor: "pointer", outline: "none", backgroundColor: "transparent", border: "none", textAlign: "center", color: (0, import_stack_utils5.getVar)(textButtonTextColorVar), "&:focus": { outline: "revert" } }); var addonCss2 = (0, import_react5.css)({ display: "inline-flex", alignItems: "center", justifyContent: "center" }); var textButtonColorVariants = { default: { [textButtonTextColorVar]: import_stack_tokens3.vars.neutral[50] }, link: { [textButtonTextColorVar]: import_stack_tokens3.vars.blue[70] } }; var buttonIconSizeCss3 = (sizes) => sizes.map( ({ breakpoint, size }) => import_react5.css` @media screen and (min-width: ${breakpoint}px) { ${textButtonIconVariants[size]} } ` ); // src/TextButton/TextButton.tsx var import_jsx_runtime3 = require("@emotion/react/jsx-runtime"); var TextButton = (0, import_react6.forwardRef)( function _TextButton(_a, ref) { var _b = _a, { variant = "default", size: sizeFromProps = "md", style: styleFromProps, leftAddon, rightAddon, children } = _b, restProps = __objRest(_b, [ "variant", "size", "style", "leftAddon", "rightAddon", "children" ]); var _a2, _b2, _c, _d, _e, _f; const breakpoints = (0, import_stack_core3.useBreakpointContext)("TextButton"); const sizes = (0, import_stack_core3.mapResponsive)( breakpoints, sizeFromProps, (breakpoint, size) => ({ breakpoint, size }) ); const style = __spreadValues(__spreadValues({}, textButtonColorVariants[variant]), styleFromProps); const buttonContainerCss = [ buttonBaseCss3, buttonIconSizeCss3(sizes), buttonTextCss2(sizes) ]; const color = variant === "link" ? import_stack_tokens4.vars.blue[70] : import_stack_tokens4.vars.neutral[50]; const enhancedLeftIcon = (0, import_react6.isValidElement)(leftAddon) ? (0, import_react6.cloneElement)(leftAddon, __spreadProps(__spreadValues({}, (_a2 = leftAddon.props) != null ? _a2 : {}), { color: (_c = (_b2 = leftAddon.props) == null ? void 0 : _b2.color) != null ? _c : color })) : leftAddon; const enhancedRightIcon = (0, import_react6.isValidElement)(rightAddon) ? (0, import_react6.cloneElement)(rightAddon, __spreadProps(__spreadValues({}, (_d = rightAddon.props) != null ? _d : {}), { color: (_f = (_e = rightAddon.props) == null ? void 0 : _e.color) != null ? _f : color })) : rightAddon; return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("button", __spreadProps(__spreadValues({ ref, style, css: buttonContainerCss }, restProps), { children: [ leftAddon && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { css: addonCss2, children: enhancedLeftIcon }), children, rightAddon && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { css: addonCss2, children: enhancedRightIcon }) ] })); } ); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Button, IconButton, TextButton });