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

Add Avatar Component #40

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion webapp/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"semi": true,
"bracketSpacing": true,
"trailingComma": "none",
"endOfLine": "lf"
"endOfLine": "auto"
}
3 changes: 3 additions & 0 deletions webapp/src/app/ui/avatar/avatar.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div [class]="computedClass()">
<img [ngSrc]="computedSrc()" [alt]="alt()" [class]="computedImageClass()" (error)="onError()" fill />
</div>
52 changes: 52 additions & 0 deletions webapp/src/app/ui/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ChangeDetectionStrategy, Component, computed, input, signal } from '@angular/core';
import type { ClassValue } from 'clsx';
import type { VariantProps } from 'class-variance-authority';
import { cn } from 'app/utils';
import { cva } from 'app/storybook.helper';
import { NgOptimizedImage } from '@angular/common';

const [avatarVariants, args, argTypes] = cva('relative flex shrink-0 overflow-hidden rounded-full', {
variants: {
size: {
default: 'h-10 w-10',
sm: 'h-6 w-6 text-xs',
lg: 'h-14 w-14 text-lg'
}
},
defaultVariants: {
size: 'default'
}
});

export { args, argTypes };

interface AvatarVariants extends VariantProps<typeof avatarVariants> {}

@Component({
selector: 'app-avatar',
standalone: true,
imports: [NgOptimizedImage],
changeDetection: ChangeDetectionStrategy.OnPush,
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved
templateUrl: './avatar.component.html'
})
export class AppAvatarComponent {
class = input<ClassValue>('');
size = input<AvatarVariants['size']>('default');

src = input<string>('');
GODrums marked this conversation as resolved.
Show resolved Hide resolved
alt = input<string>('');
imageClass = input<string>('');
fallback = input<string>('https://placehold.co/56');

canShow = signal(true);

onError = () => {
this.canShow.set(false);
};

computedClass = computed(() => cn(avatarVariants({ size: this.size() }), this.class()));

computedSrc = computed(() => (this.canShow() ? this.src() : this.fallback()));

computedImageClass = computed(() => cn('aspect-square object-cover h-full w-full', this.imageClass()));
}
96 changes: 96 additions & 0 deletions webapp/src/app/ui/avatar/avatar.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { argsToTemplate, type Meta, type StoryObj } from '@storybook/angular';
import { AppAvatarComponent, args, argTypes } from './avatar.component';

const meta: Meta<AppAvatarComponent> = {
title: 'UI/Avatar',
component: AppAvatarComponent,
tags: ['autodocs'],
args: {
...args,
size: 'default'
FelixTJDietrich marked this conversation as resolved.
Show resolved Hide resolved
},
argTypes: {
...argTypes
}
};

export default meta;
type Story = StoryObj<AppAvatarComponent>;

export const Default: Story = {
args: {
src: 'https://i.pravatar.cc/40?img=1',
alt: 'avatar',
class: ''
},

render: (args) => ({
props: args,
template: `<app-avatar ${argsToTemplate(args)}></app-avatar>`
})
};

export const Small: Story = {
args: {
size: 'sm',
src: 'https://i.pravatar.cc/24?img=1'
},

render: (args) => ({
props: args,
template: `<app-avatar ${argsToTemplate(args)}></app-avatar>`
})
};

export const Medium: Story = {
args: {
size: 'default',
src: 'https://i.pravatar.cc/40?img=1'
},

render: (args) => ({
props: args,
template: `<app-avatar ${argsToTemplate(args)}>MD</app-avatar>`
GODrums marked this conversation as resolved.
Show resolved Hide resolved
})
};

export const Large: Story = {
args: {
size: 'lg',
src: 'https://i.pravatar.cc/56?img=1',
alt: 'avatar',
class: ''
},

render: (args) => ({
props: args,
template: `<app-avatar ${argsToTemplate(args)}>LG</app-avatar>`
})
};

export const WithRandomImage: Story = {
args: {
size: 'lg',
src: 'https://i.pravatar.cc/56',
alt: 'avatar'
},

render: (args) => ({
props: args,
template: `<app-avatar ${argsToTemplate(args)}></app-avatar>`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can also define the template shared for all stories somehow if it is the same for all.

})
};

export const WithFallback: Story = {
args: {
size: 'default',
src: 'foobar.jpg',
fallback: 'https://placehold.co/40',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me, I might try to do a follow-up that uses content projection just to see how that works.

Copy link
Collaborator Author

@GODrums GODrums Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The disadvantage of the current approach (variables) vs. child-components is of course that it's not possible to use anything other than image URLs.
Spartan's approach of doubled components with child directives was the only alternative I was able to find.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible via contentChild in an app-avatar component and then having app-avatar-image and app-avatar-fallback as children 🤔

alt: 'fallback'
},

render: (args) => ({
props: args,
template: `<app-avatar ${argsToTemplate(args)}></app-avatar>`
})
};