@uiw/react-avatar
TypeScript icon, indicating that this package has built-in type declarations

4.22.3 • Public • Published

Avatar 头像

Buy me a coffee Open in unpkg NPM Downloads npm version

用来代表用户或事物,支持图片、图标或字符展示。

import { Avatar } from 'uiw';
// or
import Avatar from '@uiw/react-avatar';

基本用法

头像有四种尺寸,两种形状可选。

import React from 'react';
import { Avatar, Icon } from 'uiw';

export default function Demo() {
  return (
    <div>
      <div style={{ paddingBottom: 20 }}>
        <Avatar size="large" icon={<Icon type="user"/>} />
        <Avatar icon="user" />
        <Avatar size="small" icon="user" />
        <Avatar size="mini" icon={<Icon type="user"/>} />
      </div>
      <div>
        <Avatar shape="square" size="large" icon="user" />
        <Avatar shape="square" icon="user" />
        <Avatar shape="square" size="small" icon="user" />
        <Avatar shape="square" size="mini" icon="user" />
      </div>
    </div>
  )
}

其它类型

支持三种类型:Icon 以及字符,其中 Icon 和字符型可以自定义图标颜色及背景色。

import React from 'react';
import { Avatar, Icon } from 'uiw';

export default function Demo() {
  return (
    <div>
      <Avatar icon={<Icon type="user"/>} />
      <Avatar>U</Avatar>
      <Avatar src="https://avatars2.githubusercontent.com/u/1680273?s=40&v=4" />
      <Avatar style={{ color: '#f56a00', backgroundColor: '#fde3cf' }}>U</Avatar>
      <Avatar style={{ backgroundColor: '#87d068' }} icon="user" />
    </div>
  )
}

图片支持

在组件上使用onError事件,处理显示错误的图片。

import React from 'react';
import { Avatar, Icon } from 'uiw';

const App = () => {
  const [src, setSrc] = React.useState('https://avatars2.githubusercontent.com/ua/1680273?s=40&v=4')
  return (
    <div>
      <Avatar
        src='https://avatars2.githubusercontent.com/ua/1680273?s=40&v=4'
        onError={(e) => {
          if (e) {
            e.target.src = 'https://avatars2.githubusercontent.com/u/1680275?s=40&v=4'
          }
          return false;
        }}
      />
      <Avatar
        src={src}
        onError={(e) => {
          if (e) {
            setSrc('https://avatars2.githubusercontent.com/u/1680275?s=40&v=4');
          }
        }}
      />
    </div>
  )
}

export default App;

带徽标的头像

import React from 'react';
import { Avatar, Badge } from 'uiw';

export default function Demo() {
  return (
    <div>
      <span style={{ marginRight: 24 }}>
        <Badge count={1}><Avatar shape="square" icon="user" /></Badge>
      </span>
      <span>
        <Badge dot><Avatar shape="square" icon="user" /></Badge>
      </span>
    </div>
  )
}

Props

参数 说明 类型 默认值
icon 设置头像的图标类型,参考 Icon 组件 String、ReactNode -
shape 指定头像的形状 square 正方形或者 circle Enum{'circle', 'square' } -
size 设置头像的大小 Enum{ 'large', 'small','mini', 'default' } default
src 图片类头像的资源地址 String -
alt 规定图像的替代文本 String -
onError 图片加载失败的事件,返回 false 会关闭组件默认的 fallback 行为 () => boolean -

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.22.3
127latest
5.0.0-bate.2.10bate

Version History

VersionDownloads (Last 7 Days)Published
4.22.3
127
4.22.2
0
4.22.1
1
4.22.02
4.21.282
4.21.271
4.21.261
4.21.251
5.0.0-bate.2.10
4.21.240
5.0.0-bate.2.00
4.21.231
4.21.221
4.21.210
5.0.0-bate-190
4.21.200
5.0.0-bate-180
5.0.0-bate-170
5.0.0-bate-142
5.0.0-bate-121
5.0.0-bate-110
5.0.0-bate-100
5.0.0-bate-90
5.0.0-bate-81
5.0.0-bate-70
5.0.0-bate-60
4.21.190
4.21.180
5.0.0-bate-50
5.0.0-bate-41
5.0.0-bate-30
4.21.170
5.0.0-bate-20
5.0.0-bate-10
4.21.160
4.21.150
5.0.0-bate-00
4.21.140
4.21.130
4.21.120
4.21.110
4.21.100
4.21.90
4.21.81
4.21.71
4.21.60
4.21.50
4.21.40
4.21.30
4.21.21
4.21.10
4.21.00
4.20.00
4.19.00
4.18.10
4.18.00
4.17.00
4.16.21
4.16.10
4.16.01
4.15.11
4.15.00
4.14.20
4.14.10
4.14.00
4.13.120
4.13.110
4.13.102
4.13.90
4.13.81
4.13.70
4.13.60
4.13.51
4.13.40
4.13.31
4.13.20
4.13.10
4.13.00
4.12.20
4.12.11
4.12.00
4.11.60
4.11.50
4.11.41
4.11.30
4.11.20
4.11.11
4.11.00
4.10.71
4.10.62
4.10.50
4.10.42
4.10.37
4.10.20
4.10.10
4.10.00
4.9.110
4.9.101
4.9.90
4.9.70
4.9.61
4.9.40
4.9.30
4.9.20
4.9.10
4.9.00
4.8.91
4.8.80
4.8.70
4.8.60
4.8.50
4.8.40
4.8.30
4.8.21
4.8.10
4.8.00
4.7.160
4.7.150
4.7.143
4.7.130
4.7.120
4.7.111
4.7.100
4.7.90
4.7.80
4.7.70
4.7.62
4.7.51
4.7.40
4.7.31
4.7.20
4.7.00
4.6.191
4.6.180
4.6.170
4.6.160
4.6.150
4.6.140
4.6.130
4.6.120
4.6.110
4.6.101
4.6.90
4.6.81
4.6.70
4.6.61
4.6.40
4.6.30
4.6.20
4.6.10
4.6.01
4.5.00
4.4.10
4.4.00
4.3.11
4.3.01
4.2.141
4.2.50
4.2.30
4.2.11
4.2.01
4.1.20
4.1.11
4.1.00
4.0.01
4.0.0-alpha.80
4.0.0-alpha.70
4.0.0-alpha.60
4.0.0-alpha.30
4.0.0-alpha.20
4.0.0-alpha.10
4.0.0-alpha.00

Package Sidebar

Install

npm i @uiw/react-avatar

Weekly Downloads

143

Version

4.22.3

License

MIT

Unpacked Size

17.2 kB

Total Files

13

Last publish

Collaborators

  • uiwjs
  • wcjiang