WithCopy

View source
带复制功能的输入框组件

简介

WithCopy 是一个带有一键复制功能的输入框组件。当输入框有内容时,会在右侧显示一个复制按钮,点击可快速复制输入框中的内容到剪贴板。

基于 Nuxt UI 的 Input 组件封装

基础用法

最简单的复制功能输入框:

<script setup lang="ts">
const apiKey = ref('sk-1234567890abcdef')
</script>

<template>
  <MWithCopy v-model="apiKey" placeholder="输入内容..." />
</template>

带图标

为输入框添加语义化图标:

<script setup lang="ts">
const token = ref('ghp_abcdef1234567890')
</script>

<template>
  <MWithCopy v-model="token" leading-icon="i-lucide-key" placeholder="GitHub Token" />
</template>

自定义样式

通过 buttonProps 自定义复制按钮样式:

<script setup lang="ts">
const code = ref('ABC123XYZ')
</script>

<template>
  <MWithCopy
    v-model="code"
    color="primary"
    size="lg"
    :button-props="{
      variant: 'soft',
      color: 'primary'
    }"
    placeholder="兑换码"
  />
</template>

复制事件

通过 @copy 事件监听复制操作:

<script setup lang="ts">
const toast = useToast()
const content = ref('这是一段可复制的文本')

function handleCopy(value: string) {
  toast.add({
    title: '已复制',
    description: `复制内容: ${value}`,
    color: 'success'
  })
}
</script>

<template>
  <MWithCopy v-model="content" @copy="handleCopy" />
</template>

API

Props

Prop Default Type
as'div'any

The element or component this component should render as.

buttonPropsButtonProps

Custom props for the copy button

tooltipPropsTooltipProps

Custom props for the tooltip

idstring
namestring
type"number" | "color" | "button" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "hidden" | "image" | "month" | "password" | "radio" | "range" | "reset" | "search" | "submit" | "tel" | "text" | "time" | "url" | "week" | string & {}
placeholderstring

The placeholder text when the input is empty.

color'primary'"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"
variant'outline'"outline" | "soft" | "subtle" | "ghost" | "none"
size'md'"xs" | "sm" | "md" | "lg" | "xl"
autocompletestring & {} | "on" | "off"
autofocusDelaynumber
defaultValueT
modelModifiersModelModifiers
iconany

Display an icon based on the leading and trailing props.

avatarAvatarProps

Display an avatar on the left side.

leadingIconany

Display an icon on the left side.

trailingIconany

Display an icon on the right side.

loadingIconappConfig.ui.icons.loadingany

The icon when the loading prop is true.

liststring
maxstring | number
maxlengthstring | number
minstring | number
minlengthstring | number
patternstring
readonlyfalse | true | "true" | "false"
stepstring | number
modelValueT
requiredboolean
autofocusboolean
disabledboolean
highlightboolean

Highlight the ring color like a focus state.

fixedboolean

Keep the mobile text size on all breakpoints.

leadingboolean

When true, the icon will be displayed on the left side.

trailingboolean

When true, the icon will be displayed on the right side.

loadingboolean

When true, the loading icon will be displayed.

ui{ root?: ClassNameValue; base?: ClassNameValue; leading?: ClassNameValue; leadingIcon?: ClassNameValue; leadingAvatar?: ClassNameValue; leadingAvatarSize?: ClassNameValue; trailing?: ClassNameValue; trailingIcon?: ClassNameValue; }

Emits

Event Type
update:modelValue[value: T]
blur[event: FocusEvent]
change[event: Event]
copy[value: string]

Slots

Slot Type
leading{ ui: { root: (props?: Record<string, any>) => string; base: (props?: Record<string, any>) => string; leading: (props?: Record<string, any>) => string; leadingIcon: (props?: Record<string, any>) => string; leadingAvatar: (props?: Record<string, any>) => string; leadingAvatarSize: (props?: Record<string, any>) => string; trailing: (props?: Record<string, any>) => string; trailingIcon: (props?: Record<string, any>) => string; }; }
default{ ui: { root: (props?: Record<string, any>) => string; base: (props?: Record<string, any>) => string; leading: (props?: Record<string, any>) => string; leadingIcon: (props?: Record<string, any>) => string; leadingAvatar: (props?: Record<string, any>) => string; leadingAvatarSize: (props?: Record<string, any>) => string; trailing: (props?: Record<string, any>) => string; trailingIcon: (props?: Record<string, any>) => string; }; }

Changelog

Soon
  • 29974 — chore: 升级依赖并修复类型问题
  • 30ead — refactor: 重组项目结构以提升可维护性
v0.1.0
  • d4a6c — refactor: 将组件 Props 类型定义提取到独立文件
  • 9bdda — refactor(components): 优化组件代码格式
  • e49b2 — ♻️ refactor: 为输入增强组件添加泛型类型支持
  • 0151f — ✨ feat: 新增输入增强组件 WithCopy 和 WithCharacterLimit
Copyright © 2025 - 2026 YiXuan - MIT License