react-intials-avatar-generator 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +240 -0
- package/dist/react-initials-avatar.es.js +55 -0
- package/dist/react-initials-avatar.umd.js +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# React Initials Avatar
|
|
2
|
+
|
|
3
|
+
A lightweight, customizable React component for generating beautiful initials-based avatars. Perfect for user profiles, contact lists, and any application that needs elegant placeholder avatars.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎨 Fully customizable colors, sizes, and styles
|
|
8
|
+
- 🔤 Smart initials extraction from names
|
|
9
|
+
- 📦 Zero dependencies (except React)
|
|
10
|
+
- 🎯 TypeScript support
|
|
11
|
+
- âš¡ Lightweight and performant
|
|
12
|
+
- 🎠Flexible styling options
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install react-initials-avatar
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
or
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
yarn add react-initials-avatar
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
or
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm add react-initials-avatar
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Basic Usage
|
|
33
|
+
|
|
34
|
+
```jsx
|
|
35
|
+
import { InitialsAvatar } from 'react-initials-avatar';
|
|
36
|
+
|
|
37
|
+
function App() {
|
|
38
|
+
return (
|
|
39
|
+
<InitialsAvatar name="John Doe" />
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Props
|
|
45
|
+
|
|
46
|
+
### Component Props
|
|
47
|
+
|
|
48
|
+
| Prop | Type | Default | Description |
|
|
49
|
+
|------|------|---------|-------------|
|
|
50
|
+
| `name` | `string` | **required** | The full name to extract initials from |
|
|
51
|
+
| `styles` | `StyleProps` | `{}` | Styling configuration object |
|
|
52
|
+
| `options` | `InitialOptions` | `{}` | Options for initials extraction |
|
|
53
|
+
|
|
54
|
+
### StyleProps
|
|
55
|
+
|
|
56
|
+
Configure the visual appearance of the avatar:
|
|
57
|
+
|
|
58
|
+
| Property | Type | Default | Description |
|
|
59
|
+
|----------|------|---------|-------------|
|
|
60
|
+
| `size` | `number` | `50` | Avatar size in pixels (width and height) |
|
|
61
|
+
| `radius` | `number \| string` | `"50%"` | Border radius (use "50%" for circle, number for rounded square) |
|
|
62
|
+
| `bgColor` | `string` | `"#4f46e5"` | Background color (any valid CSS color) |
|
|
63
|
+
| `textColor` | `string` | `"#ffffff"` | Text color for initials |
|
|
64
|
+
| `fontFamily` | `string` | `"inherit"` | Font family for the initials |
|
|
65
|
+
| `fontSize` | `number` | `size * 0.4` | Font size in pixels (auto-calculated if not provided) |
|
|
66
|
+
| `fontWeight` | `number \| string` | `600` | Font weight (100-900 or CSS keywords) |
|
|
67
|
+
| `lineHeight` | `number \| string` | `size + "px"` | Line height for vertical centering |
|
|
68
|
+
| `letterSpacing` | `number \| string` | `0` | Letter spacing in pixels or CSS units |
|
|
69
|
+
|
|
70
|
+
### InitialOptions
|
|
71
|
+
|
|
72
|
+
Control how initials are extracted from names:
|
|
73
|
+
|
|
74
|
+
| Property | Type | Default | Description |
|
|
75
|
+
|----------|------|---------|-------------|
|
|
76
|
+
| `maxLength` | `number` | `Infinity` | Maximum number of initials to display |
|
|
77
|
+
| `uppercase` | `boolean` | `true` | Convert initials to uppercase |
|
|
78
|
+
| `fallback` | `string` | `""` | Fallback text when name is empty or invalid |
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
### Basic Avatar
|
|
83
|
+
|
|
84
|
+
```jsx
|
|
85
|
+
<InitialsAvatar name="John Doe" />
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Custom Size and Colors
|
|
89
|
+
|
|
90
|
+
```jsx
|
|
91
|
+
<InitialsAvatar
|
|
92
|
+
name="Jane Smith"
|
|
93
|
+
styles={{
|
|
94
|
+
size: 80,
|
|
95
|
+
bgColor: "#10b981",
|
|
96
|
+
textColor: "#ffffff"
|
|
97
|
+
}}
|
|
98
|
+
/>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Square Avatar with Rounded Corners
|
|
102
|
+
|
|
103
|
+
```jsx
|
|
104
|
+
<InitialsAvatar
|
|
105
|
+
name="Alice Johnson"
|
|
106
|
+
styles={{
|
|
107
|
+
size: 60,
|
|
108
|
+
radius: 8,
|
|
109
|
+
bgColor: "#f59e0b"
|
|
110
|
+
}}
|
|
111
|
+
/>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Custom Font Styling
|
|
115
|
+
|
|
116
|
+
```jsx
|
|
117
|
+
<InitialsAvatar
|
|
118
|
+
name="Bob Wilson"
|
|
119
|
+
styles={{
|
|
120
|
+
size: 100,
|
|
121
|
+
fontFamily: "Arial, sans-serif",
|
|
122
|
+
fontSize: 45,
|
|
123
|
+
fontWeight: 700,
|
|
124
|
+
letterSpacing: 2
|
|
125
|
+
}}
|
|
126
|
+
/>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Limit Initials Length
|
|
130
|
+
|
|
131
|
+
```jsx
|
|
132
|
+
<InitialsAvatar
|
|
133
|
+
name="Christopher Alexander Montgomery"
|
|
134
|
+
options={{
|
|
135
|
+
maxLength: 2 // Will show "CA" instead of "CAM"
|
|
136
|
+
}}
|
|
137
|
+
/>
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Lowercase Initials
|
|
141
|
+
|
|
142
|
+
```jsx
|
|
143
|
+
<InitialsAvatar
|
|
144
|
+
name="David Brown"
|
|
145
|
+
options={{
|
|
146
|
+
uppercase: false
|
|
147
|
+
}}
|
|
148
|
+
/>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### With Fallback
|
|
152
|
+
|
|
153
|
+
```jsx
|
|
154
|
+
<InitialsAvatar
|
|
155
|
+
name=""
|
|
156
|
+
options={{
|
|
157
|
+
fallback: "?"
|
|
158
|
+
}}
|
|
159
|
+
styles={{
|
|
160
|
+
bgColor: "#6b7280"
|
|
161
|
+
}}
|
|
162
|
+
/>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Complete Custom Example
|
|
166
|
+
|
|
167
|
+
```jsx
|
|
168
|
+
<InitialsAvatar
|
|
169
|
+
name="Emily Rose Parker"
|
|
170
|
+
styles={{
|
|
171
|
+
size: 120,
|
|
172
|
+
radius: "20%",
|
|
173
|
+
bgColor: "#8b5cf6",
|
|
174
|
+
textColor: "#fef3c7",
|
|
175
|
+
fontFamily: "Georgia, serif",
|
|
176
|
+
fontSize: 50,
|
|
177
|
+
fontWeight: 500,
|
|
178
|
+
letterSpacing: 3
|
|
179
|
+
}}
|
|
180
|
+
options={{
|
|
181
|
+
maxLength: 2,
|
|
182
|
+
uppercase: true
|
|
183
|
+
}}
|
|
184
|
+
/>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## How Initials Are Extracted
|
|
188
|
+
|
|
189
|
+
The component intelligently extracts initials from names:
|
|
190
|
+
|
|
191
|
+
1. Removes special characters like hyphens, periods, and apostrophes
|
|
192
|
+
2. Splits the name by spaces
|
|
193
|
+
3. Takes the first letter of each word
|
|
194
|
+
4. Applies the `maxLength` limit if specified
|
|
195
|
+
5. Converts to uppercase by default (unless `uppercase: false`)
|
|
196
|
+
|
|
197
|
+
**Examples:**
|
|
198
|
+
- `"John Doe"` → `"JD"`
|
|
199
|
+
- `"Mary-Jane Watson"` → `"MJW"`
|
|
200
|
+
- `"Dr. James Smith Jr."` → `"DJSJ"`
|
|
201
|
+
- `"Anne O'Brien"` → `"AOB"`
|
|
202
|
+
|
|
203
|
+
## TypeScript Support
|
|
204
|
+
|
|
205
|
+
The package includes full TypeScript definitions:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { InitialsAvatar } from 'react-initials-avatar';
|
|
209
|
+
import type { StyleProps, InitialOptions } from 'react-initials-avatar';
|
|
210
|
+
|
|
211
|
+
const styles: StyleProps = {
|
|
212
|
+
size: 60,
|
|
213
|
+
bgColor: "#3b82f6"
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const options: InitialOptions = {
|
|
217
|
+
maxLength: 2,
|
|
218
|
+
uppercase: true
|
|
219
|
+
};
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Browser Support
|
|
223
|
+
|
|
224
|
+
Works in all modern browsers that support React 18+:
|
|
225
|
+
- Chrome (latest)
|
|
226
|
+
- Firefox (latest)
|
|
227
|
+
- Safari (latest)
|
|
228
|
+
- Edge (latest)
|
|
229
|
+
|
|
230
|
+
## Contributing
|
|
231
|
+
|
|
232
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
MIT © Ankit Kumar Pandey
|
|
237
|
+
|
|
238
|
+
## Keywords
|
|
239
|
+
|
|
240
|
+
react, avatar, initials, react-avatar, profile-avatar, user-avatar, placeholder-avatar, avatar-component
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { jsx as u } from "react/jsx-runtime";
|
|
2
|
+
const h = (t, s = {}) => {
|
|
3
|
+
const {
|
|
4
|
+
maxLength: i = 1 / 0,
|
|
5
|
+
uppercase: e = !0,
|
|
6
|
+
fallback: n = ""
|
|
7
|
+
} = s;
|
|
8
|
+
if (typeof t != "string") return n;
|
|
9
|
+
const r = t.trim();
|
|
10
|
+
if (!r) return n;
|
|
11
|
+
const o = r.replace(/[-.']/g, " ").replace(/[^a-zA-Z\s]/g, "").split(/\s+/).map((c) => c[0]).join("");
|
|
12
|
+
return e ? o.slice(0, i).toUpperCase() : o.slice(0, i);
|
|
13
|
+
}, x = ({
|
|
14
|
+
name: t,
|
|
15
|
+
styles: s = {},
|
|
16
|
+
options: i = {}
|
|
17
|
+
}) => {
|
|
18
|
+
const {
|
|
19
|
+
size: e = 50,
|
|
20
|
+
radius: n = "50%",
|
|
21
|
+
bgColor: r = "#4f46e5",
|
|
22
|
+
textColor: a = "#ffffff",
|
|
23
|
+
fontFamily: o = "inherit",
|
|
24
|
+
fontSize: c,
|
|
25
|
+
fontWeight: l = 600,
|
|
26
|
+
lineHeight: f,
|
|
27
|
+
letterSpacing: p = 0
|
|
28
|
+
} = s, g = h(t, i), d = c ?? e * 0.4;
|
|
29
|
+
return /* @__PURE__ */ u(
|
|
30
|
+
"div",
|
|
31
|
+
{
|
|
32
|
+
style: {
|
|
33
|
+
width: e,
|
|
34
|
+
height: e,
|
|
35
|
+
borderRadius: n,
|
|
36
|
+
backgroundColor: r,
|
|
37
|
+
color: a,
|
|
38
|
+
display: "flex",
|
|
39
|
+
alignItems: "center",
|
|
40
|
+
justifyContent: "center",
|
|
41
|
+
fontFamily: o,
|
|
42
|
+
fontSize: d,
|
|
43
|
+
fontWeight: l,
|
|
44
|
+
lineHeight: f ?? `${e}px`,
|
|
45
|
+
letterSpacing: p,
|
|
46
|
+
userSelect: "none"
|
|
47
|
+
},
|
|
48
|
+
children: g
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
export {
|
|
53
|
+
x as InitialsAvatar,
|
|
54
|
+
h as getInitials
|
|
55
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react/jsx-runtime")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime"],i):(e=typeof globalThis<"u"?globalThis:e||self,i(e.ReactInitialsAvatar={},e["react/jsx-runtime"]))})(this,(function(e,i){"use strict";const l=(n,a={})=>{const{maxLength:r=1/0,uppercase:t=!0,fallback:s=""}=a;if(typeof n!="string")return s;const o=n.trim();if(!o)return s;const c=o.replace(/[-.']/g," ").replace(/[^a-zA-Z\s]/g,"").split(/\s+/).map(f=>f[0]).join("");return t?c.slice(0,r).toUpperCase():c.slice(0,r)},d=({name:n,styles:a={},options:r={}})=>{const{size:t=50,radius:s="50%",bgColor:o="#4f46e5",textColor:u="#ffffff",fontFamily:c="inherit",fontSize:f,fontWeight:p=600,lineHeight:g,letterSpacing:m=0}=a,h=l(n,r),y=f??t*.4;return i.jsx("div",{style:{width:t,height:t,borderRadius:s,backgroundColor:o,color:u,display:"flex",alignItems:"center",justifyContent:"center",fontFamily:c,fontSize:y,fontWeight:p,lineHeight:g??`${t}px`,letterSpacing:m,userSelect:"none"},children:h})};e.InitialsAvatar=d,e.getInitials=l,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-intials-avatar-generator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Customizable initials-based avatar component for React applications.",
|
|
5
|
+
"keywords": ["react", "avatar", "initials", "react-avatar", "profile-avatar"],
|
|
6
|
+
"author": "Ankit Kumar Pandey",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/Ankit91153/react-initials-avatar.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Ankit91153/react-initials-avatar/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/Ankit91153/react-initials-avatar#readme",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/react-initials-avatar.umd.js",
|
|
18
|
+
"module": "./dist/react-initials-avatar.es.js",
|
|
19
|
+
"types": "./dist/main.d.ts",
|
|
20
|
+
"files": ["dist/*.js", "dist/*.d.ts"],
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": ">=18",
|
|
23
|
+
"react-dom": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^25.2.3",
|
|
27
|
+
"@types/react": "^18.3.28",
|
|
28
|
+
"@types/react-dom": "^18.3.7",
|
|
29
|
+
"@vitejs/plugin-react": "^5.1.1",
|
|
30
|
+
"react": "^18.3.1",
|
|
31
|
+
"react-dom": "^18.3.1",
|
|
32
|
+
"typescript": "~5.9.3",
|
|
33
|
+
"vite": "^7.3.1"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"dev": "vite",
|
|
37
|
+
"build": "npm run build:types && vite build",
|
|
38
|
+
"build:types": "tsc --project tsconfig.build.json",
|
|
39
|
+
"lint": "eslint .",
|
|
40
|
+
"prepare": "npm run build"
|
|
41
|
+
},
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"types": "./dist/main.d.ts",
|
|
45
|
+
"import": "./dist/react-initials-avatar.es.js",
|
|
46
|
+
"require": "./dist/react-initials-avatar.umd.js"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|