Skip to content

Commit 210912f

Browse files
RBV1antfu
andauthoredApr 10, 2025··
feat: allow :customize="false" (#334)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 065e8ee commit 210912f

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ const customize = (content: string, name: string, prefix: string, provider: stri
293293
<template>
294294
<Icon name="tabler:star" :customize="customize" />
295295
</template>
296+
297+
<!-- You can also use `:customize="false"` to disabled the global customization function per-usage -->
296298
```
297299

298300
In the App Configuration File:

‎src/runtime/components/css.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getIconCSS } from '@iconify/utils/lib/css/icon'
44
import type { PropType } from 'vue'
55
import type { IconifyIcon } from '@iconify/types'
66
import type { NuxtIconRuntimeOptions, NuxtIconRuntimeServerOptions, IconifyIconCustomizeCallback } from '../../types'
7-
import { loadIcon } from './shared'
7+
import { loadIcon, resolveCustomizeFn } from './shared'
88
import { useAppConfig, useNuxtApp, useHead, useRuntimeConfig, onServerPrefetch } from '#imports'
99

1010
// This should only be used in the client side
@@ -72,7 +72,8 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
7272
required: true,
7373
},
7474
customize: {
75-
type: Function as PropType<IconifyIconCustomizeCallback>,
75+
type: [Function, Boolean, null] as PropType<IconifyIconCustomizeCallback | boolean | null>,
76+
default: null,
7677
required: false,
7778
},
7879
},
@@ -101,10 +102,11 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
101102
if (options.cssWherePseudo) {
102103
iconSelector = `:where(${iconSelector})`
103104
}
105+
104106
const css = getIconCSS(icon, {
105107
iconSelector,
106108
format: 'compressed',
107-
customise: props.customize ?? options.customize,
109+
customise: resolveCustomizeFn(props.customize, options.customize),
108110
})
109111
if (options.cssLayer && withLayer) {
110112
return `@layer ${options.cssLayer} { ${css} }`

‎src/runtime/components/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export default defineComponent({
2525
default: null,
2626
},
2727
customize: {
28-
type: Function as PropType<IconifyIconCustomizeCallback>,
28+
type: [Function, Boolean, null] as PropType<IconifyIconCustomizeCallback | boolean | null>,
29+
default: null,
2930
required: false,
3031
},
3132
},
@@ -45,7 +46,6 @@ export default defineComponent({
4546
? { fontSize: Number.isNaN(+size) ? size : size + 'px' }
4647
: null
4748
})
48-
const customize = props.customize || runtimeOptions.customize
4949

5050
return () => h(
5151
component.value,
@@ -54,7 +54,7 @@ export default defineComponent({
5454
name: name.value,
5555
class: runtimeOptions.class,
5656
style: style.value,
57-
customize,
57+
customize: props.customize,
5858
},
5959
slots,
6060
)

‎src/runtime/components/shared.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { computed } from 'vue'
22
import { getIcon as _getIcon, addIcon as _addIcon, loadIcon as _loadIcon } from '@iconify/vue'
33
import type { IconifyIcon } from '@iconify/types'
4-
import type { NuxtIconRuntimeOptions } from '../../types'
4+
import type { IconifyIconCustomizeCallback, NuxtIconRuntimeOptions } from '../../types'
55
import { useAppConfig } from '#imports'
66
import { init as initClientBundle } from '#build/nuxt-icon-client-bundle'
77

@@ -60,3 +60,13 @@ export function useResolvedName(getName: () => string) {
6060
return resolved
6161
})
6262
}
63+
64+
export function resolveCustomizeFn(
65+
customize: IconifyIconCustomizeCallback | boolean | null | undefined,
66+
globalCustomize: IconifyIconCustomizeCallback | undefined,
67+
):
68+
IconifyIconCustomizeCallback | undefined {
69+
if (customize === false) return undefined
70+
if (customize === true || customize === null) return globalCustomize
71+
return customize
72+
}

‎src/runtime/components/svg.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Icon as Iconify, addIcon } from '@iconify/vue'
22
import { h } from 'vue'
33
import type { PropType } from 'vue'
44
import type { NuxtIconRuntimeOptions, IconifyIconCustomizeCallback } from '../../types'
5-
import { initClientBundle, loadIcon, useResolvedName } from './shared'
5+
import { initClientBundle, loadIcon, useResolvedName, resolveCustomizeFn } from './shared'
66
import { useAsyncData, useNuxtApp, defineComponent, useAppConfig, onServerPrefetch } from '#imports'
77

88
export const NuxtIconSvg = /* @__PURE__ */ defineComponent({
@@ -13,7 +13,8 @@ export const NuxtIconSvg = /* @__PURE__ */ defineComponent({
1313
required: true,
1414
},
1515
customize: {
16-
type: Function as PropType<IconifyIconCustomizeCallback>,
16+
type: [Function, Boolean, null] as PropType<IconifyIconCustomizeCallback | boolean | null>,
17+
default: null,
1718
required: false,
1819
},
1920
},
@@ -54,7 +55,7 @@ export const NuxtIconSvg = /* @__PURE__ */ defineComponent({
5455
icon: name.value,
5556
ssr: true,
5657
// Iconify uses `customise`, where we expose `customize` for consistency
57-
customise: props.customize ?? options.customize,
58+
customise: resolveCustomizeFn(props.customize, options.customize),
5859
}, slots)
5960
},
6061
})

0 commit comments

Comments
 (0)
Please sign in to comment.