Phosphor Icons for Vue
Phosphor-vue provides a comprehensive set of highly customizable SVG icons specifically tailored for Vue.js applications. The library is currently stable at version `2.2.0` for Vue 3 projects, offering a wide range of icons with multiple display weights (thin, light, regular, bold, fill, duotone). For Vue 2 users, version `1.x` branches are maintained separately. Releases typically align with updates to the core Phosphor Icons library, introducing new icons and bug fixes. Key differentiators include robust TypeScript support, a focus on flexibility through various icon weights for dynamic styling and state representation, and support for both global and individual component registration to balance convenience with bundle size optimization. It integrates seamlessly into Vue's component system, accepting standard SVG attributes and custom props for styling.
Common errors
-
[Vue warn]: Unknown custom element: <ph-icon-name> - did you register the component correctly?
cause The icon component (e.g., `<ph-horse>`) was used in a template but not imported and registered globally via `app.use(PhosphorVue)` or locally/globally via `app.component('PhHorse', PhHorse)`.fixEither register `PhosphorVue` as a global plugin (`app.use(PhosphorVue)`) or explicitly import and register each icon component you use, e.g., `import { PhHorse } from 'phosphor-vue'; app.component('PhHorse', PhHorse);`. -
Property 'weight' does not exist on type 'IntrinsicAttributes & SVGAttributes<SVGElement> & { color?: string | undefined; size?: string | number | undefined; weight?: "thin" | "light" | "regular" | "bold" | "fill" | "duotone" | undefined; }'cause A type mismatch or incorrect value passed to the `weight` prop. This usually happens if a string outside the allowed literal union types ("thin", "light", etc.) is provided.fixEnsure the `weight` prop value is one of the explicitly defined types: `'thin'`, `'light'`, `'regular'`, `'bold'`, `'fill'`, or `'duotone'`. -
Error: Cannot find module 'phosphor-vue' or Error: require() of ES Module ... not supported
cause Attempting to use `require()` for `phosphor-vue` v2.x in a CommonJS environment, or a bundling issue where the module resolution fails for the ESM-only package.fixEnsure your project uses ES Modules (`import ... from 'phosphor-vue'`) and a compatible bundler. If targeting older Node.js or CommonJS, consider transpilation or using the Vue 2 compatible `phosphor-vue@1`.
Warnings
- breaking Version 2.0.0 introduced breaking changes by dropping support for Vue 2.x. Projects using Vue 2 must remain on the 1.x branch of `phosphor-vue`.
- gotcha While a global plugin installation method exists, it is strongly advised against for production applications as it can significantly increase bundle size and prevent effective tree-shaking.
- gotcha Tree-shaking for `phosphor-vue` (especially in early v2.x versions) could be problematic, causing build systems to hang or generate excessively large bundles. This was particularly noted with Nuxt 3 and certain Vue 3 configurations.
- deprecated The ability to globally install icons utilizing the default export was added in v2.1.4, but the maintainers still 'strongly advise against global installs, since they break tree-shaking'.
Install
-
npm install phosphor-vue -
yarn add phosphor-vue -
pnpm add phosphor-vue
Imports
- PhosphorVue
const PhosphorVue = require('phosphor-vue')import PhosphorVue from 'phosphor-vue'
- PhHorse, PhHeart
import PhosphorVue from 'phosphor-vue/icons/PhHorse'
import { PhHorse, PhHeart } from 'phosphor-vue' - IconProps, IconWeight
import { IconProps } from 'phosphor-vue'import type { IconProps, IconWeight } from 'phosphor-vue'
Quickstart
import { createApp } from 'vue';
import PhosphorVue from 'phosphor-vue';
import App from './App.vue';
const app = createApp(App);
// Register PhosphorVue as a global plugin
app.use(PhosphorVue);
app.mount('#app');
// App.vue example
/*
<template>
<div>
<h1>My App</h1>
<ph-heart :size="32" color="red" weight="fill" />
<ph-cube :size="24" color="blue" />
<ph-star :weight="isStarred ? 'fill' : 'regular'" @click="toggleStar" />
</div>
</template>
<script setup>
import { ref } from 'vue';
const isStarred = ref(false);
const toggleStar = () => { isStarred.value = !isStarred.value; };
</script>
*/