Skip to content

Commit

Permalink
Merge pull request #41 from farm-fe/feat-icons
Browse files Browse the repository at this point in the history
feat(icons): suport viewbox
  • Loading branch information
CCherry07 committed Jul 13, 2024
2 parents ac92094 + d5b2add commit aeaf096
Show file tree
Hide file tree
Showing 23 changed files with 1,998 additions and 361 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/icons/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "farmfe_plugin_icons"
version = "0.0.0"
version = "0.0.1"

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -13,7 +13,6 @@ farmfe_macro_plugin = { version = "*" }
farmfe_toolkit = "0.0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rand = "0.8"
farmfe_utils = "0.1.4"
svgr-rs = "0.1.2"
once_cell = "1.19.0"
Expand All @@ -22,3 +21,4 @@ reqwest = "0.12.5"
tokio = { version = "1.38.0", features = ["full"] }
walkdir = "2.5.0"
xmltree = "0.10.3"
# rand = "0.8"
217 changes: 217 additions & 0 deletions packages/icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# @farmfe/plugin-icons

Access thousands of icons as components **on-demand** universally.

### Features

- 🌏 Universal
- 🤹 **Any** icon sets - ~150 popular sets with over 200,000 icons, logos, emojis, etc. Powered by [Iconify](https://github.com/iconify/iconify).
- 🚀 **Major** frameworks - Vanilla, Web Components, React, Vue 3, Solid, Svelte, and more. [Contribute](./src/compiler).
- 🍱 **Any** combinations of them!
- ☁️ On-demand - Only bundle the icons you really use, while having all the options.
- 🌈 Stylable - Change size, color, or even add animations as you would with styles and classes.
- 📥 [Custom icons](#custom-icons) - load your custom icons to get universal integrations at ease.
- 🦾 TypeScript support.
- 🔍 [Browse Icons](https://icones.js.org/)

## Usage

Import icons names with the convention `~icons/{collection}/{icon}` and use them directly as components.

###### React

```jsx
import IconAccessibility from '~icons/carbon/accessibility'
import IconAccountBox from '~icons/mdi/account-box'

function App() {
return (
<div>
<IconAccessibility />
<IconAccountBox style={{ fontSize: '2em', color: 'red' }} />
</div>
)
}
```

###### Vue

```html
<script setup>
import IconAccessibility from '~icons/carbon/accessibility'
import IconAccountBox from '~icons/mdi/account-box'
</script>

<template>
<icon-accessibility/>
<icon-account-box style="font-size: 2em; color: red"/>
</template>
```

## Install

### Plugin

```bash
npm i -D @farmfe/plugin-icons
```

### Icons Data

We use [Iconify](https://iconify.design/) as the icons data source (supports 100+ iconsets).

You have two ways to install them:

###### Install Full Collection

```bash
npm i -D @iconify/json
```

`@iconify/json` (~120MB) includes all the iconsets from Iconify so you can install once and use any of them as you want (only the icons you actually use will be bundle into the production build).

###### Install by Icon Set

If you only want to use a few of the icon sets and don't want to download the entire collection, you can also install them individually with `@iconify-json/[collection-id]`.
For example, to install [Material Design Icons](https://icon-sets.iconify.design/mdi/), you can do:

```bash
npm i -D @iconify-json/mdi
```

To boost your workflow, it's also possible to let `unplugin-icons` handle that installation by enabling the `autoInstall` option.

```ts
Icons({
// experimental
autoInstall: true,
})
```

It will install the icon set when you import them. The right package manager will be auto-detected (`npm`, `yarn` or `pnpm`).

## Configuration

Create a `farm.config.js` [configuration file](https://www.farmfe.org/docs/config/configuring-farm) and import the plugin:

```ts
import { defineConfig } from '@farmfe/core';
import Icons from '@farmfe/plugin-plugin';

export default defineConfig({
plugins: [
["@farmfe/plugin-icons", {
/**
* @description Whether to automatically install the required dependencies
* @type {boolean}
* @default true
*/
autoInstall: true,
/**
* @description The compiler used by the plugin
* @type {string}
* @default "jsx"
* @enum ["jsx", "vue","react","preact","solid","svelte"]
*/
compiler: "jsx",
/**
* @description The default style to apply to the svg element
* @type {object}
* @default {}
*/
defaultStyle: {},
/**
* @description The default class to apply to the svg element
* @type {string}
*/
defaultClass: "",
/**
* @description Custom icon collection, support local svg and remote svg
* @type {string}
* @uses [iconname] to replace the icon name
* @example
* import icon from "~icons/local/icon.svg"
* import icon from "~icons/remote/icon.svg"
*/
customCollections: {
local: './src/assets',
remote: "https://cdn.simpleicons.org/[iconname]/"
}
}],
],
});
```

## Use RAW compiler from query params

From `v0.13.2` you can also use `raw` compiler to access the `svg` icon and use it on your html templates, just add `raw` to the icon query param.

For example, using `vue3`:

```vue
<script setup lang='ts'>
import RawMdiAlarmOff from '~icons/mdi/alarm-off?raw&width=4em&height=4em'
import RawMdiAlarmOff2 from '~icons/mdi/alarm-off?raw&width=1em&height=1em'
</script>
<template>
<!-- raw example -->
<pre>
import RawMdiAlarmOff from '~icons/mdi/alarm-off?raw&width=4em&height=4em'
{{ RawMdiAlarmOff }}
import RawMdiAlarmOff2 from '~icons/mdi/alarm-off?raw&width=1em&height=1em'
{{ RawMdiAlarmOff2 }}
</pre>
<!-- svg example -->
<span v-html="RawMdiAlarmOff" />
<span v-html="RawMdiAlarmOff2" />
</template>
```

## Custom Icons

you can now load your own icons!

```ts

Icons({
customCollections: {
'my-other-icons': "https://example.com/icons/[iconname].svg",
// a helper to load icons from the file system
// files under `./assets/icons` with `.svg` extension will be loaded as it's file name
'my-yet-other-icons': './assets/icons',
},
})
```

Then use as

```ts
import IconAccount from '~icons/my-icons/account'
import IconFoo from '~icons/my-other-icons/foo'
import IconBar from '~icons/my-yet-other-icons/bar'
```

## Icon customizer

you can also customize each icon using query params when importing them.

you can use `query` params to apply to individual icons:

<!-- eslint-skip -->

```vue
<script setup lang='ts'>
import MdiAlarmOff from 'virtual:icons/mdi/alarm-off?width=4em&height=4em'
import MdiAlarmOff2 from 'virtual:icons/mdi/alarm-off?width=1em&height=1em'
</script>
<template>
<!-- width=4em and height=4em -->
<mdi-alarm-off />
<!-- width=4em and height=4em -->
<MdiAlarmOff />
<!-- width=1em and height=1em -->
<MdiAlarmOff2 />
</template>
```
44 changes: 38 additions & 6 deletions packages/icons/playground-react/farm.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from "@farmfe/core";

import farmJsPluginPostcss from '@farmfe/js-plugin-postcss';
import visualizer from '@farmfe/js-plugin-visualizer'
export default defineConfig({
compilation: {
input: {
Expand All @@ -9,15 +10,46 @@ export default defineConfig({
progress: false,
},
plugins: [
farmJsPluginPostcss(),
visualizer(),
["@farmfe/plugin-react", { runtime: "automatic" }],
["@farmfe/plugin-icons", {
/**
* @description Whether to automatically install the required dependencies
* @type {boolean}
* @default true
*/
autoInstall: true,
/**
* @description The compiler used by the plugin
* @type {string}
* @default "jsx"
* @enum ["jsx", "vue","react","preact","solid","svelte"]
*/
compiler: "jsx",
defaultStyle: {
width: "2em",
height: "2em",
},
defaultClass:"icon"
/**
* @description The default style to apply to the svg element
* @type {object}
* @default {}
*/
defaultStyle: {},
/**
* @description The default class to apply to the svg element
* @type {string}
*/
defaultClass: "",
/**
* @description Custom icon collection, support local svg and remote svg
* @type {string}
* @uses [iconname] to replace the icon name
* @ex
* import icon from "~icons/local/icon.svg"
* import icon from "~icons/remote/icon.svg"
*/
customCollections: {
local: './src/assets',
remote: "https://cdn.simpleicons.org/[iconname]/"
}
}],
],
});
27 changes: 17 additions & 10 deletions packages/icons/playground-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@
"clean": "farm clean"
},
"dependencies": {
"@farmfe/js-plugin-postcss": "^1.7.0",
"@farmfe/js-plugin-visualizer": "^1.0.0",
"@farmfe/plugin-icons": "workspace:*",
"@iconify-json/carbon": "^1.1.36",
"@iconify/json": "^2.2.225",
"@iconify-json/logos": "^1.1.43",
"@iconify/json": "^2.2.227",
"browser": "link:@unocss/preset-icons/browser",
"clsx": "^1.2.1",
"react": "18",
"react-dom": "18"
"postcss": "^8.4.39",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@farmfe/cli": "^1.0.0",
"@farmfe/core": "^1.0.22",
"@farmfe/plugin-react": "^1.0.1",
"@types/react": "18",
"@types/react-dom": "18",
"core-js": "^3.36.1",
"react-refresh": "^0.14.0"
"@farmfe/cli": "^1.0.2",
"@farmfe/core": "^1.3.0",
"@farmfe/plugin-react": "^1.1.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@unocss/postcss": "^0.61.3",
"core-js": "^3.37.1",
"react-refresh": "^0.14.2",
"unocss": "^0.61.3"
}
}
8 changes: 8 additions & 0 deletions packages/icons/playground-react/postcss.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// postcss.config.mjs
import UnoCSS from '@unocss/postcss'

export default {
plugins: [
UnoCSS(),
],
}
1 change: 1 addition & 0 deletions packages/icons/playground-react/src/main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@unocss;
#root {
max-width: 1280px;
margin: 0 auto;
Expand Down
8 changes: 6 additions & 2 deletions packages/icons/playground-react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { useState } from "react";
import "./main.css";
import reactLogo from "./assets/react.svg";
import FarmLogo from "./assets/logo.png";
import IconAccessibility from '~icons/fontisto/ambulance'
// import 'virtual:uno.css'
import ReactLogo from '~icons/logos/react'
export function Main() {
const [count, setCount] = useState(0);
console.log("rendering Main component")
Expand All @@ -15,9 +16,12 @@ export function Main() {
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
<div style={{display:'flex',alignItems:"center"}}>
<div><ReactLogo className=" text-100px text-#00D8FF"/></div>
<div className="i-logos-react text-100px text-#00D8FF"></div>
</div>
</div>
<h1>Farm + React</h1>
<IconAccessibility color={'#0080ff'}/>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>
count is {count}
Expand Down
Loading

0 comments on commit aeaf096

Please sign in to comment.