-
-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Icon not showing in simple JavaScript #30
Comments
If you are able to use it without a bundler, you can simply add this line to your header <script src="https://code.iconify.design/1/1.0.7/iconify.min.js"></script> |
I do not have an HTML file to include this in. I am writing a package for Atom which is written inside Electron. I am using Rollup, but I declared iconify as external so the bundler does not make a difference. See this to know what I am talking about: https://github.com/suda/tool-bar/pull/310/files. I switched to @mdi/js here, however, I was wondering if I can use iconify which allows me to have access to all of the icons. My question is that what this import does? import Iconify from '@iconify/iconify'; Is this a function I can call or a class I should initialize? Basic documentation is needed. This single import will be removed with any type of bundler or minifier. I have disabled all of those but I do not have still any luck |
That is expected behavior. Script scans DOM for placeholder. Your node
isn't in DOM yet, so script cannot find it.
I'll see if it would be possible to add functionality to parse nodes
outside of DOM. Function ScanDOM in version 2 supports custom root node as
first parameter, but it needs testing.
…On Sun, 26 Jul 2020, 13:21 Anthony Fu, ***@***.***> wrote:
I am not very familiar with the import usage. Trying to pin @cyberalien
<https://github.com/cyberalien> here and I think he may able to help you
:)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGIYD6K7C74IOHF5OEOPHLR5P7Q3ANCNFSM4PH5BGSQ>
.
|
Can you try it with version 2? `@iconify/iconify@beta`
…On Sun, 26 Jul 2020, 13:29 Amin Yahyaabadi, ***@***.***> wrote:
This is my final HTML using the previous method
[image: image]
<https://user-images.githubusercontent.com/16418197/88476782-c927cd80-cf00-11ea-8039-a8b5cd057cb8.png>
If it needs to be created using DOM, I can do that too. It is pretty easy
for this case.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGIYDZHUCN2OS4P73FHOALR5QAP5ANCNFSM4PH5BGSQ>
.
|
Could you add this simple example to the documentation so others can use? import Iconify from '@iconify/iconify';
export function createIconElement(iconName) {
const icon = document.createElement("span")
icon.classList.add("iconify")
icon.setAttribute("data-icon", iconName)
return icon
}
const icon = createIconElement("ic:baseline-access-time") // gives your desired icon as an element I believe something like this import Iconify from '@iconify/iconify';
Iconify.createIconElement("..") |
Good idea! Thanks
…On Sun, 26 Jul 2020, 13:45 Amin Yahyaabadi, ***@***.***> wrote:
Could you add this simple example to the documentation so others can use?
import Iconify from ***@***.***/iconify';
export function createIcon(iconName) {
const icon = document.createElement("span")
icon.classList.add("iconify")
icon.setAttribute("data-icon", `${iconName}`)
return icon}
const icon = createIcon("ic:baseline-access-time") // gives your desired icon as an element
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGIYD2WHCWSILFD6M3KZHDR5QCLJANCNFSM4PH5BGSQ>
.
|
I will keep this close until you add those. Thanks! |
Do you know of a very simple Atom package that I can play with, which won't require a lot of Electron/Atom knowledge to setup? I'd like to test Iconify behaviour in it. |
I set up an environment for you.
|
Thanks! Got it working. 2 missing commands:
Not sure if first one was needed. Unfortunately I couldn't debug it today, so will do it tomorrow. It is working! |
I forgot that in version 2 I've already added function similar to createElement: renderSVG export function createIconElement(icon) {
return Iconify.renderSVG(icon);
} It will return null if icon does not exist, which would usually complicate things a bit, however because you also want offline usage, this is not an issue because icon should always exist. If you want to make sure an element is always returned, even when icon is missing, use this: export function createIconElement(icon) {
return Iconify.iconExists(icon) ? Iconify.renderSVG(icon) : document.createElement('span');
} As for offline use, best option is to create a bundle. In your test files I've made const fs = require("fs");
const iconFinder = require("@iconify/json");
// Source file for Iconify
const iconifySource = "@iconify/iconify/dist/iconify.without-api";
// Bundle file
const outputFile = "./lib/iconify-bundle.js";
// Icon sets to load. Key = prefix in Iconify sets, value = prefix in output
const iconSets = {
octicon: "octicon",
ion: "ion",
foundation: "fi", // Rename "foundation" to "fi"
"icomoon-free": "icomoon", // Rename "icomoon-free" to "icomoon"
mdi: "mdi",
ic: "ic",
"fa-brands": "fab", // Rename "fa-brands" to "fab"
fa: "fa",
};
// Bundle Iconify
const resolvedIconify = require.resolve(iconifySource);
let bundle = fs.readFileSync(resolvedIconify, "utf8");
// Bundle icon sets
Object.keys(iconSets).forEach((prefix) => {
const source = iconFinder.locate(prefix);
if (!source) {
throw new Error(`Unable to locate icon set "${prefix}"`);
}
const data = JSON.parse(fs.readFileSync(source, "utf8"));
// Remove useless metadata
["info", "categories", "themes", "chars"].forEach((attr) => {
delete data[attr];
});
// Change prefix
data.prefix = iconSets[prefix];
// Add to bundle
bundle += "\nIconify.addCollection(" + JSON.stringify(data) + ");";
});
// Save bundle
fs.writeFileSync(outputFile, bundle, "utf8");
console.log(`Saved ${outputFile} (${bundle.length} bytes)`);
// Try to copy .d.ts
const tsSource = resolvedIconify.replace(".js", ".d.ts");
try {
const tsContent = fs.readFileSync(tsSource);
fs.writeFileSync(outputFile.replace(".js", ".d.ts"), tsContent);
} catch (err) {
//
} Install Then I've replaced import Iconify from "../iconify-bundle";
export function createIconElement(icon) {
return Iconify.iconExists(icon) ? Iconify.renderSVG(icon) : document.createElement('span');
}
// the icon property will be passed into createIconElement automatically
export const testIcons = [
{ icon: "ic:baseline-access-time", color: "yellow" },
{ icon: "mdi:content-save", color: "red" },
]; Bundle is 7.35mb in size, which I think is reasonable considering that it includes multiple icon sets. In Few notes about icon sets:
|
There are big changes in beta 4 that has just been released, which can solve your problem differently. You don't really need it, but I think it is worth mentioning if it helps anyone else to solve similar issue. In original message you wanted Iconify to replace icons in node that isn't attached to DOM. It wasn't possible in old version because Iconify scanned only body. Now it is possible. Method 1 can scan node once, do not watch it for changes: Method 2 will scan node and will observe it for changes: |
@cyberalien Thank you so much! I will try these and will let you know how it goes |
I am trying to use iconify but I cannot. Is this correct? Nothing shows for me. What should I do with
Iconify
after import? The documentation is very vauge.The text was updated successfully, but these errors were encountered: