Why do pseudoclasses on the host element have to be inside of the host function?

自作多情 提交于 2019-12-02 05:01:44

问题


I do not understand why pseudo classes like :focus-within need to be within the :host() function brackets when acting on the host itself. Why can it not be :host:focus-within div? It's even more weird that it works on :host inside of another :host().

class MyElementFail extends HTMLElement {

	constructor(...args) {
		super(...args)

		this.attachShadow({mode: 'open'}).innerHTML = `
		<style>
    :host{
      display: block;
      padding: 20px;
      background-color: salmon;
    }
    :host div{
      background-color: white;
    }
    /*This part is different:*/
		:host:focus-within div{
			background-color: green;
		}
		</style>
    <input type="text" value="click in here"/>
		<div>
    Change to green
		</div>`
	}
}
window.customElements.define('my-element-fail', MyElementFail);


class MyElement extends HTMLElement {

	constructor(...args) {
		super(...args)

		this.attachShadow({mode: 'open'}).innerHTML = `
		<style>
    :host{
      display: block;
      padding: 20px;
      background-color: salmon;
    }
    :host div{
      background-color: white;
    }
    /*This part is different:*/
		:host(my-element:focus-within) div{
			background-color: green;
		}
		</style>
    <input type="text" value="click in here"/>
		<div>
    Change to green
		</div>`
	}
}
window.customElements.define('my-element', MyElement);


class MyElementTwo extends HTMLElement {

	constructor(...args) {
		super(...args)

		this.attachShadow({mode: 'open'}).innerHTML = `
		<style>
    :host{
      display: block;
      padding: 20px;
      background-color: salmon;
    }
    :host div{
      background-color: white;
    }
    /*This part is different:*/
		:host(:host:focus-within) div{
			background-color: green;
		}
		</style>
    <input type="text" value="click in here"/>
		<div>
    Change to green
		</div>`
	}
}
window.customElements.define('my-element-two', MyElementTwo);
No Good:
<my-element-fail></my-element-fail>
Good:
<my-element></my-element>
Good also:
<my-element-two></my-element-two>

Essentially, why does,

:host(:host:focus-within) div{ work, and

:host(my-element:focus-within) div{ work, but

:host:focus-within div{ not work?


回答1:


:host is only to indicate the host element of the shadowDOM.

:host(.something) indicated the host with a class of .something.

You can not use :host.something you must use the parenthesis.

:host() is not a function. It is just how to select a :host with additional specificity.

class MyElement extends HTMLElement {
	constructor() {
		super();
		this.attachShadow({mode: 'open'}).innerHTML = `
		<style>
    :host{
      display: block;
      padding: 20px;
      background-color: salmon;
    }

    div{
      background-color: white;
    }

    :host(:focus-within) div{
			background-color: green;
		}
		</style>
    <input type="text" value="click in here"/>
		<div>Change to green</div>`;
	}
}
window.customElements.define('my-element', MyElement);
<my-element></my-element>



回答2:


Actually the reason is given in Selector Level 4 specification:

The shadow host in a shadow tree is featureless and therefore cannot be matched by any pseudo-class except for :host [...].

It is illustrated in the hyperlink in the example (and actually also the link you pointed in your comment to @Intervalia's answer).

Transposed to your use case:

:focus-within doesn't match the shadow host. So, :host:focus-within which is more specific, should/could not match anything (that would be contradictory to the CSS selection fundamental).

Hence the :host() function pseudo-class that will mimic the other selectors but won't break their logic.



来源:https://stackoverflow.com/questions/54815145/why-do-pseudoclasses-on-the-host-element-have-to-be-inside-of-the-host-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!