|
| 1 | +<img src="/meta/phosphor-mark-tight-yellow.png" width="128" align="right" /> |
| 2 | + |
| 3 | +# phosphor-icons-tailwindcss |
| 4 | + |
| 5 | +A [TailwindCSS] plugin for the [Phosphor icon set][phosphor]. |
| 6 | + |
| 7 | +[![MIT][license.badge]][license] [![npm.badge]][npm] |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +1. install package: |
| 12 | + |
| 13 | + ```bash |
| 14 | + pnpm add -D phosphor-icons-tailwindcss # or via npm, yarn, ... |
| 15 | + ``` |
| 16 | + |
| 17 | +2. register the plugin in your `tailwind.config.js`: |
| 18 | + |
| 19 | + ```js |
| 20 | + // tailwind.config.js |
| 21 | + import phosphorIcons from "phosphor-icons-tailwindcss"; |
| 22 | + |
| 23 | + /** @type {import("tailwindcss").Config } */ |
| 24 | + export default { |
| 25 | + plugins: [phosphorIcons()], |
| 26 | + }; |
| 27 | + ``` |
| 28 | + |
| 29 | + Or if you are using Tailwind 4: |
| 30 | + |
| 31 | + ```css |
| 32 | + /* app.css, or whatever your entry CSS is */ |
| 33 | + @import 'tailwindcss'; |
| 34 | + @plugin 'phosphor-icons-tailwindcss'; |
| 35 | + ``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +You need to add two classes to your markup: |
| 40 | + |
| 41 | +1. the base `ph` class, |
| 42 | +2. a class with the syntax: `ph-[<name><--weight>]`, corresponding to your desired icon. |
| 43 | + |
| 44 | +> [!NOTE] |
| 45 | +> `weight` is optional and defaults to "regular" if not specified. |
| 46 | +
|
| 47 | +For example: |
| 48 | + |
| 49 | +```html |
| 50 | +<p> |
| 51 | + <i class="ph ph-[info]"></i> <!-- render the regular info icon --> |
| 52 | + <i class="ph ph-[pulse--duotone]"></i> <!-- render the pulse icon in duotone weight --> |
| 53 | +</p> |
| 54 | +``` |
| 55 | + |
| 56 | +For all available icon names and weight, visit [phosphoricons.com][phosphor]. |
| 57 | + |
| 58 | +The output CSS look something like this: |
| 59 | + |
| 60 | +```css |
| 61 | +.ph { |
| 62 | + --ph-url: none; |
| 63 | + width: 1em; |
| 64 | + height: 1em; |
| 65 | + background-color: currentcolor; |
| 66 | + color: inherit; |
| 67 | + mask-image: var(--ph-url); |
| 68 | + mask-size: 100% 100%; |
| 69 | + mask-repeat: no-repeat; |
| 70 | +} |
| 71 | + |
| 72 | +.ph-\[info\] { |
| 73 | + url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTYgMjU2IiBmaWxsPSJjdXJyZW50Q29sb3IiPjxwYXRoIGQ9Ik0xMjgsMjRBMTA0LDEwNCwwLDEsMCwyMzIsMTI4LDEwNC4xMSwxMDQuMTEsMCwwLDAsMTI4LDI0Wm0wLDE5MmE4OCw4OCwwLDEsMSw4OC04OEE4OC4xLDg4LjEsMCwwLDEsMTI4LDIxNlptMTYtNDBhOCw4LDAsMCwxLTgsOCwxNiwxNiwwLDAsMS0xNi0xNlYxMjhhOCw4LDAsMCwxLDAtMTYsMTYsMTYsMCwwLDEsMTYsMTZ2NDBBOCw4LDAsMCwxLDE0NCwxNzZaTTExMiw4NGExMiwxMiwwLDEsMSwxMiwxMkExMiwxMiwwLDAsMSwxMTIsODRaIi8+PC9zdmc+); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Configuration |
| 78 | + |
| 79 | +You may pass a configuration object to the plugin to customize the generated CSS. The following |
| 80 | +shows the default configuration: |
| 81 | + |
| 82 | +```js |
| 83 | +// tailwind.config.js |
| 84 | +import phosphorIcons from "phosphor-icons-tailwindcss"; |
| 85 | + |
| 86 | +/** @type {import("tailwindcss").Config } */ |
| 87 | +export default { |
| 88 | + plugins: [phosphorIcons({ |
| 89 | + prefix: 'ph', // for the icon classes |
| 90 | + customProperty: '--ph-url', |
| 91 | + })], |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +Similarly, for Tailwind 4: |
| 96 | + |
| 97 | +```css |
| 98 | +@import 'tailwindcss'; |
| 99 | +@plugin 'phosphor-icons-tailwindcss' { |
| 100 | + prefix: ph; |
| 101 | + customProperty: --ph-url; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Why `ph-[info]` and not `ph-info`? |
| 106 | + |
| 107 | +You may notice this library utilizes Tailwind's support for [arbitrary value](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values), i.e `ph-[info]` instead of `ph-info` to map to the regular info icon. This is to avoid unnecessary parsing during development, especially for [Taliwind language server](https://github.com/tailwindlabs/tailwindcss-intellisense). Arbitrary value syntax allows only parsing the icons actually being used. Otherwise, parsing 9000+ icons may cause slow-down that negatively impacts developer experience. |
| 108 | + |
| 109 | +## Acknowledgements |
| 110 | + |
| 111 | +You may find [@iconify/tailwindcss](https://iconify.design/docs/usage/css/tailwind/) helpful if you |
| 112 | +are already using the [iconify](https://iconify.design/) ecosystem in your codebase. |
| 113 | + |
| 114 | +`phosphor-icons-tailwindcss` tries to stay minimal by only covering [Phosphor] icons, and it references directly [@phosphor-icons/core](https://github.com/phosphor-icons/core) for the SVG assets. |
| 115 | + |
| 116 | +[phosphor]: https://phosphoricons.com |
| 117 | +[tailwindcss]: https://tailwindcss.com |
| 118 | +<!-- header badges --> |
| 119 | +[license.badge]: https://img.shields.io/badge/license-MIT-blue.svg |
| 120 | +[license]: ./LICENSE |
| 121 | +[npm.badge]: https://img.shields.io/npm/v/phosphor-icons-tailwindcss/qr |
| 122 | +[npm]: https://www.npmjs.com/package/phosphor-icons-tailwindcss/qr |
0 commit comments