-
Notifications
You must be signed in to change notification settings - Fork 51
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
Convert to component? #77
Comments
This can be as for Glimmer component, not sure if worth to do with classic component? Related issue with class attributes #36 . |
ContextI wanted to create a component for custom (uploaded) Font Awesome icons so that I can keep the default way of installing FA packages via package manager and using the component provided by FA for all icons and handle only custom icons this way. DisclaimerThis is a very minimal implementation which I'll further extend so I have one component to render all FA icons from the kit.
import Component from '@glimmer/component';
import { htmlSafe } from '@ember/string';
import { assert } from '@ember/debug';
import { dottify } from 'ember-inline-svg/utils/general';
import type { SafeString } from '@ember/template/-private/handlebars';
const XMLNS_RE = /xmlns="([^"]*)"/;
const VIEWBOX_RE = /viewBox="([^"]*)"/;
const SVG_CONTENT_RE = /^<svg[^>]*>(.*)<\/svg>$/;
function extract(tag: string, attrRe: RegExp): string {
return (tag.match(attrRe) as RegExpMatchArray)[1];
}
interface Args {
path: string;
}
export default class IconComponent extends Component<Args> {
xmlns!: string;
viewBox!: string;
svgContent!: SafeString;
constructor(owner: unknown, args: Args) {
super(owner, args);
// @ts-expect-error not bothering to type resolveRegistration
const svgs = owner.resolveRegistration('svgs:main') || [];
const svg = svgs[dottify(this.args.path)];
assert('No SVG found for ' + this.args.path, svg);
this.xmlns = extract(svg, XMLNS_RE);
this.viewBox = extract(svg, VIEWBOX_RE);
this.svgContent = htmlSafe(extract(svg, SVG_CONTENT_RE));
}
}
<svg xmlns={{this.xmlns}} viewBox={{this.viewBox}} ...attributes>
{{this.svgContent}}
</svg> |
I'm also kinda wondering why this is helper?
It'd be great if we could wrap this in something like:
there the invocation could then become:
The text was updated successfully, but these errors were encountered: