@justeattakeaway/pie-avatar 0.3.0 → 0.4.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.
@@ -70,6 +70,14 @@
70
70
  },
71
71
  "privacy": "public"
72
72
  },
73
+ {
74
+ "kind": "field",
75
+ "name": "src",
76
+ "type": {
77
+ "text": "AvatarProps['src']"
78
+ },
79
+ "privacy": "public"
80
+ },
73
81
  {
74
82
  "kind": "method",
75
83
  "name": "getInitials",
@@ -117,7 +125,26 @@
117
125
  "text": "TemplateResult"
118
126
  }
119
127
  },
120
- "description": "Renders the icon (placeholder span for now)."
128
+ "description": "Renders the user icon."
129
+ },
130
+ {
131
+ "kind": "method",
132
+ "name": "renderImage",
133
+ "privacy": "private",
134
+ "return": {
135
+ "type": {
136
+ "text": "TemplateResult"
137
+ }
138
+ },
139
+ "parameters": [
140
+ {
141
+ "name": "imgSrc",
142
+ "type": {
143
+ "text": "string"
144
+ }
145
+ }
146
+ ],
147
+ "description": "Renders an image.\nWe assign an empty string to the alt attribute for a11y clarity as it explicitly declares the image as decorative"
121
148
  },
122
149
  {
123
150
  "kind": "field",
package/dist/index.d.ts CHANGED
@@ -14,9 +14,13 @@ export declare interface AvatarProps {
14
14
  * What HTML element the avatar should be such as button, a or div.
15
15
  */
16
16
  tag: typeof tags[number];
17
+ /**
18
+ * The src attribute for the avatar image. When provided, displays an image instead of initials.
19
+ */
20
+ src?: string;
17
21
  }
18
22
 
19
- export declare type DefaultProps = ComponentDefaultProps<AvatarProps, keyof Omit<AvatarProps, 'label'>>;
23
+ export declare type DefaultProps = ComponentDefaultProps<AvatarProps, keyof Omit<AvatarProps, 'label' | 'src'>>;
20
24
 
21
25
  export declare const defaultProps: DefaultProps;
22
26
 
@@ -31,6 +35,7 @@ export declare type Initials = {
31
35
  export declare class PieAvatar extends PieAvatar_base implements AvatarProps {
32
36
  tag: "a" | "button" | "div";
33
37
  label: AvatarProps['label'];
38
+ src: AvatarProps['src'];
34
39
  /**
35
40
  * Attempts to extract initials from the label string.
36
41
  * If the label is not provided or is invalid, it returns null.
@@ -45,11 +50,18 @@ export declare class PieAvatar extends PieAvatar_base implements AvatarProps {
45
50
  */
46
51
  private renderInitials;
47
52
  /**
48
- * Renders the icon (placeholder span for now).
53
+ * Renders the user icon.
49
54
  *
50
55
  * @private
51
56
  */
52
57
  private renderIcon;
58
+ /**
59
+ * Renders an image.
60
+ * We assign an empty string to the alt attribute for a11y clarity as it explicitly declares the image as decorative
61
+ *
62
+ * @private
63
+ */
64
+ private renderImage;
53
65
  /**
54
66
  * Renders the inner content of the avatar such as initials, an icon or an image.
55
67
  * It is a getter because the value is computed based on properties
package/dist/index.js CHANGED
@@ -1,26 +1,26 @@
1
- import { LitElement as h, html as n, unsafeCSS as f } from "lit";
2
- import { RtlMixin as g, validPropertyValues as b, safeCustomElement as y } from "@justeattakeaway/pie-webc-core";
1
+ import { LitElement as h, html as s, unsafeCSS as g } from "lit";
2
+ import { RtlMixin as f, validPropertyValues as b, safeCustomElement as m } from "@justeattakeaway/pie-webc-core";
3
3
  import "@justeattakeaway/pie-icons-webc/dist/IconUser.js";
4
- import { property as u } from "lit/decorators.js";
4
+ import { property as v } from "lit/decorators.js";
5
5
  const o = class o extends h {
6
6
  willUpdate() {
7
7
  this.getAttribute("v") || this.setAttribute("v", o.v);
8
8
  }
9
9
  };
10
- o.v = "0.3.0";
11
- let d = o;
12
- const m = "*,*:after,*:before{box-sizing:inherit}:host{display:block}.c-avatar-visuallyHidden{position:absolute;display:block;height:1px;width:1px;overflow:hidden;padding:1px;white-space:nowrap}.c-avatar-content{display:flex;justify-content:center;align-items:center;font-size:calc(var(--dt-font-body-s-size) * 1px);background-color:var(--dt-color-container-inverse);color:var(--dt-color-content-interactive-primary);width:32px;height:32px;border-radius:var(--dt-radius-rounded-e)}", x = ["a", "button", "div"], v = {
10
+ o.v = "0.4.1";
11
+ let p = o;
12
+ const y = "*,*:after,*:before{box-sizing:inherit}:host{display:block;--avatar-size: 32px}.c-avatar-visuallyHidden{position:absolute;display:block;height:1px;width:1px;overflow:hidden;padding:1px;white-space:nowrap}.c-avatar{display:flex;justify-content:center;align-items:center;font-size:calc(var(--dt-font-body-s-size) * 1px);background-color:var(--dt-color-container-inverse);color:var(--dt-color-content-interactive-primary);width:var(--avatar-size);height:var(--avatar-size);border-radius:var(--dt-radius-rounded-e);overflow:hidden}.c-avatar--image{width:100%;height:100%;object-fit:cover}.c-avatar--button{border:none;font-family:inherit}", x = ["a", "button", "div"], u = {
13
13
  tag: "div"
14
14
  };
15
- var w = Object.defineProperty, S = Object.getOwnPropertyDescriptor, c = (t, e, r, i) => {
16
- for (var a = i > 1 ? void 0 : i ? S(e, r) : e, l = t.length - 1, p; l >= 0; l--)
17
- (p = t[l]) && (a = (i ? p(e, r, a) : p(a)) || a);
18
- return i && a && w(e, r, a), a;
15
+ var w = Object.defineProperty, I = Object.getOwnPropertyDescriptor, l = (t, a, r, i) => {
16
+ for (var e = i > 1 ? void 0 : i ? I(a, r) : a, d = t.length - 1, c; d >= 0; d--)
17
+ (c = t[d]) && (e = (i ? c(a, r, e) : c(e)) || e);
18
+ return i && e && w(a, r, e), e;
19
19
  };
20
- const C = "pie-avatar";
21
- let s = class extends g(d) {
20
+ const S = "pie-avatar";
21
+ let n = class extends f(p) {
22
22
  constructor() {
23
- super(...arguments), this.tag = v.tag;
23
+ super(...arguments), this.tag = u.tag;
24
24
  }
25
25
  /**
26
26
  * Attempts to extract initials from the label string.
@@ -48,18 +48,27 @@ let s = class extends g(d) {
48
48
  * @private
49
49
  */
50
50
  renderInitials(t) {
51
- return n`
51
+ return s`
52
52
  <span aria-hidden="true" data-test-id="pie-avatar-initials-visual">${t.visual}</span>
53
53
  <span class="c-avatar-visuallyHidden" data-test-id="pie-avatar-initials-screenreader">${t.screenreader}</span>
54
54
  `;
55
55
  }
56
56
  /**
57
- * Renders the icon (placeholder span for now).
57
+ * Renders the user icon.
58
58
  *
59
59
  * @private
60
60
  */
61
61
  renderIcon() {
62
- return n`<icon-user size="s" aria-hidden="true" data-test-id="pie-avatar-icon"></icon-user>`;
62
+ return s`<icon-user size="s" aria-hidden="true" data-test-id="pie-avatar-icon"></icon-user>`;
63
+ }
64
+ /**
65
+ * Renders an image.
66
+ * We assign an empty string to the alt attribute for a11y clarity as it explicitly declares the image as decorative
67
+ *
68
+ * @private
69
+ */
70
+ renderImage(t) {
71
+ return s`<img class="c-avatar--image" src="${t}" data-test-id="pie-avatar-image" alt=""/>`;
63
72
  }
64
73
  /**
65
74
  * Renders the inner content of the avatar such as initials, an icon or an image.
@@ -68,6 +77,8 @@ let s = class extends g(d) {
68
77
  * @private
69
78
  */
70
79
  get avatarContent() {
80
+ if (this.src)
81
+ return this.renderImage(this.src);
71
82
  if (this.label) {
72
83
  const t = this.getInitials(this.label);
73
84
  if (t)
@@ -82,26 +93,29 @@ let s = class extends g(d) {
82
93
  * @private
83
94
  */
84
95
  renderAvatarWrapper(t) {
85
- const { tag: e } = this;
86
- return e === "button" ? n`<button data-test-id="pie-avatar-button">${t}</button>` : e === "a" ? n`<a data-test-id="pie-avatar-anchor">${t}</a>` : n`<div class="c-avatar-content" data-test-id="pie-avatar-div">${t}</div>`;
96
+ const { tag: a } = this;
97
+ return a === "button" ? s`<button data-test-id="pie-avatar-button" class="c-avatar c-avatar--button">${t}</button>` : a === "a" ? s`<a data-test-id="pie-avatar-anchor" class="c-avatar">${t}</a>` : s`<div class="c-avatar" data-test-id="pie-avatar-div" ?aria-hidden="${this.src}">${t}</div>`;
87
98
  }
88
99
  render() {
89
100
  return this.renderAvatarWrapper(this.avatarContent);
90
101
  }
91
102
  };
92
- s.styles = f(m);
93
- c([
94
- u({ type: String }),
95
- b(C, x, v.tag)
96
- ], s.prototype, "tag", 2);
97
- c([
98
- u({ type: String })
99
- ], s.prototype, "label", 2);
100
- s = c([
101
- y("pie-avatar")
102
- ], s);
103
+ n.styles = g(y);
104
+ l([
105
+ v({ type: String }),
106
+ b(S, x, u.tag)
107
+ ], n.prototype, "tag", 2);
108
+ l([
109
+ v({ type: String })
110
+ ], n.prototype, "label", 2);
111
+ l([
112
+ v({ type: String })
113
+ ], n.prototype, "src", 2);
114
+ n = l([
115
+ m("pie-avatar")
116
+ ], n);
103
117
  export {
104
- s as PieAvatar,
105
- v as defaultProps,
118
+ n as PieAvatar,
119
+ u as defaultProps,
106
120
  x as tags
107
121
  };
package/dist/react.d.ts CHANGED
@@ -15,9 +15,13 @@ export declare interface AvatarProps {
15
15
  * What HTML element the avatar should be such as button, a or div.
16
16
  */
17
17
  tag: typeof tags[number];
18
+ /**
19
+ * The src attribute for the avatar image. When provided, displays an image instead of initials.
20
+ */
21
+ src?: string;
18
22
  }
19
23
 
20
- export declare type DefaultProps = ComponentDefaultProps<AvatarProps, keyof Omit<AvatarProps, 'label'>>;
24
+ export declare type DefaultProps = ComponentDefaultProps<AvatarProps, keyof Omit<AvatarProps, 'label' | 'src'>>;
21
25
 
22
26
  export declare const defaultProps: DefaultProps;
23
27
 
@@ -34,6 +38,7 @@ export declare const PieAvatar: React_2.ForwardRefExoticComponent<React_2.PropsW
34
38
  declare class PieAvatar_2 extends PieAvatar_base implements AvatarProps {
35
39
  tag: "a" | "button" | "div";
36
40
  label: AvatarProps['label'];
41
+ src: AvatarProps['src'];
37
42
  /**
38
43
  * Attempts to extract initials from the label string.
39
44
  * If the label is not provided or is invalid, it returns null.
@@ -48,11 +53,18 @@ declare class PieAvatar_2 extends PieAvatar_base implements AvatarProps {
48
53
  */
49
54
  private renderInitials;
50
55
  /**
51
- * Renders the icon (placeholder span for now).
56
+ * Renders the user icon.
52
57
  *
53
58
  * @private
54
59
  */
55
60
  private renderIcon;
61
+ /**
62
+ * Renders an image.
63
+ * We assign an empty string to the alt attribute for a11y clarity as it explicitly declares the image as decorative
64
+ *
65
+ * @private
66
+ */
67
+ private renderImage;
56
68
  /**
57
69
  * Renders the inner content of the avatar such as initials, an icon or an image.
58
70
  * It is a getter because the value is computed based on properties
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@justeattakeaway/pie-avatar",
3
3
  "description": "PIE Design System Avatar built using Web Components",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
@@ -34,13 +34,13 @@
34
34
  "devDependencies": {
35
35
  "@custom-elements-manifest/analyzer": "0.9.0",
36
36
  "@justeattakeaway/pie-components-config": "0.21.0",
37
- "@justeattakeaway/pie-css": "0.19.0",
37
+ "@justeattakeaway/pie-css": "0.22.0",
38
38
  "@justeattakeaway/pie-monorepo-utils": "0.7.0",
39
39
  "cem-plugin-module-file-extensions": "0.0.5"
40
40
  },
41
41
  "dependencies": {
42
- "@justeattakeaway/pie-icons-webc": "1.14.2",
43
- "@justeattakeaway/pie-webc-core": "1.1.0"
42
+ "@justeattakeaway/pie-icons-webc": "1.15.0",
43
+ "@justeattakeaway/pie-webc-core": "2.0.0"
44
44
  },
45
45
  "volta": {
46
46
  "extends": "../../../package.json"
package/src/avatar.scss CHANGED
@@ -4,21 +4,34 @@
4
4
  // Note: For consistency sake, the recommended display should be either
5
5
  // "block" or "inline-block", otherwise it can be ommited for floating elements
6
6
  display: block;
7
+
8
+ --avatar-size: 32px;
7
9
  }
8
10
 
9
11
  .c-avatar-visuallyHidden {
10
12
  @include p.visually-hidden;
11
13
  }
12
14
 
13
- .c-avatar-content {
15
+ .c-avatar {
14
16
  display: flex;
15
17
  justify-content: center;
16
18
  align-items: center;
17
19
  @include p.font-size(--dt-font-body-s-size);
18
20
  background-color: var(--dt-color-container-inverse);
19
21
  color: var(--dt-color-content-interactive-primary);
20
- width: 32px;
21
- height: 32px;
22
+ width: var(--avatar-size);
23
+ height: var(--avatar-size);
22
24
  border-radius:var(--dt-radius-rounded-e);
25
+ overflow: hidden;
26
+ }
23
27
 
28
+ .c-avatar--image {
29
+ width: 100%;
30
+ height: 100%;
31
+ object-fit: cover;
24
32
  }
33
+
34
+ .c-avatar--button {
35
+ border: none;
36
+ font-family: inherit;
37
+ }
package/src/defs.ts CHANGED
@@ -7,14 +7,20 @@ export interface AvatarProps {
7
7
  * Label for the username that will be turned into initials inside the avatar, if provided.
8
8
  */
9
9
  label?: string;
10
+
10
11
  /**
11
12
  * What HTML element the avatar should be such as button, a or div.
12
13
  */
13
14
  tag: typeof tags[number];
14
15
 
16
+ /**
17
+ * The src attribute for the avatar image. When provided, displays an image instead of initials.
18
+ */
19
+ src?: string;
20
+
15
21
  }
16
22
 
17
- export type DefaultProps = ComponentDefaultProps<AvatarProps, keyof Omit<AvatarProps, 'label'>>;
23
+ export type DefaultProps = ComponentDefaultProps<AvatarProps, keyof Omit<AvatarProps, 'label' | 'src'>>;
18
24
  export const defaultProps: DefaultProps = {
19
25
  tag: 'div',
20
26
  };
package/src/index.ts CHANGED
@@ -29,6 +29,9 @@ export class PieAvatar extends RtlMixin(PieElement) implements AvatarProps {
29
29
  @property({ type: String })
30
30
  public label: AvatarProps['label'];
31
31
 
32
+ @property({ type: String })
33
+ public src: AvatarProps['src'];
34
+
32
35
  /**
33
36
  * Attempts to extract initials from the label string.
34
37
  * If the label is not provided or is invalid, it returns null.
@@ -70,7 +73,7 @@ export class PieAvatar extends RtlMixin(PieElement) implements AvatarProps {
70
73
  }
71
74
 
72
75
  /**
73
- * Renders the icon (placeholder span for now).
76
+ * Renders the user icon.
74
77
  *
75
78
  * @private
76
79
  */
@@ -78,6 +81,16 @@ export class PieAvatar extends RtlMixin(PieElement) implements AvatarProps {
78
81
  return html`<icon-user size="s" aria-hidden="true" data-test-id="pie-avatar-icon"></icon-user>`;
79
82
  }
80
83
 
84
+ /**
85
+ * Renders an image.
86
+ * We assign an empty string to the alt attribute for a11y clarity as it explicitly declares the image as decorative
87
+ *
88
+ * @private
89
+ */
90
+ private renderImage (imgSrc: string): TemplateResult {
91
+ return html`<img class="c-avatar--image" src="${imgSrc}" data-test-id="pie-avatar-image" alt=""/>`;
92
+ }
93
+
81
94
  /**
82
95
  * Renders the inner content of the avatar such as initials, an icon or an image.
83
96
  * It is a getter because the value is computed based on properties
@@ -85,7 +98,9 @@ export class PieAvatar extends RtlMixin(PieElement) implements AvatarProps {
85
98
  * @private
86
99
  */
87
100
  private get avatarContent (): TemplateResult {
88
- // TODO: handle unauthenticated and src here
101
+ if (this.src) {
102
+ return this.renderImage(this.src);
103
+ }
89
104
 
90
105
  if (this.label) {
91
106
  const initials = this.getInitials(this.label);
@@ -107,14 +122,14 @@ export class PieAvatar extends RtlMixin(PieElement) implements AvatarProps {
107
122
  const { tag } = this;
108
123
 
109
124
  if (tag === 'button') {
110
- return html`<button data-test-id="pie-avatar-button">${content}</button>`;
125
+ return html`<button data-test-id="pie-avatar-button" class="c-avatar c-avatar--button">${content}</button>`;
111
126
  }
112
127
 
113
128
  if (tag === 'a') {
114
- return html`<a data-test-id="pie-avatar-anchor">${content}</a>`;
129
+ return html`<a data-test-id="pie-avatar-anchor" class="c-avatar">${content}</a>`;
115
130
  }
116
131
 
117
- return html`<div class="c-avatar-content" data-test-id="pie-avatar-div">${content}</div>`;
132
+ return html`<div class="c-avatar" data-test-id="pie-avatar-div" ?aria-hidden="${this.src}">${content}</div>`;
118
133
  }
119
134
 
120
135
  render () {