Skip to content

Commit

Permalink
chore(avatar): refactor avatar component and update imports
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Jan 18, 2024
1 parent 401288c commit d3c2771
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
17 changes: 2 additions & 15 deletions packages/vue/src/avatar/Avatar.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
<script lang="ts">
import { createProvideScope } from '@oku-ui/provide'
import type { PrimitiveProps } from '@oku-ui/primitive'
import type { Ref } from 'vue'
export interface ScopeAvatar {
scopeOkuAvatar?: any
}
export type AvatarProvide = {
_names: 'OkuAvatar'
imageLoadingStatus: Ref<ImageLoadingStatus>
onImageLoadingStatusChange(status: ImageLoadingStatus): void
}
export const { composeProviderScopes, createProvide }
= createProvideScope<AvatarProvide['_names']>('OkuAvatar')
export const { useProvider, useInject: useAvatarInject }
= createProvide<Omit<AvatarProvide, '_names'>>('OkuAvatar')
export interface AvatarProps extends PrimitiveProps {
scopeOkuAvatar?: any
}
Expand All @@ -29,6 +15,7 @@ import { defineOptions, ref } from 'vue'
import { useComponentRef } from '@oku-ui/use-composable'
import { Primitive } from '@oku-ui/primitive'
import type { ImageLoadingStatus } from './types'
import { useAvatarProvider } from './utils'
defineOptions({
name: 'OkuAvatar',
Expand All @@ -42,7 +29,7 @@ const imageLoadingStatus = ref<ImageLoadingStatus>('idle')
const { componentRef, currentElement } = useComponentRef<HTMLLabelElement | null>()
useProvider({
useAvatarProvider({
scope: props.scopeOkuAvatar,
imageLoadingStatus,
onImageLoadingStatusChange: (status: ImageLoadingStatus) => imageLoadingStatus.value = status,
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/src/avatar/AvatarFallback.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface AvatarFallbackProps extends PrimitiveProps {
import { defineOptions, onBeforeUnmount, onMounted, ref } from 'vue'
import { useComponentRef } from '@oku-ui/use-composable'
import { Primitive } from '@oku-ui/primitive'
import { useAvatarInject } from './Avatar.vue'
import { useAvatarInject } from './utils'
defineOptions({
name: 'OkuAvatarFallback',
Expand Down
3 changes: 1 addition & 2 deletions packages/vue/src/avatar/AvatarImage.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import type { PrimitiveProps } from '@oku-ui/primitive'
import type { ImageLoadingStatus } from './types'
import { useImageLoadingStatus } from './utils'
import { useAvatarInject, useImageLoadingStatus } from './utils'
export interface AvatarImageProps extends PrimitiveProps {
scopeOkuAvatar?: any
Expand All @@ -18,7 +18,6 @@ export type AvatarImageEmits = {
import { defineOptions, watchEffect, withDefaults } from 'vue'
import { useComponentRef } from '@oku-ui/use-composable'
import { Primitive } from '@oku-ui/primitive'
import { useAvatarInject } from './Avatar.vue'
defineOptions({
name: 'OkuAvatarImage',
Expand Down
20 changes: 17 additions & 3 deletions packages/vue/src/avatar/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
export { default as OkuAvatar } from './Avatar.vue'
export { default as OkuAvatarImage } from './AvatarImage.vue'
export { default as OkuAvatarFallback } from './AvatarFallback.vue'
export {
default as OkuAvatar,
type AvatarProps,
} from './Avatar.vue'
export {
default as OkuAvatarImage,
type AvatarImageProps,
type AvatarImageEmits,
} from './AvatarImage.vue'
export {
default as OkuAvatarFallback,
type AvatarFallbackProps,
} from './AvatarFallback.vue'

export type * from './types'

export {
createAvatarScope,
} from './utils'
13 changes: 13 additions & 0 deletions packages/vue/src/avatar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { onBeforeUnmount, ref, watchEffect } from 'vue'
import { isClient } from '@oku-ui/use-composable'
import { createScope } from '@oku-ui/provide'
import type { Ref } from 'vue'
import type { ImageLoadingStatus } from './types'

export const [createAvatarProvide, createAvatarScope]
= createScope<'OkuAvatar'>('OkuAvatar')

export type AvatarProvide = {
imageLoadingStatus: Ref<ImageLoadingStatus>
onImageLoadingStatusChange(status: ImageLoadingStatus): void
}

export const [useAvatarProvider, useAvatarInject]
= createAvatarProvide<AvatarProvide>('OkuAvatar')

export function useImageLoadingStatus(src?: string) {
const loadingStatus = ref<ImageLoadingStatus>('idle')

Expand Down

0 comments on commit d3c2771

Please sign in to comment.