Skip to content
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

Closed
aminya opened this issue Jul 26, 2020 · 16 comments
Closed

Icon not showing in simple JavaScript #30

aminya opened this issue Jul 26, 2020 · 16 comments

Comments

@aminya
Copy link

aminya commented Jul 26, 2020

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.

import Iconify from '@iconify/iconify';
export function createIcon(iconName) {
  const icon = document.createElement("div");
  icon.innerHTML = `
    <span class="iconify" data-icon="${iconName}"></span>
  `
  return icon
}

image

@antfu
Copy link
Contributor

antfu commented Jul 26, 2020

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>

@aminya
Copy link
Author

aminya commented Jul 26, 2020

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

@cyberalien
Copy link
Member

cyberalien commented Jul 26, 2020 via email

@aminya
Copy link
Author

aminya commented Jul 26, 2020

This is my final HTML using the previous method
image

If it needs to be created using DOM, I can do that too. It is pretty easy for this case.

@cyberalien
Copy link
Member

cyberalien commented Jul 26, 2020 via email

@aminya
Copy link
Author

aminya commented Jul 26, 2020

Using DOM:

import Iconify from '@iconify/iconify';
export async function createMDIIcon(iconName) {
  const icon = document.createElement("span")
  icon.classList.add("iconify")
  icon.setAttribute("data-icon", "ic:baseline-access-time")
  return icon
}

image

@aminya
Copy link
Author

aminya commented Jul 26, 2020

I got it working using the beta version.

image

Also, the async seems to cause some issues. I went sync here:

@aminya
Copy link
Author

aminya commented Jul 26, 2020

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 createIconElement function should be part of @iconify itself.

import Iconify from '@iconify/iconify';
Iconify.createIconElement("..")

@cyberalien
Copy link
Member

cyberalien commented Jul 26, 2020 via email

@aminya
Copy link
Author

aminya commented Jul 26, 2020

I will keep this close until you add those. Thanks!

@cyberalien
Copy link
Member

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.

@aminya
Copy link
Author

aminya commented Jul 26, 2020

I set up an environment for you.

  1. install atom
  2. (if you like) install atom-ide-javascript
apm install atom-ide-javascript
  1. clone my branch:
cd ~/.atom/packages   
git clone --single-branch --branch iconify https://github.com/aminya/tool-bar.git
cd tool-bar
  1. Install the deps for the package and fire up Rollup
npm install
npm run dev
  1. open Atom in dev mode
atom --dev .
  1. I set up the lib/icon-service/iconify.js for you to edit. This is the only file you need to care about. It has my simple function which makes the icon element.

  2. To see the changes press (CTRL+SHIFT+F5) to reload Atom.

  3. You can open Chrome's dev menu by (CTRL+Shift+P -> running "Window: toggle dev tools")

@cyberalien
Copy link
Member

Thanks! Got it working.

2 missing commands:

apm install .
apm link

Not sure if first one was needed.

Unfortunately I couldn't debug it today, so will do it tomorrow. It is working!

@cyberalien
Copy link
Member

cyberalien commented Jul 28, 2020

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 build-iconify.js in root directory that creates lib/iconify-bundle.js:

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 @iconify/json as dev dependency, run node build-iconify.

Then I've replaced lib/icon-service/iconify.js with this:

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 build-iconify.js change icon sets list as needed. Because you have prefixes for icon sets that are sometimes different from ones used in Iconify, I've made variable iconSet an object, where key is Iconify prefix, value is prefix you want to use.

Few notes about icon sets:

  • Do not use FontAwesome, especially version 4. Version 4 was imported from icon font, icons have very inconsistent spacing and might look terrible. Version 5 is better, but still doesn't follow any sensible grid. It is a badly designed icon set.
  • IonIcons you've listed use "ios-" and "md-" prefixes. That's outdated version of IonIcons. New version no longer uses prefixes. Good news is Iconify icon set does include icons with those prefixes, so it will work. Iconify used to include those icons a while ago, crawler instead of removing icons makes them hidden, so old icons are always available in any icon set.

@cyberalien
Copy link
Member

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: Iconify.scan(node).

Method 2 will scan node and will observe it for changes: Iconify.observe(node). To remove observer, use Iconify.stopObserving(node).

@aminya
Copy link
Author

aminya commented Aug 5, 2020

@cyberalien Thank you so much! I will try these and will let you know how it goes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants