无法使 material-components-web 与 Stencil 协同工作

huangapple go评论54阅读模式
英文:

Can't get material-components-web to work with Stencil

问题

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

@Component({
  tag: "my-textfield",
  styleUrl: "my-textfield.scss",
  shadow: false,
})
export class MyTextfield {
  render() {
    return (
      <label className="mdc-text-field mdc-text-field--filled">
        <span className="mdc-text-field__ripple"></span>
        <input
          type="text"
          className="mdc-text-field__input"
          aria-labelledby="my-label"
        ></input>
        <span className="mdc-floating-label" id="my-label">
          Label
        </span>
        <span className="mdc-line-ripple"></span>
      </label>
    );
  }
}
@use "@material/floating-label/mdc-floating-label";
@use "@material/line-ripple/mdc-line-ripple";
@use "@material/notched-outline/mdc-notched-outline";
@use "@material/textfield";

@include textfield.core-styles;

.mdc-text-field {
  font-size: 24px;
}

My custom font size is correctly applied, however material styles are not. What's the issue here? I have Stencil's sass() plugin installed.


<details>
<summary>英文:</summary>

I have a Stencil component where I use material-components-web to customize material components:

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

@Component({
tag: "my-textfield",
styleUrl: "my-textfield.scss",
shadow: false,
})
export class MyTextfield {
render() {
return (
<label className="mdc-text-field mdc-text-field--filled">
<span className="mdc-text-field__ripple"></span>
<input
type="text"
className="mdc-text-field__input"
aria-labelledby="my-label"
></input>
<span className="mdc-floating-label" id="my-label">
Label
</span>
<span className="mdc-line-ripple"></span>
</label>
);
}
}

My stylesheet looks like this:

@use "@material/floating-label/mdc-floating-label";
@use "@material/line-ripple/mdc-line-ripple";
@use "@material/notched-outline/mdc-notched-outline";
@use "@material/textfield";

@include textfield.core-styles;

.mdc-text-field {
font-size: 24px;
}

My custom font size is correctly applied, however material styles are not. What&#39;s the issue here? I have Stencil&#39;s sass() plugin installed.

</details>


# 答案1
**得分**: 1

当在使用HTML或JSX时,请使用`class`而不是`className`。`className`用于在JavaScript/TypeScript中作为元素对象的属性使用,例如`myElement.className = 'my-class';`。

```jsx
import { Component, Prop, h } from "@stencil/core";

@Component({
  tag: "my-textfield",
  styleUrl: "my-textfield.scss",
  shadow: false,
})
export class MyTextfield {
  render() {
    return (
      <label class="mdc-text-field mdc-text-field--filled">
        <span class="mdc-text-field__ripple"></span>
        <input
          type="text"
          class="mdc-text-field__input"
          aria-labelledby="my-label"
        ></input>
        <span class="mdc-floating-label" id="my-label">
          Label
        </span>
        <span class="mdc-line-ripple"></span>
      </label>
    );
  }
}
英文:

When using html or jsx, use class not className. className is for use in js/ts as a property of an element object e.g. myElement.className = &#39;my-class&#39;;.

import { Component, Prop, h } from &quot;@stencil/core&quot;;

@Component({
  tag: &quot;my-textfield&quot;,
  styleUrl: &quot;my-textfield.scss&quot;,
  shadow: false,
})
export class MyTextfield {
  render() {
    return (
      &lt;label class=&quot;mdc-text-field mdc-text-field--filled&quot;&gt;
        &lt;span class=&quot;mdc-text-field__ripple&quot;&gt;&lt;/span&gt;
        &lt;input
          type=&quot;text&quot;
          class=&quot;mdc-text-field__input&quot;
          aria-labelledby=&quot;my-label&quot;
        &gt;&lt;/input&gt;
        &lt;span class=&quot;mdc-floating-label&quot; id=&quot;my-label&quot;&gt;
          Label
        &lt;/span&gt;
        &lt;span class=&quot;mdc-line-ripple&quot;&gt;&lt;/span&gt;
      &lt;/label&gt;
    );
  }
}

huangapple
  • 本文由 发表于 2023年6月5日 14:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定