@galaxy-io/dls 2.12.2 → 2.13.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.
@@ -13,6 +13,8 @@ export declare enum AvatarSize {
13
13
  /** 40px (`size.control.xLarge`); initials `body_lg`. */
14
14
  X_LARGE = "X_LARGE"
15
15
  }
16
+ /** The badge avatar: what it shows and its shape. Its size comes from the avatar it sits on. */
17
+ export type AvatarBadge = Pick<AvatarProps, "name" | "img" | "seed" | "isSquare" | "color" | "hex">;
16
18
  export interface AvatarProps extends AriaAttributes {
17
19
  /** Name of the person or entity. It gives the initials fallback and the accessible name. */
18
20
  name?: string;
@@ -44,6 +46,12 @@ export interface AvatarProps extends AriaAttributes {
44
46
  * @default false
45
47
  */
46
48
  isLoading?: boolean;
49
+ /**
50
+ * A smaller avatar in the bottom-end corner for this one's context: a person on an organization
51
+ * (`isSquare` outside, a round badge) or an organization on a person. Sized from this avatar
52
+ * (8 / 9 / 12 / 13 / 14px on the rungs) and named with it ("Ada Lovelace, Galaxy").
53
+ */
54
+ badge?: AvatarBadge;
47
55
  /** Accessible name, when it should differ from `name` (for example "Ada Lovelace, owner"). Without it and without `name`, the avatar is decorative (`aria-hidden`). */
48
56
  ariaLabel?: string;
49
57
  /** DOM id on the root `<span>`. */
@@ -1,10 +1,11 @@
1
1
  import Skeleton, { SkeletonVariant } from "../feedback/Skeleton.js";
2
2
  import Text, { TextSize, TextVariant, TextWeight } from "../text/Text.js";
3
3
  import { CONTROL_SIZE } from "../theme/tokens/scales.js";
4
+ import { getAvatarAccessibleName, getAvatarBadgeDiameter, getAvatarBadgedInitials } from "./internal/avatarBadge.js";
4
5
  /* empty css */
5
6
  import { styled } from "@linaria/react";
6
7
  import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
7
- import { jsx } from "react/jsx-runtime";
8
+ import { jsx, jsxs } from "react/jsx-runtime";
8
9
  import { cx } from "@linaria/core";
9
10
  //#region src/avatar/Avatar.tsx
10
11
  /**
@@ -19,6 +20,11 @@ import { cx } from "@linaria/core";
19
20
  * (`--gx-avatar-tint`, the surface fill and hairline) and `hex` is the `--gx-avatar-hex` custom
20
21
  * property, so a theme switch needs no re-render and nothing reads the theme at runtime.
21
22
  *
23
+ * A `badge` is a second, smaller avatar in the bottom-end corner for the context of the first: a
24
+ * person's organization, or the person acting in a workspace. It sits inside the avatar's box, and
25
+ * the main avatar is cut away around it by a mask (no ring, no shadow), so the pair reads on any
26
+ * surface. The two are one image with one accessible name.
27
+ *
22
28
  * Not a button (wrap it in one), not a stack (`AvatarGroup`), not a name label (put a `Text`
23
29
  * beside it), not a generic image container.
24
30
  */
@@ -51,61 +57,77 @@ var Surface = /*#__PURE__*/ styled("span")({
51
57
  class: "gx-Avatar-Surface-s3r0r1h",
52
58
  propsAsIs: false
53
59
  });
60
+ /** The badge's box: the bottom-end corner, inside the avatar's own bounds. */
61
+ var BadgeSlot = /*#__PURE__*/ styled("span")({
62
+ name: "BadgeSlot",
63
+ class: "gx-Avatar-BadgeSlot-b1n32hoy",
64
+ propsAsIs: false
65
+ });
54
66
  var Image = /*#__PURE__*/ styled("img")({
55
67
  name: "Image",
56
- class: "gx-Avatar-Image-i1n32hoy",
68
+ class: "gx-Avatar-Image-i94shpk",
57
69
  propsAsIs: false
58
70
  });
59
71
  /** The generated pattern: one `viewBox` grid that scales to any diameter. Tinted by `hex` or `color`. */
60
72
  var Pattern = /*#__PURE__*/ styled("svg")({
61
73
  name: "Pattern",
62
- class: "gx-Avatar-Pattern-p94shpk",
74
+ class: "gx-Avatar-Pattern-pblj6sc",
63
75
  propsAsIs: false
64
76
  });
65
77
  /** Diameter per rung. */
66
78
  var AVATAR_SIZE_TO_CSS_MAP = {
67
- ["X_SMALL"]: "gx-Avatar-X_SMALL-xblj6sc",
68
- ["SMALL"]: "gx-Avatar-SMALL-s116bpx4",
69
- ["MEDIUM"]: "gx-Avatar-MEDIUM-m1ykihtn",
70
- ["LARGE"]: "gx-Avatar-LARGE-l1vt38m2",
71
- ["X_LARGE"]: "gx-Avatar-X_LARGE-xjtmhfq"
79
+ ["X_SMALL"]: "gx-Avatar-X_SMALL-x116bpx4",
80
+ ["SMALL"]: "gx-Avatar-SMALL-s1ykihtn",
81
+ ["MEDIUM"]: "gx-Avatar-MEDIUM-m1vt38m2",
82
+ ["LARGE"]: "gx-Avatar-LARGE-ljtmhfq",
83
+ ["X_LARGE"]: "gx-Avatar-X_LARGE-xtukwhw"
72
84
  };
73
85
  /** A px diameter, set as `--gx-avatar-size` in `style` (a continuous value). */
74
- var PX_SIZE_CSS = "gx-Avatar-PX_SIZE_CSS-ptukwhw";
86
+ var PX_SIZE_CSS = "gx-Avatar-PX_SIZE_CSS-pdv3w7l";
75
87
  /** `isSquare`: control-radius corners at every size. */
76
- var SQUARE_CSS = "gx-Avatar-SQUARE_CSS-sdv3w7l";
88
+ var SQUARE_CSS = "gx-Avatar-SQUARE_CSS-s1c3dn12";
89
+ /** The gap between the badge and the cut-away edge of the avatar under it. */
90
+ /** A round badge: a circular notch centered on the badge, one gap wider. Mirrors under RTL. */
91
+ var ROUND_NOTCH_CSS = "gx-Avatar-ROUND_NOTCH_CSS-r63z1lx";
92
+ /**
93
+ * A square badge: a notch in the corner one gap wider than the badge, square where it meets the
94
+ * avatar's edges and rounded at its inner corner by `radius.md`, the radius of every control. Two
95
+ * rectangles and a quarter circle make the notch; together they are subtracted from the avatar.
96
+ * Mirrors under RTL.
97
+ */
98
+ var SQUARE_NOTCH_CSS = "gx-Avatar-SQUARE_NOTCH_CSS-s1x7b2uf";
77
99
  /** The initials' color, which the `INHERIT` text takes. Tinted by `hex` or `color`. */
78
- var INITIALS_CSS = "gx-Avatar-INITIALS_CSS-i1c3dn12";
100
+ var INITIALS_CSS = "gx-Avatar-INITIALS_CSS-iz59h18";
79
101
  /** Categorical `color` on the fallback surface: the tint the initials and pattern read, the family's fill and hairline. */
80
102
  var AVATAR_COLOR_TO_CSS_MAP = {
81
- red: "gx-Avatar-red-r63z1lx",
82
- orange: "gx-Avatar-orange-o1x7b2uf",
83
- amber: "gx-Avatar-amber-az59h18",
84
- yellow: "gx-Avatar-yellow-y1y5rsmb",
85
- lime: "gx-Avatar-lime-lzxsfel",
86
- green: "gx-Avatar-green-g414m3u",
87
- emerald: "gx-Avatar-emerald-e1uew7pg",
88
- teal: "gx-Avatar-teal-t6z9q1b",
89
- cyan: "gx-Avatar-cyan-chef53l",
90
- sky: "gx-Avatar-sky-s1o5jkv9",
91
- blue: "gx-Avatar-blue-be52zro",
92
- indigo: "gx-Avatar-indigo-i9qjv1q",
93
- violet: "gx-Avatar-violet-v6zn1e0",
94
- purple: "gx-Avatar-purple-pc3yopb",
95
- fuchsia: "gx-Avatar-fuchsia-fzee674",
96
- pink: "gx-Avatar-pink-p1f7v2mb",
97
- rose: "gx-Avatar-rose-r87xw4u"
103
+ red: "gx-Avatar-red-r1y5rsmb",
104
+ orange: "gx-Avatar-orange-ozxsfel",
105
+ amber: "gx-Avatar-amber-a414m3u",
106
+ yellow: "gx-Avatar-yellow-y1uew7pg",
107
+ lime: "gx-Avatar-lime-l6z9q1b",
108
+ green: "gx-Avatar-green-ghef53l",
109
+ emerald: "gx-Avatar-emerald-e1o5jkv9",
110
+ teal: "gx-Avatar-teal-te52zro",
111
+ cyan: "gx-Avatar-cyan-c9qjv1q",
112
+ sky: "gx-Avatar-sky-s6zn1e0",
113
+ blue: "gx-Avatar-blue-bc3yopb",
114
+ indigo: "gx-Avatar-indigo-izee674",
115
+ violet: "gx-Avatar-violet-v1f7v2mb",
116
+ purple: "gx-Avatar-purple-p87xw4u",
117
+ fuchsia: "gx-Avatar-fuchsia-f17r187b",
118
+ pink: "gx-Avatar-pink-p9ksu8k",
119
+ rose: "gx-Avatar-rose-r1ql1bax"
98
120
  };
99
121
  /**
100
122
  * The pattern's five tones of `currentColor`, indexed by the PRNG (a palette index, not an enum
101
123
  * axis). They replace the 1.x JS color math: base, lighter, darker, 30% and 15% opaque.
102
124
  */
103
125
  var PATTERN_TONE_CSS = [
104
- "gx-Avatar-PATTERN_TONE_CSS-p17r187b",
105
- "gx-Avatar-PATTERN_TONE_CSS-p9ksu8k",
106
- "gx-Avatar-PATTERN_TONE_CSS-p1ql1bax",
107
126
  "gx-Avatar-PATTERN_TONE_CSS-p12sxa1c",
108
- "gx-Avatar-PATTERN_TONE_CSS-pq3238h"
127
+ "gx-Avatar-PATTERN_TONE_CSS-pq3238h",
128
+ "gx-Avatar-PATTERN_TONE_CSS-p826b8b",
129
+ "gx-Avatar-PATTERN_TONE_CSS-p7662vu",
130
+ "gx-Avatar-PATTERN_TONE_CSS-p1j9rxb"
109
131
  ];
110
132
  /** Initials text per rung (Constitution III-A.4): lines 12 / 16 / 16 / 20 / 20px, so whole-pixel offsets. */
111
133
  var AVATAR_SIZE_TO_TEXT_SIZE_MAP = {
@@ -140,6 +162,13 @@ var PATTERN_GRID_MAX = 9;
140
162
  var PATTERN_DOT_RADIUS = .3;
141
163
  /** Number of pattern tones (`PATTERN_TONE_CSS`). */
142
164
  var PATTERN_TONE_COUNT = 5;
165
+ /** Initials text steps from smallest to largest, for stepping down under a badge. */
166
+ var AVATAR_TEXT_SIZE_STEPS = AVATAR_PX_TO_TEXT_SIZE_STEPS.map(([, step]) => step).reverse();
167
+ /** One initials step smaller (never below `CAPTION`), so the letters fit beside a badge. */
168
+ function getBadgedTextSize(textSize) {
169
+ const index = AVATAR_TEXT_SIZE_STEPS.indexOf(textSize);
170
+ return AVATAR_TEXT_SIZE_STEPS[Math.max(0, index - 1)] ?? TextSize.CAPTION;
171
+ }
143
172
  /** The initials text step for a px diameter. */
144
173
  function getTextSizeForPx(px) {
145
174
  return AVATAR_PX_TO_TEXT_SIZE_STEPS.find(([min]) => min <= px)?.[1] ?? TextSize.CAPTION;
@@ -196,7 +225,7 @@ function getInitials(name) {
196
225
  return `${firstChars[0] ?? ""}${Array.from(last)[0] ?? ""}`.toLocaleUpperCase();
197
226
  }
198
227
  /** A person or entity: image, initials or generated pattern, round or square. */
199
- var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSquare = false, color, hex, isLoading = false, ariaLabel, id, className, style, ...rest }, ref) {
228
+ var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSquare = false, color, hex, isLoading = false, badge, ariaLabel, id, className, style, ...rest }, ref) {
200
229
  const imageRef = useRef(null);
201
230
  const [failedSrc, setFailedSrc] = useState(null);
202
231
  useEffect(() => {
@@ -205,16 +234,18 @@ var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSq
205
234
  }, [img]);
206
235
  const isPx = typeof size === "number";
207
236
  const diameter = isPx ? size : AVATAR_SIZE_TO_DIAMETER_PX_MAP[size];
208
- const textSize = isPx ? getTextSizeForPx(size) : AVATAR_SIZE_TO_TEXT_SIZE_MAP[size];
237
+ const baseTextSize = isPx ? getTextSizeForPx(size) : AVATAR_SIZE_TO_TEXT_SIZE_MAP[size];
238
+ const textSize = badge === void 0 ? baseTextSize : getBadgedTextSize(baseTextSize);
209
239
  const imageSrc = typeof img === "string" && img.length > 0 && img !== failedSrc ? img : void 0;
210
240
  const hasImage = imageSrc !== void 0;
211
- const initials = getInitials(name);
241
+ const nameInitials = getInitials(name);
242
+ const initials = badge === void 0 ? nameInitials : getAvatarBadgedInitials(nameInitials, diameter);
212
243
  const isInitials = !hasImage && seed === void 0 && initials.length > 0;
213
244
  const tintCss = color !== void 0 && hex === void 0 && !hasImage ? AVATAR_COLOR_TO_CSS_MAP[color] : void 0;
214
245
  const seedValue = seed ?? hex ?? "default";
215
246
  const grid = getPatternGrid(diameter);
216
247
  const cells = useMemo(() => getPatternCells(seedValue, grid), [seedValue, grid]);
217
- const accessibleName = ariaLabel ?? name;
248
+ const accessibleName = getAvatarAccessibleName(ariaLabel, name, badge?.name);
218
249
  const accessibility = accessibleName !== void 0 && accessibleName.length > 0 ? {
219
250
  role: "img",
220
251
  "aria-label": accessibleName
@@ -222,6 +253,9 @@ var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSq
222
253
  const variables = {};
223
254
  if (isPx) variables["--gx-avatar-size"] = `${size}px`;
224
255
  if (hex !== void 0) variables["--gx-avatar-hex"] = hex;
256
+ const badgeDiameter = badge === void 0 ? void 0 : getAvatarBadgeDiameter(diameter);
257
+ if (badgeDiameter !== void 0) variables["--gx-avatar-badge-size"] = `${badgeDiameter}px`;
258
+ const notchCss = badge === void 0 ? void 0 : badge.isSquare ? SQUARE_NOTCH_CSS : ROUND_NOTCH_CSS;
225
259
  let content;
226
260
  if (imageSrc !== void 0) content = /* @__PURE__ */ jsx(Image, {
227
261
  ref: imageRef,
@@ -247,7 +281,7 @@ var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSq
247
281
  className: PATTERN_TONE_CSS[tone]
248
282
  }, key))
249
283
  });
250
- const root = /* @__PURE__ */ jsx(Root, {
284
+ const root = /* @__PURE__ */ jsxs(Root, {
251
285
  ...accessibility,
252
286
  ...rest,
253
287
  ref,
@@ -257,10 +291,16 @@ var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSq
257
291
  ...variables,
258
292
  ...style
259
293
  },
260
- children: /* @__PURE__ */ jsx(Surface, {
261
- className: cx(tintCss, isInitials && INITIALS_CSS),
294
+ children: [/* @__PURE__ */ jsx(Surface, {
295
+ className: cx(tintCss, isInitials && INITIALS_CSS, notchCss),
262
296
  children: content
263
- })
297
+ }), badge !== void 0 && badgeDiameter !== void 0 && /* @__PURE__ */ jsx(BadgeSlot, {
298
+ className: cx(badge.isSquare && SQUARE_CSS),
299
+ children: /* @__PURE__ */ jsx(AvatarBadgeImage, {
300
+ badge,
301
+ size: badgeDiameter
302
+ })
303
+ })]
264
304
  });
265
305
  if (!isLoading) return root;
266
306
  return /* @__PURE__ */ jsx(Skeleton, {
@@ -270,5 +310,19 @@ var Avatar = forwardRef(function Avatar({ name, img, seed, size = "MEDIUM", isSq
270
310
  });
271
311
  });
272
312
  Avatar.displayName = "Avatar";
313
+ /**
314
+ * The badge, drawn by the exported `Avatar`: inside `Avatar`'s render function that name is the
315
+ * render function itself, which would receive the wrong ref. Decorative, since the badged avatar
316
+ * already names both, so it keeps one initial: two do not fit at badge size.
317
+ */
318
+ function AvatarBadgeImage({ badge: { name, ...badge }, size }) {
319
+ const initial = Array.from(getInitials(name))[0];
320
+ return /* @__PURE__ */ jsx(Avatar, {
321
+ ...badge,
322
+ ...initial === void 0 ? {} : { name: initial },
323
+ size,
324
+ "aria-hidden": true
325
+ });
326
+ }
273
327
  //#endregion
274
328
  export { AvatarSize, Avatar as default };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The badge's geometry and the badged avatar's accessible name. Kept out of `Avatar.tsx` so the
3
+ * sizing rule and the name rule are tested on their own.
4
+ */
5
+ /**
6
+ * The badge diameter for a main avatar diameter: 36% of it to the nearest px, never below 8px. The
7
+ * 20 / 24 / 32 / 36 / 40px rungs give 8 / 9 / 12 / 13 / 14px.
8
+ */
9
+ export declare function getAvatarBadgeDiameter(diameter: number): number;
10
+ /** The initials drawn beside a badge: the first one alone on avatars of 24px or less. */
11
+ export declare function getAvatarBadgedInitials(initials: string, diameter: number): string;
12
+ /**
13
+ * The accessible name of an avatar and its badge, read as one image: `ariaLabel` when set,
14
+ * otherwise the non-empty names joined by a comma ("Ada Lovelace, Galaxy"), or `undefined` when
15
+ * there is nothing to read (the avatar is then decorative).
16
+ */
17
+ export declare function getAvatarAccessibleName(ariaLabel: string | undefined, name: string | undefined, badgeName: string | undefined): string | undefined;
@@ -0,0 +1,34 @@
1
+ //#region src/avatar/internal/avatarBadge.ts
2
+ /**
3
+ * The badge's geometry and the badged avatar's accessible name. Kept out of `Avatar.tsx` so the
4
+ * sizing rule and the name rule are tested on their own.
5
+ */
6
+ /** Badge diameter as a share of the avatar it sits on. */
7
+ var AVATAR_BADGE_RATIO = .36;
8
+ /** Smallest badge: below this a face or initials stop reading as anything. */
9
+ var AVATAR_BADGE_MIN_PX = 8;
10
+ /**
11
+ * The badge diameter for a main avatar diameter: 36% of it to the nearest px, never below 8px. The
12
+ * 20 / 24 / 32 / 36 / 40px rungs give 8 / 9 / 12 / 13 / 14px.
13
+ */
14
+ function getAvatarBadgeDiameter(diameter) {
15
+ return Math.max(AVATAR_BADGE_MIN_PX, Math.round(diameter * AVATAR_BADGE_RATIO));
16
+ }
17
+ /** Largest diameter whose badged initials keep only the first letter: two do not clear the notch. */
18
+ var AVATAR_BADGE_ONE_INITIAL_MAX_PX = 24;
19
+ /** The initials drawn beside a badge: the first one alone on avatars of 24px or less. */
20
+ function getAvatarBadgedInitials(initials, diameter) {
21
+ return diameter <= AVATAR_BADGE_ONE_INITIAL_MAX_PX ? Array.from(initials)[0] ?? "" : initials;
22
+ }
23
+ /**
24
+ * The accessible name of an avatar and its badge, read as one image: `ariaLabel` when set,
25
+ * otherwise the non-empty names joined by a comma ("Ada Lovelace, Galaxy"), or `undefined` when
26
+ * there is nothing to read (the avatar is then decorative).
27
+ */
28
+ function getAvatarAccessibleName(ariaLabel, name, badgeName) {
29
+ if (ariaLabel !== void 0) return ariaLabel;
30
+ const names = [name, badgeName].filter((value) => value !== void 0 && value.trim().length > 0);
31
+ return names.length > 0 ? names.join(", ") : void 0;
32
+ }
33
+ //#endregion
34
+ export { getAvatarAccessibleName, getAvatarBadgeDiameter, getAvatarBadgedInitials };
@@ -57,7 +57,9 @@ var ToggleInputVariant = /* @__PURE__ */ function(ToggleInputVariant) {
57
57
  /**
58
58
  * The radiogroup: the one frame around the segments (an attached `ButtonGroup`'s silhouette). The
59
59
  * seam color is a custom property so `isGhost` can clear the seams with the frame. `isolation:
60
- * isolate` keeps the focused segment's `z-index: 1` inside the control.
60
+ * isolate` keeps the focused segment's `z-index: 1` inside the control. At content width it never
61
+ * shrinks: a flex row that runs out of room squeezes its siblings, not the segments (which cannot
62
+ * shrink, and would spill past the frame). `fillWidth` makes it shrinkable and truncates labels.
61
63
  */
62
64
  var Track = /*#__PURE__*/ styled("div")({
63
65
  name: "Track",
@@ -69,7 +71,9 @@ var Track = /*#__PURE__*/ styled("div")({
69
71
  * frame's full inner height. Every segment after the first draws the seam before it as its own
70
72
  * leading border, so each seam is one hairline. Only the first and last segments round their outer
71
73
  * corners, by the frame's radius less its border, so no fill pokes past the rounded frame. The
72
- * focused segment is lifted so its focus ring is never painted over by the next segment.
74
+ * focused segment is lifted so its focus ring is never painted over by the next segment. Its
75
+ * content is clipped sideways to its own box (the label truncates first, the icon never shrinks),
76
+ * so a squeezed segment never paints into its neighbor.
73
77
  */
74
78
  var Segment = /*#__PURE__*/ styled("button")({
75
79
  name: "Segment",
@@ -101,7 +105,10 @@ var TOGGLE_INPUT_SIZE_TO_CSS_MAP = {
101
105
  var ERROR_CSS = "gx-ToggleInput-ERROR_CSS-e6je9tz";
102
106
  /** Group disabled: the selected segment steps down to the pressed fill; the frame keeps its resting hairline (field-frame note). */
103
107
  var DISABLED_CSS = "gx-ToggleInput-DISABLED_CSS-d8pd3s2";
104
- /** Equal shares: the track fills its parent and every segment (icon-only too) takes `1 1 0`. */
108
+ /**
109
+ * Equal shares: the track fills its parent, may shrink with it, and every segment (icon-only too)
110
+ * takes `1 1 0`; a label that no longer fits truncates.
111
+ */
105
112
  var FILL_WIDTH_CSS = "gx-ToggleInput-FILL_WIDTH_CSS-fc2kmgg";
106
113
  /** The selected segment: the selection fill (the chosen-item role) and primary text; it fades in with the segment's color transition. */
107
114
  var SEGMENT_SELECTED_CSS = "gx-ToggleInput-SEGMENT_SELECTED_CSS-s1d7otlt";
@@ -247,6 +254,7 @@ function TrackBody({ trackProps, forwardedRef, isRoot }) {
247
254
  weight: TextWeight.MEDIUM,
248
255
  variant: TextVariant.INHERIT,
249
256
  lineClamp: 1,
257
+ shouldTooltipOnOverflow: option.tooltip === void 0,
250
258
  children: option.label
251
259
  })]
252
260
  }, option.id);