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

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

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

问题

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

<h1 id="heading-2">
  salam
  <vaadin-tooltip
    theme="dark"
    for="heading-2"
    position="bottom-center"
    text="Tooltip with position bottom-center"
  ></vaadin-tooltip>
</h1>

<h1 id="heading-3">
  salam
  <vaadin-tooltip
    theme="light"
    for="heading-3"
    position="bottom-end"
    text="Tooltip with position bottom-end"
  ></vaadin-tooltip>
</h1>

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

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

import { registerStyles } from "@vaadin/vaadin-themable-mixin";
import { css } from "lit";

registerStyles(
  "vaadin-tooltip",
  css`
    :host[theme="dark"] {
      font-family: var(--lumo-font-family);
      // background-color: yellow;
    }
  `
);

registerStyles(
  "vaadin-tooltip-overlay",
  css`
    :host {
      font-family: var(--lumo-font-family);
      // background-color: yellow;
    }

    :host::part(overlay) {
      // background-color: red;
    }
  `
);

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

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

英文:

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

      &lt;h1 id=&quot;heading-2&quot;&gt;
        salam
        &lt;vaadin-tooltip
          theme=&quot;dark&quot;
          for=&quot;heading-2&quot;
          position=&quot;bottom-center&quot;
          text=&quot;Tooltip with position bottom-center&quot;
        &gt;&lt;/vaadin-tooltip&gt;
      &lt;/h1&gt;

      &lt;h1 id=&quot;heading-3&quot;&gt;
        salam
        &lt;vaadin-tooltip
          theme=&quot;light&quot;
          for=&quot;heading-3&quot;
          position=&quot;bottom-end&quot;
          text=&quot;Tooltip with position bottom-end&quot;
        &gt;&lt;/vaadin-tooltip&gt;
      &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:

import { registerStyles } from &quot;@vaadin/vaadin-themable-mixin&quot;;
import { css } from &quot;lit&quot;;

registerStyles(
  &quot;vaadin-tooltip&quot;,
  css`
    :host[theme=&quot;dark&quot;] {
      font-family: var(--lumo-font-family);
      // background-color: yellow;
    }
  `
);

registerStyles(
  &quot;vaadin-tooltip-overlay&quot;,
  css`
    :host {
      font-family: var(--lumo-font-family);
      // background-color: yellow;
    }

    :host::part(overlay) {
      // background-color: red;
    }
  `
);

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属性来将类名传递到其叠加层的自定义机制实现:

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

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

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

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

&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):

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

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:

确定