mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-02-02 05:08:03 +01:00
feat(Avatar): use NuxtImg component when available
Resolves nuxt/ui#2078
This commit is contained in:
@@ -11,6 +11,10 @@ links:
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
::tip
|
||||||
|
The Avatar uses the `NuxtImg` component when `@nuxt/image` is installed, falling back to `img` otherwise.
|
||||||
|
::
|
||||||
|
|
||||||
### Src
|
### Src
|
||||||
|
|
||||||
Use the `src` prop to set the image URL. You can pass any property from HTML `<img>` element such as `alt`, `loading`, etc.
|
Use the `src` prop to set the image URL. You can pass any property from HTML `<img>` element such as `alt`, `loading`, etc.
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ type AvatarVariants = VariantProps<typeof avatar>
|
|||||||
export interface AvatarProps extends Pick<AvatarFallbackProps, 'delayMs'> {
|
export interface AvatarProps extends Pick<AvatarFallbackProps, 'delayMs'> {
|
||||||
/**
|
/**
|
||||||
* The element or component this component should render as.
|
* The element or component this component should render as.
|
||||||
* @defaultValue 'img'
|
* @defaultValue 'span'
|
||||||
*/
|
*/
|
||||||
as?: string | object
|
as?: any
|
||||||
src?: string
|
src?: string
|
||||||
alt?: string
|
alt?: string
|
||||||
icon?: string
|
icon?: string
|
||||||
@@ -36,10 +36,11 @@ import { AvatarRoot, AvatarImage, AvatarFallback, useForwardProps } from 'radix-
|
|||||||
import { reactivePick } from '@vueuse/core'
|
import { reactivePick } from '@vueuse/core'
|
||||||
import { useAvatarGroup } from '../composables/useAvatarGroup'
|
import { useAvatarGroup } from '../composables/useAvatarGroup'
|
||||||
import UIcon from './Icon.vue'
|
import UIcon from './Icon.vue'
|
||||||
|
import ImageComponent from '#build/ui-image-component'
|
||||||
|
|
||||||
defineOptions({ inheritAttrs: false })
|
defineOptions({ inheritAttrs: false })
|
||||||
|
|
||||||
const props = defineProps<AvatarProps>()
|
const props = withDefaults(defineProps<AvatarProps>(), { as: 'span' })
|
||||||
|
|
||||||
const fallbackProps = useForwardProps(reactivePick(props, 'delayMs'))
|
const fallbackProps = useForwardProps(reactivePick(props, 'delayMs'))
|
||||||
|
|
||||||
@@ -54,10 +55,10 @@ const ui = computed(() => avatar({
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<AvatarRoot :class="ui.root({ class: [props.class, props.ui?.root] })">
|
<AvatarRoot :as="as" :class="ui.root({ class: [props.class, props.ui?.root] })">
|
||||||
<AvatarImage
|
<AvatarImage
|
||||||
v-if="src"
|
v-if="src"
|
||||||
:as="as"
|
:as="ImageComponent"
|
||||||
:src="src"
|
:src="src"
|
||||||
:alt="alt"
|
:alt="alt"
|
||||||
v-bind="$attrs"
|
v-bind="$attrs"
|
||||||
|
|||||||
@@ -87,6 +87,16 @@ export {}
|
|||||||
`
|
`
|
||||||
})
|
})
|
||||||
|
|
||||||
|
templates.push({
|
||||||
|
filename: 'ui-image-component.ts',
|
||||||
|
write: true,
|
||||||
|
getContents: ({ app }) => {
|
||||||
|
const image = app?.components?.find(c => c.pascalName === 'NuxtImg' && !c.filePath.includes('nuxt/dist/app'))
|
||||||
|
|
||||||
|
return image ? `export { default } from "${image.filePath}"` : 'export default "img"'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return templates
|
return templates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user