Skip to content

Commit

Permalink
feat(design): convert image to standalone (#3054)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelaint authored and damienwebdev committed Sep 27, 2024
1 parent 8fd50a6 commit 19ca741
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 25 deletions.
50 changes: 43 additions & 7 deletions libs/design/image/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,54 @@
# Image
Image utilizes the native HTML `<img>` element to display responsive images on a page and prevent content jumping while images are loading. `<daff-image>` is an opinionated version that encourages friendly end-user usage.

<design-land-example-viewer-container example="load-image"></design-land-example-viewer-container>

## Attributes
The `src`, `width`, `height`, and `alt` attributes must be defined. An error will be thrown any of these attributes are missing.

`width` and `height` are required to calculate the aspect ratio of an image, used for the [aspect ratio padding trick](https://css-tricks.com/aspect-ratio-boxes/) that helps to prevent content jumping while images are loading. The `width` and `height` values are rendered as pixels.

## Usage

### Within a standalone component
To use image in a standalone component, import it directly into your custom component:

```ts
@Component({
selector: 'custom-component',
templateUrl: './custom-component.component.html',
standalone: true,
imports: [
DAFF_IMAGE_COMPONENTS,
],
})
export class CustomComponent {}
```

### Within a module (deprecated)
To use image in a module, import `DaffImageModule` into your custom module:

```ts
import { NgModule } from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';

@NgModule({
declarations: [
CustomComponent,
],
exports: [
CustomComponent,
],
imports: [
DaffImageModule,
],
})
export class CustomComponentModule { }
```

> This method is deprecated. It's recommended to update all custom components to standalone.
## Errors

**DaffImageComponent must have a defined src attribute.**
Expand All @@ -22,10 +65,3 @@ This error appears when `<daff-image>` is missing a `height` attribute. The heig

## Accessbility
Images should be given a meaningful description using the native `alt` attribute to ensure an accessible experience by default. An error will be thrown if the `alt` attribute is missing.

## Usage
### Basic Image
<design-land-example-viewer-container example="load-image"></design-land-example-viewer-container>

### Image Load Output
<design-land-example-viewer-container example="load-image"></design-land-example-viewer-container>
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import {
Component,
} from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';
import { DAFF_IMAGE_COMPONENTS } from '@daffodil/design/image';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'basic-image',
templateUrl: './basic-image.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [DaffImageModule],
imports: [
DAFF_IMAGE_COMPONENTS,
],
})
export class BasicImageComponent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import {
Component,
} from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';
import { DAFF_IMAGE_COMPONENTS } from '@daffodil/design/image';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'load-image',
templateUrl: './load-image.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [DaffImageModule, NgIf],
imports: [
DAFF_IMAGE_COMPONENTS,
NgIf,
],
})
export class LoadImageComponent {
loaded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import {
Component,
} from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';
import { DAFF_IMAGE_COMPONENTS } from '@daffodil/design/image';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'skeleton-image',
templateUrl: './skeleton-image.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [DaffImageModule],
imports: [
DAFF_IMAGE_COMPONENTS,
],
})
export class SkeletonImageComponent {

Expand Down
7 changes: 4 additions & 3 deletions libs/design/image/src/image.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { NgModule } from '@angular/core';

import { DaffImageComponent } from './image/image.component';

/**
* @deprecated in favor of {@link DAFF_IMAGE_COMPONENTS}
* */
@NgModule({
declarations: [
DaffImageComponent,
],
imports: [
CommonModule,
DaffImageComponent,
],
exports: [
DaffImageComponent,
Expand Down
5 changes: 5 additions & 0 deletions libs/design/image/src/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DaffImageComponent } from './image/image.component';

export const DAFF_IMAGE_COMPONENTS = <const>[
DaffImageComponent,
];
7 changes: 5 additions & 2 deletions libs/design/image/src/image/image.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { DaffImageComponent } from './image.component';

@Component({
template: `<daff-image [src]="src" [alt]="alt" [width]="width" [height]="height" [skeleton]="skeleton"></daff-image>`,
standalone: true,
imports: [
DaffImageComponent,
],
})

class WrapperComponent {
Expand All @@ -32,8 +36,7 @@ describe('@daffodil/design/image | DaffImageComponent', () => {

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
DaffImageComponent,
imports: [
WrapperComponent,
],
})
Expand Down
1 change: 1 addition & 0 deletions libs/design/image/src/image/image.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const validateProperties = (object: Record<string, any>, props: string[]) => {
directive: DaffSkeletonableDirective,
inputs: ['skeleton'],
}],
standalone: true,
})
export class DaffImageComponent implements OnInit {

Expand Down
4 changes: 3 additions & 1 deletion libs/design/image/src/image/specs/image-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ describe('DaffImageComponent | Props Validation', () => {

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
imports: [
DaffImageComponent,
],
declarations: [
WrapperComponent,
],
})
Expand Down
1 change: 1 addition & 0 deletions libs/design/image/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { DaffImageModule } from './image.module';
export { DaffImageComponent } from './image/image.component';
export { DAFF_IMAGE_COMPONENTS } from './image';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Component,
} from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';
import { DAFF_IMAGE_COMPONENTS } from '@daffodil/design/image';
import { DaffMediaGalleryModule } from '@daffodil/design/media-gallery';

@Component({
Expand All @@ -12,7 +12,7 @@ import { DaffMediaGalleryModule } from '@daffodil/design/media-gallery';
templateUrl: './basic-media-gallery.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [DaffMediaGalleryModule, DaffImageModule],
imports: [DaffMediaGalleryModule, DAFF_IMAGE_COMPONENTS],
})
export class BasicMediaGalleryComponent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Component,
} from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';
import { DAFF_IMAGE_COMPONENTS } from '@daffodil/design/image';
import { DaffMediaGalleryModule } from '@daffodil/design/media-gallery';

@Component({
Expand All @@ -12,6 +12,9 @@ import { DaffMediaGalleryModule } from '@daffodil/design/media-gallery';
templateUrl: './mismatched-sizes-media-gallery.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [DaffMediaGalleryModule, DaffImageModule],
imports: [
DaffMediaGalleryModule,
DAFF_IMAGE_COMPONENTS,
],
})
export class MismatchedSizesMediaGalleryComponent {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Component,
} from '@angular/core';

import { DaffImageModule } from '@daffodil/design/image';
import { DAFF_IMAGE_COMPONENTS } from '@daffodil/design/image';
import { DaffMediaGalleryModule } from '@daffodil/design/media-gallery';

@Component({
Expand All @@ -12,7 +12,10 @@ import { DaffMediaGalleryModule } from '@daffodil/design/media-gallery';
templateUrl: './skeleton-media-gallery.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [DaffMediaGalleryModule, DaffImageModule],
imports: [
DaffMediaGalleryModule,
DAFF_IMAGE_COMPONENTS,
],
})
export class SkeletonMediaGalleryComponent {

Expand Down

0 comments on commit 19ca741

Please sign in to comment.