JGuang / HeroUI

Button 按钮

可点击的按钮组件,支持多种变体与状态。

引入

import { Button } from '@heroui/react';

用法

HeroUI 示例

变体

HeroUI 示例

带图标

HeroUI 示例

仅图标

HeroUI 示例

加载中

HeroUI 示例

加载状态

HeroUI 示例

尺寸

HeroUI 示例

全宽

HeroUI 示例

禁用状态

HeroUI 示例

社交按钮

HeroUI 示例

自定义渲染函数

HeroUI 示例

样式

传入 Tailwind CSS 类

import { Button } from '@heroui/react';

function CustomButton() {
  return (
    <Button className="bg-purple-500 text-white hover:bg-purple-600">
      Purple Button
    </Button>
  );
}

自定义组件类

若要自定义 Button 组件类,可以使用 @layer components 指令。

了解更多。

@layer components {
  .button {
    @apply bg-purple-500 text-white hover:bg-purple-600;
  }

  .button--icon-only {
    @apply rounded-lg bg-blue-500;
  }
}

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

添加自定义变体

你可以通过封装 HeroUI 组件并添加自定义变体来扩展其能力。

HeroUI 示例

添加涟漪效果

Button 组件支持通过组合方式实现涟漪效果,你可以将涟漪组件作为子节点嵌套。此示例使用 m3-ripple。

HeroUI 示例

CSS 类

Button 组件使用以下 CSS 类(查看源码样式):

基础与尺寸类

  • .button - 按钮基础样式
  • .button--sm - 小尺寸变体
  • .button--md - 中尺寸变体
  • .button--lg - 大尺寸变体

变体类

  • .button--primary
  • .button--secondary
  • .button--tertiary
  • .button--outline
  • .button--ghost
  • .button--danger

修饰符类

  • .button--icon-only
  • .button--icon-only.button--sm
  • .button--icon-only.button--lg

交互状态

该按钮同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制:

  • 悬停::hover 或 [data-hovered="true"]
  • 激活/按压::active 或 [data-pressed="true"](包含缩放变换)
  • 聚焦::focus-visible 或 [data-focus-visible="true"](显示焦点环)
  • 禁用::disabled 或 [aria-disabled="true"](降低透明度,禁用指针事件)
  • 等待中:[data-pending](加载期间禁用指针事件)

API 参考

Button Props

Prop类型默认值描述
variant'primary' | 'secondary' | 'tertiary' | 'outline' | 'ghost' | 'danger''primary'视觉样式变体
size'sm' | 'md' | 'lg''md'按钮尺寸
fullWidthbooleanfalse按钮是否占满容器宽度
isDisabledbooleanfalse按钮是否禁用
isPendingbooleanfalse按钮是否处于加载状态
isIconOnlybooleanfalse按钮是否仅包含图标
onPress(e: PressEvent) => void-按钮被按下时的事件处理函数
childrenReact.ReactNode | (values: ButtonRenderProps) => React.ReactNode-按钮内容或渲染 prop
renderDOMRenderFunction<keyof React.JSX.IntrinsicElements, ButtonRenderProps>-使用自定义渲染函数覆盖默认 DOM 元素。

ButtonRenderProps

使用渲染 prop 模式时,会提供以下值:

Prop类型描述
isPendingboolean按钮是否处于加载状态
isPressedboolean按钮当前是否被按压
isHoveredboolean按钮是否处于悬停状态
isFocusedboolean按钮是否处于聚焦状态
isFocusVisibleboolean按钮是否应显示焦点指示
isDisabledboolean按钮是否禁用

本地来源: vendor/zh/react/components/(buttons)/button.mdx

文档根据 HeroUI v3 固定提交生成,已调整内部链接与交互预览以适配 JGuang。

On this page