/** * Handlebars {{malibu-icon size='28' iconClass='gradient-dark-gray' name='app-28' title="Optional Title"}} * * Javascript import { malibuIcon } from '@heroku/ember-malibu-icon/helpers/malibu-icon'; malibuIcon({ name: 'app-16', size: 16, iconClass: 'v-mid gradient-purple', title='Optional Title' }) **/ import Helper from '@ember/component/helper'; import type { TrustedHTML } from '@ember/template'; import { htmlSafe } from '@ember/template'; import { guidFor } from '@ember/object/internals'; import { warn } from '@ember/debug'; // Simple HTML escape function to replace Ember.Handlebars.Utils.escapeExpression function escape(str: string | number | undefined | null): string { if (str == null) { return ''; } if (typeof str !== 'string') { str = String(str); } return str .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/`/g, '`'); } interface MalibuIconParams { name: string; size: number | string; height?: number | string; width?: number | string; iconClass?: string; title?: string; ariaHidden?: boolean | string; 'aria-hidden'?: boolean | string; role?: string; // _warn: optional (test-only) dependency injection; do not pass from templates or app code _warn?: typeof warn; } export function malibuIcon( params: unknown[] | MalibuIconParams, hash?: object, ): TrustedHTML { // if we didn't get a hash of params then use the object passed in // we expect the hash when used as a handlebars component and the params // when used via javascript const parameters = (hash === undefined ? params : hash) as MalibuIconParams; const { title, size, height, width, iconClass, name } = parameters; const iconWidth = width || size; const iconHeight = height || size; const labelID = guidFor({}); const ariaHiddenRaw = parameters.ariaHidden ?? parameters['aria-hidden'] ?? false; const ariaHidden = ariaHiddenRaw === true || ariaHiddenRaw === 'true'; const role = parameters.role || 'img'; // _warn: optional (test-only) dependency injection; do not pass from templates or app code const warnFn = parameters._warn ?? warn; warnFn( `Please set a title for the ${name} icon ` + 'or set aria-hidden to true if you believe the icon does not ' + 'need a title and is solely decorative. ' + 'Learn more about accessible icons here: ' + 'http://design.herokai.com/malibu', parameters.title !== undefined || ariaHidden, { id: 'ember-malibu-icon.missing-title' }, ); return htmlSafe(` ${escape(title)} `); } export default Helper.helper(malibuIcon);