Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor module to include a configuration when calling #forRoot #77

Merged
merged 13 commits into from
Jun 10, 2021
Merged
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/bazel-out

# dependencies
/node_modules
**/node_modules
willmendesneto marked this conversation as resolved.
Show resolved Hide resolved

# profiling files
chrome-profiler-events.json
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ After that, you can use the `ngx-skeleton-loader` components in your templates,

Also, you can import the module in your app by calling `NgxSkeletonLoaderModule.forRoot()` when adding it. So it will be available across your Angular application.

Importing the module this way also allows you to globally configure the default values for the `ngx-skeleton-loader` components in your application.
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved

```typescript
...
import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
Expand All @@ -98,7 +100,7 @@ import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
],
imports: [
...
NgxSkeletonLoaderModule.forRoot(),
NgxSkeletonLoaderModule.forRoot({ animation: 'pulse', loadingText: 'This item is actually loading...' }),
...
],
providers: [],
Expand All @@ -112,6 +114,7 @@ export class YourAppComponent {}
```html
<div class="item">
<ngx-skeleton-loader count="5" appearance="circle"></ngx-skeleton-loader>
<!-- above line will produce the rendering of 5 circles with the pulse animation and the aria-valuetext attribute set with "This item is actually loading..." -->
</div>
```

Expand All @@ -125,7 +128,8 @@ You can also define which appearance want to use in your skeleton loader by pass

### Options

- `''` - _default_: it will use it `''` as appearance. At the end, it will render like a line, but line is not a expected appearance to be passed;
- `''` - _default_: it will use it `''` as appearance. At the end, it will render like a line;
- `'line'`: it will render like a line. This is the same behavior than passing an empty string;
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved
- `circle`: it will use `circle` as appearance. Great for avatar skeletons, for example :);

## Animations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { NgxSkeletonLoaderConfigService } from './ngx-skeleton-loader-config.service';

describe('NgxSkeletonLoaderConfigService', () => {
let service: NgxSkeletonLoaderConfigService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NgxSkeletonLoaderConfigService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Inject, Injectable, Optional } from '@angular/core';
import {
DEFAULT_NGX_SKELETON_LOADER_CONFIG,
NgxSkeletonLoaderConfig,
NGX_SKELETON_LOADER_CONFIG,
} from './ngx-skeleton-loader-config.types';

@Injectable({
providedIn: 'root',
})
export class NgxSkeletonLoaderConfigService {
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved
readonly config: NgxSkeletonLoaderConfig = DEFAULT_NGX_SKELETON_LOADER_CONFIG;

constructor(@Inject(NGX_SKELETON_LOADER_CONFIG) @Optional() config: NgxSkeletonLoaderConfig) {
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved
if (config) {
this.config = config;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { InjectionToken } from '@angular/core';

export interface NgxSkeletonLoaderConfig {
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved
appearance?: 'circle' | 'line' | '';
animation?: 'progress' | 'progress-dark' | 'pulse' | 'false' | false;
theme?: {
// This is required since ngStyle is using `any` as well
// More details in https://angular.io/api/common/NgStyle
// tslint:disable-next-line: no-any
[k: string]: any;
};
loadingText?: string;
count?: number;
}

export const DEFAULT_NGX_SKELETON_LOADER_CONFIG = {
appearance: 'line',
animation: 'progress',
theme: {},
loadingText: 'Loading...',
count: 1,
} as NgxSkeletonLoaderConfig;
willmendesneto marked this conversation as resolved.
Show resolved Hide resolved

export const NGX_SKELETON_LOADER_CONFIG = new InjectionToken<NgxSkeletonLoaderConfig>('ngx-skeleton-loader.config');
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('NgxSkeletonLoaderComponent', () => {
it('should console errors if `appearance` is an invalid option and is running in development mode', () => {
expect(console.error).toHaveBeenCalledWith(
// tslint:disable-next-line: max-line-length
`\`NgxSkeletonLoaderComponent\` need to receive 'appearance' as: circle or empty string. Forcing default to "''".`,
`\`NgxSkeletonLoaderComponent\` need to receive 'appearance' as: circle or line or empty string. Forcing default to "''".`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
SimpleChanges,
} from '@angular/core';
import { start, end } from 'perf-marks/marks';
import { NgxSkeletonLoaderConfigService } from './ngx-skeleton-loader-config.service';
import { NgxSkeletonLoaderConfig } from './ngx-skeleton-loader-config.types';

@Component({
selector: 'ngx-skeleton-loader',
Expand All @@ -24,25 +26,25 @@ export class NgxSkeletonLoaderComponent implements OnInit, AfterViewInit, OnDest
static ngAcceptInputType_animation: boolean | string;

@Input()
count = 1;
count: NgxSkeletonLoaderConfig['count'] = this.configService.config.count;
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved

@Input()
loadingText = 'Loading...';
loadingText: NgxSkeletonLoaderConfig['loadingText'] = this.configService.config.loadingText;

@Input()
appearance: 'circle' | '' = '';
appearance: NgxSkeletonLoaderConfig['appearance'] = this.configService.config.appearance;

@Input()
animation: 'progress' | 'progress-dark' | 'pulse' | 'false' | false = 'progress';
animation: NgxSkeletonLoaderConfig['animation'] = this.configService.config.animation;

// This is required since ngStyle is using `any` as well
// More details in https://angular.io/api/common/NgStyle
// tslint:disable-next-line: no-any
@Input() theme: { [k: string]: any } = {};
@Input()
theme: NgxSkeletonLoaderConfig['theme'] = this.configService.config.theme;
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved

// tslint:disable-next-line: no-any
items: Array<any> = [];

constructor(private configService: NgxSkeletonLoaderConfigService) {}

ngOnInit() {
start('NgxSkeletonLoader:Rendered');
start('NgxSkeletonLoader:Loaded');
Expand All @@ -61,8 +63,7 @@ export class NgxSkeletonLoaderComponent implements OnInit, AfterViewInit, OnDest
}
this.count = 1;
}

this.items.length = this.count;
this.items.length = this.count || 1;
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved

const allowedAnimations = ['progress', 'progress-dark', 'pulse', 'false'];
if (allowedAnimations.indexOf(String(this.animation)) === -1) {
Expand All @@ -77,11 +78,11 @@ export class NgxSkeletonLoaderComponent implements OnInit, AfterViewInit, OnDest
this.animation = 'progress';
}

if (['circle', ''].indexOf(String(this.appearance)) === -1) {
if (['circle', 'line', ''].indexOf(String(this.appearance)) === -1) {
// Shows error message only in Development
if (isDevMode()) {
console.error(
`\`NgxSkeletonLoaderComponent\` need to receive 'appearance' as: circle or empty string. Forcing default to "''".`,
`\`NgxSkeletonLoaderComponent\` need to receive 'appearance' as: circle or line or empty string. Forcing default to "''".`,
);
}
this.appearance = '';
Expand Down
14 changes: 11 additions & 3 deletions projects/ngx-skeleton-loader/src/lib/ngx-skeleton-loader.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { NgxSkeletonLoaderComponent } from './ngx-skeleton-loader.component';
import { CommonModule } from '@angular/common';

import { NgxSkeletonLoaderComponent } from './ngx-skeleton-loader.component';
import {
DEFAULT_NGX_SKELETON_LOADER_CONFIG,
NgxSkeletonLoaderConfig,
NGX_SKELETON_LOADER_CONFIG,
} from './ngx-skeleton-loader-config.types';

@NgModule({
declarations: [NgxSkeletonLoaderComponent],
imports: [CommonModule],
exports: [NgxSkeletonLoaderComponent],
})

export class NgxSkeletonLoaderModule {
static forRoot(): ModuleWithProviders<NgxSkeletonLoaderModule> {
static forRoot(
config: NgxSkeletonLoaderConfig = DEFAULT_NGX_SKELETON_LOADER_CONFIG,
): ModuleWithProviders<NgxSkeletonLoaderModule> {
return {
ngModule: NgxSkeletonLoaderModule,
providers: [{ provide: NGX_SKELETON_LOADER_CONFIG, useValue: config }],
};
}
}
9 changes: 2 additions & 7 deletions projects/ngx-skeleton-loader/src/lib/ngx-skeleton-loader.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
left: 0;
width: 200px;
height: 100%;
content: '';
content: "";
HunteRoi marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -78,12 +78,7 @@

&.progress-dark {
&:before {
background-image: linear-gradient(
90deg,
transparent,
rgba(0, 0, 0, 0.2),
transparent
);
background-image: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.2), transparent);
}
}

Expand Down
1 change: 1 addition & 0 deletions projects/ngx-skeleton-loader/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

export * from './lib/ngx-skeleton-loader.component';
export * from './lib/ngx-skeleton-loader.module';
export * from './lib/ngx-skeleton-loader-config.types';