Skip to content

Commit 7f4c81e

Browse files
authored
Updated styling for image without placeholder (#266)
* Updated styling for image without placeholder
1 parent 0409ccb commit 7f4c81e

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

projects/angular-cld/src/lib/cloudinary-image.component.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,63 @@ describe('CloudinaryImage', () => {
527527
});
528528
});
529529

530+
describe('cl-image styling without placeholder', () => {
531+
@Component({
532+
template: `<cl-image public-id="sample.jpg" ></cl-image>`
533+
})
534+
class TestComponent {}
535+
536+
let fixture: ComponentFixture<TestComponent>;
537+
let des: DebugElement[]; // the elements w/ the directive
538+
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
539+
{ cloud_name: '@@fake_angular2_sdk@@', client_hints: true } as CloudinaryConfiguration);
540+
beforeEach(() => {
541+
fixture = TestBed.configureTestingModule({
542+
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent],
543+
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
544+
}).createComponent(TestComponent);
545+
546+
fixture.detectChanges(); // initial binding
547+
// all elements with an attached CloudinaryImage
548+
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
549+
});
550+
551+
it('should not have style opacity and position', () => {
552+
const img = des[0].children[0].nativeElement as HTMLImageElement;
553+
expect(img.getAttribute('style')).toEqual(jasmine.stringMatching(''));
554+
});
555+
});
556+
557+
describe('cl-image styling with placeholder', () => {
558+
@Component({
559+
template: `<div style="margin-top: 4000px"></div>
560+
<cl-image public-id="sample.jpg" loading="lazy">
561+
<cl-placeholder></cl-placeholder>
562+
</cl-image>`
563+
})
564+
class TestComponent {}
565+
566+
let fixture: ComponentFixture<TestComponent>;
567+
let des: DebugElement[]; // the elements w/ the directive
568+
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
569+
{ cloud_name: '@@fake_angular2_sdk@@', client_hints: true } as CloudinaryConfiguration);
570+
beforeEach(() => {
571+
fixture = TestBed.configureTestingModule({
572+
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent, CloudinaryPlaceHolder],
573+
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
574+
}).createComponent(TestComponent);
575+
576+
fixture.detectChanges(); // initial binding
577+
// all elements with an attached CloudinaryImage
578+
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
579+
});
580+
581+
it('should have style opacity and position', () => {
582+
const img = des[0].children[0].nativeElement as HTMLImageElement;
583+
expect(img.getAttribute('style')).toEqual(jasmine.stringMatching('opacity: 0; position: absolute;'));
584+
});
585+
});
586+
530587
describe('lazy load image', async () => {
531588
@Component({
532589
template: `

projects/angular-cld/src/lib/cloudinary-image.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import { accessibilityEffect } from './constants';
2222

2323
@Component({
2424
selector: 'cl-image',
25-
template: `<img [ngStyle]="{opacity: shouldShowPlaceHolder ? '0' : '1', position: shouldShowPlaceHolder ? 'absolute' : 'unset'}"(load)="hasLoaded()">
26-
<div [style.display]="shouldShowPlaceHolder ? 'inline' : 'none'">
25+
template: `<img [ngStyle]="getPlaceHolderStyle()"(load)="hasLoaded()">
26+
<div *ngIf="placeholderComponent"[style.display]="shouldShowPlaceHolder ? 'inline' : 'none'">
2727
<ng-content></ng-content>
2828
</div>
2929
`,
@@ -94,6 +94,11 @@ export class CloudinaryImage
9494
}
9595
}
9696

97+
getPlaceHolderStyle() {
98+
return {[this.shouldShowPlaceHolder ? 'opacity' : ''] : '0',
99+
[this.shouldShowPlaceHolder ? 'position' : ''] : 'absolute'}
100+
}
101+
97102
hasLoaded() {
98103
this.shouldShowPlaceHolder = false;
99104
}

0 commit comments

Comments
 (0)