@gopherium/godmin 0.17.0 → 0.18.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/dist/avatar.d.ts CHANGED
@@ -14,10 +14,12 @@ export interface InitialsAvatarProps {
14
14
  name: string;
15
15
  /** The avatar's width and height in pixels, the stylesheet size when absent. */
16
16
  size?: number;
17
+ /** The address of a picture shown in place of the letter, the letter when absent, empty or failing to load. */
18
+ src?: string;
17
19
  }
18
20
  /**
19
- * Renders a round avatar with the first letter of a name, in the letter colour of the circle colour the name picks.
20
- * @param props - The name the avatar stands for and its size.
21
+ * Renders a round avatar with a picture, or the first letter of a name in the colours the name picks.
22
+ * @param props - The name the avatar stands for, its size and the address of its picture.
21
23
  * @returns The avatar element.
22
24
  */
23
- export declare function InitialsAvatar({ name, size }: InitialsAvatarProps): import("react").JSX.Element;
25
+ export declare function InitialsAvatar({ name, size, src }: InitialsAvatarProps): import("react").JSX.Element;
package/dist/avatar.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ /* oxlint-disable react/only-export-components -- the palette ships beside the avatar that paints with it. */
3
+ import { useState } from 'react';
2
4
  /**
3
5
  * The colours an initials avatar paints with, the WordPress blue, raspberry and purple first with their pale letters.
4
6
  */
@@ -17,7 +19,7 @@ export const AVATAR_COLORS = [
17
19
  */
18
20
  function colorFor(name) {
19
21
  let hash = 0;
20
- for (const letter of name.trim().toLocaleLowerCase()) {
22
+ for (const letter of name.trim().toLowerCase()) {
21
23
  hash = (hash * 31 + letter.codePointAt(0)) % 2147483647;
22
24
  }
23
25
  return AVATAR_COLORS[hash % AVATAR_COLORS.length];
@@ -29,7 +31,7 @@ function colorFor(name) {
29
31
  */
30
32
  function initialOf(name) {
31
33
  const [first = ''] = Array.from(name.trim());
32
- return first.toLocaleUpperCase();
34
+ return first.toUpperCase();
33
35
  }
34
36
  /**
35
37
  * The share of the circle a letter takes, 12px in the 32px stylesheet circle.
@@ -47,11 +49,34 @@ function sizedBy(size) {
47
49
  return { inlineSize: size, blockSize: size, fontSize: size * LETTER_SHARE };
48
50
  }
49
51
  /**
50
- * Renders a round avatar with the first letter of a name, in the letter colour of the circle colour the name picks.
52
+ * Renders a round avatar with a picture, or the first letter of a name in the colours the name picks.
53
+ * @param props - The name the avatar stands for, its size and the address of its picture.
54
+ * @returns The avatar element.
55
+ */
56
+ export function InitialsAvatar({ name, size, src }) {
57
+ if (!src) {
58
+ return _jsx(LetterAvatar, { name: name, size: size });
59
+ }
60
+ return _jsx(PictureAvatar, { name: name, size: size, src: src }, src);
61
+ }
62
+ /**
63
+ * Renders the picture at one address, or the letter once that picture fails to load.
64
+ * @param props - The name the avatar stands for, its size and the address of its picture.
65
+ * @returns The avatar element.
66
+ */
67
+ function PictureAvatar({ name, size, src }) {
68
+ const [failed, setFailed] = useState(false);
69
+ if (failed) {
70
+ return _jsx(LetterAvatar, { name: name, size: size });
71
+ }
72
+ return (_jsx("span", { className: "godmin-avatar", "aria-hidden": "true", style: sizedBy(size), children: _jsx("img", { className: "godmin-avatar__image", src: src, alt: "", onError: () => setFailed(true) }) }));
73
+ }
74
+ /**
75
+ * Renders the first letter of a name in the circle and letter colours the name picks.
51
76
  * @param props - The name the avatar stands for and its size.
52
77
  * @returns The avatar element.
53
78
  */
54
- export function InitialsAvatar({ name, size }) {
79
+ function LetterAvatar({ name, size }) {
55
80
  const { background, letter } = colorFor(name);
56
81
  return (_jsx("span", { className: "godmin-avatar", "aria-hidden": "true", style: { backgroundColor: background, color: letter, ...sizedBy(size) }, children: initialOf(name) }));
57
82
  }
package/dist/base.css CHANGED
@@ -618,6 +618,14 @@ body {
618
618
  user-select: none;
619
619
  }
620
620
 
621
+ .godmin-avatar__image {
622
+ display: block;
623
+ inline-size: 100%;
624
+ block-size: 100%;
625
+ border-radius: 50%;
626
+ object-fit: cover;
627
+ }
628
+
621
629
  .godmin-table__actions * {
622
630
  white-space: nowrap;
623
631
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gopherium/godmin",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "The admin kit for Gopherium projects: host layer for React admin applications on the WordPress Design System.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",