@justeattakeaway/pie-thumbnail 0.3.0 → 0.5.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.
@@ -36,13 +36,26 @@
36
36
  },
37
37
  "default": "{\n default: 'c-thumbnail--backgroundDefault',\n subtle: 'c-thumbnail--backgroundSubtle',\n strong: 'c-thumbnail--backgroundStrong',\n dark: 'c-thumbnail--backgroundDark',\n inverse: 'c-thumbnail--backgroundInverse',\n 'inverse-alternative': 'c-thumbnail--backgroundInverseAlternative',\n}"
38
38
  },
39
+ {
40
+ "kind": "variable",
41
+ "name": "aspectRatios",
42
+ "type": {
43
+ "text": "['1by1', '4by3', '16by9']"
44
+ },
45
+ "default": "['1by1', '4by3', '16by9']"
46
+ },
47
+ {
48
+ "kind": "variable",
49
+ "name": "sizes",
50
+ "default": "Object.freeze(Array.from(\n { length: (SIZE_MAX - SIZE_MIN) / SIZE_INCREMENT_BY + 1 },\n (_, i) => SIZE_MIN + i * SIZE_INCREMENT_BY,\n))"
51
+ },
39
52
  {
40
53
  "kind": "variable",
41
54
  "name": "defaultProps",
42
55
  "type": {
43
56
  "text": "DefaultProps"
44
57
  },
45
- "default": "{\n variant: 'default',\n src: '',\n alt: '',\n disabled: false,\n hasPadding: false,\n backgroundColor: 'default',\n placeholder: {\n src: '',\n alt: '',\n },\n}"
58
+ "default": "{\n variant: 'default',\n size: 48,\n src: '',\n alt: '',\n disabled: false,\n hasPadding: false,\n backgroundColor: 'default',\n aspectRatio: '1by1',\n placeholder: {\n src: '',\n alt: '',\n },\n}"
46
59
  }
47
60
  ],
48
61
  "exports": [
@@ -70,6 +83,22 @@
70
83
  "module": "src/defs.js"
71
84
  }
72
85
  },
86
+ {
87
+ "kind": "js",
88
+ "name": "aspectRatios",
89
+ "declaration": {
90
+ "name": "aspectRatios",
91
+ "module": "src/defs.js"
92
+ }
93
+ },
94
+ {
95
+ "kind": "js",
96
+ "name": "sizes",
97
+ "declaration": {
98
+ "name": "sizes",
99
+ "module": "src/defs.js"
100
+ }
101
+ },
73
102
  {
74
103
  "kind": "js",
75
104
  "name": "defaultProps",
@@ -95,6 +124,18 @@
95
124
  "privacy": "public",
96
125
  "attribute": "variant"
97
126
  },
127
+ {
128
+ "kind": "field",
129
+ "name": "size",
130
+ "privacy": "public",
131
+ "attribute": "size"
132
+ },
133
+ {
134
+ "kind": "field",
135
+ "name": "aspectRatio",
136
+ "privacy": "public",
137
+ "attribute": "aspectRatio"
138
+ },
98
139
  {
99
140
  "kind": "field",
100
141
  "name": "src",
@@ -142,6 +183,17 @@
142
183
  },
143
184
  "privacy": "private"
144
185
  },
186
+ {
187
+ "kind": "method",
188
+ "name": "_generateSizeStyles",
189
+ "privacy": "private",
190
+ "return": {
191
+ "type": {
192
+ "text": "string"
193
+ }
194
+ },
195
+ "description": "Assigns the thumbnail size and border radius CSS variables\nbased on the size prop."
196
+ },
145
197
  {
146
198
  "kind": "method",
147
199
  "name": "_handleImageError",
@@ -160,6 +212,14 @@
160
212
  "name": "variant",
161
213
  "fieldName": "variant"
162
214
  },
215
+ {
216
+ "name": "size",
217
+ "fieldName": "size"
218
+ },
219
+ {
220
+ "name": "aspectRatio",
221
+ "fieldName": "aspectRatio"
222
+ },
163
223
  {
164
224
  "name": "src",
165
225
  "fieldName": "src"
package/dist/index.d.ts CHANGED
@@ -3,11 +3,13 @@ import type { CSSResult } from 'lit';
3
3
  import type { LitElement } from 'lit';
4
4
  import type { TemplateResult } from 'lit-html';
5
5
 
6
+ export declare const aspectRatios: readonly ["1by1", "4by3", "16by9"];
7
+
6
8
  export declare const backgroundColorClassNames: Record<typeof backgroundColors[number], string>;
7
9
 
8
10
  export declare const backgroundColors: readonly ["default", "subtle", "strong", "dark", "inverse", "inverse-alternative"];
9
11
 
10
- export declare type DefaultProps = ComponentDefaultProps<ThumbnailProps, 'variant' | 'src' | 'alt' | 'disabled' | 'hasPadding' | 'backgroundColor' | 'placeholder'>;
12
+ export declare type DefaultProps = ComponentDefaultProps<ThumbnailProps>;
11
13
 
12
14
  export declare const defaultProps: DefaultProps;
13
15
 
@@ -16,6 +18,8 @@ export declare const defaultProps: DefaultProps;
16
18
  */
17
19
  export declare class PieThumbnail extends LitElement implements ThumbnailProps {
18
20
  variant: "default" | "outline";
21
+ size: number;
22
+ aspectRatio: "1by1" | "4by3" | "16by9";
19
23
  src: string;
20
24
  alt: string;
21
25
  disabled: boolean;
@@ -23,6 +27,11 @@ export declare class PieThumbnail extends LitElement implements ThumbnailProps {
23
27
  backgroundColor: "default" | "subtle" | "strong" | "dark" | "inverse" | "inverse-alternative";
24
28
  placeholder: ThumbnailProps['placeholder'];
25
29
  private img;
30
+ /**
31
+ * Assigns the thumbnail size and border radius CSS variables
32
+ * based on the size prop.
33
+ */
34
+ private _generateSizeStyles;
26
35
  /**
27
36
  * Handles image load errors by replacing the src and alt props
28
37
  * with the placeholder props.
@@ -43,14 +52,45 @@ declare type PlaceholderProps = {
43
52
  alt?: string;
44
53
  };
45
54
 
55
+ export declare const sizes: readonly number[];
56
+
46
57
  export declare interface ThumbnailProps {
58
+ /**
59
+ * What style variant the thumbnail should be such as default or outline.
60
+ */
47
61
  variant?: typeof variants[number];
62
+ /**
63
+ * Specify the size as a number between 24 and 128 (inclusive) and a multiple of 8.
64
+ */
65
+ size?: typeof sizes[number];
66
+ /**
67
+ * The src attribute for the underlying image tag.
68
+ */
48
69
  src?: string;
70
+ /**
71
+ * The alt attribute for the underlying image tag.
72
+ */
49
73
  alt?: string;
74
+ /**
75
+ * When true, the disabled styles are applied.
76
+ */
50
77
  disabled?: boolean;
78
+ /**
79
+ * When true, an extra spacing around the thumbnail container is applied.
80
+ */
51
81
  hasPadding?: boolean;
82
+ /**
83
+ * What background color the thumbnail should be.
84
+ */
52
85
  backgroundColor?: typeof backgroundColors[number];
86
+ /**
87
+ * What placeholder should be used when the image fails to load.
88
+ */
53
89
  placeholder?: PlaceholderProps;
90
+ /**
91
+ * Sets the aspect-ratio of the thumbnail image.
92
+ */
93
+ aspectRatio?: typeof aspectRatios[number];
54
94
  }
55
95
 
56
96
  export declare const variants: readonly ["default", "outline"];
package/dist/index.js CHANGED
@@ -1,52 +1,69 @@
1
- import { LitElement as p, html as v, unsafeCSS as f } from "lit";
2
- import { validPropertyValues as h, defineCustomElement as k } from "@justeattakeaway/pie-webc-core";
3
- import { classMap as y } from "lit/directives/class-map.js";
1
+ import { LitElement as f, html as k, unsafeCSS as S } from "lit";
2
+ import { validPropertyValues as d, defineCustomElement as z } from "@justeattakeaway/pie-webc-core";
3
+ import { classMap as _ } from "lit/directives/class-map.js";
4
4
  import { property as n, query as C } from "lit/decorators.js";
5
- const S = [
5
+ const E = [
6
6
  "default",
7
7
  "outline"
8
- ], E = [
8
+ ], I = [
9
9
  "default",
10
10
  "subtle",
11
11
  "strong",
12
12
  "dark",
13
13
  "inverse",
14
14
  "inverse-alternative"
15
- ], I = {
15
+ ], R = {
16
16
  default: "c-thumbnail--backgroundDefault",
17
17
  subtle: "c-thumbnail--backgroundSubtle",
18
18
  strong: "c-thumbnail--backgroundStrong",
19
19
  dark: "c-thumbnail--backgroundDark",
20
20
  inverse: "c-thumbnail--backgroundInverse",
21
21
  "inverse-alternative": "c-thumbnail--backgroundInverseAlternative"
22
- }, e = {
22
+ }, $ = ["1by1", "4by3", "16by9"], m = 8, p = 24, x = 128, P = Object.freeze(Array.from(
23
+ { length: (x - p) / m + 1 },
24
+ (u, t) => p + t * m
25
+ )), r = {
23
26
  variant: "default",
27
+ size: 48,
24
28
  src: "",
25
29
  alt: "",
26
30
  disabled: !1,
27
31
  hasPadding: !1,
28
32
  backgroundColor: "default",
33
+ aspectRatio: "1by1",
29
34
  placeholder: {
30
35
  src: "",
31
36
  alt: ""
32
37
  }
33
- }, _ = ".c-thumbnail{--thumbnail-size: var(--dt-spacing-g);--thumbnail-border-radius: var(--dt-radius-rounded-b);--thumbnail-bg-color: var(--dt-color-container-default);--thumbnail-border-color: transparent;--thumbnail-padding: calc(var(--dt-spacing-a) / 2);box-sizing:border-box;overflow:hidden;width:var(--thumbnail-size);height:var(--thumbnail-size);border-radius:var(--thumbnail-border-radius);border:1px solid var(--thumbnail-border-color);background-color:var(--thumbnail-bg-color)}.c-thumbnail.c-thumbnail--outline{--thumbnail-border-color: var(--dt-color-border-default)}.c-thumbnail.c-thumbnail--disabled{opacity:.5}.c-thumbnail.c-thumbnail--backgroundSubtle{--thumbnail-bg-color: var(--dt-color-container-subtle)}.c-thumbnail.c-thumbnail--backgroundStrong{--thumbnail-bg-color: var(--dt-color-container-strong)}.c-thumbnail.c-thumbnail--backgroundDark{--thumbnail-bg-color: var(--dt-color-container-dark)}.c-thumbnail.c-thumbnail--backgroundInverse{--thumbnail-bg-color: var(--dt-color-container-inverse)}.c-thumbnail.c-thumbnail--backgroundInverseAlternative{--thumbnail-bg-color: var(--dt-color-container-inverse-alternative)}.c-thumbnail.c-thumbnail--padding{padding:var(--thumbnail-padding)}.c-thumbnail .c-thumbnail-img{width:100%;height:100%;object-fit:contain;display:block}";
34
- var P = Object.defineProperty, i = (b, r, a, d) => {
35
- for (var o = void 0, l = b.length - 1, c; l >= 0; l--)
36
- (c = b[l]) && (o = c(r, a, o) || o);
37
- return o && P(r, a, o), o;
38
+ }, A = ".c-thumbnail{--thumbnail-size: 48px;--thumbnail-border-radius: var(--dt-radius-rounded-b);--thumbnail-bg-color: var(--dt-color-container-default);--thumbnail-padding: calc(var(--dt-spacing-a) / 2);--thumbnail-img-border-radius: unset;display:flex;justify-content:center;align-items:center;box-sizing:border-box;overflow:hidden;width:var(--thumbnail-size);height:var(--thumbnail-size);border-radius:var(--thumbnail-border-radius);background-color:var(--thumbnail-bg-color)}.c-thumbnail.c-thumbnail--outline{border:1px solid var(--dt-color-border-default)}.c-thumbnail.c-thumbnail--disabled{opacity:.5}.c-thumbnail.c-thumbnail--backgroundSubtle{--thumbnail-bg-color: var(--dt-color-container-subtle)}.c-thumbnail.c-thumbnail--backgroundStrong{--thumbnail-bg-color: var(--dt-color-container-strong)}.c-thumbnail.c-thumbnail--backgroundDark{--thumbnail-bg-color: var(--dt-color-container-dark)}.c-thumbnail.c-thumbnail--backgroundInverse{--thumbnail-bg-color: var(--dt-color-container-inverse)}.c-thumbnail.c-thumbnail--backgroundInverseAlternative{--thumbnail-bg-color: var(--dt-color-container-inverse-alternative)}.c-thumbnail.c-thumbnail--padding{--thumbnail-img-border-radius: calc(var(--thumbnail-border-radius) - var(--thumbnail-padding));padding:var(--thumbnail-padding)}.c-thumbnail.c-thumbnail--4by3{height:calc(var(--thumbnail-size) * 3 / 4)}.c-thumbnail.c-thumbnail--16by9{height:calc(var(--thumbnail-size) * 9 / 16)}.c-thumbnail .c-thumbnail-img{width:100%;height:100%;border-radius:var(--thumbnail-img-border-radius);object-fit:cover}";
39
+ var j = Object.defineProperty, i = (u, t, a, b) => {
40
+ for (var o = void 0, l = u.length - 1, c; l >= 0; l--)
41
+ (c = u[l]) && (o = c(t, a, o) || o);
42
+ return o && j(t, a, o), o;
38
43
  };
39
- const s = "pie-thumbnail", u = class u extends p {
44
+ const s = "pie-thumbnail", h = class h extends f {
40
45
  constructor() {
41
- super(...arguments), this.variant = e.variant, this.src = e.src, this.alt = e.alt, this.disabled = e.disabled, this.hasPadding = e.hasPadding, this.backgroundColor = e.backgroundColor, this.placeholder = e.placeholder;
46
+ super(...arguments), this.variant = r.variant, this.size = r.size, this.aspectRatio = r.aspectRatio, this.src = r.src, this.alt = r.alt, this.disabled = r.disabled, this.hasPadding = r.hasPadding, this.backgroundColor = r.backgroundColor, this.placeholder = r.placeholder;
47
+ }
48
+ /**
49
+ * Assigns the thumbnail size and border radius CSS variables
50
+ * based on the size prop.
51
+ */
52
+ _generateSizeStyles() {
53
+ const { size: t } = this;
54
+ let a = "--dt-radius-rounded-c";
55
+ return t <= 40 ? a = "--dt-radius-rounded-a" : t <= 56 && (a = "--dt-radius-rounded-b"), `
56
+ --thumbnail-size: ${t}px;
57
+ --thumbnail-border-radius: var(${a});
58
+ `;
42
59
  }
43
60
  /**
44
61
  * Handles image load errors by replacing the src and alt props
45
62
  * with the placeholder props.
46
63
  */
47
64
  _handleImageError() {
48
- var r, a;
49
- (r = this.placeholder) != null && r.src && this.setAttribute("src", this.placeholder.src), (a = this.placeholder) != null && a.alt && this.setAttribute("alt", this.placeholder.alt);
65
+ var t, a;
66
+ (t = this.placeholder) != null && t.src && this.setAttribute("src", this.placeholder.src), (a = this.placeholder) != null && a.alt && this.setAttribute("alt", this.placeholder.alt);
50
67
  }
51
68
  /**
52
69
  * Detects image load status and applies the placeholder on failure.
@@ -54,8 +71,8 @@ const s = "pie-thumbnail", u = class u extends p {
54
71
  */
55
72
  _checkImageError() {
56
73
  if (this.img) {
57
- const { complete: r, naturalHeight: a } = this.img;
58
- r && a === 0 && this._handleImageError.call(this);
74
+ const { complete: t, naturalHeight: a } = this.img;
75
+ t && a === 0 && this._handleImageError.call(this);
59
76
  }
60
77
  }
61
78
  firstUpdated() {
@@ -63,66 +80,79 @@ const s = "pie-thumbnail", u = class u extends p {
63
80
  }
64
81
  render() {
65
82
  const {
66
- variant: r,
83
+ variant: t,
67
84
  src: a,
68
- alt: d,
85
+ alt: b,
69
86
  disabled: o,
70
87
  hasPadding: l,
71
88
  backgroundColor: c,
72
- _handleImageError: m
73
- } = this, g = {
89
+ aspectRatio: g
90
+ } = this, v = {
74
91
  "c-thumbnail": !0,
75
- [`c-thumbnail--${r}`]: !0,
76
- [I[c]]: !0,
92
+ [`c-thumbnail--${t}`]: !0,
93
+ [`c-thumbnail--${g}`]: !0,
94
+ [R[c]]: !0,
77
95
  "c-thumbnail--disabled": o,
78
96
  "c-thumbnail--padding": l
79
- };
80
- return v`
81
- <div data-test-id="pie-thumbnail" class="${y(g)}">
97
+ }, y = this._generateSizeStyles();
98
+ return k`
99
+ <div data-test-id="pie-thumbnail"
100
+ class="${_(v)}"
101
+ style="${y}">
82
102
  <img
83
103
  data-test-id="pie-thumbnail-img"
84
104
  src="${a}"
85
105
  class="c-thumbnail-img"
86
- alt="${d}"
87
- @error="${m}"
106
+ alt="${b}"
107
+ @error="${this._handleImageError}"
88
108
  />
89
109
  </div>
90
110
  `;
91
111
  }
92
112
  };
93
- u.styles = f(_);
94
- let t = u;
113
+ h.styles = S(A);
114
+ let e = h;
115
+ i([
116
+ n({ type: String }),
117
+ d(s, E, r.variant)
118
+ ], e.prototype, "variant");
119
+ i([
120
+ n({ type: Number }),
121
+ d(s, P, r.size)
122
+ ], e.prototype, "size");
95
123
  i([
96
124
  n({ type: String }),
97
- h(s, S, e.variant)
98
- ], t.prototype, "variant");
125
+ d(s, $, r.aspectRatio)
126
+ ], e.prototype, "aspectRatio");
99
127
  i([
100
128
  n({ type: String })
101
- ], t.prototype, "src");
129
+ ], e.prototype, "src");
102
130
  i([
103
131
  n({ type: String })
104
- ], t.prototype, "alt");
132
+ ], e.prototype, "alt");
105
133
  i([
106
134
  n({ type: Boolean })
107
- ], t.prototype, "disabled");
135
+ ], e.prototype, "disabled");
108
136
  i([
109
137
  n({ type: Boolean })
110
- ], t.prototype, "hasPadding");
138
+ ], e.prototype, "hasPadding");
111
139
  i([
112
140
  n({ type: String }),
113
- h(s, E, e.backgroundColor)
114
- ], t.prototype, "backgroundColor");
141
+ d(s, I, r.backgroundColor)
142
+ ], e.prototype, "backgroundColor");
115
143
  i([
116
144
  n({ type: Object })
117
- ], t.prototype, "placeholder");
145
+ ], e.prototype, "placeholder");
118
146
  i([
119
147
  C("img")
120
- ], t.prototype, "img");
121
- k(s, t);
148
+ ], e.prototype, "img");
149
+ z(s, e);
122
150
  export {
123
- t as PieThumbnail,
124
- I as backgroundColorClassNames,
125
- E as backgroundColors,
126
- e as defaultProps,
127
- S as variants
151
+ e as PieThumbnail,
152
+ $ as aspectRatios,
153
+ R as backgroundColorClassNames,
154
+ I as backgroundColors,
155
+ r as defaultProps,
156
+ P as sizes,
157
+ E as variants
128
158
  };
package/dist/react.d.ts CHANGED
@@ -4,11 +4,13 @@ import type { LitElement } from 'lit';
4
4
  import * as React_2 from 'react';
5
5
  import type { TemplateResult } from 'lit-html';
6
6
 
7
+ export declare const aspectRatios: readonly ["1by1", "4by3", "16by9"];
8
+
7
9
  export declare const backgroundColorClassNames: Record<typeof backgroundColors[number], string>;
8
10
 
9
11
  export declare const backgroundColors: readonly ["default", "subtle", "strong", "dark", "inverse", "inverse-alternative"];
10
12
 
11
- export declare type DefaultProps = ComponentDefaultProps<ThumbnailProps, 'variant' | 'src' | 'alt' | 'disabled' | 'hasPadding' | 'backgroundColor' | 'placeholder'>;
13
+ export declare type DefaultProps = ComponentDefaultProps<ThumbnailProps>;
12
14
 
13
15
  export declare const defaultProps: DefaultProps;
14
16
 
@@ -19,6 +21,8 @@ export declare const PieThumbnail: React_2.ForwardRefExoticComponent<ThumbnailPr
19
21
  */
20
22
  declare class PieThumbnail_2 extends LitElement implements ThumbnailProps {
21
23
  variant: "default" | "outline";
24
+ size: number;
25
+ aspectRatio: "1by1" | "4by3" | "16by9";
22
26
  src: string;
23
27
  alt: string;
24
28
  disabled: boolean;
@@ -26,6 +30,11 @@ declare class PieThumbnail_2 extends LitElement implements ThumbnailProps {
26
30
  backgroundColor: "default" | "subtle" | "strong" | "dark" | "inverse" | "inverse-alternative";
27
31
  placeholder: ThumbnailProps['placeholder'];
28
32
  private img;
33
+ /**
34
+ * Assigns the thumbnail size and border radius CSS variables
35
+ * based on the size prop.
36
+ */
37
+ private _generateSizeStyles;
29
38
  /**
30
39
  * Handles image load errors by replacing the src and alt props
31
40
  * with the placeholder props.
@@ -48,14 +57,45 @@ declare type PlaceholderProps = {
48
57
 
49
58
  declare type ReactBaseType = React_2.HTMLAttributes<HTMLElement>;
50
59
 
60
+ export declare const sizes: readonly number[];
61
+
51
62
  export declare interface ThumbnailProps {
63
+ /**
64
+ * What style variant the thumbnail should be such as default or outline.
65
+ */
52
66
  variant?: typeof variants[number];
67
+ /**
68
+ * Specify the size as a number between 24 and 128 (inclusive) and a multiple of 8.
69
+ */
70
+ size?: typeof sizes[number];
71
+ /**
72
+ * The src attribute for the underlying image tag.
73
+ */
53
74
  src?: string;
75
+ /**
76
+ * The alt attribute for the underlying image tag.
77
+ */
54
78
  alt?: string;
79
+ /**
80
+ * When true, the disabled styles are applied.
81
+ */
55
82
  disabled?: boolean;
83
+ /**
84
+ * When true, an extra spacing around the thumbnail container is applied.
85
+ */
56
86
  hasPadding?: boolean;
87
+ /**
88
+ * What background color the thumbnail should be.
89
+ */
57
90
  backgroundColor?: typeof backgroundColors[number];
91
+ /**
92
+ * What placeholder should be used when the image fails to load.
93
+ */
58
94
  placeholder?: PlaceholderProps;
95
+ /**
96
+ * Sets the aspect-ratio of the thumbnail image.
97
+ */
98
+ aspectRatio?: typeof aspectRatios[number];
59
99
  }
60
100
 
61
101
  export declare const variants: readonly ["default", "outline"];
package/dist/react.js CHANGED
@@ -1,18 +1,20 @@
1
1
  import * as a from "react";
2
2
  import { createComponent as e } from "@lit/react";
3
3
  import { PieThumbnail as o } from "./index.js";
4
- import { backgroundColorClassNames as l, backgroundColors as p, defaultProps as u, variants as b } from "./index.js";
5
- const m = e({
4
+ import { aspectRatios as l, backgroundColorClassNames as p, backgroundColors as c, defaultProps as u, sizes as b, variants as h } from "./index.js";
5
+ const t = e({
6
6
  displayName: "PieThumbnail",
7
7
  elementClass: o,
8
8
  react: a,
9
9
  tagName: "pie-thumbnail",
10
10
  events: {}
11
- }), r = m;
11
+ }), s = t;
12
12
  export {
13
- r as PieThumbnail,
14
- l as backgroundColorClassNames,
15
- p as backgroundColors,
13
+ s as PieThumbnail,
14
+ l as aspectRatios,
15
+ p as backgroundColorClassNames,
16
+ c as backgroundColors,
16
17
  u as defaultProps,
17
- b as variants
18
+ b as sizes,
19
+ h as variants
18
20
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@justeattakeaway/pie-thumbnail",
3
3
  "description": "PIE Design System Thumbnail built using Web Components",
4
- "version": "0.3.0",
4
+ "version": "0.5.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
package/src/defs.ts CHANGED
@@ -17,30 +17,72 @@ export const backgroundColorClassNames: Record<typeof backgroundColors[number],
17
17
  'inverse-alternative': 'c-thumbnail--backgroundInverseAlternative',
18
18
  };
19
19
 
20
+ export const aspectRatios = ['1by1', '4by3', '16by9'] as const;
21
+
22
+ const SIZE_INCREMENT_BY = 8;
23
+ const SIZE_MIN = 24;
24
+ const SIZE_MAX = 128;
25
+
26
+ export const sizes = Object.freeze(Array.from(
27
+ { length: (SIZE_MAX - SIZE_MIN) / SIZE_INCREMENT_BY + 1 },
28
+ (_, i) => SIZE_MIN + i * SIZE_INCREMENT_BY,
29
+ )) as readonly number[];
30
+
20
31
  type PlaceholderProps = {
21
32
  src?: string;
22
33
  alt?: string;
23
34
  }
24
35
 
25
36
  export interface ThumbnailProps {
37
+ /**
38
+ * What style variant the thumbnail should be such as default or outline.
39
+ */
26
40
  variant?: typeof variants[number];
41
+ /**
42
+ * Specify the size as a number between 24 and 128 (inclusive) and a multiple of 8.
43
+ */
44
+ size?: typeof sizes[number];
45
+ /**
46
+ * The src attribute for the underlying image tag.
47
+ */
27
48
  src?: string;
49
+ /**
50
+ * The alt attribute for the underlying image tag.
51
+ */
28
52
  alt?: string;
53
+ /**
54
+ * When true, the disabled styles are applied.
55
+ */
29
56
  disabled?: boolean;
57
+ /**
58
+ * When true, an extra spacing around the thumbnail container is applied.
59
+ */
30
60
  hasPadding?: boolean;
61
+ /**
62
+ * What background color the thumbnail should be.
63
+ */
31
64
  backgroundColor?: typeof backgroundColors[number];
65
+ /**
66
+ * What placeholder should be used when the image fails to load.
67
+ */
32
68
  placeholder?: PlaceholderProps;
69
+ /**
70
+ * Sets the aspect-ratio of the thumbnail image.
71
+ */
72
+ aspectRatio?: typeof aspectRatios[number];
33
73
  }
34
74
 
35
- export type DefaultProps = ComponentDefaultProps<ThumbnailProps, 'variant' | 'src' | 'alt' | 'disabled' | 'hasPadding' | 'backgroundColor' | 'placeholder'>;
75
+ export type DefaultProps = ComponentDefaultProps<ThumbnailProps>;
36
76
 
37
77
  export const defaultProps: DefaultProps = {
38
78
  variant: 'default',
79
+ size: 48,
39
80
  src: '',
40
81
  alt: '',
41
82
  disabled: false,
42
83
  hasPadding: false,
43
84
  backgroundColor: 'default',
85
+ aspectRatio: '1by1',
44
86
  placeholder: {
45
87
  src: '',
46
88
  alt: '',
package/src/index.ts CHANGED
@@ -12,6 +12,8 @@ import {
12
12
  variants,
13
13
  backgroundColors,
14
14
  backgroundColorClassNames,
15
+ sizes,
16
+ aspectRatios,
15
17
  } from './defs';
16
18
  import styles from './thumbnail.scss?inline';
17
19
 
@@ -28,6 +30,14 @@ export class PieThumbnail extends LitElement implements ThumbnailProps {
28
30
  @validPropertyValues(componentSelector, variants, defaultProps.variant)
29
31
  public variant = defaultProps.variant;
30
32
 
33
+ @property({ type: Number })
34
+ @validPropertyValues(componentSelector, sizes, defaultProps.size)
35
+ public size = defaultProps.size;
36
+
37
+ @property({ type: String })
38
+ @validPropertyValues(componentSelector, aspectRatios, defaultProps.aspectRatio)
39
+ public aspectRatio = defaultProps.aspectRatio;
40
+
31
41
  @property({ type: String })
32
42
  public src = defaultProps.src;
33
43
 
@@ -50,6 +60,25 @@ export class PieThumbnail extends LitElement implements ThumbnailProps {
50
60
  @query('img')
51
61
  private img!: HTMLImageElement;
52
62
 
63
+ /**
64
+ * Assigns the thumbnail size and border radius CSS variables
65
+ * based on the size prop.
66
+ */
67
+ private _generateSizeStyles (): string {
68
+ const { size } = this;
69
+ let borderRadius = '--dt-radius-rounded-c';
70
+ if (size <= 40) {
71
+ borderRadius = '--dt-radius-rounded-a';
72
+ } else if (size <= 56) {
73
+ borderRadius = '--dt-radius-rounded-b';
74
+ }
75
+
76
+ return `
77
+ --thumbnail-size: ${size}px;
78
+ --thumbnail-border-radius: var(${borderRadius});
79
+ `;
80
+ }
81
+
53
82
  /**
54
83
  * Handles image load errors by replacing the src and alt props
55
84
  * with the placeholder props.
@@ -83,25 +112,30 @@ export class PieThumbnail extends LitElement implements ThumbnailProps {
83
112
  disabled,
84
113
  hasPadding,
85
114
  backgroundColor,
86
- _handleImageError,
115
+ aspectRatio,
87
116
  } = this;
88
117
 
89
118
  const wrapperClasses = {
90
119
  'c-thumbnail': true,
91
120
  [`c-thumbnail--${variant}`]: true,
121
+ [`c-thumbnail--${aspectRatio}`]: true,
92
122
  [backgroundColorClassNames[backgroundColor]]: true,
93
123
  'c-thumbnail--disabled': disabled,
94
124
  'c-thumbnail--padding': hasPadding,
95
125
  };
96
126
 
127
+ const sizeStyles = this._generateSizeStyles();
128
+
97
129
  return html`
98
- <div data-test-id="pie-thumbnail" class="${classMap(wrapperClasses)}">
130
+ <div data-test-id="pie-thumbnail"
131
+ class="${classMap(wrapperClasses)}"
132
+ style="${sizeStyles}">
99
133
  <img
100
134
  data-test-id="pie-thumbnail-img"
101
135
  src="${src}"
102
136
  class="c-thumbnail-img"
103
137
  alt="${alt}"
104
- @error="${_handleImageError}"
138
+ @error="${this._handleImageError}"
105
139
  />
106
140
  </div>
107
141
  `;
@@ -1,20 +1,22 @@
1
1
  .c-thumbnail {
2
- --thumbnail-size: var(--dt-spacing-g);
2
+ --thumbnail-size: 48px;
3
3
  --thumbnail-border-radius: var(--dt-radius-rounded-b);
4
4
  --thumbnail-bg-color: var(--dt-color-container-default);
5
- --thumbnail-border-color: transparent;
6
5
  --thumbnail-padding: calc(var(--dt-spacing-a) / 2);
6
+ --thumbnail-img-border-radius: unset;
7
7
 
8
+ display: flex;
9
+ justify-content: center;
10
+ align-items: center;
8
11
  box-sizing: border-box;
9
12
  overflow: hidden;
10
13
  width: var(--thumbnail-size);
11
14
  height: var(--thumbnail-size);
12
15
  border-radius: var(--thumbnail-border-radius);
13
- border: 1px solid var(--thumbnail-border-color);
14
16
  background-color: var(--thumbnail-bg-color);
15
17
 
16
18
  &.c-thumbnail--outline {
17
- --thumbnail-border-color: var(--dt-color-border-default);
19
+ border: 1px solid var(--dt-color-border-default);
18
20
  }
19
21
 
20
22
  &.c-thumbnail--disabled {
@@ -42,13 +44,22 @@
42
44
  }
43
45
 
44
46
  &.c-thumbnail--padding {
47
+ --thumbnail-img-border-radius: calc(var(--thumbnail-border-radius) - var(--thumbnail-padding));
45
48
  padding: var(--thumbnail-padding);
46
49
  }
47
50
 
51
+ &.c-thumbnail--4by3 {
52
+ height: calc(var(--thumbnail-size) * 3 / 4)
53
+ }
54
+
55
+ &.c-thumbnail--16by9 {
56
+ height: calc(var(--thumbnail-size) * 9 / 16)
57
+ }
58
+
48
59
  .c-thumbnail-img {
49
60
  width: 100%;
50
61
  height: 100%;
51
- object-fit: contain;
52
- display: block;
62
+ border-radius: var(--thumbnail-img-border-radius);
63
+ object-fit: cover;
53
64
  }
54
65
  }