Skip to content

Commit 7f772a2

Browse files
committed
feat(Icon): add global options on Vue-only side
1 parent 3e18df8 commit 7f772a2

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

src/runtime/components/Icon.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<script lang="ts">
2+
import type { RuntimeOptions } from '@nuxt/icon'
3+
24
export interface IconProps {
35
name: string | object
46
mode?: 'svg' | 'css'
57
size?: string | number
6-
customize?: (
7-
content: string,
8-
name?: string,
9-
prefix?: string,
10-
provider?: string
11-
) => string
8+
customize?: RuntimeOptions['customize']
129
}
1310
</script>
1411

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
<script lang="ts">
2-
import type { IconProps as NuxtIconProps } from '../../types'
3-
4-
export interface IconProps {
5-
name: NuxtIconProps['name']
6-
}
7-
</script>
8-
91
<script setup lang="ts">
2+
import type { IconProps } from '../../types'
3+
import type { IconifyRenderMode } from '@iconify/vue'
4+
import { computed } from 'vue'
105
import { Icon as IconifyIcon } from '@iconify/vue'
6+
import { useAppConfig } from '#imports'
7+
import type { RuntimeOptions } from '@nuxt/icon'
8+
9+
const props = defineProps<IconProps>()
10+
11+
const appConfig = useAppConfig()
12+
13+
function resolveCustomizeFn(
14+
customize: RuntimeOptions['customize'] | boolean | null | undefined,
15+
globalCustomize: RuntimeOptions['customize'] | undefined
16+
): RuntimeOptions['customize'] | undefined {
17+
if (customize === false) return undefined
18+
if (customize === true || customize === null) return globalCustomize
19+
return customize
20+
}
1121
12-
defineProps<IconProps>()
22+
const resolvedMode = computed(() => {
23+
const mode = props.mode || appConfig.icon?.mode
24+
if (mode === 'css') return 'style'
25+
return mode as IconifyRenderMode
26+
})
1327
</script>
1428

1529
<template>
16-
<IconifyIcon v-if="typeof name === 'string'" :icon="name.replace(/^i-/, '')" />
30+
<IconifyIcon
31+
v-if="typeof name === 'string'"
32+
:icon="name.replace(/^i-/, '')"
33+
:mode="resolvedMode"
34+
:width="size || appConfig.icon?.size"
35+
:height="size || appConfig.icon?.size"
36+
:customise="resolveCustomizeFn(props.customize, appConfig.icon?.customize)"
37+
/>
1738
<component :is="name" v-else />
1839
</template>

src/unplugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Options as ComponentsOptions } from 'unplugin-vue-components/types
88
import { defu } from 'defu'
99
import tailwind from '@tailwindcss/vite'
1010
import type colors from 'tailwindcss/colors'
11+
import type { RuntimeOptions } from '@nuxt/icon'
1112

1213
import type * as ui from '#build/ui'
1314

@@ -37,6 +38,10 @@ export interface NuxtUIOptions extends Omit<ModuleOptions, 'fonts' | 'colorMode'
3738
/** Whether to generate declaration files for auto-imported components. */
3839
dts?: boolean
3940
ui?: AppConfigUI
41+
/**
42+
* Default props for the `Icon` component
43+
*/
44+
icon?: Pick<RuntimeOptions, 'customize' | 'size' | 'mode'>
4045
/**
4146
* Enable or disable `@vueuse/core` color-mode integration
4247
* @defaultValue `true`
@@ -64,7 +69,7 @@ export const NuxtUIPlugin = createUnplugin<NuxtUIOptions | undefined>((_options
6469
options.theme = options.theme || {}
6570
options.theme.colors = resolveColors(options.theme.colors)
6671

67-
const appConfig = defu({ ui: options.ui, colorMode: options.colorMode }, { ui: getDefaultUiConfig(options.theme.colors) })
72+
const appConfig = defu({ ui: options.ui, colorMode: options.colorMode, icon: options.icon }, { ui: getDefaultUiConfig(options.theme.colors) })
6873

6974
return [
7075
NuxtEnvironmentPlugin(options),

0 commit comments

Comments
 (0)