import { resolvePaintedTheme, useConfigAppearance } from "../config/use-config-appearance"; import clsx from "clsx"; import { Loading } from "kui-icons"; import React, { useContext } from "react"; import { SizeContext } from "../config/size-context"; import type { ButtonType, ShapeType, SizeType, SurfaceThemeType } from "../const/types"; import { colors } from "../const/var"; import Icon, { type IconType } from "../icon"; import { getChildren } from "../utils/react-node"; import { ButtonGroupContext } from "./button-group-context"; // The tiers `button/styles/index.less` actually paints. `default` is not a theme value any more — // asking for the button's own look means writing no `theme`, and that look is type-driven, so the // class `k-btn-default` belongs to `type="default"` and no tier may collide with it. // `underlined` is absent too — an underline is a surface tier, and a button has nothing to underline. const PAINTED_THEMES = ["fill", "outline", "plain", "solid", "dashed"] as const; export interface ButtonProps extends Omit< React.ButtonHTMLAttributes, "type" | "onClick" | "defaultValue" | "defaultChecked" > { htmlType?: "button" | "submit" | "reset"; icon?: IconType[]; block?: boolean; size?: SizeType; color?: string; loading?: boolean; type?: ButtonType; disabled?: boolean; theme?: SurfaceThemeType; shape?: ShapeType; href?: string; target?: string; onClick?: React.MouseEventHandler; } const Button = React.forwardRef( ( { htmlType = "button", icon, block = false, size: sizeProp, color, loading = false, type = "default", disabled = false, theme: themeProp, shape: shapeProp, href, target, children, className = "", onClick, ...rest }, ref, ) => { const inheritedAppearance = useConfigAppearance(); const size = sizeProp ?? inheritedAppearance.size; const theme = resolvePaintedTheme(themeProp ?? inheritedAppearance.theme, PAINTED_THEMES); const shape = shapeProp ?? inheritedAppearance.shape; const buttonGroup = useContext(ButtonGroupContext); const parentSize = useContext(SizeContext); const computedSize = sizeProp ?? buttonGroup?.size ?? parentSize ?? size ?? "default"; const computedShape = shapeProp ?? buttonGroup?.shape ?? shape; const handleClick = (e: React.MouseEvent) => { if (loading || disabled) { e.preventDefault(); return; } onClick?.(e); }; const iconOnly = () => { const childList = getChildren(children); if (!childList.length) { return !!icon || !!loading; } if (childList.length === 1) { const firstChild = childList[0]; if (React.isValidElement(firstChild)) { const childType = firstChild.type; return childType === Icon; } } return false; }; // A `color` only counts when `button/styles/index.less` can paint it, which is the shared palette and // nothing else — there is no inline arm here. So a name outside the palette must be read as "no color // written" rather than as a color that cancels `type` and leaves the button with no surface at all. const isPresetColor = !!color && colors.some((preset) => preset === color); const classes = clsx( "k-btn", { [`k-btn-${type}`]: type && !isPresetColor, "k-btn-sm": computedSize === "small", "k-btn-block": block, "k-btn-loading": loading, "k-btn-icon-only": iconOnly(), [`k-btn-${color}`]: isPresetColor, "k-btn-lg": computedSize === "large", "k-btn-circle": computedShape === "circle", "k-btn-square": computedShape === "square", [`k-btn-${theme}`]: theme, }, className, ); let childNodes: React.ReactNode[] = []; const iconType = loading ? Loading : icon; if (iconType) { childNodes.push( loading ? ( ) : ( ), ); } const processedChildren = getChildren(children).map((c, index) => { return typeof c === "string" ? {c.trim()} : c; }); if (processedChildren.length > 0) { childNodes = childNodes.concat(processedChildren); } const commonProps = { ...rest, className: classes, onClick: handleClick, }; const setElementRef = (node: HTMLButtonElement | HTMLAnchorElement | null) => { if (typeof ref === "function") ref(node); else if (ref) ref.current = node; }; if (type === "link" && href && !disabled) { const anchorProps = rest as React.AnchorHTMLAttributes; return ( {childNodes} ); } return ( ); }, ); Button.displayName = "Button"; export default Button;