-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Picture element in media component to improve responsiveness (#…
…17841) Closes: CXSPA-4114
- Loading branch information
Showing
11 changed files
with
339 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
projects/storefrontlib/shared/components/media/media-sources.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
import { MediaSourcesPipe } from './media-sources.pipe'; | ||
|
||
const mockImgUrl = 'https://test.url/image'; | ||
const mockSrcset = `${mockImgUrl} 96w, ${mockImgUrl} 284w`; | ||
|
||
describe('MediaSourcesPipe', () => { | ||
let pipe: MediaSourcesPipe; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [MediaSourcesPipe], | ||
}); | ||
|
||
pipe = TestBed.inject(MediaSourcesPipe); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
|
||
it('should transform the srcset string into an array of sources with widths', () => { | ||
const result = pipe.transform(mockSrcset); | ||
|
||
expect(result.length).toBe(2); | ||
expect(result[0].srcset).toBe(mockImgUrl); | ||
expect(result[0].media).toBe('(min-width: 284px)'); | ||
expect(result[1].srcset).toBe(mockImgUrl); | ||
expect(result[1].media).toBe('(min-width: 96px)'); | ||
}); | ||
|
||
it('should handle srcset with missing widths', () => { | ||
const srcset = `${mockImgUrl}, ${mockImgUrl} 284w`; | ||
|
||
const result = pipe.transform(srcset); | ||
|
||
expect(result.length).toBe(1); | ||
expect(result[0].srcset).toBe(mockImgUrl); | ||
expect(result[0].media).toBe('(min-width: 284px)'); | ||
}); | ||
|
||
it('should handle srcset with missing URLs', () => { | ||
const srcset = `96w, ${mockImgUrl} 284w`; | ||
|
||
const result = pipe.transform(srcset); | ||
console.log(result); | ||
|
||
expect(result.length).toBe(1); | ||
expect(result[0].srcset).toBe(mockImgUrl); | ||
expect(result[0].media).toBe('(min-width: 284px)'); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
projects/storefrontlib/shared/components/media/media-sources.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'cxMediaSources', | ||
}) | ||
export class MediaSourcesPipe implements PipeTransform { | ||
transform(sizes: string) { | ||
const sources: Pick<HTMLSourceElement, 'srcset' | 'media'>[] = []; | ||
|
||
return sizes.split(/,\s*/).reduceRight((acc, set) => { | ||
let [srcset, media] = set.split(' '); | ||
|
||
if (!srcset || !media) { | ||
return acc; | ||
} | ||
|
||
media = `(min-width: ${media.replace('w', 'px')})`; | ||
|
||
acc.push({ srcset, media }); | ||
|
||
return acc; | ||
}, sources); | ||
} | ||
} |
35 changes: 25 additions & 10 deletions
35
projects/storefrontlib/shared/components/media/media.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
<img | ||
*ngIf="media && media.src" | ||
[attr.src]="media.src" | ||
[attr.srcset]="media.srcset" | ||
[attr.alt]="media.alt" | ||
[attr.role]="media.role" | ||
[attr.loading]="loading" | ||
(load)="loadHandler()" | ||
(error)="errorHandler()" | ||
/> | ||
<picture *ngIf="media?.srcset && !isLegacy; else tmpImg"> | ||
<source | ||
*ngFor=" | ||
let source of media!.srcset! | cxMediaSources; | ||
trackBy: trackByMedia | ||
" | ||
[srcset]="source.srcset" | ||
[media]="source.media" | ||
/> | ||
|
||
<ng-container [ngTemplateOutlet]="tmpImg"></ng-container> | ||
</picture> | ||
|
||
<ng-template #tmpImg> | ||
<img | ||
*ngIf="media && media.src" | ||
[alt]="media.alt" | ||
[src]="media.src" | ||
[srcset]="media.srcset || media.src" | ||
[attr.role]="media.role" | ||
[loading]="loading" | ||
(load)="loadHandler()" | ||
(error)="errorHandler()" | ||
/> | ||
</ng-template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.