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

Helper as delivery mechanism #152

Open
sandstrom opened this issue Jun 17, 2020 · 0 comments
Open

Helper as delivery mechanism #152

sandstrom opened this issue Jun 17, 2020 · 0 comments

Comments

@sandstrom
Copy link

sandstrom commented Jun 17, 2020

Awesome addon! 🏅

Have you thought about using a helper for icon delivery? We're using something like this and it's really fast. 🚀

Maybe this addon could break apart the two tasks of:

  1. Retrieving icons from NPM etc. and subsetting
  2. Delivery of icon into template/code. Could be left to the user, or could provide both helper and component as base (but I wouldn't go overboard with the component, easier to let people subclass the component if they need special stuff, aria, etc)
import { helper as buildHelper } from '@ember/component/helper';
import { htmlSafe } from '@ember/template';

const ICON_CLASS = 'icon';

const TYPE_CLASS = {
  brand: 'fab',
  light: 'fal',
  regular: 'far',
  solid: 'fas',
  duotone: 'fad',
};

const FALLBACK_TYPE = 'solid';

const ICON_PREFIX = 'fa-';
const SIZE_PREFIX = 'fa-';

// Simple icon helper
// `{{icon 'file-alt' type='regular' size='4x'}}` outputs `<i class='far fa-file-alt fa-4x'></i>`
// (do not rely on the `fa-` class names, consider them internal to this component)
export default buildHelper(function(args, options) {
  let name = args[0];
  let type = options.type || FALLBACK_TYPE;

  if (!Object.keys(TYPE_CLASS).includes(type)) {
    throw new Error(`Icon font type '${type}' not supported`);
  }

  let typeKlass = TYPE_CLASS[type];

  let klass = `${ICON_CLASS} ${typeKlass} ${ICON_PREFIX}${name}`;

  if (options.size) {
    klass += ` ${SIZE_PREFIX}${options.size}`;
  }

  if (options.class) {
    klass += ` ${options.class}`;
  }

  return htmlSafe(`<i class='${klass}'></i>`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant