Skip to content

Commit 66556ea

Browse files
committed
feat(custom-elements): export ionicons/components
Also provide ionicons as a custom element which can be defined using `customElements.define()` ``` import { IonIcon } from "ionicons/components/ion-icon"; customElements.define("ion-icon", IonIcon); ``` The custom element build works the same as the lazy load build, and is also able to lazy load svgs on demand, either by giving it an exact url in the `src` attribute `<ion-icon src="./path/to/airplane.svg"></ion-icon>`, or by providing the name of the ionicon to load `<ion-icon name="airplane"></ion-icon>`. If using the `name` attribute, you'll also need to set where to find the svg directory using `setAssetPath()` ``` import { setAssetPath } from "ionicons/components"; setAssetPath(location.href); ```
1 parent b1c23f8 commit 66556ea

File tree

7 files changed

+4102
-17388
lines changed

7 files changed

+4102
-17388
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ log.txt
1010
.idea/
1111
.versions/
1212
.vscode/
13+
components/
1314
node_modules/
1415
tmp/
1516
dist/

package-lock.json

+4,061-17,370
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+15-13
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"dist/",
77
"icons/"
88
],
9-
"module": "dist/index.mjs",
10-
"main": "dist/index.js",
9+
"main": "./dist/index.cjs.js",
10+
"module": "./dist/index.js",
1111
"types": "dist/types/index.d.ts",
1212
"unpkg": "dist/ionicons.js",
1313
"collection": "dist/collection/collection-manifest.json",
@@ -22,17 +22,20 @@
2222
"release": "np --no-2fa",
2323
"version": "npm run build"
2424
},
25+
"dependencies": {
26+
"@stencil/core": "^2.4.0"
27+
},
2528
"devDependencies": {
26-
"@stencil/core": "^1.15.0",
27-
"@types/fs-extra": "^9.0.1",
28-
"@types/jest": "^26.0.14",
29-
"@types/node": "^14.11.5",
29+
"@types/fs-extra": "^9.0.6",
30+
"@types/jest": "^26.0.20",
31+
"@types/node": "^14.14.22",
3032
"@types/svgo": "^1.3.3",
31-
"fs-extra": "9.0.1",
32-
"jest": "^26.5.2",
33-
"jest-cli": "^26.5.2",
34-
"np": "^6.5.0",
35-
"svgo": "1.3.2"
33+
"fs-extra": "^9.1.0",
34+
"jest": "^26.6.3",
35+
"jest-cli": "^26.6.3",
36+
"np": "^7.2.0",
37+
"svgo": "1.3.2",
38+
"typescript": "^4.1.3"
3639
},
3740
"keywords": [
3841
"icon pack",
@@ -67,6 +70,5 @@
6770
"license": "MIT",
6871
"sideEffects": [
6972
"icons/imports/"
70-
],
71-
"dependencies": {}
73+
]
7274
}

scripts/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs-extra';
2-
import { join, basename } from 'path';
2+
import { join } from 'path';
33
import Svgo from 'svgo';
44

55
async function build(rootDir: string) {

scripts/collection-copy.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs-extra';
22
import { join } from 'path';
33

4-
54
async function collectionCopy(rootDir: string) {
65
// move optimized svgs to correct collection location
76
const optimizedSrc = join(rootDir, 'dist', 'ionicons', 'svg');
@@ -11,6 +10,23 @@ async function collectionCopy(rootDir: string) {
1110
// we don't to copy the src svgs to collection
1211
await fs.remove(join(rootDir, 'dist', 'collection', 'svg'));
1312

13+
const cePackageDir = join(rootDir, 'components');
14+
const cePackageJsonPath = join(cePackageDir, 'package.json');
15+
const ceCjsPath = join(cePackageDir, 'index.cjs.js');
16+
17+
const emptyCjs = `/*empty cjs*/`;
18+
await fs.writeFile(ceCjsPath, emptyCjs);
19+
20+
const cePackageJson = {
21+
name: 'ionicons/components',
22+
description: 'Ionicons custom element.',
23+
main: './index.cjs.js',
24+
module: './index.js',
25+
types: './index.d.ts',
26+
private: true,
27+
};
28+
await fs.writeFile(cePackageJsonPath, JSON.stringify(cePackageJson, null, 2));
29+
1430
// this is temporary!!!!
1531
// removing the `type` from the d.ts export
1632
// to make it easier for users migrating between
@@ -23,4 +39,4 @@ async function collectionCopy(rootDir: string) {
2339
await fs.writeFile(typesDist, types);
2440
}
2541

26-
collectionCopy(join(__dirname, '..'));
42+
collectionCopy(join(__dirname, '..'));

src/components/icon/icon.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getName, getUrl } from './utils';
44

55
@Component({
66
tag: 'ion-icon',
7-
assetsDir: 'svg',
7+
assetsDirs: ['svg'],
88
styleUrl: 'icon.css',
99
shadow: true,
1010
})
@@ -29,7 +29,7 @@ export class Icon {
2929
/**
3030
* Specifies the label to use for accessibility. Defaults to the icon name.
3131
*/
32-
@Prop({ mutable: true, reflectToAttr: true }) ariaLabel?: string;
32+
@Prop({ mutable: true, reflect: true }) ariaLabel?: string;
3333

3434
/**
3535
* Set the icon to hidden, respectively `true`, to remove it from the accessibility tree.

stencil.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export const config: Config = {
77
type: 'dist',
88
empty: false,
99
},
10+
{
11+
type: 'dist-custom-elements',
12+
dir: './components',
13+
},
1014
{
1115
type: 'docs-readme',
1216
},

0 commit comments

Comments
 (0)