-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add NeAvatar component #91
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
base: main
Are you sure you want to change the base?
Changes from all commits
d490dcf
9a4f769
366eb6c
a276634
bf68cc4
4353d6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,100 @@ | ||||||
<!-- | ||||||
Copyright (C) 2025 Nethesis S.r.l. | ||||||
SPDX-License-Identifier: GPL-3.0-or-later | ||||||
--> | ||||||
<script lang="ts" setup> | ||||||
import { faUser } from '@fortawesome/free-solid-svg-icons' | ||||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome' | ||||||
import { computed, ref, useSlots } from 'vue' | ||||||
|
||||||
export type AvatarSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | ||||||
|
||||||
const slots = useSlots() | ||||||
|
||||||
const { | ||||||
size = 'md', | ||||||
img = '', | ||||||
initials = '', | ||||||
squared = false, | ||||||
alt = 'Avatar' | ||||||
} = defineProps<{ | ||||||
size?: AvatarSize | ||||||
img?: string | ||||||
initials?: string | ||||||
squared?: boolean | ||||||
alt?: string | ||||||
}>() | ||||||
|
||||||
const avatarSizeClasses: Record<AvatarSize, string> = { | ||||||
xs: 'size-6', | ||||||
sm: 'size-8', | ||||||
md: 'size-10', | ||||||
lg: 'size-12', | ||||||
xl: 'size-14', | ||||||
'2xl': 'size-16', | ||||||
'3xl': 'size-20', | ||||||
'4xl': 'size-24' | ||||||
} | ||||||
|
||||||
const placeholderColorClasses = 'bg-gray-700 text-white dark:bg-gray-200 dark:text-gray-950' | ||||||
|
||||||
const placeholderIconSizeClasses: Record<AvatarSize, string> = { | ||||||
xs: 'size-3', | ||||||
sm: 'size-4', | ||||||
md: 'size-5', | ||||||
lg: 'size-6', | ||||||
xl: 'size-7', | ||||||
'2xl': 'size-8', | ||||||
'3xl': 'size-10', | ||||||
'4xl': 'size-12' | ||||||
} | ||||||
|
||||||
const initialsSizeClasses: Record<AvatarSize, string> = { | ||||||
xs: 'text-xs', | ||||||
sm: 'text-sm', | ||||||
md: 'text-base', | ||||||
lg: 'text-xl', | ||||||
xl: 'text-2xl', | ||||||
'2xl': 'text-3xl', | ||||||
'3xl': 'text-4xl', | ||||||
'4xl': 'text-5xl' | ||||||
} | ||||||
|
||||||
const imageError = ref(false) | ||||||
|
||||||
const hasPlaceholder = computed(() => slots.placeholder) | ||||||
|
||||||
const placeholderContainerClasses = computed( | ||||||
() => | ||||||
`flex items-center justify-center ${placeholderColorClasses} ${squared ? 'rounded-sm' : 'rounded-full'} ${avatarSizeClasses[size]}` | ||||||
) | ||||||
|
||||||
function setImageError() { | ||||||
imageError.value = true | ||||||
} | ||||||
</script> | ||||||
<template> | ||||||
<div> | ||||||
<img | ||||||
v-if="img && !imageError" | ||||||
:alt="alt" | ||||||
:class="`${avatarSizeClasses[size]} ${squared ? 'rounded-sm' : 'rounded-full'}`" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
:src="img" | ||||||
@error="setImageError" | ||||||
/> | ||||||
<div v-else-if="!initials && hasPlaceholder"> | ||||||
<slot name="placeholder" /> | ||||||
</div> | ||||||
<div v-else-if="(imageError || !img) && !initials" :class="placeholderContainerClasses"> | ||||||
<FontAwesomeIcon | ||||||
:icon="faUser" | ||||||
:class="`${placeholderColorClasses} ${placeholderIconSizeClasses[size]}`" | ||||||
andre8244 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/> | ||||||
</div> | ||||||
<div v-else :class="`${placeholderContainerClasses} font-medium`"> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
<div :class="initialsSizeClasses[size]"> | ||||||
{{ initials }} | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</template> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (C) 2025 Nethesis S.r.l. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import type { Meta, StoryObj } from '@storybook/vue3-vite' | ||
import { NeAvatar } from '../src/main' | ||
import AvatarImage from '../src/assets/avatar.png' | ||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome' | ||
import { faUserSecret } from '@fortawesome/free-solid-svg-icons' | ||
|
||
const meta = { | ||
title: 'NeAvatar', | ||
component: NeAvatar, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
size: { control: 'inline-radio', options: ['xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl', '4xl'] } | ||
}, | ||
args: { | ||
size: 'md', | ||
img: '', | ||
initials: '', | ||
squared: false, | ||
alt: 'Avatar' | ||
} // default values | ||
} satisfies Meta<typeof NeAvatar> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
const defaultTemplate = `<NeAvatar v-bind="args" />` | ||
|
||
export const Default: Story = { | ||
render: (args) => ({ | ||
components: { NeAvatar }, | ||
setup() { | ||
return { args } | ||
}, | ||
template: defaultTemplate | ||
}), | ||
args: {} | ||
} | ||
|
||
export const Squared: Story = { | ||
render: (args) => ({ | ||
components: { NeAvatar }, | ||
setup() { | ||
return { args } | ||
}, | ||
template: defaultTemplate | ||
}), | ||
args: { squared: true } | ||
} | ||
|
||
export const Image: Story = { | ||
render: (args) => ({ | ||
components: { NeAvatar }, | ||
setup() { | ||
return { args } | ||
}, | ||
template: defaultTemplate | ||
}), | ||
args: { img: AvatarImage, alt: 'User avatar' } | ||
} | ||
|
||
export const Initials: Story = { | ||
render: (args) => ({ | ||
components: { NeAvatar }, | ||
setup() { | ||
return { args } | ||
}, | ||
template: defaultTemplate | ||
}), | ||
args: { initials: 'JD' } | ||
} | ||
|
||
const placeholderSlotTemplate = `<NeAvatar v-bind="args"> | ||
<template #placeholder> | ||
<div class="flex items-center justify-center size-16 rounded-full bg-emerald-500 text-emerald-950 dark:bg-emerald-200 dark:text-emerald-950"> | ||
<FontAwesomeIcon :icon="faUserSecret" class="size-8" /> | ||
</div> | ||
</template> | ||
</NeAvatar>` | ||
|
||
export const PlaceholderSlot: Story = { | ||
render: (args) => ({ | ||
components: { NeAvatar, FontAwesomeIcon }, | ||
setup() { | ||
return { args, faUserSecret } | ||
}, | ||
template: placeholderSlotTemplate | ||
}), | ||
args: {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remember to remove the pack pushes