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

Fix documentation of :host and :host() pseudo-classes #458

Merged
merged 2 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions docs/_guide/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ slug: styles
* ToC
{:toc}

## Use the :host CSS pseudo-class
## Use the :host and :host() CSS pseudo-classes

In a style block, use the `:host` CSS pseudo-class to select the host element:
When styling your custom element, you can use the `:host` and `:host()` CSS pseudo-classes in a `<style>` block to select the host element (the element hosting the root of your shadow DOM). The two pseudo-classes slightly differ in usage:

* Use `:host(...)` when you need to apply a CSS selector (e.g. a class or attribute selector).
* Use `:host` to refer to the host element, wihout further selection.

Please note, that `:host` and `:host()` (with empty parentheses) do not behave the same. Here's a simple example:

_my-element.js_

```js
render() {
return html`
<style>
:host[hidden] { display: none; }
:host { display: block;
:host([hidden]) { display: none; }
:host {
display: block;
border: 1px solid black;
}
</style>
Expand All @@ -34,19 +40,19 @@ render() {

{% include project.html folder="docs/style/hostselector" openFile="my-element.js" %}

See the MDN documentation on [:host](https://developer.mozilla.org/en-US/docs/Web/CSS/:host()) and [pseudo-classes](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes) for more information.
See the MDN documentation on [:host](https://developer.mozilla.org/en-US/docs/Web/CSS/:host), [:host()](https://developer.mozilla.org/en-US/docs/Web/CSS/:host()), and [pseudo-classes](https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes) for more information.

### Set :host display styles
### Set host element display styles

Two best practices for working with custom elements are:

* Set a `:host` display style such as `block` or `inline-block` so that your component's `width` and `height` can be set.

* Set a `:host` display style that respects the `hidden` attribute.
* Set a `:host()` display style that respects the `hidden` attribute.

```html
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block; }
</style>
```
Expand Down Expand Up @@ -248,7 +254,7 @@ _my-element.js_
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block;
color: var(--myColor, aliceblue);
font-family: var(--myFont, Verdana);
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/projects/docs/style/bestpracs/my-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block; }
</style>
<p>Hello world</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block;
background-color: var(--myBackground, yellow);
color: var(--myColor, black);
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/projects/docs/style/host/my-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host {
display: block;
font-family: Roboto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block;
border: 1px solid black;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/projects/docs/style/inherited/my-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host {
display: block;
font-family: Roboto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host {
display: block;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/_includes/projects/docs/style/slotted/my-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block; }
::slotted(*) { font-family: Roboto; }
::slotted(span) { color: blue; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyElement extends LitElement {
render() {
return html`
<style>
:host[hidden] { display: none; }
:host([hidden]) { display: none; }
:host { display: block;
color: var(--myColor);
font-family: var(--myFont);
Expand Down