GithubHelp home page GithubHelp logo

Comments (16)

cyberalien avatar cyberalien commented on June 18, 2024 1

from iconify.

cyberalien avatar cyberalien commented on June 18, 2024 1

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!

from iconify.

cyberalien avatar cyberalien commented on June 18, 2024 1

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.

from iconify.

cyberalien avatar cyberalien commented on June 18, 2024 1

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).

from iconify.

antfu avatar antfu commented on June 18, 2024

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>

from iconify.

aminya avatar aminya commented on June 18, 2024

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

from iconify.

cyberalien avatar cyberalien commented on June 18, 2024

from iconify.

aminya avatar aminya commented on June 18, 2024

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.

from iconify.

aminya avatar aminya commented on June 18, 2024

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

from iconify.

aminya avatar aminya commented on June 18, 2024

I got it working using the beta version.

image

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

from iconify.

aminya avatar aminya commented on June 18, 2024

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("..")

from iconify.

cyberalien avatar cyberalien commented on June 18, 2024

from iconify.

aminya avatar aminya commented on June 18, 2024

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

from iconify.

cyberalien avatar cyberalien commented on June 18, 2024

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.

from iconify.

aminya avatar aminya commented on June 18, 2024

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")

from iconify.

aminya avatar aminya commented on June 18, 2024

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

from iconify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.