Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 010c6f2

Browse files
committedJan 30, 2018
adding example to explain :defined
1 parent 7cf4281 commit 010c6f2

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ A series of web components examples, related to the MDN web components documenta
33

44
The following examples are available:
55

6+
* [defined-pseudo-class](defined-pseudo-class). A very simple example that shows how the <code>[:defined pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:defined)</code> works.
67
* [element-details](element-details) — <code>&lt;element-details&gt;</code>. Displays a box containing an HTML element name and description. Provides an example of an autonomous custom element that gets its structure from a <code>&lt;template&gt;</code> element (that also has its own styling defined), and also contains <code>&lt;slot&gt;</code> elements populated at runtime. [See element-details live](https://mdn.github.io/web-components-examples/element-details/).
78
* [expanding-list-web-component](expanding-list-web-component) — <code>&lt;ul is="expanding-list"&gt;</code>. Creates an unordered list with expandable/collapsible children. Provides an example of a customized built-in element (the class inherits from <code>HTMLUListElement</code> rather than <code>HTMLElement</code>). [See expanding-list live](https://mdn.github.io/web-components-examples/expanding-list-web-component/).
89
* [life-cycle-callbacks](life-cycle-callbacks) — <code>&lt;custom-square l="" c=""&gt;</code>. A trivial example web component that creates a square colored box on the page. The demo also includes buttons to create, destroy, and change attributes on the element, to demonstrate how the [web components life cycle callbacks](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#Using_the_lifecycle_callbacks) work [See life-cycle-callbacks live](https://mdn.github.io/web-components-examples/life-cycle-callbacks/).

‎defined-pseudo-class/index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>:defined demo</title>
6+
<script src="main.js" defer></script>
7+
<style>
8+
p {
9+
background: yellow;
10+
}
11+
12+
simple-custom {
13+
background: cyan;
14+
}
15+
16+
:defined {
17+
font-style: italic;
18+
}
19+
20+
simple-custom:not(:defined) {
21+
display: none;
22+
}
23+
24+
simple-custom:defined {
25+
display: block;
26+
}
27+
</style>
28+
</head>
29+
<body>
30+
<h1><code>:defined</code> demo</h1>
31+
32+
<simple-custom text="Custom element example text"></simple-custom>
33+
34+
<p>Standard paragraph example text</p>
35+
36+
</body>
37+
</html>

‎defined-pseudo-class/main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
customElements.define('simpl-custom',
2+
class extends HTMLElement {
3+
constructor() {
4+
super();
5+
6+
let divElem = document.createElement('div');
7+
divElem.textContent = this.getAttribute('text');
8+
9+
let shadowRoot = this.attachShadow({mode: 'open'})
10+
.appendChild(divElem);
11+
}
12+
})

0 commit comments

Comments
 (0)
Please sign in to comment.