import Component from '@glimmer/component'; import { setComponentTemplate } from '@ember/component'; import { precompileTemplate } from '@ember/template-compilation'; import type Owner from '@ember/owner'; import { requireNumeric } from '../-private/utils.ts'; import { ensureSpriteInjected } from '../-private/inject-sprite.ts'; import malibuIconA11yCheck from '../modifiers/malibu-icon-a11y-check.ts'; let labelCounter = 0; // Allows for backwards compatibility with the old string-based ariaHidden prop type AriaHiddenTrue = true | 'true'; type AriaHiddenFalseish = false | 'false' | undefined; type CommonArgs = { name: string; size: number; width?: number; height?: number; iconClass?: string; role?: 'img' | 'presentation' | 'none'; 'aria-hidden'?: AriaHiddenTrue | AriaHiddenFalseish; }; type DecorativeArgs = CommonArgs & { ariaHidden: AriaHiddenTrue; title?: never; }; type TitledArgs = CommonArgs & { title: string; ariaHidden?: AriaHiddenFalseish; }; export interface MalibuIconSignature { Element: SVGSVGElement; Args: DecorativeArgs | TitledArgs; } class MalibuIcon extends Component { labelId = `malibu-icon-title-${labelCounter++}`; constructor(owner: Owner, args: MalibuIconSignature['Args']) { super(owner, args); void ensureSpriteInjected(); } get iconWidth(): number { const { width, size } = this.args; return width !== undefined ? requireNumeric(width) : requireNumeric(size); } get iconHeight(): number { const { height, size } = this.args; return height !== undefined ? requireNumeric(height) : requireNumeric(size); } get ariaHidden(): boolean { const v = this.args.ariaHidden ?? this.args['aria-hidden']; return v === true || v === 'true'; } get role(): string { return this.args.role ?? 'img'; } get svgClass(): string { return `icon malibu-icon ${this.args.iconClass ?? ''}`.trim(); } get dataTestTarget(): string { return 'malibu-icon'; } /** ViewBox matches the sprite symbol's native size so the icon scales to width/height. */ get viewBox(): string { const name = this.args.name ?? ''; const size = name.includes('-16') ? 16 : 28; return `0 0 ${size} ${size}`; } } const template = precompileTemplate( ` {{#if @title}} {{@title}} {{/if}} `, { strictMode: true, scope: () => ({ malibuIconA11yCheck }) }, ); // Type assertion so declaration emit is Component, not object export default setComponentTemplate(template, MalibuIcon) as typeof MalibuIcon;