如何为Vaadin提示添加浅色主题和深色主题?

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

How to add light theme and dark theme to Vaadin tooltip?

问题

在我的HTML中,我正在使用Vaadin提示组件,如下所示:

  1. <h1 id="heading-2">
  2. salam
  3. <vaadin-tooltip
  4. theme="dark"
  5. for="heading-2"
  6. position="bottom-center"
  7. text="Tooltip with position bottom-center"
  8. ></vaadin-tooltip>
  9. </h1>
  10. <h1 id="heading-3">
  11. salam
  12. <vaadin-tooltip
  13. theme="light"
  14. for="heading-3"
  15. position="bottom-end"
  16. text="Tooltip with position bottom-end"
  17. ></vaadin-tooltip>
  18. </h1>

我想根据主题更改工具提示的背景颜色。

tooltip.ts 目前看起来像这样:

  1. import { registerStyles } from "@vaadin/vaadin-themable-mixin";
  2. import { css } from "lit";
  3. registerStyles(
  4. "vaadin-tooltip",
  5. css`
  6. :host[theme="dark"] {
  7. font-family: var(--lumo-font-family);
  8. // background-color: yellow;
  9. }
  10. `
  11. );
  12. registerStyles(
  13. "vaadin-tooltip-overlay",
  14. css`
  15. :host {
  16. font-family: var(--lumo-font-family);
  17. // background-color: yellow;
  18. }
  19. :host::part(overlay) {
  20. // background-color: red;
  21. }
  22. `
  23. );

我不知道如何根据工具提示具有的类样式化叠加层。我也不知道如何将类添加到叠加层。

我愿意在HTML中静态添加样式。

英文:

I'm using Vaadin tooltip component like this in my html:

  1. &lt;h1 id=&quot;heading-2&quot;&gt;
  2. salam
  3. &lt;vaadin-tooltip
  4. theme=&quot;dark&quot;
  5. for=&quot;heading-2&quot;
  6. position=&quot;bottom-center&quot;
  7. text=&quot;Tooltip with position bottom-center&quot;
  8. &gt;&lt;/vaadin-tooltip&gt;
  9. &lt;/h1&gt;
  10. &lt;h1 id=&quot;heading-3&quot;&gt;
  11. salam
  12. &lt;vaadin-tooltip
  13. theme=&quot;light&quot;
  14. for=&quot;heading-3&quot;
  15. position=&quot;bottom-end&quot;
  16. text=&quot;Tooltip with position bottom-end&quot;
  17. &gt;&lt;/vaadin-tooltip&gt;
  18. &lt;/h1&gt;

and I want to change the background color of the tooltip based on the theme.

The tooltip.ts currently looks like this:

  1. import { registerStyles } from &quot;@vaadin/vaadin-themable-mixin&quot;;
  2. import { css } from &quot;lit&quot;;
  3. registerStyles(
  4. &quot;vaadin-tooltip&quot;,
  5. css`
  6. :host[theme=&quot;dark&quot;] {
  7. font-family: var(--lumo-font-family);
  8. // background-color: yellow;
  9. }
  10. `
  11. );
  12. registerStyles(
  13. &quot;vaadin-tooltip-overlay&quot;,
  14. css`
  15. :host {
  16. font-family: var(--lumo-font-family);
  17. // background-color: yellow;
  18. }
  19. :host::part(overlay) {
  20. // background-color: red;
  21. }
  22. `
  23. );

I don't know how to give styles to overlay based on the class that tooltip has. I also don't know how to add the class to the overlay.

I am open to adding the styles statically inside the html too.

答案1

得分: 2

Tooltip通过使用overlay-class属性来将类名传递到其叠加层的自定义机制实现:

  1. &lt;vaadin-tooltip overlay-class=&quot;custom-tooltip&quot; ...&gt;&lt;/vaadin-tooltip&gt;

相应的CSS规则需要放在全局CSS中(而不是像您的示例中使用registerStyles):

  1. vaadin-tooltip-overlay.custom-tooltip::part(overlay) {
  2. background: green;
  3. }
英文:

Tooltip implements a custom mechanism for forwarding class names to its overlay by using the overlay-class attribute:

  1. &lt;vaadin-tooltip overlay-class=&quot;custom-tooltip&quot; ...&gt;&lt;/vaadin-tooltip&gt;

The corresponding CSS rule needs to be put into the global CSS (instead of using registerStyles as in your example):

  1. vaadin-tooltip-overlay.custom-tooltip::part(overlay) {
  2. background: green;
  3. }

huangapple
  • 本文由 发表于 2023年6月22日 18:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531081.html
匿名

发表评论

匿名网友

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

确定