koel/resources/assets/js/components/profile-preferences/EditableProfileAvatar.vue

92 lines
2.6 KiB
Vue
Raw Normal View History

2024-03-19 22:48:12 +00:00
<template>
2024-04-04 22:20:42 +00:00
<div class="avatar-width ring-4 ring-white mt-8 rounded-full relative overflow-hidden aspect-square">
<UserAvatar v-if="profile.avatar" :user="profile" class="avatar-width" />
<div
class="absolute top-0 rounded-full w-full aspect-square flex items-center justify-center gap-2 pt-[50%]
bg-black/50 opacity-0 hover:opacity-100 transition-opacity duration-300"
>
<button class="control" title="Pick a new avatar" type="button" @click.prevent="openFileDialog">
2024-03-19 22:48:12 +00:00
<Icon :icon="faUpload" />
</button>
2024-04-04 22:20:42 +00:00
<button v-if="avatarChanged" class="control" title="Reset avatar" type="button" @click.prevent="resetAvatar">
2024-03-19 22:48:12 +00:00
<Icon :icon="faRefresh" />
</button>
2024-04-04 22:20:42 +00:00
<button v-else class="control" title="Remove avatar" type="button" @click.prevent="removeAvatar">
2024-03-19 22:48:12 +00:00
<Icon :icon="faTimes" />
</button>
</div>
<ImageCropper v-if="cropperSource" :source="cropperSource" @cancel="onCancel" @crop="onCrop" />
2024-03-19 22:48:12 +00:00
</div>
</template>
2024-04-23 21:01:27 +00:00
<script lang="ts" setup>
2024-04-04 22:20:42 +00:00
import { faRefresh, faTimes, faUpload } from '@fortawesome/free-solid-svg-icons'
2024-03-19 22:48:12 +00:00
import { computed, ref, toRefs } from 'vue'
import { useFileDialog } from '@vueuse/core'
import { userStore } from '@/stores'
import { useFileReader } from '@/composables'
import { gravatar } from '@/utils'
import UserAvatar from '@/components/user/UserAvatar.vue'
import ImageCropper from '@/components/utils/ImageCropper.vue'
2024-03-19 22:48:12 +00:00
const props = defineProps<{ profile: Pick<User, 'name' | 'avatar'> }>()
const { profile } = toRefs(props)
2024-04-04 22:20:42 +00:00
const { open, onChange } = useFileDialog({
2024-03-19 22:48:12 +00:00
accept: 'image/*',
multiple: false,
reset: true,
2024-03-19 22:48:12 +00:00
})
2024-04-04 22:20:42 +00:00
const openFileDialog = () => open()
2024-03-19 22:48:12 +00:00
const cropperSource = ref<string | null>(null)
onChange(files => {
if (!files?.length) {
profile.value.avatar = userStore.current.avatar
cropperSource.value = null
return
}
useFileReader().readAsDataUrl(files[0], url => {
cropperSource.value = url
})
})
const removeAvatar = () => profile.value.avatar = gravatar(userStore.current.email)
const resetAvatar = () => {
profile.value.avatar = userStore.current.avatar
cropperSource.value = null
}
const avatarChanged = computed(() => profile.value.avatar !== userStore.current.avatar)
const onCrop = (result: string) => {
profile.value.avatar = result
2024-03-19 22:48:12 +00:00
cropperSource.value = null
}
const onCancel = () => (cropperSource.value = null)
2024-03-19 22:48:12 +00:00
</script>
2024-04-23 21:01:27 +00:00
<style lang="postcss" scoped>
2024-04-04 22:20:42 +00:00
@tailwind utilities;
@layer utilities {
.control {
@apply bg-black/5 w-[28px] aspect-square rounded-full px-2 py-1 hover:bg-black/70;
2024-03-19 22:48:12 +00:00
}
2024-04-04 22:20:42 +00:00
.avatar-width {
@apply w-[105px];
2024-03-19 22:48:12 +00:00
}
}
</style>