Skip to content

bug: unable to render foreignObject with attributes inside SVG #3193

@peterpeterparker

Description

@peterpeterparker

Prerequisites

Stencil Version

v2.x

Current Behavior

foreignObject with attributes in SVG cannot be rendered.

Stencil expect the foreignObject to be provided as foreign-object in order to be able to render attributes but, the browser expect the foreignObject to be provided as <foreignObject/> and not <foreign-object/>.

It seems Stencil has an issue only with a subset of SVGAttributes respectively width, height, x and y.

Expected Behavior

Being able to use <foreignObject/> with attributes withing svg in Stencil render functions.

Steps to Reproduce

import { Component, h } from '@stencil/core';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
})
export class MyComponent {
  render() {
    return (
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300" x="0" y="0" height="300" width="300">
        <circle r="142" cx="150" cy="150" fill="none" stroke="#000000" stroke-width="2" />

        <foreignObject x={0} y={0} width="200" height="200">
          <p>Hello World</p>
        </foreignObject>
      </svg>
    );
  }
}

Code Reproduction URL

https://github.com/peterpeterparker/stencil-svg-foreignobject

Additional Information

Notes

a fixed issue regarding foreign object has been opened in the past, see #1733

Screenshots

Capture d’écran 2021-12-20 à 09 43 49

Capture d’écran 2021-12-20 à 09 47 29

Workaround

import {Component, h, ComponentInterface} from '@stencil/core';

@Component({
  tag: 'deckgo-social-img',
  styleUrl: 'social-img.scss',
  shadow: true
})
export class SocialImg implements ComponentInterface {
  private foreignObjectRef!: SVGForeignObjectElement;

  componentDidLoad() {
    this.setForeignObjectAttributes();
  }

  private setForeignObjectAttributes() {
    this.foreignObjectRef?.setAttribute('x', '0');
    this.foreignObjectRef?.setAttribute('y', '0');
    this.foreignObjectRef?.setAttribute('width', '200');
    this.foreignObjectRef?.setAttribute('height', '100');
  }

  render() {
    return (
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300" x="0" y="0" height="300" width="300">
        <circle r="142" cx="150" cy="150" fill="none" stroke="#000000" stroke-width="2" />

        <foreignObject ref={(el) => (this.foreignObjectRef = el as SVGForeignObjectElement)}>
          <p>Hello World</p>
        </foreignObject>
      </svg>
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug: ValidatedThis PR or Issue is verified to be a bug within StencilHas WorkaroundThis PR or Issue has a work around detailed within it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions