@skeletonizer/angular 0.0.7-alpha.0 → 0.0.9-alpha.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 +258 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,3 +4,261 @@ This is the Angular adapter for Skeletonizer. It provides a simple way to create
|
|
|
4
4
|
## Installation
|
|
5
5
|
To install the package, run the following command:
|
|
6
6
|
`npm install @skeletonizer/angular @skeletonizer/utils --save`
|
|
7
|
+
|
|
8
|
+
`@skeletonizer/angular` is always used in conjunction with `@skeletonizer/utils`. The `@skeletonizer/utils` package provides the core functionality for creating skeletonized views, while the `@skeletonizer/angular` package provides the Angular-specific functionality. **The versions should always match**.
|
|
9
|
+
|
|
10
|
+
## Template Adjustments
|
|
11
|
+
Generally speaking, all you need to do to use the skeletonizer in the template, is to wrap the part of the template you wish to skeletonize in the `<skeletonizer-skeleton>` component and in a `<ng-template let-foo>` (all examples henceforth assume `foo` is called `context`). The `skeletonConfig`, `showSkeleton` and `scope` properties must be passed to the `<skeletonizer-skeleton>` as inputs.
|
|
12
|
+
Furthermore, all the data that you wish to access in the skeletonized part of the template must be accessed through the `proxy(context)` method, except the data that you provide and is available even whilst the data is being loaded (ie. `[showSkeleton]="true"`)..
|
|
13
|
+
|
|
14
|
+
As far as the template goes, it essentially means transforming the code from this:
|
|
15
|
+
```html
|
|
16
|
+
<div>{{ somePropOrMethodCallAvailableAsync }}</div>
|
|
17
|
+
<div>{{ someAlreadyHardCodedOrInputBoundPropAvailableSync }} </div>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
into this:
|
|
21
|
+
|
|
22
|
+
```html
|
|
23
|
+
<skeletonizer-skeleton [showSkeleton]="showSkeleton" [config]="skeletonConfig" [scope]="{ somePropOrMethodCallAvailableAsync } ">
|
|
24
|
+
<ng-template let-context>
|
|
25
|
+
<div>{{ proxy(context).somePropOrMethodCallAvailableAsync }}</div>
|
|
26
|
+
<div>{{ someAlreadyHardCodedOrInputBoundPropAvailableSync }} </div>
|
|
27
|
+
</ng-template>
|
|
28
|
+
</skeletonizer-skeleton>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
As long as the shape of the properties you access within the skeleton part of the template matches the shape of the data you provide in the `scope` and `skeletonConfig` properties, the skeletonized view will be in sync with the actual view, regardless of changes to the design.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
You can use Skeletonizer either in a standalone component or in a component that is a part of a module.
|
|
35
|
+
|
|
36
|
+
If you wish to use Skeletonizer in a standalone component, you need to add `SkeletonizerSkeletonComponent` in the imports of the component.
|
|
37
|
+
The usage in a component that is a part of a module is the same as the standalone component, but you need to add `SkeletonizerSkeletonComponent` in the imports of the **module** where the component is declared.
|
|
38
|
+
|
|
39
|
+
Every component that uses Skeletonizer should extend `SkeletonAbstractComponent`, which is available in `@skeletonizer/utils`.
|
|
40
|
+
The `SkeletonAbstractComponent` requires you to pass a type argument that represents the data model of the **part(s) of the component that you intend to skeletonize**.
|
|
41
|
+
It also requires you to implement the `skeletonConfig` (type validated against the type argument you pass to `SkeletonAbstractComponent`) and `showSkeleton` properties which must be passed to the `SkeletonizerSkeletonComponent` as inputs.
|
|
42
|
+
By extending the `SkeletonAbstractComponent`, you also get access to the `proxy` method via which you can (type) safely access props and methods **within the skeletonized part of the current component**.
|
|
43
|
+
|
|
44
|
+
In the skeletonized part of the template, you **must** access the data through the `proxy(context)` method.
|
|
45
|
+
You can think of `proxy(context)` in the same way as you would think of `this` in a class method (or in the template, where the `this` is usually omitted when accessing props / methods and we usually call `foo` instead of `this.foo`). The only difference is that when using `proxy(context)`, the content-projected template will use the mocked values when the `showSkeleton` is `true`, and resolved values when `showSkeleton` is `false` - all while maintaining the type safety.
|
|
46
|
+
|
|
47
|
+
For more details about the `SkeletonAbstractComponent`, see the [SkeletonAbstractComponent](/packages/utils/README.md#SkeletonAbstractComponent) section.
|
|
48
|
+
|
|
49
|
+
For more details about the `SchemaItem` property, see the [SchemaItem](/packages/utils/README.md#SchemaItem) section.
|
|
50
|
+
|
|
51
|
+
For more details about the `skeletonConfig` property, see the [TSchemaConfig](/packages/utils/README.md#TSchemaConfig) section.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { Component } from '@angular/core';
|
|
56
|
+
import { SkeletonizerSkeletonComponent } from '@skeletonizer/angular';
|
|
57
|
+
import { SchemaItem, SkeletonAbstractComponent, TSchemaConfig } from '@skeletonizer/utils';
|
|
58
|
+
import { DomSanitizer } from '@angular/platform-browser';
|
|
59
|
+
|
|
60
|
+
interface IResource {
|
|
61
|
+
title: string;
|
|
62
|
+
link: string;
|
|
63
|
+
svg: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// the svgs are just for the sake of the example
|
|
67
|
+
const learnNgSvg: string = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M5 13.18v4L12 21l7-3.82v-4L12 17l-7-3.82zM12 3L1 9l11 6 9-4.91V17h2V9L12 3z"/></svg>';
|
|
68
|
+
|
|
69
|
+
const cliDocsSvg: string = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z"/></svg>';
|
|
70
|
+
|
|
71
|
+
const loadingSvg: string = '<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><style>.spinner{transform-origin:center;animation:spinner .75s linear infinite}@keyframes spinner{from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style><g class="spinner"><circle cx="12" cy="2.5" r="1.5" opacity=".14"/><circle cx="16.75" cy="3.77" r="1.5" opacity=".29"/><circle cx="20.23" cy="7.25" r="1.5" opacity=".43"/><circle cx="21.50" cy="12.00" r="1.5" opacity=".57"/><circle cx="20.23" cy="16.75" r="1.5" opacity=".71"/><circle cx="16.75" cy="20.23" r="1.5" opacity=".86"/><circle cx="12" cy="21.5" r="1.5"/></g></svg>';
|
|
72
|
+
|
|
73
|
+
type TSkeletonizedPart = Pick<AppComponent, 'resources' | 'otherPropWeWantToUseInSkeletonizedPart'>;
|
|
74
|
+
|
|
75
|
+
@Component({
|
|
76
|
+
selector: 'my-component',
|
|
77
|
+
template: `
|
|
78
|
+
<h2>{{ pageTitle }}</h2>
|
|
79
|
+
|
|
80
|
+
<skeletonizer-skeleton [showSkeleton]="showSkeleton" [config]="skeletonConfig" [scope]="{ resources, otherPropWeWantToUseInSkeletonizedPart }">
|
|
81
|
+
<ng-template let-context>
|
|
82
|
+
<span>{{ proxy(context).otherPropWeWantToUseInSkeletonizedPart }}</span>
|
|
83
|
+
|
|
84
|
+
<a *ngFor="let resource of proxy(context).resources" target="_blank" rel="noopener" [href]="resource.link">
|
|
85
|
+
<div [innerHTML]="sanitizer.bypassSecurityTrustHtml(resource.svg)"></div>
|
|
86
|
+
<span>{{ resource.title }}</span>
|
|
87
|
+
</a>
|
|
88
|
+
</ng-template>
|
|
89
|
+
</skeletonizer-skeleton>
|
|
90
|
+
`,
|
|
91
|
+
styleUrls: ['./my-component.component.scss'],
|
|
92
|
+
// for standalone components, otherwise add SkeletonizerSkeletonComponent to the module imports of the module where MyComponent is declared
|
|
93
|
+
// standalone: true,
|
|
94
|
+
// imports: [SkeletonizerSkeletonComponent, NgFor], // ngFor is just for the sake of the example where the *ngFor is used in the template
|
|
95
|
+
})
|
|
96
|
+
export class MyComponent extends SkeletonAbstractComponent<TSkeletonizedPart> implements OnInit {
|
|
97
|
+
public pageTitle: string = 'Some prop that we do not wish to skeletonize, but wish to use in the view nonetheless';
|
|
98
|
+
public otherPropWeWantToUseInSkeletonizedPart: string = 'angular';
|
|
99
|
+
|
|
100
|
+
public resources: IResource[] = [];
|
|
101
|
+
public showSkeleton: boolean = true;
|
|
102
|
+
|
|
103
|
+
public readonly skeletonConfig: TSchemaConfig<TSkeletonizedPart> = {
|
|
104
|
+
repeat: 1,
|
|
105
|
+
schemaGenerator: () => ({
|
|
106
|
+
otherPropWeWantToUseInSkeletonizedPart: new SchemaItem<string>().words(3),
|
|
107
|
+
// Array.from({ length: 5 }, () => ({ ... })) is just a simple way of creating an array of 5 objects - you could also hardcode the array if you wanted to
|
|
108
|
+
resources: Array.from({ length: 5 }, () => ({
|
|
109
|
+
title: new SchemaItem<string>().words(3),
|
|
110
|
+
link: new SchemaItem().identical('https://www.google.com'),
|
|
111
|
+
svg: new SchemaItem().identical(loadingSvg),
|
|
112
|
+
})),
|
|
113
|
+
}),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
public constructor(
|
|
117
|
+
public readonly sanitizer: DomSanitizer,
|
|
118
|
+
) {
|
|
119
|
+
super();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public ngOnInit(): void {
|
|
123
|
+
// simulate loading data
|
|
124
|
+
setTimeout(() => {
|
|
125
|
+
this.resources = [
|
|
126
|
+
{
|
|
127
|
+
title: 'Mocked Resolved Resource #1',
|
|
128
|
+
link: 'https://github.com/lukaVarga/skeletonizer/tree/main/packages/angular/projects/skeletonizer',
|
|
129
|
+
svg: learnNgSvg,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
title: 'Mocked Resolved Resource #2',
|
|
133
|
+
link: 'https://github.com/lukaVarga/skeletonizer/tree/main',
|
|
134
|
+
svg: cliDocsSvg,
|
|
135
|
+
},
|
|
136
|
+
];
|
|
137
|
+
|
|
138
|
+
this.otherPropWeWantToUseInSkeletonizedPart = 'loaded title'
|
|
139
|
+
|
|
140
|
+
this.showSkeleton = false;
|
|
141
|
+
}, Math.max(3_000, Math.random() * 10_000));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
You can also skeletonize multiple independent parts (ie. parts for which the data gets loaded independently of each-other) of the same component by using the `<skeletonizer-skeleton>` multiple times in the template. Each `skeletonizer-skeleton` component should, in this case, receive its own `showSkeleton` input property, while the `config` and the `scope` can be shared for all of them. That way, the config is defined in a single place and all skeletonized parts enjoy the same level of type safety via the known `proxy(context)` method.
|
|
147
|
+
You can also provide separate config and scope for each `skeletonizer-skeleton` component if needed, although it is recommended that you do not extend `SkeletonAbstractComponent` in this case, and you will need to provide your own (separate) `proxy`-like methods for each of the skeletonized parts of the component to maintain the same level of type safety in the template.
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { Component } from '@angular/core';
|
|
151
|
+
import { SkeletonizerSkeletonComponent } from '@skeletonizer/angular';
|
|
152
|
+
import { SchemaItem, SkeletonAbstractComponent, TSchemaConfig } from '@skeletonizer/utils';
|
|
153
|
+
import { DomSanitizer } from '@angular/platform-browser';
|
|
154
|
+
|
|
155
|
+
interface IResource {
|
|
156
|
+
title: string;
|
|
157
|
+
link: string;
|
|
158
|
+
svg: string;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// the svgs are just for the sake of the example
|
|
162
|
+
const learnNgSvg: string = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M5 13.18v4L12 21l7-3.82v-4L12 17l-7-3.82zM12 3L1 9l11 6 9-4.91V17h2V9L12 3z"/></svg>';
|
|
163
|
+
|
|
164
|
+
const loadingSvg: string = '<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><style>.spinner{transform-origin:center;animation:spinner .75s linear infinite}@keyframes spinner{from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style><g class="spinner"><circle cx="12" cy="2.5" r="1.5" opacity=".14"/><circle cx="16.75" cy="3.77" r="1.5" opacity=".29"/><circle cx="20.23" cy="7.25" r="1.5" opacity=".43"/><circle cx="21.50" cy="12.00" r="1.5" opacity=".57"/><circle cx="20.23" cy="16.75" r="1.5" opacity=".71"/><circle cx="16.75" cy="20.23" r="1.5" opacity=".86"/><circle cx="12" cy="21.5" r="1.5"/></g></svg>';
|
|
165
|
+
|
|
166
|
+
type TSkeletonizedPart = Pick<AppComponent, 'resources' | 'otherPropWeWantToUseInSkeletonizedPart'>;
|
|
167
|
+
|
|
168
|
+
@Component({
|
|
169
|
+
selector: 'my-component',
|
|
170
|
+
template: `
|
|
171
|
+
<h2>{{ pageTitle }}</h2>
|
|
172
|
+
|
|
173
|
+
<skeletonizer-skeleton [showSkeleton]="showSkeleton" [config]="skeletonConfig" [scope]="{ resources, otherPropWeWantToUseInSkeletonizedPart }">
|
|
174
|
+
<ng-template let-context>
|
|
175
|
+
<div class="card-container">
|
|
176
|
+
<a *ngFor="let resource of proxy(context).resources" class="card" target="_blank" rel="noopener" [href]="resource.link">
|
|
177
|
+
<div [innerHTML]="sanitizer.bypassSecurityTrustHtml(resource.svg)"></div>
|
|
178
|
+
<span>{{ resource.title }}</span>
|
|
179
|
+
</a>
|
|
180
|
+
</div>
|
|
181
|
+
</ng-template>
|
|
182
|
+
</skeletonizer-skeleton>
|
|
183
|
+
|
|
184
|
+
<skeletonizer-skeleton [showSkeleton]="showOtherSkeleton" [config]="skeletonConfig" [scope]="{ resources, otherPropWeWantToUseInSkeletonizedPart }">
|
|
185
|
+
<ng-template let-context>
|
|
186
|
+
<span>{{ proxy(context).otherPropWeWantToUseInSkeletonizedPart }}</span>
|
|
187
|
+
</ng-template>
|
|
188
|
+
</skeletonizer-skeleton>
|
|
189
|
+
`,
|
|
190
|
+
styleUrls: ['./my-component.component.scss'],
|
|
191
|
+
// for standalone components, otherwise add SkeletonizerSkeletonComponent to the module imports of the module where MyComponent is declared
|
|
192
|
+
// standalone: true,
|
|
193
|
+
// imports: [SkeletonizerSkeletonComponent, NgFor], // ngFor is just for the sake of the example where the *ngFor is used in the template
|
|
194
|
+
})
|
|
195
|
+
export class MyComponent extends SkeletonAbstractComponent<TSkeletonizedPart> implements OnInit {
|
|
196
|
+
public pageTitle: string = 'Some prop that we do not wish to skeletonize, but wish to use in the view nonetheless';
|
|
197
|
+
public otherPropWeWantToUseInSkeletonizedPart: string = 'angular';
|
|
198
|
+
|
|
199
|
+
public resources: IResource[] = [];
|
|
200
|
+
public showSkeleton: boolean = true;
|
|
201
|
+
|
|
202
|
+
public readonly skeletonConfig: TSchemaConfig<TSkeletonizedPart> = {
|
|
203
|
+
repeat: 1,
|
|
204
|
+
schemaGenerator: () => ({
|
|
205
|
+
otherPropWeWantToUseInSkeletonizedPart: new SchemaItem<string>().words(3),
|
|
206
|
+
// Array.from({ length: 5 }, () => ({ ... })) is just a simple way of creating an array of 5 objects - you could also hardcode the array if you wanted to
|
|
207
|
+
resources: Array.from({ length: 5 }, () => ({
|
|
208
|
+
title: new SchemaItem<string>().words(3),
|
|
209
|
+
link: new SchemaItem().identical('https://www.google.com'),
|
|
210
|
+
svg: new SchemaItem().identical(loadingSvg),
|
|
211
|
+
})),
|
|
212
|
+
}),
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
public constructor(
|
|
216
|
+
public readonly sanitizer: DomSanitizer,
|
|
217
|
+
) {
|
|
218
|
+
super();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public ngOnInit(): void {
|
|
222
|
+
setTimeout(() => {
|
|
223
|
+
this.resources = [
|
|
224
|
+
{
|
|
225
|
+
title: 'Mocked Resolved Resource #1',
|
|
226
|
+
link: 'https://github.com/lukaVarga/skeletonizer/tree/main/packages/angular/projects/skeletonizer',
|
|
227
|
+
svg: learnNgSvg,
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
title: 'Mocked Resolved Resource #2',
|
|
231
|
+
link: 'https://github.com/lukaVarga/skeletonizer/tree/main',
|
|
232
|
+
svg: cliDocsSvg,
|
|
233
|
+
},
|
|
234
|
+
];
|
|
235
|
+
|
|
236
|
+
this.showSkeleton = false;
|
|
237
|
+
}, Math.max(3_000, Math.random() * 10_000));
|
|
238
|
+
|
|
239
|
+
setTimeout(() => {
|
|
240
|
+
this.showOtherSkeleton = false;
|
|
241
|
+
}, Math.max(6_000, Math.random() * 10_000));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Color Scheme
|
|
247
|
+
Generally speaking, you shouldn't need to adjust the color scheme of the skeletonized component in most cases. However, should you need to, the color scheme of the skeletonized views can be customized by providing the `colorScheme` property to the `SkeletonizerSkeletonComponent`.
|
|
248
|
+
|
|
249
|
+
For more details about the `colorScheme` property, see the [colorScheme](/packages/utils/README.md#colorScheme) section.
|
|
250
|
+
|
|
251
|
+
## Contributing
|
|
252
|
+
For Angular adapter-specific contributions, run the following commands to get started:
|
|
253
|
+
- `npm install`
|
|
254
|
+
- adjust the code in the `packages/angular` directory
|
|
255
|
+
- run `npm run build` in the `packages/angular` directory
|
|
256
|
+
- adjust the code in the `packages/angular/src/app` directory to make sure the changes can easily be seen in the example app
|
|
257
|
+
- `npm run dev` in the `packages/angular` directory to start the example app
|
|
258
|
+
- update readme file in the `packages/angular` directory
|
|
259
|
+
|
|
260
|
+
Before submitting a pull request, make sure to run the following commands in `packages/angular` directory:
|
|
261
|
+
- `npm run lint`
|
|
262
|
+
- `npm run type-check`
|
|
263
|
+
- `npm run coverage`
|
|
264
|
+
- `npm run build`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skeletonizer/angular",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9-alpha.0",
|
|
4
4
|
"description": "The way to skeletonize your Angular components",
|
|
5
5
|
"author": "Luka Varga",
|
|
6
6
|
"license": "MIT",
|
|
@@ -88,5 +88,5 @@
|
|
|
88
88
|
"ng-packagr": "^17.3.0",
|
|
89
89
|
"typescript": "~5.4.3"
|
|
90
90
|
},
|
|
91
|
-
"gitHead": "
|
|
91
|
+
"gitHead": "0e08a76697be0fd52ee105c366bdca6c43145ce0"
|
|
92
92
|
}
|