components icon indicating copy to clipboard operation
components copied to clipboard

icon: Keep symbol viewBox value when register an iconSet

Open ilaborie opened this issue 9 years ago • 11 comments

Bug, feature request, or proposal:

Bug

What is the expected behavior?

When registering an SVG iconSet, every symbol may provide a custom viewBox attribute. This attribute should be keep in the newly SVG element created.

What is the current behavior?

The viewBox attribute is swallowed.

What are the steps to reproduce?

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

At least since beta 7

Is there anything else we should know?

This is related to #5188 #2981 For SVG icon set with symbol, see https://css-tricks.com/svg-symbol-good-choice-icons/

ilaborie avatar Jul 04 '17 06:07 ilaborie

I think this is captured by work on #5188. md-icon is specifically intended to cover the Material Design icon treatment; a cdk-svg directive would be able to be a lot more general.

jelbourn avatar Jul 06 '17 15:07 jelbourn

hi, i don't see the fix of @ilaborie in the current MatIconRegistry ? The fix is current and I always need a override of MatIconRegistry with this fix.

choucry13 avatar Nov 24 '17 15:11 choucry13

I still do not understand why this 3-lines patch is not applied?

ilaborie avatar Nov 25 '17 09:11 ilaborie

hi @jelbourn, any news please ?

choucry13 avatar Dec 11 '17 18:12 choucry13

I struggled with trying to use a sprite svg file exported from FlatIcon.com (svg file using inline elements) with the addSvgIconSetInNamespace function. I finally realized the viewBox attribute is NOT inserted into the output svg code with the mat-icon component. I'm glad I found this github issue to confirm.

I'm wondering if anyone has a solution? Without the viewBox values, the SVG cannot be rendered and scaled properly. Is it just not possible? I'll register the icons individually with addSvgIcon if I have to; just thought it would be so much more efficient to have one combined svg file. Advice?

Meliovation avatar Jun 05 '18 14:06 Meliovation

Potentially revisiting this now

jelbourn avatar Sep 04 '18 20:09 jelbourn

Do you support both <defs> and <symbol> in an svg icon set? Symbols seem to work with the exception of the viewbox missing, which causes the icons to not be centered. I'm hesitant to manually fix my entire sprite file since it was easily dynamically generated, unless you don't support <symbol>.

bsteffl avatar Dec 10 '18 22:12 bsteffl

Here is the fix I applied in my code by overriding _toSvgElement method exactly like @ilaborie did:

@NgModule({
  providers: [
    {
      provide: APP_INITIALIZER,
      deps: [MatIconRegistry],
      useFactory: preserveSvgViewBox,
      multi: true
    }
  ]
})
export class IconModule {
}

function preserveSvgViewBox(matIconRegistry: MatIconRegistry) {
  return () => {
    const oldToSvgElement = matIconRegistry['_toSvgElement'];

    matIconRegistry['_toSvgElement'] = (element: Element) => {
      const svg = oldToSvgElement.call(matIconRegistry, element);

      const viewBox = element.getAttribute('viewBox');
      if (viewBox !== null) {
        svg.setAttribute('viewBox', viewBox);
      }
      return svg;
    };
  };
}

Humberd avatar Dec 18 '18 12:12 Humberd

Wonderful @Humberd . It is working like a charm. What a bad documentation though. And hard to find some issues. A pity foor such a good framework.

elpupi avatar Dec 18 '18 17:12 elpupi

@bsteffl , from material documentation on icon sets:

This is done by creating a single root tag that contains multiple nested tags in its section. Each of these nested tags is identified with an id attribute. This id is used as the name of the icon.

The problem is that icons services like icomoon.io creats icon sets with <symbol>. You can replace it to <svg>, but why?

devHand avatar Dec 30 '18 13:12 devHand

I think it's fixed in the latest version :

https://github.com/angular/components/blob/d6e7893691425bbf17f610754e37ced7793a8029/src/material/icon/icon-registry.ts#L530-L537

Wykks avatar Oct 23 '19 15:10 Wykks