astro-icon
Advanced tools
Comparing version 0.8.1 to 1.0.0-next.0
{ | ||
"name": "astro-icon", | ||
"version": "0.8.1", | ||
"version": "1.0.0-next.0", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": "./index.ts", | ||
"./pack": "./lib/createIconPack.ts" | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"./components": "./components/index.js" | ||
}, | ||
"files": [ | ||
"lib", | ||
"index.ts" | ||
"components", | ||
"dist", | ||
"typings" | ||
], | ||
@@ -20,2 +25,3 @@ "author": { | ||
"astro", | ||
"astro-integration", | ||
"astro-component", | ||
@@ -40,6 +46,17 @@ "image", | ||
"dependencies": { | ||
"node-fetch": "^3.1.0", | ||
"resolve-pkg": "^2.0.0", | ||
"svgo": "^2.8.0" | ||
"@iconify/tools": "^3.0.1", | ||
"@iconify/types": "^2.0.0", | ||
"@iconify/utils": "^2.1.5" | ||
}, | ||
"devDependencies": { | ||
"@astrojs/ts-plugin": "^1.0.6", | ||
"@types/node": "^16.11.7", | ||
"astro": "^2.5.0", | ||
"typescript": "^5.0.4", | ||
"vite": "^4.3.8" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"start": "tsc --watch" | ||
} | ||
} | ||
} |
286
README.md
@@ -1,61 +0,68 @@ | ||
# Astro Icon | ||
# astro-icon | ||
A straight-forward `Icon` component for [Astro](https://astro.build). | ||
This **[Astro integration](https://docs.astro.build/en/guides/integrations-guide/)** provides a straight-forward `Icon` component for [Astro](https://astro.build). | ||
## Install `astro-icon`. | ||
- <strong>[Why Astro Icon](#why-astro-icon)</strong> | ||
- <strong>[Installation](#installation)</strong> | ||
- <strong>[Usage](#usage)</strong> | ||
- <strong>[Configuration](#configuration)</strong> | ||
- <strong>[Examples](#examples)</strong> | ||
- <strong>[Migrating to v1](#migrating-to-v1)</strong> | ||
- <strong>[Contributing](#contributing)</strong> | ||
- <strong>[Changelog](#changelog)</strong> | ||
```bash | ||
npm i astro-icon | ||
# or | ||
yarn add astro-icon | ||
``` | ||
## Why Astro Icon | ||
## Icon Packs | ||
## Installation | ||
`astro-icon` automatically includes all of the most common icon packs, powered by [Iconify](https://iconify.design/)! | ||
### Quick Install | ||
To browse supported icons, check the official [Icon Sets reference](https://icon-sets.iconify.design/) or visit [Icônes](https://icones.js.org/). | ||
Possibly coming soon! | ||
### Usage | ||
<!-- | ||
**Icon** will inline the SVG directly in your HTML. | ||
The `astro add` command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one. | ||
```astro | ||
--- | ||
import { Icon } from 'astro-icon' | ||
--- | ||
```sh | ||
# Using NPM | ||
npx astro add icon | ||
# Using Yarn | ||
yarn astro add icon | ||
# Using PNPM | ||
pnpm astro add icon | ||
``` | ||
<!-- Automatically fetches and inlines Material Design Icon's "account" SVG --> | ||
<Icon pack="mdi" name="account" /> | ||
If you run into any issues, [feel free to report them to us on GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below. | ||
<!-- Equivalent shorthand --> | ||
<Icon name="mdi:account" /> | ||
--> | ||
### Manual Install | ||
First, install the `astro-icon` package using your package manager. If you're using npm or aren't sure, run this in the terminal: | ||
```sh | ||
npm install astro-icon | ||
``` | ||
**Sprite** will reference the SVG from a spritesheet via [`<use>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use). | ||
Then, apply this integration to your `astro.config.*` file using the `integrations` property: | ||
```astro | ||
--- | ||
import { Sprite } from 'astro-icon' | ||
--- | ||
**`astro.config.mjs`** | ||
<!-- Required ONCE per page as a parent of any <Sprite> components! Creates `<symbol>` for each icon --> | ||
<!-- Can also be included in your Layout component! --> | ||
<Sprite.Provider> | ||
<!-- Automatically fetches and inlines Material Design Icon's "account" SVG --> | ||
<Sprite pack="mdi" name="account" /> | ||
```js ins={2} "icon()" | ||
import { defineConfig } from "astro/config"; | ||
import icon from "astro-icon"; | ||
<!-- Equivalent shorthand --> | ||
<Sprite name="mdi:account" /> | ||
</Sprite.Provider> | ||
export default defineConfig({ | ||
// ... | ||
integrations: [icon()], | ||
}); | ||
``` | ||
You may also create [Local Icon Packs](#local-icon-packs). | ||
## Usage | ||
## Local Icons | ||
Astro Icon should be ready to go with zero config. The `Icon` component is provided and allows you to inline `svg`s directly into your HTML. | ||
By default, `astro-icon` supports custom local `svg` icons. They are optimized with [`svgo`](https://github.com/svg/svgo) automatically with no extra build step. See ["A Pretty Good SVG Icon System"](https://css-tricks.com/pretty-good-svg-icon-system/#just-include-the-icons-inline) from CSS Tricks. | ||
### Local Icons | ||
### Usage | ||
By default, Astro Icon supports custom local `svg` icons. They are optimized with [`svgo`](https://github.com/svg/svgo) automatically with no extra build step. See ["A Pretty Good SVG Icon System"](https://css-tricks.com/pretty-good-svg-icon-system/#just-include-the-icons-inline) from CSS Tricks. | ||
@@ -66,7 +73,5 @@ 1. Create a directory inside of `src/` named `icons/`. | ||
**Icon** will inline the SVG directly in your HTML. | ||
```astro | ||
--- | ||
import { Icon } from 'astro-icon'; | ||
import { Icon } from 'astro-icon/components'; | ||
--- | ||
@@ -78,67 +83,71 @@ | ||
**Sprite** will reference the SVG from a spritesheet via [`<use>`](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use). | ||
## Iconify Icons | ||
```astro | ||
--- | ||
import { Sprite } from 'astro-icon'; | ||
--- | ||
Astro Icon also supports Iconify out-of-the-box with minimal configuration. To use an icon set from Iconify follow the instructions below: | ||
<!-- Required ONCE per page as a parent of any <Sprite> components! Creates `<symbol>` for each icon --> | ||
<!-- Can also be included in your Layout component! --> | ||
<Sprite.Provider> | ||
<!-- Uses the sprite from `/src/icons/filename.svg` --> | ||
<Sprite name="filename" /> | ||
</Sprite.Provider> | ||
``` | ||
1. Find an Icon Set to use on the [Iconify Icon Sets website](https://icon-sets.iconify.design/) | ||
2. Install the package (eg. `npm i -D @iconify-json/mdi`) | ||
3. Add an entry to the `astro.config.mjs` file | ||
## Local Icon Packs | ||
**`astro.config.mjs`** | ||
`astro-icon` supports custom local icon packs. These are also referenced with the `pack` and/or `name` props. | ||
```js ins={2} | ||
import { defineConfig } from "astro/config"; | ||
import icon from "astro-icon"; | ||
1. Create a directory inside of `src/` named `icons/`. | ||
2. Inside that directory, create a JS/TS file with your `pack` name inside of that directory, eg `src/icons/my-pack.ts` | ||
3. Export a `default` function that takes an icon name and returns a svg string. Utilize the `createIconPack` utility to handle most common situations. | ||
export default defineConfig({ | ||
// ... | ||
integrations: [ | ||
icon({ | ||
include: { | ||
mdi: ["*"], // Loads entire Material Design Icon set | ||
}, | ||
}), | ||
], | ||
}); | ||
``` | ||
If using a package from NPM, eg. `heroicons`, the icon pack file would resemble the following: | ||
4. Reference a specific icon using the `name` prop with (eg. `mdi:account`) | ||
```js | ||
import { createIconPack } from "astro-icon/pack"; | ||
```astro | ||
--- | ||
import { Icon } from 'astro-icon/components' | ||
--- | ||
// Resolves `heroicons` dependency and reads SVG files from the `heroicons/outline` directory | ||
export default createIconPack({ package: "heroicons", dir: "outline" }); | ||
<!-- Automatically fetches and inlines Material Design Icon's "account" SVG --> | ||
<Icon name="mdi:account" /> | ||
``` | ||
If using an icon set from a remote server, the icon pack file would resemble the following: | ||
### Props | ||
```js | ||
import { createIconPack } from "astro-icon/pack"; | ||
The `Icon` component allows these custom properties: | ||
// Resolves `name` from a remote server, like GitHub! Notice that the `dir` option is not required | ||
export default createIconPack({ | ||
url: "https://raw.githubusercontent.com/radix-ui/icons/master/packages/radix-icons/icons/", | ||
}); | ||
```ts | ||
interface Props extends HTMLAttributes<"svg"> { | ||
/** | ||
* References a specific Icon | ||
*/ | ||
name: string; | ||
title?: string; | ||
size?: number; | ||
width?: number; | ||
height?: number; | ||
} | ||
``` | ||
If you have custom constraints, you can always create the resolver yourself. Export a `default` function that resolves the `name` argument to an SVG string. | ||
The `Icon` also accepts any global HTML attributes and `aria` attributes. They will be forwarded to the rendered `<svg>` element. | ||
```ts | ||
import { loadMyPackSvg } from "my-pack"; | ||
See the [`Props.ts`](./packages/core/lib/Props.ts) file for more details. | ||
export default async (name: string): Promise<string> => { | ||
const svgString = await loadMyPackSvg(name); | ||
return svgString; | ||
}; | ||
``` | ||
### Styling | ||
## Styling | ||
Styling your `astro-icon` is straightforward. Any styles can be targeted to the `[data-icon]` attribute selector. If you want to target a specific icon, you may target it by name using `[data-icon="filename"]`. | ||
Styling your `astro-icon` is straightforward. Any styles can be targeted to the `[astro-icon]` attribute selector. If you want to target a specific icon, you may target it by name using `[astro-icon="filename"]`. | ||
```astro | ||
--- | ||
import { Icon } from 'astro-icon'; | ||
import { Icon } from 'astro-icon/components'; | ||
--- | ||
<style lang="css"> | ||
[astro-icon] { | ||
[data-icon] { | ||
color: blue; | ||
@@ -148,3 +157,3 @@ /* OR */ | ||
} | ||
[astro-icon="annotation"] { | ||
[data-icon="annotation"] { | ||
color: red; | ||
@@ -163,18 +172,107 @@ /* OR */ | ||
## Props | ||
### Using with Frameworks | ||
`<Icon>` and `<Sprite>` share the same interface. | ||
Astro Icon can be used with other frameworks utilizing the [`slot` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot). You can read more about how to use Slots in Astro here. [Passing Children to Framework Components](https://docs.astro.build/en/core-concepts/framework-components/#passing-children-to-framework-components) | ||
The `name` prop references a specific icon. It is required. | ||
## Configuration | ||
The `optimize` prop is a boolean. Defaults to `true`. In the future it will control `svgo` options. | ||
### Configuring the Integration | ||
Both components also accepts any global HTML attributes and `aria` attributes. They will be forwarded to the rendered `<svg>` element. | ||
The Astro Icon integration has its own options for controlling the `Icon` component. Change these in the `astro.config.mjs` file which is where your project's integration settings live. | ||
See the [`Props.ts`](./packages/core/lib/Props.ts) file for more details. | ||
#### config.include | ||
## Troubleshooting | ||
If you want to use icon sets from Iconify, specify that set's name using this integration's `config.include` option. To specify including an entire pack use the wildcard `['*']`. Alternatively, loading individual icons is permitted as an array of those icon names. | ||
### Icon not found | ||
**`astro.config.mjs`** | ||
When an icon is not found, `Icon` uses a fallback icon of a black box. This is likely either a typo on the `name` prop or a missing svg file in the `src/icons` folder. | ||
```js ins={2} | ||
import { defineConfig } from "astro/config"; | ||
import icon from "astro-icon"; | ||
export default defineConfig({ | ||
// ... | ||
integrations: [ | ||
icon({ | ||
include: { | ||
mdi: ["*"], // Loads entire Material Design Icon set | ||
// or | ||
mdi: ["account"], // Only loads the Material Design Icon's "account" SVG | ||
}, | ||
}), | ||
], | ||
}); | ||
``` | ||
#### config.iconDir | ||
If you want to use a different custom svg icon directory instead of the default `src/icons/`, specify that file path using `config.iconDir` | ||
```js ins={2} | ||
import { defineConfig } from "astro/config"; | ||
import icon from "astro-icon"; | ||
export default defineConfig({ | ||
// ... | ||
integrations: [ | ||
icon({ | ||
iconDir: "src/images/icons", | ||
}), | ||
], | ||
}); | ||
``` | ||
#### config.svgoOptions | ||
If you want to change the `svgo` options instead of using the defaults, specify the options using `config.svgoOptions`. Read more about the available [`svgo` options here](https://github.com/svg/svgo#configuration) | ||
```js ins={2} | ||
import { defineConfig } from "astro/config"; | ||
import icon from "astro-icon"; | ||
export default defineConfig({ | ||
// ... | ||
integrations: [ | ||
icon({ | ||
svgoOptions: { | ||
multipass: true, | ||
plugins: [ | ||
{ | ||
name: "preset-default", | ||
params: { | ||
overrides: { | ||
// customize default plugin options | ||
inlineStyles: { | ||
onlyMatchedOnce: false, | ||
}, | ||
// or disable plugins | ||
removeDoctype: false, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}), | ||
], | ||
}); | ||
``` | ||
## Examples | ||
TODO: Examples | ||
## Migrating to v1 | ||
TODO: | ||
- sprite | ||
- icon packs | ||
## Contributing | ||
You're welcome to submit an issue or PR! | ||
## Changelog | ||
See [CHANGELOG.md](CHANGELOG.md) for a history of changes to this integration. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
275
15351
5
10
31
1
+ Added@iconify/tools@^3.0.1
+ Added@iconify/types@^2.0.0
+ Added@iconify/utils@^2.1.5
+ Added@antfu/install-pkg@0.4.1(transitive)
+ Added@antfu/utils@0.7.10(transitive)
+ Added@iconify/tools@3.0.7(transitive)
+ Added@iconify/types@2.0.0(transitive)
+ Added@iconify/utils@2.1.33(transitive)
+ Added@types/cheerio@0.22.35(transitive)
+ Added@types/node@22.7.4(transitive)
+ Added@types/tar@6.1.13(transitive)
+ Added@types/yauzl@2.10.3(transitive)
+ Addedacorn@8.12.1(transitive)
+ Addedbuffer-crc32@0.2.13(transitive)
+ Addedcheerio@1.0.0(transitive)
+ Addedcheerio-select@2.1.0(transitive)
+ Addedchownr@2.0.0(transitive)
+ Addedconfbox@0.1.7(transitive)
+ Addedcss-select@5.1.0(transitive)
+ Addedcss-tree@2.2.12.3.1(transitive)
+ Addedcsso@5.0.5(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddom-serializer@2.0.0(transitive)
+ Addeddomhandler@5.0.3(transitive)
+ Addeddomutils@3.1.0(transitive)
+ Addedencoding-sniffer@0.2.0(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedentities@4.5.0(transitive)
+ Addedextract-zip@2.0.1(transitive)
+ Addedfd-slicer@1.1.0(transitive)
+ Addedfs-minipass@2.1.0(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedhtmlparser2@9.1.0(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedkolorist@1.8.0(transitive)
+ Addedlocal-pkg@0.4.30.5.0(transitive)
+ Addedmdn-data@2.0.282.0.30(transitive)
+ Addedminipass@3.3.64.2.85.0.0(transitive)
+ Addedminizlib@2.1.2(transitive)
+ Addedmkdirp@1.0.4(transitive)
+ Addedmlly@1.7.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpackage-manager-detector@0.2.0(transitive)
+ Addedparse5@7.1.2(transitive)
+ Addedparse5-htmlparser2-tree-adapter@7.0.0(transitive)
+ Addedparse5-parser-stream@7.1.2(transitive)
+ Addedpathe@1.1.2(transitive)
+ Addedpend@1.2.0(transitive)
+ Addedpkg-types@1.2.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsource-map-js@1.2.1(transitive)
+ Addedsvgo@3.0.3(transitive)
+ Addedtar@6.2.1(transitive)
+ Addedtinyexec@0.3.0(transitive)
+ Addedufo@1.5.4(transitive)
+ Addedundici@6.19.8(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addedwhatwg-encoding@3.1.1(transitive)
+ Addedwhatwg-mimetype@4.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyallist@4.0.0(transitive)
+ Addedyauzl@2.10.0(transitive)
- Removednode-fetch@^3.1.0
- Removedresolve-pkg@^2.0.0
- Removedsvgo@^2.8.0
- Removedcss-select@4.3.0(transitive)
- Removedcss-tree@1.1.3(transitive)
- Removedcsso@4.2.0(transitive)
- Removeddata-uri-to-buffer@4.0.1(transitive)
- Removeddom-serializer@1.4.1(transitive)
- Removeddomhandler@4.3.1(transitive)
- Removeddomutils@2.8.0(transitive)
- Removedentities@2.2.0(transitive)
- Removedfetch-blob@3.2.0(transitive)
- Removedformdata-polyfill@4.0.10(transitive)
- Removedmdn-data@2.0.14(transitive)
- Removednode-domexception@1.0.0(transitive)
- Removednode-fetch@3.3.2(transitive)
- Removedresolve-from@5.0.0(transitive)
- Removedresolve-pkg@2.0.0(transitive)
- Removedsource-map@0.6.1(transitive)
- Removedstable@0.1.8(transitive)
- Removedsvgo@2.8.0(transitive)
- Removedweb-streams-polyfill@3.3.3(transitive)