ngx-skeleton-loader

repository·main·Indexed 20 days ago

https://github.com/willmendesneto/ngx-skeleton-loader

An Angular library for creating customizable, animated loading skeletons to improve user experience during loading states. It supports various shapes (line, circle, square, custom-content), multiple animation styles (progress, pulse), and is compatible with SSR and Angular 17+ deferrable views. The library provides flexible configuration via NgxSkeletonLoaderModule.forRoot(), provideNgxSkeletonLoader for standalone applications, and the NGX_SKELETON_LOADER_CONFIG injection token.

Tokens
4.7K
Snippets
20
Records
22
Agent score
70%

What's inside ngx-skeleton-loader

  1. Extend global theme using extendsFromRoot

    main

    By default, settings in NgxSkeletonLoaderModule.forRoot({ theme: { ... } }) override local [theme] inputs on the component. To allow local themes to merge with and extend the global theme, set extendsFromRoot: true in the global configuration. When enabled, local CSS attributes will override global ones, but other global attributes will be preserved.

    // Global configuration in NgModule
    NgxSkeletonLoaderModule.forRoot({
      theme: {
        extendsFromRoot: true,
        height: '30px',
      },
    })
    <!-- Uses height: 30px from root and background: blue from local -->
    <ngx-skeleton-loader [theme="{background: 'blue'}" />
    
    <!-- Uses height: 50px from local and background: red from local -->
    <ngx-skeleton-loader [theme="{height: '50px', background: 'red'}" />
  2. Run the demo and tests locally

    main

    To run the project locally for development or testing:

    • Run Demo: Use npm start to launch the Angular application. The demo will be available at http://localhost:4200.
    • Run Tests: Use npm test to execute the test suite. For continuous testing (watch mode), use npm run tdd.
    npm start
    npm test
    npm run tdd
  3. Setup for NgModule Applications

    main

    For applications using NgModule, import NgxSkeletonLoaderModule into your application module's imports array.

    ...
    import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
    ...
    
    @NgModule({
      declarations: [
        YourAppComponent
      ],
      imports: [
        ...
        NgxSkeletonLoaderModule,
        ...
      ],
      providers: [],
      bootstrap: [YourAppComponent]
    })
    
    export class YourAppComponent {}
  4. Setup for Standalone Applications

    main

    For Angular applications using standalone components, add provideNgxSkeletonLoader to your app.config.ts providers. You can optionally pass a global configuration object to define a default theme.

    // app.config.ts
    
    import { ApplicationConfig } from '@angular/core';
    import { provideNgxSkeletonLoader } from 'ngx-skeleton-loader';
    
    export const appConfig: ApplicationConfig = {
      providers: [
        provideNgxSkeletonLoader({
          theme: {
            extendsFromRoot: true,
            height: '30px',
          },
        }),
      ]
    };
  5. Customize appearance using CSS (Not Recommended)

    main

    You can apply styles via your component's CSS file using the :host selector and the deep combinator (>>>) to target the internal .skeleton-loader class.

    ⚠️ Warning: This approach is not encouraged because:

    1. It relies on :host DOM style scoping.
    2. It targets internal classes (.skeleton-loader), meaning any changes to the library's internal class names will break your application.
    /* In your component's CSS file */
    :host >>> ngx-skeleton-loader .skeleton-loader {
      border-radius: 5px;
      height: 50px;
      background-color: #992929;
      border: 1px solid white;
    }
  6. Configure ngx-skeleton-loader in a standalone Angular application

    main

    In Angular applications using standalone components (Angular 14+), you can configure the skeleton loader globally by adding provideNgxSkeletonLoader() to the providers array within your ApplicationConfig. This allows you to define global theme settings that apply to all skeleton loaders in the application.

    Available configuration options in the provider include:

    • theme: An object to define the default appearance.
      • extendsFromRoot: A boolean indicating if the theme should inherit from the root configuration.
      • height: A string defining the default height of the skeleton elements (e.g., '30px').
    import { provideNgxSkeletonLoader } from '../../projects/ngx-skeleton-loader/src/public-api';
    
    export const appConfig: ApplicationConfig = {
      providers: [
        provideNgxSkeletonLoader({
          theme: {
            extendsFromRoot: true,
            height: '30px',
          },
        }),
      ]
    };
  7. Use with Angular 17+ Deferrable Views

    main

    You can use ngx-skeleton-loader within the @placeholder block of Angular's @defer syntax to show a skeleton while the deferred content is being loaded.

    <div class="item">
      @defer {
        <my-item-view />
      } @placeholder (minimum 1000ms) {
        <ngx-skeleton-loader />
      }
    </div>
  8. Configure global defaults with NgxSkeletonLoaderModule.forRoot()

    main

    Use NgxSkeletonLoaderModule.forRoot() in your NgModule imports to set global default values for all <ngx-skeleton-loader> components in your application. This includes settings for animation, loadingText, and theme.

    ...
    import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
    ...
    
    @NgModule({
      declarations: [YourAppComponent],
      imports: [
        NgxSkeletonLoaderModule.forRoot({
          animation: 'pulse',
          loadingText: 'This item is actually loading...'
        }),
      ],
      providers: [],
      bootstrap: [YourAppComponent]
    })
    export class YourAppComponent {}
  9. Customize appearance using the [theme] attribute

    main

    You can define custom styles for the skeleton loader by passing an object to the [theme] attribute. This attribute supports standard CSS property names (kebab-case) and also supports Angular-style style object syntax (e.g., height.px for pixel values).

    Note: If you need to change the background color of the entire wrapper, you must apply those styles to the ngx-skeleton-loader component wrapper itself.

    <!-- Using kebab-case CSS properties -->
    <ngx-skeleton-loader
      count="5"
      [theme]="{ 
        'border-radius': '5px',
        'height': '50px',
        'background-color': '#992929',
        'border': '1px solid white'
      }"
    />
    
    <!-- Using Angular-style style object syntax -->
    <ngx-skeleton-loader
      count="5"
      [theme]="{ 
        'height.px': 50,
        'background-color': '#992929'
      }"
    />
  10. Configure square appearance dimensions

    main

    When using appearance="square", you can control the dimensions using these inputs:

    • size: A number representing both width and height. Defaults to 40.
    • measureUnit: The CSS unit for size. Defaults to px. Valid options: px, em, rem, %, vh, vw.
    <ngx-skeleton-loader appearance="square" size="100" measureUnit="px" />