@behivetech/atoms.avatar 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +16 -2
- package/avatar.block.css +65 -0
- package/avatar.block.native.ts +108 -0
- package/avatar.block.ts +54 -0
- package/avatar.example.tsx +38 -0
- package/avatar.native.tsx +94 -0
- package/avatar.tsx +83 -0
- package/dist/index.cjs +128 -0
- package/dist/index.css +48 -0
- package/dist/index.d.ts +131 -0
- package/dist/index.js +101 -0
- package/dist/index.native.js +157 -0
- package/dist/native/avatar.block.d.ts +84 -0
- package/dist/native/avatar.block.native.d.ts +2 -0
- package/dist/native/avatar.d.ts +35 -0
- package/dist/native/avatar.native.d.ts +36 -0
- package/dist/native/get-initials.d.ts +7 -0
- package/dist/native/index.d.ts +2 -0
- package/docs.json +96 -0
- package/get-initials.ts +18 -0
- package/index.ts +2 -0
- package/package.json +87 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 BEhive Tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Avatar
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A picture, the initials of a name, or a fallback in a small circle or square; a picture that fails to load falls back to the initials.
|
|
4
|
+
|
|
5
|
+
Docs: https://platform.behivetech.com/behivetech/atoms/avatar
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```jsx
|
|
10
|
+
import { Avatar } from "@behivetech/atoms.avatar";
|
|
11
|
+
|
|
12
|
+
<Avatar name="Camp Greenwood" shape="square" />;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Without `alt` the avatar is decorative and hidden from assistive technology, which is right when the name is written beside it. Pass `alt` when the avatar stands alone.
|
|
16
|
+
|
|
17
|
+
The ```jsx block above runs live on the docs site. Imports are stripped and every registry export is in scope, so you can compose with other components too.
|
package/avatar.block.css
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* GENERATED from avatar.block.ts by @behivetech/blocks.builder. Do not edit by hand: run `pnpm block:build`. */
|
|
2
|
+
|
|
3
|
+
@layer bht {
|
|
4
|
+
.BHT__Avatar {
|
|
5
|
+
display: inline-flex;
|
|
6
|
+
align-items: center;
|
|
7
|
+
justify-content: center;
|
|
8
|
+
flex-shrink: 0;
|
|
9
|
+
overflow: hidden;
|
|
10
|
+
background-color: var(--md-sys-color-primary-container, #e8ddff);
|
|
11
|
+
color: var(--md-sys-color-on-primary-container, #4c3e76);
|
|
12
|
+
border-radius: var(
|
|
13
|
+
--avatar-radius,
|
|
14
|
+
var(--md-sys-shape-corner-full, 9999px)
|
|
15
|
+
);
|
|
16
|
+
font-family: var(
|
|
17
|
+
--md-sys-typescale-label-large-font,
|
|
18
|
+
"Inter",
|
|
19
|
+
system-ui,
|
|
20
|
+
-apple-system,
|
|
21
|
+
sans-serif
|
|
22
|
+
);
|
|
23
|
+
width: var(--avatar-size, 2.5rem);
|
|
24
|
+
height: var(--avatar-size, 2.5rem);
|
|
25
|
+
font-size: var(--avatar-font-size, 0.875rem);
|
|
26
|
+
font-weight: var(--md-sys-typescale-label-large-weight, 500);
|
|
27
|
+
line-height: 1;
|
|
28
|
+
user-select: none;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.BHT__Avatar__image {
|
|
32
|
+
width: 100%;
|
|
33
|
+
height: 100%;
|
|
34
|
+
object-fit: cover;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.BHT__Avatar__content {
|
|
38
|
+
display: inline-flex;
|
|
39
|
+
align-items: center;
|
|
40
|
+
justify-content: center;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.BHT__Avatar--sm.BHT__Avatar {
|
|
44
|
+
--avatar-size: 2rem;
|
|
45
|
+
--avatar-font-size: 0.75rem;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.BHT__Avatar--md.BHT__Avatar {
|
|
49
|
+
--avatar-size: 2.5rem;
|
|
50
|
+
--avatar-font-size: 0.875rem;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.BHT__Avatar--lg.BHT__Avatar {
|
|
54
|
+
--avatar-size: 3.5rem;
|
|
55
|
+
--avatar-font-size: 1.25rem;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.BHT__Avatar--circle.BHT__Avatar {
|
|
59
|
+
--avatar-radius: var(--md-sys-shape-corner-full, 9999px);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.BHT__Avatar--square.BHT__Avatar {
|
|
63
|
+
--avatar-radius: var(--md-sys-shape-corner-medium, 12px);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// GENERATED from avatar.block.ts by @behivetech/blocks.builder. Do not edit by hand: run `pnpm block:build`.
|
|
2
|
+
import { createNativeBlock as __bhtCreateNativeBlock } from "@behivetech/blocks.core/native";
|
|
3
|
+
import { StyleSheet as __bhtStyleSheet } from "react-native";
|
|
4
|
+
import type { avatar as __bht0_source } from "./avatar.block";
|
|
5
|
+
|
|
6
|
+
// Entries that read no color token are one object in both schemes.
|
|
7
|
+
const __bht0_shared = __bhtStyleSheet.create({
|
|
8
|
+
__image: { width: "100%", height: "100%" },
|
|
9
|
+
__content: {
|
|
10
|
+
display: "flex",
|
|
11
|
+
flexDirection: "row",
|
|
12
|
+
alignItems: "center",
|
|
13
|
+
justifyContent: "center",
|
|
14
|
+
},
|
|
15
|
+
"--sm": { width: 32, height: 32, fontSize: 12 },
|
|
16
|
+
"--sm~text": { fontSize: 12 },
|
|
17
|
+
"--md": { width: 40, height: 40, fontSize: 14 },
|
|
18
|
+
"--md~text": { fontSize: 14 },
|
|
19
|
+
"--lg": { width: 56, height: 56, fontSize: 20 },
|
|
20
|
+
"--lg~text": { fontSize: 20 },
|
|
21
|
+
"--square": {
|
|
22
|
+
borderTopLeftRadius: 12,
|
|
23
|
+
borderTopRightRadius: 12,
|
|
24
|
+
borderBottomRightRadius: 12,
|
|
25
|
+
borderBottomLeftRadius: 12,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const __bht0_light = __bhtStyleSheet.create({
|
|
29
|
+
root: {
|
|
30
|
+
display: "flex",
|
|
31
|
+
flexDirection: "row",
|
|
32
|
+
alignItems: "center",
|
|
33
|
+
justifyContent: "center",
|
|
34
|
+
flexShrink: 0,
|
|
35
|
+
overflow: "hidden",
|
|
36
|
+
backgroundColor: "#e8ddff",
|
|
37
|
+
color: "#4c3e76",
|
|
38
|
+
borderTopLeftRadius: 9999,
|
|
39
|
+
borderTopRightRadius: 9999,
|
|
40
|
+
borderBottomRightRadius: 9999,
|
|
41
|
+
borderBottomLeftRadius: 9999,
|
|
42
|
+
fontFamily: "Inter",
|
|
43
|
+
fontWeight: "500",
|
|
44
|
+
},
|
|
45
|
+
"root~text": { color: "#4c3e76", fontFamily: "Inter", fontWeight: "500" },
|
|
46
|
+
});
|
|
47
|
+
const __bht0_dark = __bhtStyleSheet.create({
|
|
48
|
+
root: {
|
|
49
|
+
display: "flex",
|
|
50
|
+
flexDirection: "row",
|
|
51
|
+
alignItems: "center",
|
|
52
|
+
justifyContent: "center",
|
|
53
|
+
flexShrink: 0,
|
|
54
|
+
overflow: "hidden",
|
|
55
|
+
backgroundColor: "#4c3e76",
|
|
56
|
+
color: "#e8ddff",
|
|
57
|
+
borderTopLeftRadius: 9999,
|
|
58
|
+
borderTopRightRadius: 9999,
|
|
59
|
+
borderBottomRightRadius: 9999,
|
|
60
|
+
borderBottomLeftRadius: 9999,
|
|
61
|
+
fontFamily: "Inter",
|
|
62
|
+
fontWeight: "500",
|
|
63
|
+
},
|
|
64
|
+
"root~text": { color: "#e8ddff", fontFamily: "Inter", fontWeight: "500" },
|
|
65
|
+
});
|
|
66
|
+
export const avatar: typeof __bht0_source = __bhtCreateNativeBlock<
|
|
67
|
+
typeof __bht0_source
|
|
68
|
+
>("BHT__Avatar", {
|
|
69
|
+
structure: {
|
|
70
|
+
children: { image: {}, content: {} },
|
|
71
|
+
variants: {
|
|
72
|
+
sm: { children: {} },
|
|
73
|
+
md: { children: {} },
|
|
74
|
+
lg: { children: {} },
|
|
75
|
+
circle: { children: {} },
|
|
76
|
+
square: { children: {} },
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
sheets: {
|
|
80
|
+
light: { ...__bht0_shared, ...__bht0_light },
|
|
81
|
+
dark: { ...__bht0_shared, ...__bht0_dark },
|
|
82
|
+
},
|
|
83
|
+
plan: {
|
|
84
|
+
root: [
|
|
85
|
+
{ key: "root" },
|
|
86
|
+
{ key: "--sm", when: { variant: "sm" } },
|
|
87
|
+
{ key: "--md", when: { variant: "md" } },
|
|
88
|
+
{ key: "--lg", when: { variant: "lg" } },
|
|
89
|
+
{ key: "--square", when: { variant: "square" } },
|
|
90
|
+
],
|
|
91
|
+
children: {
|
|
92
|
+
image: [
|
|
93
|
+
{ key: "root~text" },
|
|
94
|
+
{ key: "--sm~text", when: { variant: "sm" } },
|
|
95
|
+
{ key: "--md~text", when: { variant: "md" } },
|
|
96
|
+
{ key: "--lg~text", when: { variant: "lg" } },
|
|
97
|
+
{ key: "__image" },
|
|
98
|
+
],
|
|
99
|
+
content: [
|
|
100
|
+
{ key: "root~text" },
|
|
101
|
+
{ key: "--sm~text", when: { variant: "sm" } },
|
|
102
|
+
{ key: "--md~text", when: { variant: "md" } },
|
|
103
|
+
{ key: "--lg~text", when: { variant: "lg" } },
|
|
104
|
+
{ key: "__content" },
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
});
|
package/avatar.block.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Converted from avatar.module.scss by `block convert`; this file is the source from here on.
|
|
2
|
+
import { defineBlock, hook } from "@behivetech/blocks.core";
|
|
3
|
+
import { tokens } from "@behivetech/blocks.core/tokens";
|
|
4
|
+
const { color, shape, type } = tokens;
|
|
5
|
+
const avatarRadius = hook("avatar-radius", shape.cornerFull);
|
|
6
|
+
export const avatar = defineBlock("BHT__Avatar", {
|
|
7
|
+
display: "inline-flex",
|
|
8
|
+
alignItems: "center",
|
|
9
|
+
justifyContent: "center",
|
|
10
|
+
flexShrink: 0,
|
|
11
|
+
overflow: "hidden",
|
|
12
|
+
backgroundColor: color.primaryContainer,
|
|
13
|
+
color: color.onPrimaryContainer,
|
|
14
|
+
borderRadius: avatarRadius,
|
|
15
|
+
fontFamily: type.labelLarge.font,
|
|
16
|
+
children: {
|
|
17
|
+
image: { width: "100%", height: "100%", web: { objectFit: "cover" } },
|
|
18
|
+
content: {
|
|
19
|
+
display: "inline-flex",
|
|
20
|
+
alignItems: "center",
|
|
21
|
+
justifyContent: "center",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
// The size and the corner are custom properties on the web (a consumer sets `--avatar-size` and the like), and
|
|
25
|
+
// plain values on native, which has no cascade to read them from.
|
|
26
|
+
variants: {
|
|
27
|
+
sm: {
|
|
28
|
+
web: { "--avatar-size": "2rem", "--avatar-font-size": "0.75rem" },
|
|
29
|
+
native: { width: "2rem", height: "2rem", fontSize: "0.75rem" },
|
|
30
|
+
},
|
|
31
|
+
md: {
|
|
32
|
+
web: { "--avatar-size": "2.5rem", "--avatar-font-size": "0.875rem" },
|
|
33
|
+
native: { width: "2.5rem", height: "2.5rem", fontSize: "0.875rem" },
|
|
34
|
+
},
|
|
35
|
+
lg: {
|
|
36
|
+
web: { "--avatar-size": "3.5rem", "--avatar-font-size": "1.25rem" },
|
|
37
|
+
native: { width: "3.5rem", height: "3.5rem", fontSize: "1.25rem" },
|
|
38
|
+
},
|
|
39
|
+
circle: { web: { "--avatar-radius": shape.cornerFull } },
|
|
40
|
+
square: {
|
|
41
|
+
web: { "--avatar-radius": shape.cornerMedium },
|
|
42
|
+
native: { borderRadius: shape.cornerMedium },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
web: {
|
|
46
|
+
width: "var(--avatar-size, 2.5rem)",
|
|
47
|
+
height: "var(--avatar-size, 2.5rem)",
|
|
48
|
+
fontSize: "var(--avatar-font-size, 0.875rem)",
|
|
49
|
+
fontWeight: type.labelLarge.weight,
|
|
50
|
+
lineHeight: "1",
|
|
51
|
+
userSelect: "none",
|
|
52
|
+
},
|
|
53
|
+
native: { fontWeight: type.labelLarge.weight },
|
|
54
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Avatar } from "./avatar.js";
|
|
2
|
+
|
|
3
|
+
/** Initials from a name: the first letters of the first two words. */
|
|
4
|
+
export const InitialsAvatar = () => <Avatar name="Camp Greenwood" />;
|
|
5
|
+
|
|
6
|
+
/** A picture; if it fails to load, the initials show instead. `alt` names it for assistive technology. */
|
|
7
|
+
export const ImageAvatar = () => (
|
|
8
|
+
<Avatar
|
|
9
|
+
name="Ada Lovelace"
|
|
10
|
+
alt="Ada Lovelace"
|
|
11
|
+
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Crect width='8' height='8' fill='%23767'/%3E%3C/svg%3E"
|
|
12
|
+
/>
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
/** A broken picture falls back to the initials. */
|
|
16
|
+
export const BrokenImageAvatar = () => (
|
|
17
|
+
<Avatar name="Grace Hopper" src="/does-not-exist.png" />
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
/** With no name and no picture, `fallback` shows. */
|
|
21
|
+
export const FallbackAvatar = () => <Avatar fallback="?" />;
|
|
22
|
+
|
|
23
|
+
/** Three sizes. */
|
|
24
|
+
export const AvatarSizes = () => (
|
|
25
|
+
<>
|
|
26
|
+
<Avatar name="Sam Small" size="sm" />
|
|
27
|
+
<Avatar name="Mia Medium" size="md" />
|
|
28
|
+
<Avatar name="Leo Large" size="lg" />
|
|
29
|
+
</>
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/** Circle for people, square for organizations. */
|
|
33
|
+
export const AvatarShapes = () => (
|
|
34
|
+
<>
|
|
35
|
+
<Avatar name="Ada Lovelace" shape="circle" />
|
|
36
|
+
<Avatar name="Acme" shape="square" />
|
|
37
|
+
</>
|
|
38
|
+
);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { useBlock, type StylesFor } from "@behivetech/blocks.core/native";
|
|
2
|
+
import { type ReactNode, useState } from "react";
|
|
3
|
+
import {
|
|
4
|
+
Image,
|
|
5
|
+
Text,
|
|
6
|
+
View,
|
|
7
|
+
type StyleProp,
|
|
8
|
+
type ViewProps,
|
|
9
|
+
type ViewStyle,
|
|
10
|
+
} from "react-native";
|
|
11
|
+
import { avatar } from "./avatar.block";
|
|
12
|
+
import { getInitials } from "./get-initials";
|
|
13
|
+
import type { AvatarShape, AvatarSize } from "./avatar";
|
|
14
|
+
|
|
15
|
+
// The types the web renderer owns, re-exported so the native entry declares the same names. A native file's
|
|
16
|
+
// import of its own stem means the web file in the native declarations (`emitNativeDeclarations`).
|
|
17
|
+
export type { AvatarShape, AvatarSize } from "./avatar";
|
|
18
|
+
export { getInitials };
|
|
19
|
+
|
|
20
|
+
export interface AvatarProps extends Omit<ViewProps, "style" | "children"> {
|
|
21
|
+
/** Picture to show; when it is missing or fails to load, the initials of `name` show instead */
|
|
22
|
+
src?: string;
|
|
23
|
+
/** The person's or organization's name; its first letters are the fallback content */
|
|
24
|
+
name?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Accessible name of the avatar, for example "Camp Greenwood". Without it the
|
|
27
|
+
* avatar is decorative and hidden from assistive technology, which is right
|
|
28
|
+
* when the name is written beside it.
|
|
29
|
+
*/
|
|
30
|
+
alt?: string;
|
|
31
|
+
/** Shown when there is neither a loadable `src` nor a `name`, for example an icon */
|
|
32
|
+
fallback?: ReactNode;
|
|
33
|
+
/** Controls the size; defaults to md */
|
|
34
|
+
size?: AvatarSize;
|
|
35
|
+
/** "circle" for people, "square" for organizations; defaults to circle */
|
|
36
|
+
shape?: AvatarShape;
|
|
37
|
+
/** Additional styles, appended after the block's own */
|
|
38
|
+
style?: StyleProp<ViewStyle>;
|
|
39
|
+
/** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
|
|
40
|
+
styles?: StylesFor<typeof avatar>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The React Native renderer of Avatar: the same block as the web one, with the same props. The initials, or a
|
|
45
|
+
* `fallback` of any kind, sit in a `Text` (an icon element nests in it), so a string, a number or a
|
|
46
|
+
* `FormattedMessage` cannot reach a bare `View`.
|
|
47
|
+
*/
|
|
48
|
+
export const Avatar = ({
|
|
49
|
+
src,
|
|
50
|
+
name,
|
|
51
|
+
alt,
|
|
52
|
+
fallback,
|
|
53
|
+
size = "md",
|
|
54
|
+
shape = "circle",
|
|
55
|
+
style,
|
|
56
|
+
styles,
|
|
57
|
+
...rest
|
|
58
|
+
}: AvatarProps) => {
|
|
59
|
+
// The failed address, not a boolean, so a new `src` gets a fresh try.
|
|
60
|
+
const [failedSrc, setFailedSrc] = useState<string>();
|
|
61
|
+
const showImage = src !== undefined && src !== failedSrc;
|
|
62
|
+
const initials = name === undefined ? "" : getInitials(name);
|
|
63
|
+
const [rootProps, childProps] = useBlock(
|
|
64
|
+
avatar,
|
|
65
|
+
{ [size]: true, [shape]: true },
|
|
66
|
+
{ style, styles },
|
|
67
|
+
);
|
|
68
|
+
// Named, it is an image; unnamed, it is decorative and left out of the accessibility tree.
|
|
69
|
+
const accessibleProps =
|
|
70
|
+
alt === undefined
|
|
71
|
+
? {
|
|
72
|
+
accessibilityElementsHidden: true,
|
|
73
|
+
importantForAccessibility: "no-hide-descendants" as const,
|
|
74
|
+
}
|
|
75
|
+
: { accessible: true, role: "img" as const, accessibilityLabel: alt };
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<View {...rest} {...accessibleProps} {...rootProps}>
|
|
79
|
+
{showImage ? (
|
|
80
|
+
<Image
|
|
81
|
+
{...childProps("image")}
|
|
82
|
+
source={{ uri: src }}
|
|
83
|
+
resizeMode="cover"
|
|
84
|
+
accessible={false}
|
|
85
|
+
onError={() => setFailedSrc(src)}
|
|
86
|
+
/>
|
|
87
|
+
) : (
|
|
88
|
+
<Text {...childProps("content")}>
|
|
89
|
+
{initials === "" ? fallback : initials}
|
|
90
|
+
</Text>
|
|
91
|
+
)}
|
|
92
|
+
</View>
|
|
93
|
+
);
|
|
94
|
+
};
|
package/avatar.tsx
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type HTMLAttributes, type ReactNode, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import "./avatar.block.css";
|
|
4
|
+
import { useBlock, type StylesFor } from "@behivetech/blocks.core";
|
|
5
|
+
import { avatar } from "./avatar.block";
|
|
6
|
+
import { getInitials } from "./get-initials";
|
|
7
|
+
|
|
8
|
+
export type AvatarSize = "sm" | "md" | "lg";
|
|
9
|
+
export type AvatarShape = "circle" | "square";
|
|
10
|
+
|
|
11
|
+
export interface AvatarProps extends HTMLAttributes<HTMLSpanElement> {
|
|
12
|
+
/** Additional class names to merge with the component root element */
|
|
13
|
+
className?: string;
|
|
14
|
+
/** Picture to show; when it is missing or fails to load, the initials of `name` show instead */
|
|
15
|
+
src?: string;
|
|
16
|
+
/** The person's or organization's name; its first letters are the fallback content */
|
|
17
|
+
name?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Accessible name of the avatar, for example "Camp Greenwood". Without it the
|
|
20
|
+
* avatar is decorative and hidden from assistive technology, which is right
|
|
21
|
+
* when the name is written beside it.
|
|
22
|
+
*/
|
|
23
|
+
alt?: string;
|
|
24
|
+
/** Shown when there is neither a loadable `src` nor a `name`, for example an icon */
|
|
25
|
+
fallback?: ReactNode;
|
|
26
|
+
/** Controls the size; defaults to md */
|
|
27
|
+
size?: AvatarSize;
|
|
28
|
+
/** "circle" for people, "square" for organizations; defaults to circle */
|
|
29
|
+
shape?: AvatarShape;
|
|
30
|
+
|
|
31
|
+
/** An override block made with `defineBlock`, typed to this component's children and variants; it wins over the block's own styles. */
|
|
32
|
+
styles?: StylesFor<typeof avatar>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { getInitials };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A picture, or the initials of a name, or a fallback, in a small circle or
|
|
39
|
+
* square. A picture that fails to load falls back to the initials, so a broken
|
|
40
|
+
* link never shows a broken image.
|
|
41
|
+
*/
|
|
42
|
+
export const Avatar = ({
|
|
43
|
+
className,
|
|
44
|
+
src,
|
|
45
|
+
name,
|
|
46
|
+
alt,
|
|
47
|
+
fallback,
|
|
48
|
+
size = "md",
|
|
49
|
+
shape = "circle",
|
|
50
|
+
styles,
|
|
51
|
+
...rest
|
|
52
|
+
}: AvatarProps) => {
|
|
53
|
+
// The failed address, not a boolean, so a new `src` gets a fresh try.
|
|
54
|
+
const [failedSrc, setFailedSrc] = useState<string>();
|
|
55
|
+
const showImage = src !== undefined && src !== failedSrc;
|
|
56
|
+
const initials = name === undefined ? "" : getInitials(name);
|
|
57
|
+
const [rootProps, childProps] = useBlock(
|
|
58
|
+
avatar,
|
|
59
|
+
{ [size]: true, [shape]: true },
|
|
60
|
+
{ className, styles },
|
|
61
|
+
);
|
|
62
|
+
const accessibleProps =
|
|
63
|
+
alt === undefined
|
|
64
|
+
? { "aria-hidden": true }
|
|
65
|
+
: { role: "img", "aria-label": alt };
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<span {...rest} {...accessibleProps} {...rootProps}>
|
|
69
|
+
{showImage ? (
|
|
70
|
+
<img
|
|
71
|
+
{...childProps("image")}
|
|
72
|
+
src={src}
|
|
73
|
+
alt=""
|
|
74
|
+
onError={() => setFailedSrc(src)}
|
|
75
|
+
/>
|
|
76
|
+
) : (
|
|
77
|
+
<span {...childProps("content")}>
|
|
78
|
+
{initials === "" ? fallback : initials}
|
|
79
|
+
</span>
|
|
80
|
+
)}
|
|
81
|
+
</span>
|
|
82
|
+
);
|
|
83
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
Avatar: () => Avatar,
|
|
25
|
+
getInitials: () => getInitials
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// avatar.tsx
|
|
30
|
+
var import_react = require("react");
|
|
31
|
+
var import_blocks2 = require("@behivetech/blocks.core");
|
|
32
|
+
|
|
33
|
+
// avatar.block.ts
|
|
34
|
+
var import_blocks = require("@behivetech/blocks.core");
|
|
35
|
+
var import_tokens = require("@behivetech/blocks.core/tokens");
|
|
36
|
+
var { color, shape, type } = import_tokens.tokens;
|
|
37
|
+
var avatarRadius = (0, import_blocks.hook)("avatar-radius", shape.cornerFull);
|
|
38
|
+
var avatar = (0, import_blocks.defineBlock)("BHT__Avatar", {
|
|
39
|
+
display: "inline-flex",
|
|
40
|
+
alignItems: "center",
|
|
41
|
+
justifyContent: "center",
|
|
42
|
+
flexShrink: 0,
|
|
43
|
+
overflow: "hidden",
|
|
44
|
+
backgroundColor: color.primaryContainer,
|
|
45
|
+
color: color.onPrimaryContainer,
|
|
46
|
+
borderRadius: avatarRadius,
|
|
47
|
+
fontFamily: type.labelLarge.font,
|
|
48
|
+
children: {
|
|
49
|
+
image: { width: "100%", height: "100%", web: { objectFit: "cover" } },
|
|
50
|
+
content: {
|
|
51
|
+
display: "inline-flex",
|
|
52
|
+
alignItems: "center",
|
|
53
|
+
justifyContent: "center"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
// The size and the corner are custom properties on the web (a consumer sets `--avatar-size` and the like), and
|
|
57
|
+
// plain values on native, which has no cascade to read them from.
|
|
58
|
+
variants: {
|
|
59
|
+
sm: {
|
|
60
|
+
web: { "--avatar-size": "2rem", "--avatar-font-size": "0.75rem" },
|
|
61
|
+
native: { width: "2rem", height: "2rem", fontSize: "0.75rem" }
|
|
62
|
+
},
|
|
63
|
+
md: {
|
|
64
|
+
web: { "--avatar-size": "2.5rem", "--avatar-font-size": "0.875rem" },
|
|
65
|
+
native: { width: "2.5rem", height: "2.5rem", fontSize: "0.875rem" }
|
|
66
|
+
},
|
|
67
|
+
lg: {
|
|
68
|
+
web: { "--avatar-size": "3.5rem", "--avatar-font-size": "1.25rem" },
|
|
69
|
+
native: { width: "3.5rem", height: "3.5rem", fontSize: "1.25rem" }
|
|
70
|
+
},
|
|
71
|
+
circle: { web: { "--avatar-radius": shape.cornerFull } },
|
|
72
|
+
square: {
|
|
73
|
+
web: { "--avatar-radius": shape.cornerMedium },
|
|
74
|
+
native: { borderRadius: shape.cornerMedium }
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
web: {
|
|
78
|
+
width: "var(--avatar-size, 2.5rem)",
|
|
79
|
+
height: "var(--avatar-size, 2.5rem)",
|
|
80
|
+
fontSize: "var(--avatar-font-size, 0.875rem)",
|
|
81
|
+
fontWeight: type.labelLarge.weight,
|
|
82
|
+
lineHeight: "1",
|
|
83
|
+
userSelect: "none"
|
|
84
|
+
},
|
|
85
|
+
native: { fontWeight: type.labelLarge.weight }
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// get-initials.ts
|
|
89
|
+
var MAX_INITIALS = 2;
|
|
90
|
+
var getInitials = (name) => name.trim().split(/\s+/).filter(Boolean).slice(0, MAX_INITIALS).map((word) => Array.from(word)[0]).join("").toUpperCase();
|
|
91
|
+
|
|
92
|
+
// avatar.tsx
|
|
93
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
94
|
+
var Avatar = ({
|
|
95
|
+
className,
|
|
96
|
+
src,
|
|
97
|
+
name,
|
|
98
|
+
alt,
|
|
99
|
+
fallback,
|
|
100
|
+
size = "md",
|
|
101
|
+
shape: shape2 = "circle",
|
|
102
|
+
styles,
|
|
103
|
+
...rest
|
|
104
|
+
}) => {
|
|
105
|
+
const [failedSrc, setFailedSrc] = (0, import_react.useState)();
|
|
106
|
+
const showImage = src !== void 0 && src !== failedSrc;
|
|
107
|
+
const initials = name === void 0 ? "" : getInitials(name);
|
|
108
|
+
const [rootProps, childProps] = (0, import_blocks2.useBlock)(
|
|
109
|
+
avatar,
|
|
110
|
+
{ [size]: true, [shape2]: true },
|
|
111
|
+
{ className, styles }
|
|
112
|
+
);
|
|
113
|
+
const accessibleProps = alt === void 0 ? { "aria-hidden": true } : { role: "img", "aria-label": alt };
|
|
114
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { ...rest, ...accessibleProps, ...rootProps, children: showImage ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
115
|
+
"img",
|
|
116
|
+
{
|
|
117
|
+
...childProps("image"),
|
|
118
|
+
src,
|
|
119
|
+
alt: "",
|
|
120
|
+
onError: () => setFailedSrc(src)
|
|
121
|
+
}
|
|
122
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { ...childProps("content"), children: initials === "" ? fallback : initials }) });
|
|
123
|
+
};
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
Avatar,
|
|
127
|
+
getInitials
|
|
128
|
+
});
|
package/dist/index.css
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* avatar.block.css */
|
|
2
|
+
@layer bht {
|
|
3
|
+
.BHT__Avatar {
|
|
4
|
+
display: inline-flex;
|
|
5
|
+
align-items: center;
|
|
6
|
+
justify-content: center;
|
|
7
|
+
flex-shrink: 0;
|
|
8
|
+
overflow: hidden;
|
|
9
|
+
background-color: var(--md-sys-color-primary-container, #e8ddff);
|
|
10
|
+
color: var(--md-sys-color-on-primary-container, #4c3e76);
|
|
11
|
+
border-radius: var( --avatar-radius, var(--md-sys-shape-corner-full, 9999px) );
|
|
12
|
+
font-family: var( --md-sys-typescale-label-large-font, "Inter", system-ui, -apple-system, sans-serif );
|
|
13
|
+
width: var(--avatar-size, 2.5rem);
|
|
14
|
+
height: var(--avatar-size, 2.5rem);
|
|
15
|
+
font-size: var(--avatar-font-size, 0.875rem);
|
|
16
|
+
font-weight: var(--md-sys-typescale-label-large-weight, 500);
|
|
17
|
+
line-height: 1;
|
|
18
|
+
user-select: none;
|
|
19
|
+
}
|
|
20
|
+
.BHT__Avatar__image {
|
|
21
|
+
width: 100%;
|
|
22
|
+
height: 100%;
|
|
23
|
+
object-fit: cover;
|
|
24
|
+
}
|
|
25
|
+
.BHT__Avatar__content {
|
|
26
|
+
display: inline-flex;
|
|
27
|
+
align-items: center;
|
|
28
|
+
justify-content: center;
|
|
29
|
+
}
|
|
30
|
+
.BHT__Avatar--sm.BHT__Avatar {
|
|
31
|
+
--avatar-size: 2rem;
|
|
32
|
+
--avatar-font-size: 0.75rem;
|
|
33
|
+
}
|
|
34
|
+
.BHT__Avatar--md.BHT__Avatar {
|
|
35
|
+
--avatar-size: 2.5rem;
|
|
36
|
+
--avatar-font-size: 0.875rem;
|
|
37
|
+
}
|
|
38
|
+
.BHT__Avatar--lg.BHT__Avatar {
|
|
39
|
+
--avatar-size: 3.5rem;
|
|
40
|
+
--avatar-font-size: 1.25rem;
|
|
41
|
+
}
|
|
42
|
+
.BHT__Avatar--circle.BHT__Avatar {
|
|
43
|
+
--avatar-radius: var(--md-sys-shape-corner-full, 9999px);
|
|
44
|
+
}
|
|
45
|
+
.BHT__Avatar--square.BHT__Avatar {
|
|
46
|
+
--avatar-radius: var(--md-sys-shape-corner-medium, 12px);
|
|
47
|
+
}
|
|
48
|
+
}
|