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

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

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

问题

  1. import { Component, Prop, h } from "@stencil/core";
  2. @Component({
  3. tag: "my-textfield",
  4. styleUrl: "my-textfield.scss",
  5. shadow: false,
  6. })
  7. export class MyTextfield {
  8. render() {
  9. return (
  10. <label className="mdc-text-field mdc-text-field--filled">
  11. <span className="mdc-text-field__ripple"></span>
  12. <input
  13. type="text"
  14. className="mdc-text-field__input"
  15. aria-labelledby="my-label"
  16. ></input>
  17. <span className="mdc-floating-label" id="my-label">
  18. Label
  19. </span>
  20. <span className="mdc-line-ripple"></span>
  21. </label>
  22. );
  23. }
  24. }
  1. @use "@material/floating-label/mdc-floating-label";
  2. @use "@material/line-ripple/mdc-line-ripple";
  3. @use "@material/notched-outline/mdc-notched-outline";
  4. @use "@material/textfield";
  5. @include textfield.core-styles;
  6. .mdc-text-field {
  7. font-size: 24px;
  8. }

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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>
);
}
}

  1. 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;
}

  1. 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.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 当在使用HTMLJSX时,请使用`class`而不是`className``className`用于在JavaScript/TypeScript中作为元素对象的属性使用,例如`myElement.className = 'my-class';`
  6. ```jsx
  7. import { Component, Prop, h } from "@stencil/core";
  8. @Component({
  9. tag: "my-textfield",
  10. styleUrl: "my-textfield.scss",
  11. shadow: false,
  12. })
  13. export class MyTextfield {
  14. render() {
  15. return (
  16. <label class="mdc-text-field mdc-text-field--filled">
  17. <span class="mdc-text-field__ripple"></span>
  18. <input
  19. type="text"
  20. class="mdc-text-field__input"
  21. aria-labelledby="my-label"
  22. ></input>
  23. <span class="mdc-floating-label" id="my-label">
  24. Label
  25. </span>
  26. <span class="mdc-line-ripple"></span>
  27. </label>
  28. );
  29. }
  30. }
英文:

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;;.

  1. import { Component, Prop, h } from &quot;@stencil/core&quot;;
  2. @Component({
  3. tag: &quot;my-textfield&quot;,
  4. styleUrl: &quot;my-textfield.scss&quot;,
  5. shadow: false,
  6. })
  7. export class MyTextfield {
  8. render() {
  9. return (
  10. &lt;label class=&quot;mdc-text-field mdc-text-field--filled&quot;&gt;
  11. &lt;span class=&quot;mdc-text-field__ripple&quot;&gt;&lt;/span&gt;
  12. &lt;input
  13. type=&quot;text&quot;
  14. class=&quot;mdc-text-field__input&quot;
  15. aria-labelledby=&quot;my-label&quot;
  16. &gt;&lt;/input&gt;
  17. &lt;span class=&quot;mdc-floating-label&quot; id=&quot;my-label&quot;&gt;
  18. Label
  19. &lt;/span&gt;
  20. &lt;span class=&quot;mdc-line-ripple&quot;&gt;&lt;/span&gt;
  21. &lt;/label&gt;
  22. );
  23. }
  24. }

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:

确定