@justeattakeaway/pie-skeleton 0.0.1 → 0.2.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/README.md +68 -9
- package/custom-elements.json +448 -3
- package/dist/index.d.ts +157 -0
- package/dist/index.js +245 -18
- package/dist/react.d.ts +157 -0
- package/dist/react.js +19 -6
- package/package.json +3 -3
- package/src/defs.ts +125 -3
- package/src/index.ts +246 -7
- package/src/skeleton.scss +266 -2
package/dist/react.js
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
import * as e from "react";
|
|
2
|
-
import { createComponent as
|
|
3
|
-
import { PieSkeleton as
|
|
4
|
-
|
|
2
|
+
import { createComponent as o } from "@lit/react";
|
|
3
|
+
import { PieSkeleton as t } from "./index.js";
|
|
4
|
+
import { LINES_MAX as m, LINES_MIN as S, STARS_MAX as p, STARS_MIN as c, aspectRatios as k, backgroundColorClassNames as d, backgroundColors as N, borderRadii as f, buttonSizes as P, defaultProps as R, spacingIdentifiers as C, variants as I } from "./index.js";
|
|
5
|
+
const a = o({
|
|
5
6
|
displayName: "PieSkeleton",
|
|
6
|
-
elementClass:
|
|
7
|
+
elementClass: t,
|
|
7
8
|
react: e,
|
|
8
9
|
tagName: "pie-skeleton",
|
|
9
10
|
events: {}
|
|
10
|
-
}),
|
|
11
|
+
}), i = a;
|
|
11
12
|
export {
|
|
12
|
-
m as
|
|
13
|
+
m as LINES_MAX,
|
|
14
|
+
S as LINES_MIN,
|
|
15
|
+
i as PieSkeleton,
|
|
16
|
+
p as STARS_MAX,
|
|
17
|
+
c as STARS_MIN,
|
|
18
|
+
k as aspectRatios,
|
|
19
|
+
d as backgroundColorClassNames,
|
|
20
|
+
N as backgroundColors,
|
|
21
|
+
f as borderRadii,
|
|
22
|
+
P as buttonSizes,
|
|
23
|
+
R as defaultProps,
|
|
24
|
+
C as spacingIdentifiers,
|
|
25
|
+
I as variants
|
|
13
26
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justeattakeaway/pie-skeleton",
|
|
3
3
|
"description": "PIE Design System Skeleton built using Web Components",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"license": "Apache-2.0",
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@justeattakeaway/pie-components-config": "0.21.5",
|
|
39
|
-
"@justeattakeaway/pie-css": "1.9.
|
|
40
|
-
"@justeattakeaway/pie-monorepo-utils": "0.9.
|
|
39
|
+
"@justeattakeaway/pie-css": "1.9.3",
|
|
40
|
+
"@justeattakeaway/pie-monorepo-utils": "0.9.14"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@justeattakeaway/pie-webc-core": "22.0.1"
|
package/src/defs.ts
CHANGED
|
@@ -1,3 +1,125 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
1
|
+
import { type ComponentDefaultProps } from '@justeattakeaway/pie-webc-core';
|
|
2
|
+
|
|
3
|
+
export const variants = ['box', 'circle', 'pill', 'text', 'star'] as const;
|
|
4
|
+
|
|
5
|
+
export const backgroundColors = ['default', 'strong', 'subtle'] as const;
|
|
6
|
+
|
|
7
|
+
export const backgroundColorClassNames: Record<typeof backgroundColors[number], string> = {
|
|
8
|
+
default: 'c-skeleton--backgroundDefault',
|
|
9
|
+
strong: 'c-skeleton--backgroundStrong',
|
|
10
|
+
subtle: 'c-skeleton--backgroundSubtle',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const aspectRatios = ['1by1', '4by3', '16by9', '3by4', '9by16'] as const;
|
|
14
|
+
|
|
15
|
+
export const borderRadii = [
|
|
16
|
+
'radius-rounded-none',
|
|
17
|
+
'radius-rounded-a',
|
|
18
|
+
'radius-rounded-b',
|
|
19
|
+
'radius-rounded-c',
|
|
20
|
+
'radius-rounded-d',
|
|
21
|
+
'radius-rounded-e',
|
|
22
|
+
'radius-rounded-f',
|
|
23
|
+
'radius-rounded-g',
|
|
24
|
+
] as const;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The `pie-button` size names accepted by the `pill` variant's `height`.
|
|
28
|
+
*/
|
|
29
|
+
export const buttonSizes = [
|
|
30
|
+
'button-xsmall',
|
|
31
|
+
'button-small-productive',
|
|
32
|
+
'button-small-expressive',
|
|
33
|
+
'button-medium',
|
|
34
|
+
'button-large',
|
|
35
|
+
] as const;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The lettered spacing tokens accepted as a `height`, written as the token name without the
|
|
39
|
+
* `--dt-` prefix (e.g. `spacing-e`). The numbered base tokens (`--dt-spacing-24`, etc.) are not supported.
|
|
40
|
+
*/
|
|
41
|
+
export const spacingIdentifiers = [
|
|
42
|
+
'spacing-none',
|
|
43
|
+
'spacing-a-small',
|
|
44
|
+
'spacing-a',
|
|
45
|
+
'spacing-b',
|
|
46
|
+
'spacing-c',
|
|
47
|
+
'spacing-d',
|
|
48
|
+
'spacing-e',
|
|
49
|
+
'spacing-f',
|
|
50
|
+
'spacing-g',
|
|
51
|
+
'spacing-h',
|
|
52
|
+
'spacing-i',
|
|
53
|
+
'spacing-j',
|
|
54
|
+
] as const;
|
|
55
|
+
|
|
56
|
+
/** The minimum number of lines the `text` variant can draw. */
|
|
57
|
+
export const LINES_MIN = 1;
|
|
58
|
+
/** The maximum number of lines the `text` variant can draw. */
|
|
59
|
+
export const LINES_MAX = 10;
|
|
60
|
+
/** The minimum number of stars the `star` variant can draw. */
|
|
61
|
+
export const STARS_MIN = 1;
|
|
62
|
+
/** The maximum number of stars the `star` variant can draw. */
|
|
63
|
+
export const STARS_MAX = 10;
|
|
64
|
+
|
|
65
|
+
export interface SkeletonProps {
|
|
66
|
+
/**
|
|
67
|
+
* The shape the skeleton draws: a box, a circle, a pill, a block of text lines or a row of stars.
|
|
68
|
+
*/
|
|
69
|
+
variant?: typeof variants[number];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The background colour. `strong` is darker (for nested levels) and `subtle` is lighter (for nested levels or darker surfaces).
|
|
73
|
+
*/
|
|
74
|
+
backgroundColor?: typeof backgroundColors[number];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The width of the skeleton. Accepts an absolute value (e.g. `120px`) or a percentage of the container (e.g. `50%`).
|
|
78
|
+
* Ignored by the `circle` and `star` variants.
|
|
79
|
+
*/
|
|
80
|
+
width?: string;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The height of the skeleton. The accepted formats depend on the variant. See the component documentation for the full list.
|
|
84
|
+
*/
|
|
85
|
+
height?: string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The number of text lines to draw. Applies to the `text` variant only. Values are clamped between 1 and 10.
|
|
89
|
+
*/
|
|
90
|
+
lines?: number;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The number of stars to draw. Applies to the `star` variant only. Values are clamped between 1 and 10.
|
|
94
|
+
*/
|
|
95
|
+
stars?: number;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The corner radius of the `box` variant, as a radius token identifier (e.g. `radius-rounded-b`).
|
|
99
|
+
* Ignored by the `text`, `pill`, `circle` and `star` variants.
|
|
100
|
+
*/
|
|
101
|
+
borderRadius?: typeof borderRadii[number];
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The aspect ratio of the `box` variant. Ignored when both `width` and `height` are set.
|
|
105
|
+
*/
|
|
106
|
+
aspectRatio?: typeof aspectRatios[number];
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* When true, a `pill` with a `button-*` height switches to the wide button height above the `md` breakpoint,
|
|
110
|
+
* matching `pie-button`. Ignored otherwise.
|
|
111
|
+
*/
|
|
112
|
+
isResponsive?: boolean;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type DefaultProps = ComponentDefaultProps<SkeletonProps, keyof Omit<SkeletonProps, 'width' | 'height'>>;
|
|
116
|
+
|
|
117
|
+
export const defaultProps: DefaultProps = {
|
|
118
|
+
variant: 'box',
|
|
119
|
+
backgroundColor: 'default',
|
|
120
|
+
lines: 1,
|
|
121
|
+
stars: 1,
|
|
122
|
+
borderRadius: 'radius-rounded-b',
|
|
123
|
+
aspectRatio: '1by1',
|
|
124
|
+
isResponsive: false,
|
|
125
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,24 +1,263 @@
|
|
|
1
|
-
|
|
2
|
-
import { html, unsafeCSS } from 'lit';
|
|
3
|
-
import { PieElement } from '@justeattakeaway/pie-webc-core/src/internals/PieElement';
|
|
4
1
|
import {
|
|
5
|
-
|
|
6
|
-
} from '
|
|
2
|
+
html, nothing, unsafeCSS,
|
|
3
|
+
} from 'lit';
|
|
4
|
+
import { PieElement } from '@justeattakeaway/pie-webc-core/src/internals/PieElement';
|
|
5
|
+
import { safeCustomElement, validPropertyValues } from '@justeattakeaway/pie-webc-core';
|
|
6
|
+
import { classMap } from 'lit/directives/class-map.js';
|
|
7
|
+
import { property } from 'lit/decorators.js';
|
|
7
8
|
import styles from './skeleton.scss?inline';
|
|
8
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
type SkeletonProps,
|
|
11
|
+
defaultProps,
|
|
12
|
+
variants,
|
|
13
|
+
backgroundColors,
|
|
14
|
+
backgroundColorClassNames,
|
|
15
|
+
aspectRatios,
|
|
16
|
+
borderRadii,
|
|
17
|
+
buttonSizes,
|
|
18
|
+
spacingIdentifiers,
|
|
19
|
+
LINES_MIN,
|
|
20
|
+
LINES_MAX,
|
|
21
|
+
STARS_MIN,
|
|
22
|
+
STARS_MAX,
|
|
23
|
+
} from './defs';
|
|
9
24
|
|
|
10
25
|
// Valid values available to consumers
|
|
11
26
|
export * from './defs';
|
|
12
27
|
|
|
13
28
|
const componentSelector = 'pie-skeleton';
|
|
14
29
|
|
|
30
|
+
// Matches absolute (e.g. `40px`) and percentage (e.g. `50%`) height values.
|
|
31
|
+
const LENGTH_REGEX = /^\d*\.?\d+(px|rem|em|%|vh|vw)$/;
|
|
32
|
+
|
|
15
33
|
/**
|
|
16
34
|
* @tagname pie-skeleton
|
|
17
35
|
*/
|
|
18
36
|
@safeCustomElement('pie-skeleton')
|
|
19
37
|
export class PieSkeleton extends PieElement implements SkeletonProps {
|
|
38
|
+
@property({ type: String })
|
|
39
|
+
@validPropertyValues(componentSelector, variants, defaultProps.variant)
|
|
40
|
+
public variant = defaultProps.variant;
|
|
41
|
+
|
|
42
|
+
@property({ type: String })
|
|
43
|
+
@validPropertyValues(componentSelector, backgroundColors, defaultProps.backgroundColor)
|
|
44
|
+
public backgroundColor = defaultProps.backgroundColor;
|
|
45
|
+
|
|
46
|
+
@property({ type: String })
|
|
47
|
+
public width: SkeletonProps['width'];
|
|
48
|
+
|
|
49
|
+
@property({ type: String })
|
|
50
|
+
public height: SkeletonProps['height'];
|
|
51
|
+
|
|
52
|
+
@property({ type: Number })
|
|
53
|
+
public lines = defaultProps.lines;
|
|
54
|
+
|
|
55
|
+
@property({ type: Number })
|
|
56
|
+
public stars = defaultProps.stars;
|
|
57
|
+
|
|
58
|
+
@property({ type: String })
|
|
59
|
+
@validPropertyValues(componentSelector, borderRadii, defaultProps.borderRadius)
|
|
60
|
+
public borderRadius = defaultProps.borderRadius;
|
|
61
|
+
|
|
62
|
+
@property({ type: String })
|
|
63
|
+
@validPropertyValues(componentSelector, aspectRatios, defaultProps.aspectRatio)
|
|
64
|
+
public aspectRatio = defaultProps.aspectRatio;
|
|
65
|
+
|
|
66
|
+
@property({ type: Boolean })
|
|
67
|
+
public isResponsive = defaultProps.isResponsive;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Clamps a numeric prop to the given inclusive range. Non-numeric values fall back to the minimum.
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
private _clamp (value: number, min: number, max: number): number {
|
|
75
|
+
const parsed = Math.trunc(Number(value));
|
|
76
|
+
if (!Number.isFinite(parsed)) return min;
|
|
77
|
+
return Math.min(Math.max(parsed, min), max);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The number of text lines to draw, clamped between 1 and 10.
|
|
82
|
+
*
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
private get _linesCount (): number {
|
|
86
|
+
return this._clamp(this.lines, LINES_MIN, LINES_MAX);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The number of stars to draw, clamped between 1 and 10.
|
|
91
|
+
*
|
|
92
|
+
* @private
|
|
93
|
+
*/
|
|
94
|
+
private get _starsCount (): number {
|
|
95
|
+
return this._clamp(this.stars, STARS_MIN, STARS_MAX);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether a `height` value is a `pie-button` size name.
|
|
100
|
+
*
|
|
101
|
+
* @private
|
|
102
|
+
*/
|
|
103
|
+
private _isButtonSize (value?: string): value is typeof buttonSizes[number] {
|
|
104
|
+
return !!value && (buttonSizes as readonly string[]).includes(value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether a `height` value is a supported spacing identifier (e.g. `spacing-e`).
|
|
109
|
+
*
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
private _isSpacingIdentifier (value?: string): boolean {
|
|
113
|
+
return !!value && (spacingIdentifiers as readonly string[]).includes(value);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Resolves the `height` prop to a CSS length for the variants that accept absolute values,
|
|
118
|
+
* percentages or spacing identifiers (box, circle, pill and star).
|
|
119
|
+
* Returns `undefined` when the value is handled elsewhere (a `button-*` size) or isn't valid
|
|
120
|
+
* for the variant, so the CSS default applies.
|
|
121
|
+
*
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
private _resolveHeight (): string | undefined {
|
|
125
|
+
const { height, variant } = this;
|
|
126
|
+
|
|
127
|
+
if (!height) return undefined;
|
|
128
|
+
|
|
129
|
+
if (this._isSpacingIdentifier(height)) return `var(--dt-${height})`;
|
|
130
|
+
|
|
131
|
+
// For pill variants, the height is defined by a class
|
|
132
|
+
if (variant === 'pill' && this._isButtonSize(height)) return undefined;
|
|
133
|
+
|
|
134
|
+
// The star variant ignores percentages: a row of fixed-shape stars has no percentage height.
|
|
135
|
+
if (variant === 'star' && height.endsWith('%')) return undefined;
|
|
136
|
+
|
|
137
|
+
return LENGTH_REGEX.test(height) ? height : undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The classes applied to the root element.
|
|
142
|
+
*
|
|
143
|
+
* @private
|
|
144
|
+
*/
|
|
145
|
+
private get _rootClasses (): Record<string, boolean> {
|
|
146
|
+
const {
|
|
147
|
+
variant, backgroundColor, borderRadius, aspectRatio, isResponsive, height,
|
|
148
|
+
} = this;
|
|
149
|
+
|
|
150
|
+
const classes: Record<string, boolean> = {
|
|
151
|
+
'c-skeleton': true,
|
|
152
|
+
[`c-skeleton--${variant}`]: true,
|
|
153
|
+
[backgroundColorClassNames[backgroundColor]]: true,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
if (variant === 'box') {
|
|
157
|
+
classes[`c-skeleton--${borderRadius}`] = true;
|
|
158
|
+
classes[`c-skeleton--${aspectRatio}`] = true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (variant === 'pill') {
|
|
162
|
+
if (this._isButtonSize(height)) {
|
|
163
|
+
classes[`c-skeleton--${height}`] = true;
|
|
164
|
+
} else if (!this._resolveHeight()) {
|
|
165
|
+
// The pill defaults to the medium button height.
|
|
166
|
+
classes['c-skeleton--button-medium'] = true;
|
|
167
|
+
}
|
|
168
|
+
classes['c-skeleton--responsive'] = isResponsive;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return classes;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The inline styles applied to the root element.
|
|
176
|
+
*
|
|
177
|
+
* @private
|
|
178
|
+
*/
|
|
179
|
+
private get _rootStyles (): string {
|
|
180
|
+
const { variant, width } = this;
|
|
181
|
+
const styles: string[] = [];
|
|
182
|
+
|
|
183
|
+
// `width` is ignored by the circle and star variants.
|
|
184
|
+
if (width && variant !== 'circle' && variant !== 'star') {
|
|
185
|
+
styles.push(`width: ${width}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (variant === 'circle') {
|
|
189
|
+
// A circle is sized by its height; width follows so it stays 1:1.
|
|
190
|
+
const height = this._resolveHeight();
|
|
191
|
+
if (height) {
|
|
192
|
+
styles.push(`width: ${height}`, `height: ${height}`);
|
|
193
|
+
}
|
|
194
|
+
} else if (variant === 'box' || variant === 'pill') {
|
|
195
|
+
const height = this._resolveHeight();
|
|
196
|
+
if (height) styles.push(`height: ${height}`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// The star variant sizes each star from a single custom property.
|
|
200
|
+
if (variant === 'star') {
|
|
201
|
+
const size = this._resolveHeight();
|
|
202
|
+
if (size) styles.push(`--skeleton-star-size: ${size}`);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return styles.join('; ');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Renders the text lines for the `text` variant. When there's more than one line,
|
|
210
|
+
* the last line is drawn at half width (handled in CSS through the `is-last` class).
|
|
211
|
+
*
|
|
212
|
+
* @private
|
|
213
|
+
*/
|
|
214
|
+
private _renderTextLines () {
|
|
215
|
+
const lineCount = this._linesCount;
|
|
216
|
+
const { height } = this;
|
|
217
|
+
|
|
218
|
+
return Array.from({ length: lineCount }, (_, index) => {
|
|
219
|
+
const isLastLine = lineCount > 1 && index === lineCount - 1;
|
|
220
|
+
|
|
221
|
+
const lineClasses = classMap({
|
|
222
|
+
'c-skeleton-line': true,
|
|
223
|
+
'is-last': isLastLine,
|
|
224
|
+
[`c-skeleton-line--${height}`]: !!height,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
return html`<div class="${lineClasses}"></div>`;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Renders the stars for the `star` variant.
|
|
233
|
+
*
|
|
234
|
+
* @private
|
|
235
|
+
*/
|
|
236
|
+
private _renderStars () {
|
|
237
|
+
return Array.from({ length: this._starsCount }, () => html`<div class="c-skeleton-star"></div>`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Renders the shapes drawn inside the root element. The box, circle and pill variants
|
|
242
|
+
* are drawn by the root element itself, so they have no children.
|
|
243
|
+
*
|
|
244
|
+
* @private
|
|
245
|
+
*/
|
|
246
|
+
private _renderShapes () {
|
|
247
|
+
if (this.variant === 'text') return this._renderTextLines();
|
|
248
|
+
if (this.variant === 'star') return this._renderStars();
|
|
249
|
+
return nothing;
|
|
250
|
+
}
|
|
251
|
+
|
|
20
252
|
render () {
|
|
21
|
-
return html
|
|
253
|
+
return html`
|
|
254
|
+
<div
|
|
255
|
+
data-test-id="pie-skeleton"
|
|
256
|
+
class="${classMap(this._rootClasses)}"
|
|
257
|
+
style="${this._rootStyles}">
|
|
258
|
+
${this._renderShapes()}
|
|
259
|
+
</div>
|
|
260
|
+
`;
|
|
22
261
|
}
|
|
23
262
|
|
|
24
263
|
// Renders a `CSSResult` generated from SCSS by Vite
|