Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong CSS in Lit template #12950

Closed
7 tasks done
miketests opened this issue Apr 22, 2023 · 1 comment · Fixed by #12951
Closed
7 tasks done

Wrong CSS in Lit template #12950

miketests opened this issue Apr 22, 2023 · 1 comment · Fixed by #12951

Comments

@miketests
Copy link

miketests commented Apr 22, 2023

Describe the bug

If you init fresh new Lit project with Vite you will have CSS style which doesn't work.

Create lit-ts template with Vite: npm create vite@latest

Then in my-element.ts file you will have CSS for h1 tag which doesn't work.

import { LitElement, css, html } from 'lit'
import { customElement, property } from 'lit/decorators.js'
import litLogo from './assets/lit.svg'
import viteLogo from '/vite.svg'

/**
 * An example element.
 *
 * @slot - This element has a slot
 * @csspart button - The button
 */
@customElement('my-element')
export class MyElement extends LitElement {
  /**
   * Copy for the read the docs hint.
   */
  @property()
  docsHint = 'Click on the Vite and Lit logos to learn more'

  /**
   * The number of times the button has been clicked.
   */
  @property({ type: Number })
  count = 0

  render() {
    return html`
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src=${viteLogo} class="logo" alt="Vite logo" />
        </a>
        <a href="https://lit.dev" target="_blank">
          <img src=${litLogo} class="logo lit" alt="Lit logo" />
        </a>
      </div>
      <slot></slot>
      <div class="card">
        <button @click=${this._onClick} part="button">
          count is ${this.count}
        </button>
      </div>
      <p class="read-the-docs">${this.docsHint}</p>
    `
  }

  private _onClick() {
    this.count++
  }

  static styles = css`
    :host {
      max-width: 1280px;
      margin: 0 auto;
      padding: 2rem;
      text-align: center;
    }

    .logo {
      height: 6em;
      padding: 1.5em;
      will-change: filter;
      transition: filter 300ms;
    }
    .logo:hover {
      filter: drop-shadow(0 0 2em #646cffaa);
    }
    .logo.lit:hover {
      filter: drop-shadow(0 0 2em #325cffaa);
    }

    .card {
      padding: 2em;
    }

    .read-the-docs {
      color: #888;
    }

    h1 { /* <--- DOESN'T WORK */
      font-size: 3.2em;
      line-height: 1.1;
    }

    a {
      font-weight: 500;
      color: #646cff;
      text-decoration: inherit;
    }
    a:hover {
      color: #535bf2;
    }

    button {
      border-radius: 8px;
      border: 1px solid transparent;
      padding: 0.6em 1.2em;
      font-size: 1em;
      font-weight: 500;
      font-family: inherit;
      background-color: #1a1a1a;
      cursor: pointer;
      transition: border-color 0.25s;
    }
    button:hover {
      border-color: #646cff;
    }
    button:focus,
    button:focus-visible {
      outline: 4px auto -webkit-focus-ring-color;
    }

    @media (prefers-color-scheme: light) {
      a:hover {
        color: #747bff;
      }
      button {
        background-color: #f9f9f9;
      }
    }
  `
}

declare global {
  interface HTMLElementTagNameMap {
    'my-element': MyElement
  }
}

Since h1 tag is placed in index.html file and works as a slot some other approach is required for styling this tag.
Unfortunately, I didn't find any comprehensive info in the docs how to fix this issue.
For now there is a broken CSS right out of the box for Lit template which would be nice to fix.

Reproduction

https://vite.new/lit-ts

Steps to reproduce

  1. npm create vite@latest
  2. cd into the project
  3. npm run dev
  4. open running project in the browser
  5. try to change CSS for h1 tag in my-element.ts file
  6. save changes in the file
  7. you will notice that there are no changes in the browser

System Info

macOS: 10.15.7
Safari: 15.6.1
node: v16.14.2
npm: 8.5.0
vite: ^4.2.0
lit: ^2.6.1

Used Package Manager

npm

Logs

No response

Validations

@danieldeandradelopes
Copy link
Contributor

I checked this item, and really we have this problem.

@github-actions github-actions bot locked and limited conversation to collaborators May 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants