@openproject/primer-view-components 0.80.0 → 0.80.1

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.
@@ -1,15 +1,28 @@
1
+ /**
2
+ * AvatarFallbackElement implements "fallback first" loading pattern:
3
+ * 1. Fallback SVG is rendered immediately as <img> src
4
+ * 2. Real avatar URL is test-loaded in background using new Image()
5
+ * 3. On success, swaps to real image; on failure, fallback stays visible
6
+ *
7
+ * This approach prevents flicker by never showing a broken image state.
8
+ * Inspired by OpenProject's Angular PrincipalRendererService.
9
+ *
10
+ * Note: We read attributes directly via getAttribute() instead of using @attr
11
+ * due to a Catalyst bug where @attr accessors aren't properly initialized
12
+ * when elements have pre-existing attribute values.
13
+ */
1
14
  export declare class AvatarFallbackElement extends HTMLElement {
2
- uniqueId: string;
3
- altText: string;
4
- fallbackSrc: string;
5
15
  private img;
6
- private boundErrorHandler?;
16
+ private testImage;
7
17
  connectedCallback(): void;
8
18
  disconnectedCallback(): void;
9
- private isImageBroken;
10
- private handleImageError;
19
+ /**
20
+ * Test-loads the real avatar URL in background.
21
+ * On success, swaps the visible img to the real URL.
22
+ * On failure, does nothing - fallback stays visible.
23
+ */
24
+ private testLoadImage;
11
25
  private applyColor;
12
26
  private valueHash;
13
27
  private updateSvgColor;
14
- private isFallbackImage;
15
28
  }
@@ -4,57 +4,70 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- import { attr, controller } from '@github/catalyst';
7
+ import { controller } from '@github/catalyst';
8
+ /**
9
+ * AvatarFallbackElement implements "fallback first" loading pattern:
10
+ * 1. Fallback SVG is rendered immediately as <img> src
11
+ * 2. Real avatar URL is test-loaded in background using new Image()
12
+ * 3. On success, swaps to real image; on failure, fallback stays visible
13
+ *
14
+ * This approach prevents flicker by never showing a broken image state.
15
+ * Inspired by OpenProject's Angular PrincipalRendererService.
16
+ *
17
+ * Note: We read attributes directly via getAttribute() instead of using @attr
18
+ * due to a Catalyst bug where @attr accessors aren't properly initialized
19
+ * when elements have pre-existing attribute values.
20
+ */
8
21
  let AvatarFallbackElement = class AvatarFallbackElement extends HTMLElement {
9
22
  constructor() {
10
23
  super(...arguments);
11
- this.uniqueId = '';
12
- this.altText = '';
13
- this.fallbackSrc = '';
14
24
  this.img = null;
25
+ this.testImage = null;
15
26
  }
16
27
  connectedCallback() {
17
28
  this.img = this.querySelector('img') ?? null;
18
29
  if (!this.img)
19
30
  return;
20
- this.boundErrorHandler = () => this.handleImageError(this.img);
21
- // Handle image load errors (404, network failure, etc.)
22
- this.img.addEventListener('error', this.boundErrorHandler);
23
- // Check if image already failed (error event fired before listener attached)
24
- if (this.isImageBroken(this.img)) {
25
- this.handleImageError(this.img);
26
- }
27
- else if (this.isFallbackImage(this.img)) {
28
- this.applyColor(this.img);
31
+ const uniqueId = this.getAttribute('data-unique-id') || '';
32
+ const altText = this.getAttribute('data-alt-text') || '';
33
+ const avatarSrc = this.getAttribute('data-avatar-src') || '';
34
+ // Apply hashed color to fallback SVG immediately
35
+ this.applyColor(this.img, uniqueId, altText);
36
+ // Test-load real avatar URL in background
37
+ if (avatarSrc) {
38
+ this.testLoadImage(avatarSrc);
29
39
  }
30
40
  }
31
41
  disconnectedCallback() {
32
- if (this.boundErrorHandler && this.img) {
33
- this.img.removeEventListener('error', this.boundErrorHandler);
42
+ // Clean up test image and its event handler to prevent memory leaks
43
+ if (this.testImage) {
44
+ this.testImage.onload = null;
45
+ this.testImage = null;
34
46
  }
35
- this.boundErrorHandler = undefined;
36
47
  this.img = null;
37
48
  }
38
- isImageBroken(img) {
39
- // Image is broken if loading completed but no actual image data loaded
40
- // Skip check for data URIs (fallback SVGs) as they're always valid
41
- return img.complete && img.naturalWidth === 0 && !img.src.startsWith('data:');
42
- }
43
- handleImageError(img) {
44
- // Prevent infinite loop if fallback also fails
45
- if (this.isFallbackImage(img))
46
- return;
47
- if (this.fallbackSrc) {
48
- img.src = this.fallbackSrc;
49
- this.applyColor(img);
50
- }
49
+ /**
50
+ * Test-loads the real avatar URL in background.
51
+ * On success, swaps the visible img to the real URL.
52
+ * On failure, does nothing - fallback stays visible.
53
+ */
54
+ testLoadImage(url) {
55
+ this.testImage = new Image();
56
+ this.testImage.onload = () => {
57
+ // Success - swap to real image
58
+ if (this.img) {
59
+ this.img.src = url;
60
+ }
61
+ };
62
+ // On error: do nothing, fallback stays visible (no flicker)
63
+ this.testImage.src = url;
51
64
  }
52
- applyColor(img) {
65
+ applyColor(img, uniqueId, altText) {
53
66
  // If either uniqueId or altText is missing, skip color customization so the SVG
54
67
  // keeps its default gray fill defined in the source and no color override is applied.
55
- if (!this.uniqueId || !this.altText)
68
+ if (!uniqueId || !altText)
56
69
  return;
57
- const text = `${this.uniqueId}${this.altText}`;
70
+ const text = `${uniqueId}${altText}`;
58
71
  const hue = this.valueHash(text);
59
72
  const color = `hsl(${hue}, 50%, 30%)`;
60
73
  this.updateSvgColor(img, color);
@@ -72,6 +85,8 @@ let AvatarFallbackElement = class AvatarFallbackElement extends HTMLElement {
72
85
  }
73
86
  updateSvgColor(img, color) {
74
87
  const dataUri = img.src;
88
+ if (!dataUri.startsWith('data:image/svg+xml;base64,'))
89
+ return;
75
90
  const base64 = dataUri.replace('data:image/svg+xml;base64,', '');
76
91
  try {
77
92
  const svg = atob(base64);
@@ -83,19 +98,7 @@ let AvatarFallbackElement = class AvatarFallbackElement extends HTMLElement {
83
98
  // to avoid breaking the component.
84
99
  }
85
100
  }
86
- isFallbackImage(img) {
87
- return img.src === this.fallbackSrc;
88
- }
89
101
  };
90
- __decorate([
91
- attr
92
- ], AvatarFallbackElement.prototype, "uniqueId", void 0);
93
- __decorate([
94
- attr
95
- ], AvatarFallbackElement.prototype, "altText", void 0);
96
- __decorate([
97
- attr
98
- ], AvatarFallbackElement.prototype, "fallbackSrc", void 0);
99
102
  AvatarFallbackElement = __decorate([
100
103
  controller
101
104
  ], AvatarFallbackElement);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openproject/primer-view-components",
3
- "version": "0.80.0",
3
+ "version": "0.80.1",
4
4
  "description": "ViewComponents of the Primer Design System for OpenProject",
5
5
  "main": "app/assets/javascripts/primer_view_components.js",
6
6
  "module": "app/components/primer/primer.js",
@@ -5775,13 +5775,13 @@
5775
5775
  "name": "src",
5776
5776
  "type": "String",
5777
5777
  "default": "`nil`",
5778
- "description": "The source url of the avatar image. When nil or a broken URL, it renders a fallback with initials."
5778
+ "description": "The source URL of the avatar image. When provided, JavaScript will test-load it in the background and swap to it on success. When nil or blank, only the fallback SVG is displayed."
5779
5779
  },
5780
5780
  {
5781
5781
  "name": "alt",
5782
5782
  "type": "String",
5783
5783
  "default": "`nil`",
5784
- "description": "Alt text for the avatar. Used for accessibility and to generate initials when src is nil."
5784
+ "description": "Alt text for the avatar. Used for accessibility and to generate initials for the fallback SVG."
5785
5785
  },
5786
5786
  {
5787
5787
  "name": "size",
@@ -18866,7 +18866,7 @@
18866
18866
  },
18867
18867
  {
18868
18868
  "fully_qualified_name": "Primer::OpenProject::AvatarWithFallback",
18869
- "description": "OpenProject-specific Avatar component that extends Primer::Beta::Avatar\nto support fallback rendering with initials when no image source is provided.\n\nWhen `src` is nil, this component renders an SVG with initials extracted from\nthe alt text. The AvatarFallbackElement web component then enhances it client-side\nby applying a consistent background color based on the user's unique_id (using the\nsame hash function as OP Core for consistency).\n\nThis component follows the \"extension over mutation\" pattern - it extends\nPrimer::Beta::Avatar without modifying its interface, ensuring compatibility\nwith upstream changes.",
18869
+ "description": "OpenProject-specific Avatar component that extends Primer::Beta::Avatar\nto support fallback rendering with initials when no image source is provided.\n\nUses a \"fallback first\" pattern for flicker-free loading:\n1. Always renders fallback SVG as initial <img> src (visible immediately)\n2. Client-side JS test-loads the real URL in background\n3. On success, swaps to real image; on failure, fallback stays visible\n\nThis approach is inspired by OpenProject's Angular PrincipalRendererService.",
18870
18870
  "accessibility_docs": null,
18871
18871
  "is_form_component": false,
18872
18872
  "is_published": true,
@@ -18882,13 +18882,13 @@
18882
18882
  "name": "src",
18883
18883
  "type": "String",
18884
18884
  "default": "`nil`",
18885
- "description": "The source url of the avatar image. When nil or a broken URL, it renders a fallback with initials."
18885
+ "description": "The source URL of the avatar image. When provided, JavaScript will test-load it in the background and swap to it on success. When nil or blank, only the fallback SVG is displayed."
18886
18886
  },
18887
18887
  {
18888
18888
  "name": "alt",
18889
18889
  "type": "String",
18890
18890
  "default": "`nil`",
18891
- "description": "Alt text for the avatar. Used for accessibility and to generate initials when src is nil."
18891
+ "description": "Alt text for the avatar. Used for accessibility and to generate initials for the fallback SVG."
18892
18892
  },
18893
18893
  {
18894
18894
  "name": "size",