Run the CLI
Use the CLI to add the component to your project.
npx @ngzard/ui@latest add loaderThe Loader is a visual component that displays a loading animation to indicate that an action or process is in progress.
import { Component } from '@angular/core';
import { ZardLoaderComponent } from '../loader.component';
@Component({
selector: 'z-demo-loader-default',
imports: [ZardLoaderComponent],
standalone: true,
template: `<z-loader />`,
})
export class ZardDemoLoaderDefaultComponent {}
Use the CLI to add the component to your project.
npx @ngzard/ui@latest add loaderpnpm dlx @ngzard/ui@latest add loaderyarn dlx @ngzard/ui@latest add loaderbunx @ngzard/ui@latest add loaderCreate the component directory structure and add the following files to your project.
import { ChangeDetectionStrategy, Component, computed, input, ViewEncapsulation } from '@angular/core';
import type { ClassValue } from 'clsx';
import { loaderVariants, type ZardLoaderVariants } from './loader.variants';
import { mergeClasses } from '../../shared/utils/utils';
@Component({
selector: 'z-loader',
standalone: true,
template: `
<div class="relative top-1/2 left-1/2 h-[inherit] w-[inherit]">
@for (_ of bars; track $index) {
<div
class="animate-spinner absolute -top-[3.9%] -left-[10%] h-[8%] w-[24%] rounded-md bg-black dark:bg-white"
[style]="{
animationDelay: animationDelay($index),
transform: transform($index),
}"
></div>
}
</div>
`,
styles: `
@layer utilities {
@keyframes spinner {
0% {
opacity: 1;
}
100% {
opacity: 0.15;
}
}
.animate-spinner {
animation: spinner 1.2s linear infinite;
}
}
`,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
'[class]': 'classes()',
},
exportAs: 'zLoader',
})
export class ZardLoaderComponent {
readonly class = input<ClassValue>('');
readonly zSize = input<ZardLoaderVariants['zSize']>('default');
protected readonly bars = Array.from({ length: 12 });
protected readonly animationDelay = (index: number) => `-${1.3 - index * 0.1}s`;
protected readonly transform = (index: number) => `rotate(${30 * index}deg) translate(146%)`;
protected readonly classes = computed(() => mergeClasses(loaderVariants({ zSize: this.zSize() }), this.class()));
}
import { cva, type VariantProps } from 'class-variance-authority';
export const loaderVariants = cva('', {
variants: {
zSize: {
default: 'size-6',
sm: 'size-4',
lg: 'size-8',
},
},
defaultVariants: {
zSize: 'default',
},
});
export type ZardLoaderVariants = VariantProps<typeof loaderVariants>;
import { Component } from '@angular/core';
import { ZardLoaderComponent } from '../loader.component';
@Component({
selector: 'z-demo-loader-default',
imports: [ZardLoaderComponent],
standalone: true,
template: `<z-loader />`,
})
export class ZardDemoLoaderDefaultComponent {}
import { Component } from '@angular/core';
import { ZardLoaderComponent } from '../loader.component';
@Component({
selector: 'z-demo-loader-size',
imports: [ZardLoaderComponent],
standalone: true,
template: `
<z-loader zSize="sm" />
<z-loader zSize="default" />
<z-loader zSize="lg" />
`,
})
export class ZardDemoLoaderSizeComponent {}