Skip to content

Commit 9cd2417

Browse files
authored
feat: ✨ add interface for accessing ionic icons (#27)
1 parent 04fae3e commit 9cd2417

File tree

6 files changed

+69
-8
lines changed

6 files changed

+69
-8
lines changed

docs/content/2.getting-started.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Getting started
3-
description: ''
3+
description: 'Download & install Ionic and setup Capacitor for usage'
44
---
55

66
## Installation
@@ -96,6 +96,11 @@ export default defineNuxtConfig({
9696
Default: `true`
9797
Disable to configure Ionic Router yourself.
9898

99+
- **icons**
100+
101+
Default: `true`
102+
Disable to stop icons from being auto-imported.
103+
99104
### `css`
100105

101106
- **core**

docs/content/3.usage.md renamed to docs/content/3.features.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Usage
3-
description: ''
2+
title: Features
3+
description: 'All the features this module offers for building apps with Ionic at great speed'
44
---
55

66
## Page routing
77

8-
Out of the box, you can start building your Ionic application by creating routes within `~/pages` directory. To access the router, you should use `useIonRouter()` rather than an import from `vue-router`.
8+
Out of the box, you can start building your Ionic application by creating routes within `~/pages` directory. To access the router, you should use `useIonRouter()` rather than `useRouter()` (which is auto-imported from `vue-router`).
99

1010
Nuxt utilities like `definePageMeta` will continue to work, but you should avoid using `<NuxtPage>` or `<NuxtLayout>`.
1111

@@ -30,6 +30,34 @@ All Ionic Vue components should be auto-imported throughout your app. (If you fi
3030

3131
For more on how component imports work, see the [Nuxt documentation](https://v3.nuxtjs.org/guide/directory-structure/components#components-directory).
3232

33+
## Icons
34+
35+
Icons are auto-imported from `ionicons/icons` by default, following the pattern of camel case naming with `ionicons` in front of the original icon name from the [official icons website](https://ionic.io/ionicons).
36+
37+
For example, instead of this
38+
39+
```vue [component.vue]
40+
<script setup lang="ts">
41+
import { image, squareSharp, triangleOutline } from 'ionicons/icons'
42+
</script>
43+
44+
<template>
45+
<ion-icon :icon="image"></ion-icon>
46+
<ion-icon :md="squareSharp" :ios="triangleOutline"></ion-icon>
47+
</template>
48+
```
49+
50+
You would write this
51+
52+
```vue [component.vue]
53+
<template>
54+
<ion-icon :icon="ioniconsImage"></ion-icon>
55+
<ion-icon :md="ioniconsSquareSharp" :ios="ioniconsTriangleOutline"></ion-icon>
56+
</template>
57+
```
58+
59+
You can opt-out of auto-importing by setting `integrations.icons` module options in your `nuxt.config.ts` to `false`.
60+
3361
## Helper functions
3462

3563
Nuxt provides a number of Ionic hooks/composables via auto-imports within your app:

playground/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default defineNuxtConfig({
44
modules: ['nuxt-ionic'],
55
ionic: {
66
// integrations: {
7+
// icons: true,
78
// meta: true,
89
// pwa: true,
910
// router: true,

playground/pages/tabs.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts">
2-
import { images, square, triangle } from 'ionicons/icons'
32
useHead({
43
title: 'Nuxt Ionic',
54
})
@@ -12,17 +11,17 @@ useHead({
1211
<ion-router-outlet />
1312
<ion-tab-bar slot="bottom">
1413
<ion-tab-button tab="tab1" href="/tabs/tab1">
15-
<ion-icon :icon="triangle" />
14+
<ion-icon :icon="ioniconsHomeOutline" />
1615
<ion-label>Tab 1</ion-label>
1716
</ion-tab-button>
1817

1918
<ion-tab-button tab="tab2" href="/tabs/tab2">
20-
<ion-icon :icon="images" />
19+
<ion-icon :icon="ioniconsImagesOutline" />
2120
<ion-label>Photos</ion-label>
2221
</ion-tab-button>
2322

2423
<ion-tab-button tab="tab3" href="/tabs/tab3">
25-
<ion-icon :icon="square" />
24+
<ion-icon :icon="ioniconsBulbOutline" />
2625
<ion-label>Tab 3</ion-label>
2726
</ion-tab-button>
2827
</ion-tab-bar>

src/module.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { defineUnimportPreset } from 'unimport'
88
import { runtimeDir } from './utils'
99

1010
import { useCSSSetup } from './parts/css'
11+
import { setupIcons } from './parts/icons'
1112
import { setupMeta } from './parts/meta'
1213
import { setupPWA } from './parts/pwa'
1314
import { setupRouter } from './parts/router'
@@ -17,6 +18,7 @@ export interface ModuleOptions {
1718
router?: boolean
1819
pwa?: boolean
1920
meta?: boolean
21+
icons?: boolean
2022
}
2123
css?: {
2224
core?: boolean
@@ -35,6 +37,7 @@ export default defineNuxtModule<ModuleOptions>({
3537
meta: true,
3638
pwa: true,
3739
router: true,
40+
icons: true,
3841
},
3942
css: {
4043
core: true,
@@ -102,6 +105,11 @@ export default defineNuxtModule<ModuleOptions>({
102105
await setupUtilities()
103106
}
104107

108+
// Add auto-imported icons
109+
if (options.integrations?.icons) {
110+
await setupIcons()
111+
}
112+
105113
if (options.integrations?.meta) {
106114
await setupMeta()
107115
}

src/parts/icons.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useNuxt } from '@nuxt/kit'
2+
import { defineUnimportPreset } from 'unimport'
3+
import * as icons from 'ionicons/icons'
4+
5+
const iconsPreset = defineUnimportPreset({
6+
from: 'ionicons/icons',
7+
// @ts-ignore-next-line
8+
imports: Object.keys(icons.default || icons).map(name => ({
9+
name,
10+
as: 'ionicons' + name[0].toUpperCase() + name.slice(1),
11+
})),
12+
})
13+
14+
export const setupIcons = () => {
15+
const nuxt = useNuxt()
16+
17+
nuxt.hook('autoImports:sources', presets => {
18+
presets.push(iconsPreset)
19+
})
20+
}

0 commit comments

Comments
 (0)