@skeletonizer/vue
Advanced tools
Comparing version 0.0.23-alpha.0 to 1.0.0
{ | ||
"name": "@skeletonizer/vue", | ||
"version": "0.0.23-alpha.0", | ||
"version": "1.0.0", | ||
"description": "The way to skeletonize your Vue.js components", | ||
@@ -81,3 +81,3 @@ "author": "Luka Varga", | ||
}, | ||
"gitHead": "e93fde814bee1e5c1488c7bc195057a74b7d2d91" | ||
"gitHead": "b991aef461188aebee063efb7df47e70c58b0772" | ||
} |
265
README.md
@@ -11,3 +11,3 @@ # @skeletonizer/vue | ||
## main.ts | ||
In your `main.ts` file (where you do `createApp`), you need to install the `SkeletonizerPlugin`. The plugin provides the `SkeletonizerSkeleton` component and directive that will be used in your Vue components. | ||
In your `main.ts` file (where you do `createApp`), you need to install the `SkeletonizerPlugin` and import the css needed for the plugin. The plugin provides the `SkeletonizerSkeleton` component and directive that will be used in your Vue components. | ||
@@ -18,2 +18,3 @@ ```typescript | ||
import { SkeletonizerPlugin } from '@skeletonizer/vue'; | ||
import '@skeletonizer/vue/dist/index.css'; | ||
@@ -26,8 +27,14 @@ createApp(App) | ||
## Component Adjustments | ||
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. The `skeletonizer.skeletonConfig`, `skeletonizer.showSkeleton` and `scope` properties must be passed to the `<skeletonizer-skeleton>` as inputs. | ||
Furthermore, all the data that you wish to access in the skeletonized part of the template must be accessed through the `skeletonizer.proxy(context)` method, except the data that you provide and is available even whilst the data is being loaded (ie. `:show-skeleton="true"`).. | ||
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. Additionally, you will need to pass the following to `skeletonizer-skeleton`: | ||
- `v-slot="{ scope }"` | ||
- `:config="skeletonizer.skeletonConfig"` | ||
- `:show-skeleton="skeletonizer.showSkeleton"` | ||
- `:scope="{ foo, bar }"` | ||
All the data that you wish to access in the skeletonized part of the template must be provided in the `:scope` and accessed through the `skeletonizer.proxy(scope)` method, except the data that you provide and is available even whilst the data is being loaded (ie. `:show-skeleton="true"`). For example, if you have a `message` prop that you wish to be skeletonized, you'll need to pass it to the `:scope` object and access it through the `skeletonizer.proxy(scope).message` method. | ||
As far as the template goes, it essentially means transforming the code from this: | ||
```html | ||
<div>{{ somePropOrMethodCallAvailableAsync }}</div> | ||
<div>{{ otherAsyncProp }}</div> | ||
<div>{{ someAlreadyHardCodedOrInputBoundPropAvailableSync }} </div> | ||
@@ -39,4 +46,10 @@ ``` | ||
```html | ||
<skeletonizer-skeleton [showSkeleton]="showSkeleton" [config]="skeletonConfig" [scope]="{ somePropOrMethodCallAvailableAsync } "> | ||
<div>{{ proxy(context).somePropOrMethodCallAvailableAsync }}</div> | ||
<skeletonizer-skeleton | ||
v-slot="{ scope }" | ||
:config="skeletonizer.skeletonConfig" | ||
:show-skeleton="skeletonizer.showSkeleton" | ||
:scope="{ somePropOrMethodCallAvailableAsync, otherAsyncProp }" | ||
> | ||
<div>{{ skeletonizer.proxy(scope).somePropOrMethodCallAvailableAsync }}</div> | ||
<div>{{ skeletonizer.proxy(scope).otherAsyncProp }}</div> | ||
<div>{{ someAlreadyHardCodedOrInputBoundPropAvailableSync }} </div> | ||
@@ -49,17 +62,11 @@ </skeletonizer-skeleton> | ||
## Usage | ||
You can use Skeletonizer either in a standalone component or in a component that is a part of a module. | ||
Every component that uses Skeletonizer should use `SkeletonizerComponentComposable`, which is available in `@skeletonizer/vue`. Both the component and the `SkeletonizerComponentComposable` are type-safe - if the shape of `schemaGenerator` returned object does not match the shape of type generic you pass to `SkeletonizerComponentComposable`, you will get a type error. You will also get an error if you fail to pass the same shape to the `:scope` input bound property of `skeletonizer-skeleton` component. While Vue has made noticeable strides in type safety, it is still lacking a bit compared to eg. Angular. Therefor, you may still need to run a `npm run type-check` command (which, in a standard vue/vite project, executes `vue-tsc --noEmit -p tsconfig.vitest.json --composite false`) if you wish to get the extra type safety. | ||
If you wish to use Skeletonizer in a standalone component, you need to add `SkeletonizerSkeletonComponent` in the imports of the component. | ||
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. | ||
You can use multiple separate, independently skeletonized parts within the same component. In that case, you can: | ||
- provide separate `:scope`, `:show-skeleton` and `:config` properties for each `skeletonizer-skeleton` component. For easier maintenance, it is still recommended that you prefix (or suffix) all of the referenced composables with `skeletonizer` (eg. `profileSkeletonizer`, or `skeletonizerUsersList`) to make it clear that they are part of the skeletonized view. In that case, you should obviously access each `.proxy` method with the corresponding composables (eg. `profileSkeletonizer.proxy(scope)`). | ||
- provide the same `:scope` and `:config` property to all `skeletonizer-skeleton` components, with separate `:show-skeleton` properties for each `skeletonizer-skeleton` component. | ||
Every component that uses Skeletonizer should extend `SkeletonAbstractComponent`, which is available in `@skeletonizer/utils`. | ||
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**. | ||
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. | ||
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**. | ||
In the skeletonized part of the template, you **must** access the data through the `skeletonizerFoo.proxy(scope)` method. | ||
You can think of `skeletonizerFoo.proxy(scope)` in the same way as you would think of `this` in a class method. The only difference is that when using `skeletonizerFoo.proxy(scope)`, 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. | ||
In the skeletonized part of the template, you **must** access the data through the `proxy(context)` method. | ||
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. | ||
For more details about the `SkeletonAbstractComponent`, see the [SkeletonAbstractComponent](/packages/utils/README.md#skeletonabstractcomponent) section. | ||
For more details about the `SchemaItem` property, see the [SchemaItem](/packages/utils/README.md#schemaitem) section. | ||
@@ -71,6 +78,7 @@ | ||
```typescript | ||
import { Component } from '@angular/core'; | ||
import { SkeletonizerSkeletonComponent } from '@skeletonizer/angular'; | ||
import { SchemaItem, SkeletonAbstractComponent, TSchemaConfig } from '@skeletonizer/utils'; | ||
import { DomSanitizer } from '@angular/platform-browser'; | ||
// <script setup lang="ts"> | ||
import { SkeletonizerComponentComposable, SkeletonizerSkeleton } from '@skeletonizer/vue'; | ||
import { SchemaItem } from '@skeletonizer/utils'; | ||
import { type Ref, ref } from 'vue'; | ||
import assetImgUrl from '@/assets/logo.svg'; | ||
@@ -84,196 +92,63 @@ interface IResource { | ||
// the svgs are just for the sake of the example | ||
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>'; | ||
const supportSvg: string = ` | ||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor"> | ||
<path | ||
d="M10 3.22l-.61-.6a5.5 5.5 0 0 0-7.666.105 5.5 5.5 0 0 0-.114 7.665L10 18.78l8.39-8.4a5.5 5.5 0 0 0-.114-7.665 5.5 5.5 0 0 0-7.666-.105l-.61.61z" | ||
/> | ||
</svg> | ||
`; | ||
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>'; | ||
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>'; | ||
type TSkeletonizedPart = Pick<AppComponent, 'resources' | 'otherPropWeWantToUseInSkeletonizedPart'>; | ||
const pageTitle: string = 'Some prop that we do not wish to skeletonize, but wish to use in the view nonetheless'; | ||
const message: Ref<string> = ref('vue'); | ||
const resources: Ref<IResource[]> = ref([]); | ||
@Component({ | ||
selector: 'my-component', | ||
template: ` | ||
<h2>{{ pageTitle }}</h2> | ||
type TSkeletonized = { message: string, resources: IResource[] }; | ||
<skeletonizer-skeleton [showSkeleton]="showSkeleton" [config]="skeletonConfig" [scope]="{ resources, otherPropWeWantToUseInSkeletonizedPart }"> | ||
<ng-template let-context> | ||
<span>{{ proxy(context).otherPropWeWantToUseInSkeletonizedPart }}</span> | ||
<a *ngFor="let resource of proxy(context).resources" target="_blank" rel="noopener" [href]="resource.link"> | ||
<div [innerHTML]="sanitizer.bypassSecurityTrustHtml(resource.svg)"></div> | ||
<span>{{ resource.title }}</span> | ||
</a> | ||
</ng-template> | ||
</skeletonizer-skeleton> | ||
`, | ||
styleUrls: ['./my-component.component.scss'], | ||
// for standalone components, otherwise add SkeletonizerSkeletonComponent to the module imports of the module where MyComponent is declared | ||
// standalone: true, | ||
// imports: [SkeletonizerSkeletonComponent, NgFor], // ngFor is just for the sake of the example where the *ngFor is used in the template | ||
}) | ||
export class MyComponent extends SkeletonAbstractComponent<TSkeletonizedPart> implements OnInit { | ||
public pageTitle: string = 'Some prop that we do not wish to skeletonize, but wish to use in the view nonetheless'; | ||
public otherPropWeWantToUseInSkeletonizedPart: string = 'angular'; | ||
public resources: IResource[] = []; | ||
public showSkeleton: boolean = true; | ||
public readonly skeletonConfig: TSchemaConfig<TSkeletonizedPart> = { | ||
repeat: 1, | ||
// if you call this const eg. `skeletonizerFoo`, you should access the skeletonized props in the template with `skeletonizerFoo.proxy(scope).someProp` | ||
// if you need multiple separate skeletonized parts, you should create multiple separate composables using `SkeletonizerComponentComposable.generate` and provide separate `:scope`, `:show-skeleton` and `:config` properties for each `skeletonizer-skeleton` component. | ||
const skeletonizer: SkeletonizerComponentComposable<TSkeletonized> = SkeletonizerComponentComposable.generate<TSkeletonized>( | ||
{ | ||
repeat: 3, | ||
schemaGenerator: () => ({ | ||
otherPropWeWantToUseInSkeletonizedPart: new SchemaItem<string>().words(3), | ||
// 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 | ||
resources: Array.from({ length: 5 }, () => ({ | ||
title: new SchemaItem<string>().words(3), | ||
link: new SchemaItem().identical('https://www.google.com'), | ||
message: new SchemaItem().words(5), | ||
resources: Array.from({ length: 3 }, () => ({ | ||
title: new SchemaItem().words(2), | ||
link: new SchemaItem().identical('https://vuejs.org/'), | ||
svg: new SchemaItem().identical(loadingSvg), | ||
})), | ||
})) | ||
}), | ||
}; | ||
}, | ||
true, | ||
); | ||
public constructor( | ||
public readonly sanitizer: DomSanitizer, | ||
) { | ||
super(); | ||
} | ||
setTimeout(() => { | ||
message.value = 'Async update of message'; | ||
public ngOnInit(): void { | ||
// simulate loading data | ||
setTimeout(() => { | ||
this.resources = [ | ||
{ | ||
title: 'Mocked Resolved Resource #1', | ||
link: 'https://github.com/lukaVarga/skeletonizer/tree/main/packages/angular/projects/skeletonizer', | ||
svg: learnNgSvg, | ||
}, | ||
{ | ||
title: 'Mocked Resolved Resource #2', | ||
link: 'https://github.com/lukaVarga/skeletonizer/tree/main', | ||
svg: cliDocsSvg, | ||
}, | ||
]; | ||
resources.value = [ | ||
{ title: 'Vue', link: 'https://vuejs.org/', svg: assetImgUrl }, | ||
{ title: 'Vite', link: 'https://vitejs.dev/', svg: supportSvg }, | ||
]; | ||
this.otherPropWeWantToUseInSkeletonizedPart = 'loaded title' | ||
skeletonizer.showSkeleton = false; | ||
}, 5000 * Math.random()); | ||
this.showSkeleton = false; | ||
}, Math.max(3_000, Math.random() * 10_000)); | ||
} | ||
} | ||
// </script> | ||
``` | ||
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. | ||
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. | ||
```typescript | ||
import { Component } from '@angular/core'; | ||
import { SkeletonizerSkeletonComponent } from '@skeletonizer/angular'; | ||
import { SchemaItem, SkeletonAbstractComponent, TSchemaConfig } from '@skeletonizer/utils'; | ||
import { DomSanitizer } from '@angular/platform-browser'; | ||
interface IResource { | ||
title: string; | ||
link: string; | ||
svg: string; | ||
} | ||
// the svgs are just for the sake of the example | ||
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>'; | ||
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>'; | ||
type TSkeletonizedPart = Pick<AppComponent, 'resources' | 'otherPropWeWantToUseInSkeletonizedPart'>; | ||
@Component({ | ||
selector: 'my-component', | ||
template: ` | ||
<h2>{{ pageTitle }}</h2> | ||
<skeletonizer-skeleton [showSkeleton]="showSkeleton" [config]="skeletonConfig" [scope]="{ resources, otherPropWeWantToUseInSkeletonizedPart }"> | ||
<ng-template let-context> | ||
<div class="card-container"> | ||
<a *ngFor="let resource of proxy(context).resources" class="card" target="_blank" rel="noopener" [href]="resource.link"> | ||
<div [innerHTML]="sanitizer.bypassSecurityTrustHtml(resource.svg)"></div> | ||
<span>{{ resource.title }}</span> | ||
</a> | ||
</div> | ||
</ng-template> | ||
</skeletonizer-skeleton> | ||
<skeletonizer-skeleton [showSkeleton]="showOtherSkeleton" [config]="skeletonConfig" [scope]="{ resources, otherPropWeWantToUseInSkeletonizedPart }"> | ||
<ng-template let-context> | ||
<span>{{ proxy(context).otherPropWeWantToUseInSkeletonizedPart }}</span> | ||
</ng-template> | ||
</skeletonizer-skeleton> | ||
`, | ||
styleUrls: ['./my-component.component.scss'], | ||
// for standalone components, otherwise add SkeletonizerSkeletonComponent to the module imports of the module where MyComponent is declared | ||
// standalone: true, | ||
// imports: [SkeletonizerSkeletonComponent, NgFor], // ngFor is just for the sake of the example where the *ngFor is used in the template | ||
}) | ||
export class MyComponent extends SkeletonAbstractComponent<TSkeletonizedPart> implements OnInit { | ||
public pageTitle: string = 'Some prop that we do not wish to skeletonize, but wish to use in the view nonetheless'; | ||
public otherPropWeWantToUseInSkeletonizedPart: string = 'angular'; | ||
public resources: IResource[] = []; | ||
public showSkeleton: boolean = true; | ||
public readonly skeletonConfig: TSchemaConfig<TSkeletonizedPart> = { | ||
repeat: 1, | ||
schemaGenerator: () => ({ | ||
otherPropWeWantToUseInSkeletonizedPart: new SchemaItem<string>().words(3), | ||
// 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 | ||
resources: Array.from({ length: 5 }, () => ({ | ||
title: new SchemaItem<string>().words(3), | ||
link: new SchemaItem().identical('https://www.google.com'), | ||
svg: new SchemaItem().identical(loadingSvg), | ||
})), | ||
}), | ||
}; | ||
public constructor( | ||
public readonly sanitizer: DomSanitizer, | ||
) { | ||
super(); | ||
} | ||
public ngOnInit(): void { | ||
setTimeout(() => { | ||
this.resources = [ | ||
{ | ||
title: 'Mocked Resolved Resource #1', | ||
link: 'https://github.com/lukaVarga/skeletonizer/tree/main/packages/angular/projects/skeletonizer', | ||
svg: learnNgSvg, | ||
}, | ||
{ | ||
title: 'Mocked Resolved Resource #2', | ||
link: 'https://github.com/lukaVarga/skeletonizer/tree/main', | ||
svg: cliDocsSvg, | ||
}, | ||
]; | ||
this.showSkeleton = false; | ||
}, Math.max(3_000, Math.random() * 10_000)); | ||
setTimeout(() => { | ||
this.showOtherSkeleton = false; | ||
}, Math.max(6_000, Math.random() * 10_000)); | ||
} | ||
} | ||
``` | ||
### Color Scheme | ||
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`. | ||
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 `:color-scheme` property to the `<skeletonizer-skeleton>`. | ||
For more details about the `colorScheme` property, see the [colorScheme](/packages/utils/README.md#colorscheme) section. | ||
For more details about the `:color-scheme` property, see the [colorScheme](/packages/utils/README.md#colorscheme) section. | ||
## Contributing | ||
For Angular adapter-specific contributions, run the following commands to get started: | ||
For Vue adapter-specific contributions, run the following commands to get started: | ||
- `npm install` | ||
- adjust the code in the `packages/angular` directory | ||
- run `npm run build` in the `packages/angular` directory | ||
- adjust the code in the `packages/angular/src/app` directory to make sure the changes can easily be seen in the example app | ||
- `npm run dev` in the `packages/angular` directory to start the example app | ||
- update readme file in the `packages/angular` directory | ||
- adjust the code in the `packages/vue` directory | ||
- adjust the code in the `packages/vue/src/showcase` directory to make sure the changes can easily be seen in the example app | ||
- `npm run dev` in the `packages/vue` directory to start the example app | ||
- update readme file in the `packages/vue` directory | ||
Before submitting a pull request, make sure to run the following commands in `packages/angular` directory: | ||
Before submitting a pull request, make sure to run the following commands in `packages/vue` directory: | ||
- `npm run lint` | ||
@@ -280,0 +155,0 @@ - `npm run type-check` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
20300
151