2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-08 14:20:57 +00:00

chore: add customise prop to vue component

This commit is contained in:
Vjacheslav Trushkin 2024-07-16 17:35:27 +03:00
parent e60da173c6
commit bbebf09e14
5 changed files with 73 additions and 11 deletions

View File

@ -38,7 +38,7 @@ import {
addIcon as addOnlineIcon, addIcon as addOnlineIcon,
addCollection as addOnlineCollection, addCollection as addOnlineCollection,
disableCache, disableCache,
} from '@iconify/vue/dist/iconify'; } from '@iconify/vue';
import bxBarChartAlt from '@iconify-icons/bx/bx-bar-chart-alt'; import bxBarChartAlt from '@iconify-icons/bx/bx-bar-chart-alt';
import bxShapes from '@iconify-icons/bx/bxs-shapes'; import bxShapes from '@iconify-icons/bx/bxs-shapes';

View File

@ -12,6 +12,21 @@
:ssr="true" :ssr="true"
/> />
</div> </div>
<div>
Customising stroke width:
<Icon icon="line-md:arrow-right" />
<Icon icon="line-md:arrow-right" :customise="customise1" />
<Icon
icon="line-md:arrow-right"
:customise="
(content) =>
content.replace(
/stroke-width=&quot;2&quot;/g,
'stroke-width=&quot;1&quot;'
)
"
/>
</div>
<div class="alert"> <div class="alert">
<Icon icon="mdi-light:alert" />Important notice with alert icon! <Icon icon="mdi-light:alert" />Important notice with alert icon!
</div> </div>
@ -25,5 +40,20 @@ export default {
components: { components: {
Icon, Icon,
}, },
methods: {
customise1,
},
}; };
function customise1(
content: string,
name: string,
prefix: string,
provider: string
) {
if (name !== 'arrow-right' || prefix !== 'line-md' || provider !== '') {
throw new Error('Bad params in customise callback');
}
return content.replace(/stroke-width="2"/g, 'stroke-width="1.5"');
}
</script> </script>

View File

@ -2,7 +2,7 @@
"name": "@iconify/vue", "name": "@iconify/vue",
"description": "Iconify icon component for Vue 3.", "description": "Iconify icon component for Vue 3.",
"author": "Vjacheslav Trushkin", "author": "Vjacheslav Trushkin",
"version": "4.1.2", "version": "4.1.3-beta.1",
"license": "MIT", "license": "MIT",
"bugs": "https://github.com/iconify/iconify/issues", "bugs": "https://github.com/iconify/iconify/issues",
"homepage": "https://iconify.design/", "homepage": "https://iconify.design/",

View File

@ -81,6 +81,7 @@ import type {
IconifyIconCustomisations, IconifyIconCustomisations,
IconifyIconProps, IconifyIconProps,
IconifyRenderMode, IconifyRenderMode,
IconifyIconCustomiseCallback,
} from './props'; } from './props';
// Render SVG // Render SVG
@ -277,7 +278,8 @@ export const Icon = defineComponent<IconProps>({
// Get data for icon to render or null // Get data for icon to render or null
getIcon( getIcon(
icon: IconifyIcon | string, icon: IconifyIcon | string,
onload?: IconifyIconOnLoad onload?: IconifyIconOnLoad,
customise?: IconifyIconCustomiseCallback
): IconComponentData | null { ): IconComponentData | null {
// Icon is an object // Icon is an object
if ( if (
@ -304,7 +306,7 @@ export const Icon = defineComponent<IconProps>({
} }
// Load icon // Load icon
const data = getIconData(iconName); let data = getIconData(iconName);
if (!data) { if (!data) {
// Icon data is not available // Icon data is not available
if (!this._loadingIcon || this._loadingIcon.name !== icon) { if (!this._loadingIcon || this._loadingIcon.name !== icon) {
@ -333,6 +335,21 @@ export const Icon = defineComponent<IconProps>({
} }
} }
// Customise icon
if (customise) {
// Clone data and customise it
data = Object.assign({}, data);
const customised = customise(
data.body,
iconName.name,
iconName.prefix,
iconName.provider
);
if (typeof customised === 'string') {
data.body = customised;
}
}
// Add classes // Add classes
const classes: string[] = ['iconify']; const classes: string[] = ['iconify'];
if (iconName.prefix !== '') { if (iconName.prefix !== '') {
@ -354,8 +371,9 @@ export const Icon = defineComponent<IconProps>({
const props = this.$attrs; const props = this.$attrs;
// Get icon data // Get icon data
const icon: IconComponentData | null = (this.iconMounted || props.ssr) const icon: IconComponentData | null =
? this.getIcon(props.icon, props.onLoad) this.iconMounted || props.ssr
? this.getIcon(props.icon, props.onLoad, props.customise)
: null; : null;
// Validate icon object // Validate icon object

View File

@ -33,6 +33,16 @@ export const defaultExtendedIconCustomisations = {
*/ */
export type IconifyIconOnLoad = (name: string) => void; export type IconifyIconOnLoad = (name: string) => void;
/**
* Customise callback
*/
export type IconifyIconCustomiseCallback = (
content: string,
name: string,
prefix: string,
provider: string
) => string;
/** /**
* Icon properties * Icon properties
*/ */
@ -59,9 +69,6 @@ interface IconifyElementProps {
// Style // Style
style?: unknown; style?: unknown;
// Callback to call when icon data has been loaded. Used only for icons loaded from API
onLoad?: IconifyIconOnLoad;
} }
/** /**
@ -74,5 +81,12 @@ export interface IconProps extends IconifyElementProps, IconifyIconProps {
* This is a low-level API for framework integrations, you don't usually need to use it directly. * This is a low-level API for framework integrations, you don't usually need to use it directly.
* Note this might hydration mismatches if the icon data is not handled correctly, use with caution. * Note this might hydration mismatches if the icon data is not handled correctly, use with caution.
*/ */
ssr?: boolean ssr?: boolean;
// Callback to call when icon data has been loaded. Used only for icons loaded from API
onLoad?: IconifyIconOnLoad;
// Customise icon content (replace stroke-width, colors, etc...).
// Called only for icons loaded from API
customise?: IconifyIconCustomiseCallback;
} }