@shimmer-from-structure/angular 2.2.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 +911 -0
- package/ng-package.json +8 -0
- package/package.json +46 -0
- package/src/public-api.ts +14 -0
- package/src/shimmer-config.service.test.ts +88 -0
- package/src/shimmer-config.service.ts +48 -0
- package/src/shimmer.component.test.ts +153 -0
- package/src/shimmer.component.ts +264 -0
- package/src/test/setup.ts +42 -0
- package/src/types.ts +35 -0
- package/tsconfig.json +26 -0
- package/vitest.config.ts +12 -0
package/ng-package.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shimmer-from-structure/angular",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Angular adapter for shimmer-from-structure",
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"@angular/core": "^19.0.0",
|
|
7
|
+
"@angular/common": "^19.0.0"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@shimmer-from-structure/core": "*"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@angular/compiler": "^19.0.0",
|
|
14
|
+
"@angular/compiler-cli": "^19.0.0",
|
|
15
|
+
"@angular/core": "^19.0.0",
|
|
16
|
+
"@angular/common": "^19.0.0",
|
|
17
|
+
"@angular/platform-browser": "^19.0.0",
|
|
18
|
+
"@angular/platform-browser-dynamic": "^19.0.0",
|
|
19
|
+
"ng-packagr": "^19.0.0",
|
|
20
|
+
"typescript": "~5.6.0",
|
|
21
|
+
"vitest": "^4.0.17",
|
|
22
|
+
"jsdom": "^27.4.0",
|
|
23
|
+
"@testing-library/dom": "^10.4.1",
|
|
24
|
+
"zone.js": "~0.15.0",
|
|
25
|
+
"rxjs": "~7.8.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "ng-packagr -p ng-package.json",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"format": "prettier --write .",
|
|
31
|
+
"prepublishOnly": "cp ../../README.md ."
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"angular",
|
|
35
|
+
"shimmer",
|
|
36
|
+
"skeleton",
|
|
37
|
+
"loading"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/darula-hpp/shimmer-from-structure.git",
|
|
42
|
+
"directory": "packages/angular"
|
|
43
|
+
},
|
|
44
|
+
"author": "Olebogeng Mbedzi",
|
|
45
|
+
"license": "MIT"
|
|
46
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Public API Surface
|
|
2
|
+
export { ShimmerComponent } from './shimmer.component';
|
|
3
|
+
export {
|
|
4
|
+
SHIMMER_CONFIG,
|
|
5
|
+
provideShimmerConfig,
|
|
6
|
+
injectShimmerConfig,
|
|
7
|
+
shimmerDefaults,
|
|
8
|
+
} from './shimmer-config.service';
|
|
9
|
+
export type {
|
|
10
|
+
ShimmerInputs,
|
|
11
|
+
ShimmerConfig,
|
|
12
|
+
ShimmerContextValue,
|
|
13
|
+
ElementInfo,
|
|
14
|
+
} from './types';
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { TestBed } from '@angular/core/testing';
|
|
3
|
+
import {
|
|
4
|
+
provideShimmerConfig,
|
|
5
|
+
injectShimmerConfig,
|
|
6
|
+
shimmerDefaults,
|
|
7
|
+
SHIMMER_CONFIG,
|
|
8
|
+
} from './shimmer-config.service';
|
|
9
|
+
|
|
10
|
+
describe('shimmer-config.service', () => {
|
|
11
|
+
describe('shimmerDefaults', () => {
|
|
12
|
+
it('exports default values', () => {
|
|
13
|
+
expect(shimmerDefaults).toBeDefined();
|
|
14
|
+
expect(shimmerDefaults.shimmerColor).toBeDefined();
|
|
15
|
+
expect(shimmerDefaults.backgroundColor).toBeDefined();
|
|
16
|
+
expect(shimmerDefaults.duration).toBeDefined();
|
|
17
|
+
expect(shimmerDefaults.fallbackBorderRadius).toBeDefined();
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('provideShimmerConfig', () => {
|
|
22
|
+
it('creates a provider object', () => {
|
|
23
|
+
const config = { shimmerColor: '#fff', duration: 2 };
|
|
24
|
+
const provider = provideShimmerConfig(config);
|
|
25
|
+
|
|
26
|
+
expect(provider.provide).toBe(SHIMMER_CONFIG);
|
|
27
|
+
expect(provider.useValue).toBe(config);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('injectShimmerConfig', () => {
|
|
32
|
+
it('returns defaults when no provider is configured', () => {
|
|
33
|
+
TestBed.configureTestingModule({});
|
|
34
|
+
|
|
35
|
+
TestBed.runInInjectionContext(() => {
|
|
36
|
+
const config = injectShimmerConfig();
|
|
37
|
+
|
|
38
|
+
expect(config.shimmerColor).toBe(shimmerDefaults.shimmerColor);
|
|
39
|
+
expect(config.backgroundColor).toBe(shimmerDefaults.backgroundColor);
|
|
40
|
+
expect(config.duration).toBe(shimmerDefaults.duration);
|
|
41
|
+
expect(config.fallbackBorderRadius).toBe(shimmerDefaults.fallbackBorderRadius);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('merges provided config with defaults', () => {
|
|
46
|
+
TestBed.configureTestingModule({
|
|
47
|
+
providers: [
|
|
48
|
+
provideShimmerConfig({
|
|
49
|
+
shimmerColor: 'rgba(255, 255, 255, 0.8)',
|
|
50
|
+
duration: 3,
|
|
51
|
+
}),
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
TestBed.runInInjectionContext(() => {
|
|
56
|
+
const config = injectShimmerConfig();
|
|
57
|
+
|
|
58
|
+
expect(config.shimmerColor).toBe('rgba(255, 255, 255, 0.8)');
|
|
59
|
+
expect(config.duration).toBe(3);
|
|
60
|
+
// Should use defaults for non-provided values
|
|
61
|
+
expect(config.backgroundColor).toBe(shimmerDefaults.backgroundColor);
|
|
62
|
+
expect(config.fallbackBorderRadius).toBe(shimmerDefaults.fallbackBorderRadius);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('returns fully resolved ShimmerContextValue', () => {
|
|
67
|
+
TestBed.configureTestingModule({
|
|
68
|
+
providers: [
|
|
69
|
+
provideShimmerConfig({
|
|
70
|
+
shimmerColor: '#aaa',
|
|
71
|
+
backgroundColor: '#bbb',
|
|
72
|
+
duration: 1.5,
|
|
73
|
+
fallbackBorderRadius: 10,
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
TestBed.runInInjectionContext(() => {
|
|
79
|
+
const config = injectShimmerConfig();
|
|
80
|
+
|
|
81
|
+
expect(config.shimmerColor).toBe('#aaa');
|
|
82
|
+
expect(config.backgroundColor).toBe('#bbb');
|
|
83
|
+
expect(config.duration).toBe(1.5);
|
|
84
|
+
expect(config.fallbackBorderRadius).toBe(10);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { InjectionToken, inject } from '@angular/core';
|
|
2
|
+
import type { ShimmerConfig, ShimmerContextValue } from '@shimmer-from-structure/core';
|
|
3
|
+
import { shimmerDefaults } from '@shimmer-from-structure/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Injection token for global shimmer configuration.
|
|
7
|
+
* Use `provideShimmerConfig()` to configure in your app.
|
|
8
|
+
*/
|
|
9
|
+
export const SHIMMER_CONFIG = new InjectionToken<ShimmerConfig>('SHIMMER_CONFIG');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Provider function for shimmer configuration.
|
|
13
|
+
* Use in your app's providers array.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* bootstrapApplication(AppComponent, {
|
|
18
|
+
* providers: [
|
|
19
|
+
* provideShimmerConfig({
|
|
20
|
+
* shimmerColor: 'rgba(255, 255, 255, 0.3)',
|
|
21
|
+
* duration: 1.5
|
|
22
|
+
* })
|
|
23
|
+
* ]
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function provideShimmerConfig(config: ShimmerConfig) {
|
|
28
|
+
return { provide: SHIMMER_CONFIG, useValue: config };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Inject and resolve shimmer configuration.
|
|
33
|
+
* Merges injected config with defaults.
|
|
34
|
+
* Returns fully resolved ShimmerContextValue with all properties defined.
|
|
35
|
+
*/
|
|
36
|
+
export function injectShimmerConfig(): ShimmerContextValue {
|
|
37
|
+
const config = inject(SHIMMER_CONFIG, { optional: true }) ?? {};
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
shimmerColor: config.shimmerColor ?? shimmerDefaults.shimmerColor,
|
|
41
|
+
backgroundColor: config.backgroundColor ?? shimmerDefaults.backgroundColor,
|
|
42
|
+
duration: config.duration ?? shimmerDefaults.duration,
|
|
43
|
+
fallbackBorderRadius: config.fallbackBorderRadius ?? shimmerDefaults.fallbackBorderRadius,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Re-export defaults for testing and reference
|
|
48
|
+
export { shimmerDefaults };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { Component } from '@angular/core';
|
|
3
|
+
import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
|
|
4
|
+
import { ShimmerComponent } from './shimmer.component';
|
|
5
|
+
import { provideShimmerConfig, SHIMMER_CONFIG } from './shimmer-config.service';
|
|
6
|
+
|
|
7
|
+
// Test wrapper component for content projection
|
|
8
|
+
@Component({
|
|
9
|
+
selector: 'test-host',
|
|
10
|
+
standalone: true,
|
|
11
|
+
imports: [ShimmerComponent],
|
|
12
|
+
template: `
|
|
13
|
+
<shimmer [loading]="loading">
|
|
14
|
+
<div class="test-content" style="width: 100px; height: 50px;">Content</div>
|
|
15
|
+
</shimmer>
|
|
16
|
+
`,
|
|
17
|
+
})
|
|
18
|
+
class TestHostComponent {
|
|
19
|
+
loading = true;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('ShimmerComponent', () => {
|
|
23
|
+
let fixture: ComponentFixture<TestHostComponent>;
|
|
24
|
+
let hostComponent: TestHostComponent;
|
|
25
|
+
|
|
26
|
+
beforeEach(async () => {
|
|
27
|
+
vi.useFakeTimers();
|
|
28
|
+
|
|
29
|
+
await TestBed.configureTestingModule({
|
|
30
|
+
imports: [TestHostComponent, ShimmerComponent],
|
|
31
|
+
}).compileComponents();
|
|
32
|
+
|
|
33
|
+
fixture = TestBed.createComponent(TestHostComponent);
|
|
34
|
+
hostComponent = fixture.componentInstance;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('renders children normally when loading=false', fakeAsync(() => {
|
|
38
|
+
hostComponent.loading = false;
|
|
39
|
+
fixture.detectChanges();
|
|
40
|
+
tick();
|
|
41
|
+
|
|
42
|
+
const content = fixture.nativeElement.querySelector('.test-content');
|
|
43
|
+
expect(content).toBeTruthy();
|
|
44
|
+
expect(content.textContent).toBe('Content');
|
|
45
|
+
|
|
46
|
+
// Should not have the measure container when not loading
|
|
47
|
+
const measureContainer = fixture.nativeElement.querySelector('.shimmer-measure-container');
|
|
48
|
+
expect(measureContainer).toBeFalsy();
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
it('renders shimmer structure when loading=true', fakeAsync(() => {
|
|
52
|
+
hostComponent.loading = true;
|
|
53
|
+
fixture.detectChanges();
|
|
54
|
+
tick();
|
|
55
|
+
|
|
56
|
+
// Should render the measure container
|
|
57
|
+
const measureContainer = fixture.nativeElement.querySelector('.shimmer-measure-container');
|
|
58
|
+
expect(measureContainer).toBeTruthy();
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
it('applies transparent text style to measure container', fakeAsync(() => {
|
|
62
|
+
hostComponent.loading = true;
|
|
63
|
+
fixture.detectChanges();
|
|
64
|
+
tick();
|
|
65
|
+
|
|
66
|
+
// Check that the measure container has the class
|
|
67
|
+
const measureContainer = fixture.nativeElement.querySelector('.shimmer-measure-container');
|
|
68
|
+
expect(measureContainer).toBeTruthy();
|
|
69
|
+
expect(measureContainer.classList.contains('shimmer-measure-container')).toBe(true);
|
|
70
|
+
}));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('ShimmerComponent with config provider', () => {
|
|
74
|
+
let fixture: ComponentFixture<TestHostComponent>;
|
|
75
|
+
|
|
76
|
+
beforeEach(async () => {
|
|
77
|
+
vi.useFakeTimers();
|
|
78
|
+
|
|
79
|
+
await TestBed.configureTestingModule({
|
|
80
|
+
imports: [TestHostComponent, ShimmerComponent],
|
|
81
|
+
providers: [
|
|
82
|
+
provideShimmerConfig({
|
|
83
|
+
shimmerColor: 'rgba(255, 0, 0, 0.5)',
|
|
84
|
+
backgroundColor: '#ff0000',
|
|
85
|
+
duration: 2,
|
|
86
|
+
fallbackBorderRadius: 8,
|
|
87
|
+
}),
|
|
88
|
+
],
|
|
89
|
+
}).compileComponents();
|
|
90
|
+
|
|
91
|
+
fixture = TestBed.createComponent(TestHostComponent);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('uses provided config values', fakeAsync(() => {
|
|
95
|
+
fixture.componentInstance.loading = true;
|
|
96
|
+
fixture.detectChanges();
|
|
97
|
+
tick();
|
|
98
|
+
|
|
99
|
+
// The config should be injected - we can verify by checking the SHIMMER_CONFIG token
|
|
100
|
+
const config = TestBed.inject(SHIMMER_CONFIG);
|
|
101
|
+
expect(config.shimmerColor).toBe('rgba(255, 0, 0, 0.5)');
|
|
102
|
+
expect(config.backgroundColor).toBe('#ff0000');
|
|
103
|
+
expect(config.duration).toBe(2);
|
|
104
|
+
expect(config.fallbackBorderRadius).toBe(8);
|
|
105
|
+
}));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('ShimmerComponent input overrides', () => {
|
|
109
|
+
@Component({
|
|
110
|
+
selector: 'test-override-host',
|
|
111
|
+
standalone: true,
|
|
112
|
+
imports: [ShimmerComponent],
|
|
113
|
+
template: `
|
|
114
|
+
<shimmer
|
|
115
|
+
[loading]="true"
|
|
116
|
+
[shimmerColor]="'#00ff00'"
|
|
117
|
+
[backgroundColor]="'#0000ff'"
|
|
118
|
+
[duration]="3"
|
|
119
|
+
[fallbackBorderRadius]="12"
|
|
120
|
+
>
|
|
121
|
+
<div>Content</div>
|
|
122
|
+
</shimmer>
|
|
123
|
+
`,
|
|
124
|
+
})
|
|
125
|
+
class TestOverrideHostComponent { }
|
|
126
|
+
|
|
127
|
+
beforeEach(async () => {
|
|
128
|
+
vi.useFakeTimers();
|
|
129
|
+
|
|
130
|
+
await TestBed.configureTestingModule({
|
|
131
|
+
imports: [TestOverrideHostComponent, ShimmerComponent],
|
|
132
|
+
providers: [
|
|
133
|
+
provideShimmerConfig({
|
|
134
|
+
shimmerColor: 'rgba(255, 0, 0, 0.5)',
|
|
135
|
+
duration: 1,
|
|
136
|
+
}),
|
|
137
|
+
],
|
|
138
|
+
}).compileComponents();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('component inputs override provider config', fakeAsync(() => {
|
|
142
|
+
const fixture = TestBed.createComponent(TestOverrideHostComponent);
|
|
143
|
+
fixture.detectChanges();
|
|
144
|
+
tick();
|
|
145
|
+
|
|
146
|
+
// The shimmer component should use input values over config
|
|
147
|
+
const shimmer = fixture.debugElement.children[0].componentInstance as ShimmerComponent;
|
|
148
|
+
expect(shimmer.resolvedShimmerColor()).toBe('#00ff00');
|
|
149
|
+
expect(shimmer.resolvedBackgroundColor()).toBe('#0000ff');
|
|
150
|
+
expect(shimmer.resolvedDuration()).toBe(3);
|
|
151
|
+
expect(shimmer.resolvedFallbackBorderRadius()).toBe(12);
|
|
152
|
+
}));
|
|
153
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Component,
|
|
3
|
+
input,
|
|
4
|
+
signal,
|
|
5
|
+
computed,
|
|
6
|
+
effect,
|
|
7
|
+
ElementRef,
|
|
8
|
+
viewChild,
|
|
9
|
+
AfterViewInit,
|
|
10
|
+
OnDestroy,
|
|
11
|
+
ChangeDetectionStrategy,
|
|
12
|
+
ViewEncapsulation,
|
|
13
|
+
} from '@angular/core';
|
|
14
|
+
import { NgIf, NgFor } from '@angular/common';
|
|
15
|
+
import {
|
|
16
|
+
extractElementInfo,
|
|
17
|
+
createResizeObserver,
|
|
18
|
+
type ElementInfo,
|
|
19
|
+
} from '@shimmer-from-structure/core';
|
|
20
|
+
import { injectShimmerConfig } from './shimmer-config.service';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Shimmer component that creates loading skeleton overlays based on content structure.
|
|
24
|
+
* Automatically measures projected content and creates matching shimmer blocks.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```html
|
|
28
|
+
* <shimmer [loading]="isLoading">
|
|
29
|
+
* <div class="card">
|
|
30
|
+
* <h2>{{ title }}</h2>
|
|
31
|
+
* <p>{{ description }}</p>
|
|
32
|
+
* </div>
|
|
33
|
+
* </shimmer>
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
@Component({
|
|
37
|
+
selector: 'shimmer',
|
|
38
|
+
standalone: true,
|
|
39
|
+
imports: [NgIf, NgFor],
|
|
40
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
41
|
+
encapsulation: ViewEncapsulation.None,
|
|
42
|
+
template: `
|
|
43
|
+
<div style="position: relative;">
|
|
44
|
+
<!-- Always render content -->
|
|
45
|
+
<div
|
|
46
|
+
#measureContainer
|
|
47
|
+
[class.shimmer-measure-container]="loading()"
|
|
48
|
+
[attr.aria-hidden]="loading() ? 'true' : null"
|
|
49
|
+
[style.pointer-events]="loading() ? 'none' : null"
|
|
50
|
+
>
|
|
51
|
+
<ng-content></ng-content>
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
<!-- Shimmer overlay - only when loading -->
|
|
55
|
+
@if (loading()) {
|
|
56
|
+
<div
|
|
57
|
+
style="
|
|
58
|
+
position: absolute;
|
|
59
|
+
top: 0;
|
|
60
|
+
left: 0;
|
|
61
|
+
right: 0;
|
|
62
|
+
bottom: 0;
|
|
63
|
+
overflow: hidden;
|
|
64
|
+
pointer-events: none;
|
|
65
|
+
"
|
|
66
|
+
>
|
|
67
|
+
@for (element of elements(); track $index) {
|
|
68
|
+
<div
|
|
69
|
+
[style.position]="'absolute'"
|
|
70
|
+
[style.left.px]="element.x"
|
|
71
|
+
[style.top.px]="element.y"
|
|
72
|
+
[style.width.px]="element.width"
|
|
73
|
+
[style.height.px]="element.height"
|
|
74
|
+
[style.backgroundColor]="resolvedBackgroundColor()"
|
|
75
|
+
[style.borderRadius]="
|
|
76
|
+
element.borderRadius === '0px'
|
|
77
|
+
? resolvedFallbackBorderRadius() + 'px'
|
|
78
|
+
: element.borderRadius
|
|
79
|
+
"
|
|
80
|
+
[style.overflow]="'hidden'"
|
|
81
|
+
>
|
|
82
|
+
<div
|
|
83
|
+
class="shimmer-animation-element"
|
|
84
|
+
[style.background]="
|
|
85
|
+
'linear-gradient(90deg, transparent, ' + resolvedShimmerColor() + ', transparent)'
|
|
86
|
+
"
|
|
87
|
+
[style.animationDuration]="resolvedDuration() + 's'"
|
|
88
|
+
></div>
|
|
89
|
+
</div>
|
|
90
|
+
}
|
|
91
|
+
</div>
|
|
92
|
+
}
|
|
93
|
+
</div>
|
|
94
|
+
`,
|
|
95
|
+
styles: [
|
|
96
|
+
`
|
|
97
|
+
:host {
|
|
98
|
+
display: contents;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.shimmer-measure-container * {
|
|
102
|
+
color: transparent !important;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
.shimmer-measure-container img,
|
|
106
|
+
.shimmer-measure-container svg,
|
|
107
|
+
.shimmer-measure-container video {
|
|
108
|
+
opacity: 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.shimmer-animation-element {
|
|
112
|
+
position: absolute;
|
|
113
|
+
top: 0;
|
|
114
|
+
left: 0;
|
|
115
|
+
width: 100%;
|
|
116
|
+
height: 100%;
|
|
117
|
+
animation: shimmer-animation 1.5s infinite;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@keyframes shimmer-animation {
|
|
121
|
+
0% {
|
|
122
|
+
transform: translateX(-100%);
|
|
123
|
+
}
|
|
124
|
+
100% {
|
|
125
|
+
transform: translateX(100%);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
`,
|
|
129
|
+
],
|
|
130
|
+
})
|
|
131
|
+
export class ShimmerComponent implements AfterViewInit, OnDestroy {
|
|
132
|
+
// Inputs using Angular signals
|
|
133
|
+
loading = input<boolean>(true);
|
|
134
|
+
shimmerColor = input<string | undefined>(undefined);
|
|
135
|
+
backgroundColor = input<string | undefined>(undefined);
|
|
136
|
+
duration = input<number | undefined>(undefined);
|
|
137
|
+
fallbackBorderRadius = input<number | undefined>(undefined);
|
|
138
|
+
|
|
139
|
+
// View child reference
|
|
140
|
+
measureContainer = viewChild<ElementRef<HTMLDivElement>>('measureContainer');
|
|
141
|
+
|
|
142
|
+
// Internal state
|
|
143
|
+
elements = signal<ElementInfo[]>([]);
|
|
144
|
+
|
|
145
|
+
// Inject global config
|
|
146
|
+
private contextConfig = injectShimmerConfig();
|
|
147
|
+
|
|
148
|
+
// Resolved values (props > context > defaults)
|
|
149
|
+
resolvedShimmerColor = computed(() => this.shimmerColor() ?? this.contextConfig.shimmerColor);
|
|
150
|
+
resolvedBackgroundColor = computed(
|
|
151
|
+
() => this.backgroundColor() ?? this.contextConfig.backgroundColor
|
|
152
|
+
);
|
|
153
|
+
resolvedDuration = computed(() => this.duration() ?? this.contextConfig.duration);
|
|
154
|
+
resolvedFallbackBorderRadius = computed(
|
|
155
|
+
() => this.fallbackBorderRadius() ?? this.contextConfig.fallbackBorderRadius
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
// Cleanup function for ResizeObserver
|
|
159
|
+
private resizeCleanup: (() => void) | undefined;
|
|
160
|
+
private mutationObserver: MutationObserver | undefined;
|
|
161
|
+
|
|
162
|
+
constructor() {
|
|
163
|
+
// Effect to re-measure when loading state changes
|
|
164
|
+
effect((onCleanup) => {
|
|
165
|
+
const isLoading = this.loading();
|
|
166
|
+
const container = this.measureContainer();
|
|
167
|
+
|
|
168
|
+
if (isLoading && container) {
|
|
169
|
+
// Clean up existing observers before setting up new ones
|
|
170
|
+
this.cleanup();
|
|
171
|
+
|
|
172
|
+
// Set up observers for this loading session
|
|
173
|
+
this.setupObservers();
|
|
174
|
+
|
|
175
|
+
// Defer measurement to next frame to ensure content is rendered
|
|
176
|
+
requestAnimationFrame(() => this.measureElements());
|
|
177
|
+
} else {
|
|
178
|
+
// Cleanup when not loading
|
|
179
|
+
this.cleanup();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Cleanup on effect re-run or component destruction
|
|
183
|
+
onCleanup(() => {
|
|
184
|
+
this.cleanup();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
ngAfterViewInit(): void {
|
|
190
|
+
// Effect will handle setup when container becomes available
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
ngOnDestroy(): void {
|
|
194
|
+
this.cleanup();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private setupObservers(): void {
|
|
198
|
+
const container = this.measureContainer()?.nativeElement;
|
|
199
|
+
if (!container) return;
|
|
200
|
+
|
|
201
|
+
// Set up ResizeObserver
|
|
202
|
+
this.resizeCleanup = createResizeObserver(container, () => this.measureElements());
|
|
203
|
+
|
|
204
|
+
// Set up MutationObserver for content changes
|
|
205
|
+
this.mutationObserver = new MutationObserver(() => {
|
|
206
|
+
if (this.loading()) {
|
|
207
|
+
this.measureElements();
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
this.mutationObserver.observe(container, {
|
|
212
|
+
childList: true,
|
|
213
|
+
subtree: true,
|
|
214
|
+
characterData: true,
|
|
215
|
+
attributes: false,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private measureElements(): void {
|
|
220
|
+
const container = this.measureContainer()?.nativeElement;
|
|
221
|
+
if (!container || !this.loading()) return;
|
|
222
|
+
|
|
223
|
+
// Temporarily disconnect mutation observer to avoid recursion
|
|
224
|
+
this.mutationObserver?.disconnect();
|
|
225
|
+
|
|
226
|
+
const containerRect = container.getBoundingClientRect();
|
|
227
|
+
const extractedElements: ElementInfo[] = [];
|
|
228
|
+
|
|
229
|
+
Array.from(container.children).forEach((child) => {
|
|
230
|
+
extractedElements.push(...extractElementInfo(child, containerRect));
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
this.elements.set(extractedElements);
|
|
234
|
+
|
|
235
|
+
// Reconnect mutation observer
|
|
236
|
+
if (this.mutationObserver && container) {
|
|
237
|
+
this.mutationObserver.observe(container, {
|
|
238
|
+
childList: true,
|
|
239
|
+
subtree: true,
|
|
240
|
+
characterData: true,
|
|
241
|
+
attributes: false,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private cleanup(): void {
|
|
247
|
+
if (this.resizeCleanup) {
|
|
248
|
+
this.resizeCleanup();
|
|
249
|
+
this.resizeCleanup = undefined;
|
|
250
|
+
}
|
|
251
|
+
if (this.mutationObserver) {
|
|
252
|
+
this.mutationObserver.disconnect();
|
|
253
|
+
this.mutationObserver = undefined;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Manually trigger re-measurement of elements.
|
|
259
|
+
* Useful when content changes programmatically.
|
|
260
|
+
*/
|
|
261
|
+
remeasure(): void {
|
|
262
|
+
this.measureElements();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import 'zone.js';
|
|
2
|
+
import { vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
// Mock getBoundingClientRect since jsdom doesn't support layout
|
|
5
|
+
Element.prototype.getBoundingClientRect = vi.fn(() => ({
|
|
6
|
+
width: 100,
|
|
7
|
+
height: 50,
|
|
8
|
+
top: 0,
|
|
9
|
+
left: 0,
|
|
10
|
+
bottom: 50,
|
|
11
|
+
right: 100,
|
|
12
|
+
x: 0,
|
|
13
|
+
y: 0,
|
|
14
|
+
toJSON: () => { },
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
// Mock ResizeObserver
|
|
18
|
+
global.ResizeObserver = class ResizeObserver {
|
|
19
|
+
observe() { }
|
|
20
|
+
unobserve() { }
|
|
21
|
+
disconnect() { }
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Mock getComputedStyle
|
|
25
|
+
const originalGetComputedStyle = window.getComputedStyle;
|
|
26
|
+
window.getComputedStyle = (elt) => {
|
|
27
|
+
const styles = originalGetComputedStyle(elt);
|
|
28
|
+
// Add borderRadius support for our tests
|
|
29
|
+
if (!styles.borderRadius) {
|
|
30
|
+
Object.defineProperty(styles, 'borderRadius', {
|
|
31
|
+
value: '4px',
|
|
32
|
+
writable: true,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return styles;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// Mock requestAnimationFrame
|
|
39
|
+
global.requestAnimationFrame = (cb) => {
|
|
40
|
+
setTimeout(cb, 0);
|
|
41
|
+
return 0;
|
|
42
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Input properties for the Shimmer component
|
|
4
|
+
*/
|
|
5
|
+
export interface ShimmerInputs {
|
|
6
|
+
/**
|
|
7
|
+
* Whether the component is in loading state
|
|
8
|
+
* @default true
|
|
9
|
+
*/
|
|
10
|
+
loading?: boolean;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Color of the shimmer effect gradient
|
|
14
|
+
*/
|
|
15
|
+
shimmerColor?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Background color of the shimmer blocks
|
|
19
|
+
*/
|
|
20
|
+
backgroundColor?: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Duration of one shimmer animation cycle in seconds
|
|
24
|
+
*/
|
|
25
|
+
duration?: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fallback border radius (in pixels) when element has no border-radius
|
|
29
|
+
* @default 4
|
|
30
|
+
*/
|
|
31
|
+
fallbackBorderRadius?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Re-export core types for convenience
|
|
35
|
+
export type { ShimmerConfig, ShimmerContextValue, ElementInfo } from '@shimmer-from-structure/core';
|