Skip to content

Composables 组合式 API

Parrot UI 提供 4 个 Composable Hook,封装常用的跨组件逻辑。

useColor

运行时访问设计令牌颜色系统,提供颜色计算与操作能力。

基本用法

typescript
import { useColor } from '@parrotui/core'

const color = useColor()

// 获取主题色值(计算属性,响应式)
color.primary   // '#1a7eff'
color.secondary // '#7c3aed'
color.success   // '#41a906'
color.warning   // '#e6a23c'
color.danger    // '#f56c6c'
color.info      // '#6c6f78'

API

方法参数返回值说明
getColor(key)ColorKeystring获取颜色值
getPalette(key)ColorKeyobject获取色系全部 10 级
getShade(key, level)ColorKey, ShadeLevelstring获取指定级别色值
mix(c1, c2, weight)string, string, numberstring两色混合
opacity(color, alpha)string, numberstring设置透明度
contrast(color)stringstring计算对比色(黑/白)
shades(color)stringShadeMap生成 10 级梯度
isDark(color)stringboolean判断是否深色
lighten(color, amount)string, numberstring颜色变亮
darken(color, amount)string, numberstring颜色变暗

ShadeLevel 类型

typescript
type ShadeLevel = 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900

useResponsive

屏幕尺寸检测、断点判断、单位换算。

基本用法

typescript
import { useResponsive } from '@parrotui/core'

const screen = useResponsive()

// 屏幕信息
console.log(screen.screenWidth)     // 375
console.log(screen.breakpoint)      // 'sm'
console.log(screen.orientation)     // 'portrait'

// 断点判断
if (screen.isAbove('md')) {
  // 平板及以上
}

断点定义

断点最小宽度典型设备
xs0小屏手机
sm576px标准手机
md768px平板
lg992px大平板/小笔记本
xl1200px桌面

API

属性/方法类型说明
screenWidthnumber屏幕宽度 (px)
screenHeightnumber屏幕高度 (px)
statusBarHeightnumber状态栏高度 (px)
safeAreaBottomnumber安全区底部 (px)
platformstring平台标识
breakpointBreakpoint当前断点
orientationOrientation屏幕方向
isSmallboolean是否 xs/sm
isLargeboolean是否 lg/xl
isAbove(bp)(Breakpoint) => boolean大于等于断点
isBelow(bp)(Breakpoint) => boolean小于断点
rpx2px(rpx)(number) => numberrpx 转 px
px2rpx(px)(number) => numberpx 转 rpx
refresh()() => void刷新屏幕信息

useLocale

国际化多语言系统,内置 zh-CN / en-US 共 70+ 条目。

基本用法

typescript
import { useLocale } from '@parrotui/core'

const { t, lang, setLang, registerMessages } = useLocale()

// 翻译
t('common.confirm')               // '确定'
t('form.maxLength', { max: 10 })  // '最多输入 10 个字符'

// 切换语言
setLang('en-US')
t('common.confirm')               // 'Confirm'

内置语言条目分类

分类示例键zh-CNen-US
commoncommon.confirm确定Confirm
commoncommon.cancel取消Cancel
commoncommon.save保存Save
commoncommon.delete删除Delete
formform.required此项为必填This field is required
formform.email请输入有效邮箱Invalid email
formform.phone请输入有效手机号Invalid phone
messagemessage.success操作成功Success
messagemessage.error操作失败Error

API

方法参数说明
t(key, params?)string, object?翻译(支持参数插值 {key}
langcomputed当前语言代码
setLang(code)LangCode切换语言
registerMessages(code, msgs)string, object注册自定义语言包
getAvailableLangs()-获取可用语言列表
restoreLocale()-从本地存储恢复语言设置

useTheme

主题管理,支持亮色/暗色/跟随系统三种模式。

基本用法

typescript
import { useTheme } from '@parrotui/core'

const theme = useTheme()

// 读取状态
console.log(theme.mode)   // 'light' | 'dark' | 'system'
console.log(theme.isDark) // false

// 切换
theme.toggleDark()        // 明暗切换
theme.setMode('dark')     // 设置为暗色

// 自定义品牌色
theme.applyTheme({
  primary: '#ff6b6b',
  success: '#51cf66'
})

// 重置
theme.resetTheme()

API

属性/方法类型说明
modeThemeMode当前模式
isDarkboolean是否暗色
setMode(mode)(ThemeMode) => void设置模式
toggleDark()() => void切换明暗
applyTheme(patch)(ThemePatch) => void应用自定义色值
resetTheme()() => void重置为默认
restoreTheme()() => void从本地存储恢复

ThemeMode 类型

typescript
type ThemeMode = 'light' | 'dark' | 'system'

Released under the MIT License.