Sha256: 51d8950a7416d48d6a5abcad0596074160ce6f16c17aefa0d56b7d3ad0a40610

Contents?: true

Size: 1.57 KB

Versions: 30

Compression:

Stored size: 1.57 KB

Contents

const DEFAULT_ATTRIBUTES = {
  role: "img",
  "aria-hidden": "true"
};

/**
 * Generates a Decidim icon element and returns it as a string.
 * @param {String} iconKey - the key of the icon to be generated
 * @param {Object} attributes - extra attributes to define for the icon SVG
 * @param {int} wait - number of milliseconds to wait before executing the function.
 * @private
 * @returns {Void} - Returns nothing.
 */
export default function icon(iconKey, attributes = {}) {
  const iconAttributes = $.extend(DEFAULT_ATTRIBUTES, attributes);
  const title = iconAttributes.title || iconAttributes.ariaLabel;
  Reflect.deleteProperty(iconAttributes, "title");

  const htmlAttributes = {
    "class": `icon icon--${iconKey}`
  };
  Object.keys(iconAttributes).forEach((key) => {
    // Convert the key to dash-format.
    const newKey = key.replace(/([A-Z])/g, (ucw) => `-${ucw[0].toLowerCase()}`);
    if (typeof htmlAttributes[key] === "undefined") {
      htmlAttributes[newKey] = iconAttributes[key];
    } else {
      htmlAttributes[newKey] = `${htmlAttributes[newKey]} ${iconAttributes[key]}`;
    }
  });

  const iconsPath =  window.Decidim.config.get("icons_path");
  const elHtml = `<svg><use href="${iconsPath}#icon-${iconKey}"></use></svg>`;
  const $el = $(elHtml);
  if (title) {
    $el.prepend(`<title>${title}</title>`);
  } else {
    // This keeps accessibility audit tools happy
    $el.prepend(`<title>${iconKey}</title>`);
    // Force hidden if title is not defined
    htmlAttributes["aria-hidden"] = "true";
  }
  $el.attr(htmlAttributes);

  return $("<div />").append($el).html();
}

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/packs/src/decidim/icon.js
decidim-core-0.27.8 app/packs/src/decidim/icon.js
decidim-core-0.27.7 app/packs/src/decidim/icon.js
decidim-core-0.27.6 app/packs/src/decidim/icon.js
decidim-core-0.26.10 app/packs/src/decidim/icon.js
decidim-core-0.26.9 app/packs/src/decidim/icon.js
decidim-core-0.27.5 app/packs/src/decidim/icon.js
decidim-core-0.26.8 app/packs/src/decidim/icon.js
decidim-core-0.27.4 app/packs/src/decidim/icon.js
decidim-core-0.27.3 app/packs/src/decidim/icon.js
decidim-core-0.26.7 app/packs/src/decidim/icon.js
decidim-core-0.26.5 app/packs/src/decidim/icon.js
decidim-core-0.27.2 app/packs/src/decidim/icon.js
decidim-core-0.27.1 app/packs/src/decidim/icon.js
decidim-core-0.26.4 app/packs/src/decidim/icon.js
decidim-core-0.27.0 app/packs/src/decidim/icon.js
decidim-core-0.26.3 app/packs/src/decidim/icon.js
decidim-core-0.27.0.rc2 app/packs/src/decidim/icon.js
decidim-core-0.27.0.rc1 app/packs/src/decidim/icon.js
decidim-core-0.26.2 app/packs/src/decidim/icon.js