- From: Alejandro G. Carlstein Ramos Mejia <notifications@github.com>
- Date: Thu, 29 Apr 2021 14:39:35 -0700
- To: WICG/webcomponents <webcomponents@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <WICG/webcomponents/issues/66/829613433@github.com>
I would like to add that using `:target` has other applications other than just following identifiers.
There are pure CSS effects that can be use with it. 
For example, normally I could change the color of a header when clicking on a link using pure CSS:
CSS:
```css
#message:target {
 color: red;
}
```
HTML:
```html
<a href="#message">Make the message red</a>
<h2 id="message">Message in a bottle</h2> 
```
Before:

After:

By using pure CSS, we can keep a button highlighted in a toolbox, until we press another button:

by just doing:
CSS:
```css
#toolbar .item-switch:target {
  background: white;
  color: black;
}
```
HTML:
```html
  <a href="#btn-pencil">
    <span id="btn-pencil" class="item item-switch"><i class="fas fa-pencil-alt" alt="Pencil"></i></span>
  </a>
```
All this without having to write even one line of JavaScript yet.
However, by not allowing `:target` into the Web Component, this feature has being nullified. 
Plus, I had to spend hours until I found that the root cause of the problem was that event target wasn't in the shadow-root.
So, the code below will not work:
```javascript
let template = `
 <div>
  <a href="#message">Make the message red</a>
  <h2 id="message">Message in a bottle</h2>
 </div>
 <style>
  #message:target {
   color: red;
  }
 </style>
`;
class MyElement extends HTMLElement {
 constructor() {
  super();
  let shadowRoot = this.attachShadow({ mode: 'open' });
  shadowRoot.innerHTML = template;
 }
}
customElements.define('my-component', MyElement);
```
-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/66#issuecomment-829613433
Received on Thursday, 29 April 2021 21:39:48 UTC