Skip to content

Commit

Permalink
Simplify x-element.
Browse files Browse the repository at this point in the history
This closes #57, closes #52, closes #46, closes #42, closes #36,
closes #31, closes #30, closes #28, closes #26, and closes #25.
  • Loading branch information
theengineear committed Jul 8, 2020
1 parent 77a31c5 commit 358cec0
Show file tree
Hide file tree
Showing 32 changed files with 2,023 additions and 1,148 deletions.
307 changes: 243 additions & 64 deletions SPEC.md

Large diffs are not rendered by default.

39 changes: 17 additions & 22 deletions demo/demo-element-attributes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import XElement from '../x-element.js';

const styleSheet = XElement.createStyleSheet(`
:host {
display: block;
width: 200px;
}
:host([hyphenated-value]) {
background-color: magenta;
}
:host([boolean-value]) {
font-weight: bold;
}
`);

class DemoAttributesElement extends XElement {
static get properties() {
return {
Expand All @@ -27,15 +12,25 @@ class DemoAttributesElement extends XElement {
};
}

static createRenderRoot(host) {
const shadowRoot = super.createRenderRoot(host);
shadowRoot.adoptedStyleSheets = [styleSheet];
return shadowRoot;
}

static template(html) {
return ({ hyphenatedValue }) => {
return html`<div id="demo">${hyphenatedValue}</div>`;
return html`
<style>
:host {
display: block;
width: 200px;
}
:host([hyphenated-value]) {
background-color: magenta;
}
:host([boolean-value]) {
font-weight: bold;
}
</style>
<div id="demo">${hyphenatedValue}</div>
`;
};
}
}
Expand Down
39 changes: 17 additions & 22 deletions demo/demo-element-properties.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import XElement from '../x-element.js';

const styleSheet = XElement.createStyleSheet(`
:host {
display: block;
width: 200px;
}
:host([reflected]) {
background-color: cyan;
}
:host([boolean-value]) {
font-weight: bold;
}
`);

class DemoPropertiesElement extends XElement {
static get properties() {
return {
Expand All @@ -30,15 +15,25 @@ class DemoPropertiesElement extends XElement {
};
}

static createRenderRoot(host) {
const shadowRoot = super.createRenderRoot(host);
shadowRoot.adoptedStyleSheets = [styleSheet];
return shadowRoot;
}

static template(html) {
return ({ reflected }) => {
return html`<div id="demo">${reflected}</div>`;
return html`
<style>
:host {
display: block;
width: 200px;
}
:host([reflected]) {
background-color: cyan;
}
:host([boolean-value]) {
font-weight: bold;
}
</style>
<div id="demo">${reflected}</div>
`;
};
}
}
Expand Down
39 changes: 17 additions & 22 deletions demo/demo-element.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import XElement from '../x-element.js';

const styleSheet = XElement.createStyleSheet(`
:host {
display: block;
width: 200px;
}
:host([reflected]) {
background-color: yellow;
}
:host([boolean-value]) {
font-weight: bold;
}
`);

class DemoElement extends XElement {
static get properties() {
return {
Expand All @@ -30,15 +15,25 @@ class DemoElement extends XElement {
};
}

static createRenderRoot(host) {
const shadowRoot = super.createRenderRoot(host);
shadowRoot.adoptedStyleSheets = [styleSheet];
return shadowRoot;
}

static template(html) {
return ({ reflected }) => {
return html`<div id="demo">${reflected}</div>`;
return html`
<style>
:host {
display: block;
width: 200px;
}
:host([reflected]) {
background-color: yellow;
}
:host([boolean-value]) {
font-weight: bold;
}
</style>
<div id="demo">${reflected}</div>
`;
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
<script type="module" src="demo-element.js"></script>
<script type="module" src="demo-element-properties.js"></script>
<script type="module" src="demo-element-attributes.js"></script>
<script type="module" src="right-triangle.js"></script>
</head>
<body>
<demo-element></demo-element>
<demo-element-properties></demo-element-properties>
<demo-element-attributes hyphenated-value="ok" boolean-value></demo-element-attributes>
<right-triangle></right-triangle>
<script src="index.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
document.querySelector('demo-element').reflected = 'ok';
document.querySelector('demo-element-properties').reflected = 'ok';

const rightTriangle = document.querySelector('right-triangle');
const pythagoreanTriples = [
{ a: 5, b: 12 },
{ a: 7, b: 24 },
{ a: 8, b: 15 },
{ a: 9, b: 40 },
{ a: 11, b: 60 },
{ a: 0, b: 0 },
{ a: 3, b: 4 },
];
let count = 0;
setInterval(() => {
Object.assign(rightTriangle, pythagoreanTriples[count++ % pythagoreanTriples.length]);
}, 2000);
43 changes: 43 additions & 0 deletions demo/right-triangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import XElement from '../x-element.js';

class RightTriangle extends XElement {
static get properties() {
return {
a: {
type: Number,
value: 3,
},
b: {
type: Number,
value: 4,
},
c: {
type: Number,
dependencies: ['a', 'b'],
resolver: (a, b) => Math.hypot(a, b),
},
valid: {
type: Boolean,
dependencies: ['c'],
resolver: c => !!c,
reflect: true,
},
};
}

static template(html) {
return ({ a, b, c }) => html`
<style>
:host {
display: block;
}
:host(:not([valid])) {
display: none;
}
</style>
<code>Math.hypot(${a}, ${b}) = ${c}<code>
`;
}
}

customElements.define('right-triangle', RightTriangle);
123 changes: 59 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,5 @@
import XElement from './x-element.js';

const styleSheet = XElement.createStyleSheet(`
:host {
display: block;
width: var(--hello-size, 8rem);
height: var(--hello-size, 8rem);
background-color: cyan;
border-radius: 50%;
margin: 0.25rem;
box-sizing: border-box;
transition-duration: 250ms;
transition-timing-function: cubic-bezier(0.77, 0, 0.175, 1);
transition-property: transform, border;
will-change: transform;
cursor: pointer;
}
#container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
font-size: calc(var(--hello-size, 8rem) * calc(5/11));
}
:host([rank="\u2655"]) {
border: 4px dotted hsl(120, 100%, 50%);
background-color: yellow;
transform: rotateX(15deg) rotateY(15deg);
}
:host([rank="\u2654"]) {
border: 3px solid hsl(270, 100%, 50%);
background-color: magenta;
color: blue;
transform: rotateX(-10deg) rotateY(-15deg);
}
:host(:not([rank])),
:host([rank=""]) {
background-color: #ccc;
}
:host(:hover) {
border: 3px solid hsl(180, 100%, 50%);
transform: translateZ(-25px);
}
:host(:focus) {
border: 12px solid hsl(90, 100%, 50%);
outline: none;
}
#container:empty::before {
content: '\u265F';
}
`);

class HelloElement extends XElement {
static get properties() {
return {
Expand All @@ -72,15 +15,67 @@ class HelloElement extends XElement {
};
}

static createRenderRoot(host) {
const shadowRoot = super.createRenderRoot(host);
shadowRoot.adoptedStyleSheets = [styleSheet];
return shadowRoot;
}

static template(html) {
return ({ rank }) => {
return html`<div id="container">${rank}</div>`;
return html`
<style>
:host {
display: block;
width: var(--hello-size, 8rem);
height: var(--hello-size, 8rem);
background-color: cyan;
border-radius: 50%;
margin: 0.25rem;
box-sizing: border-box;
transition-duration: 250ms;
transition-timing-function: cubic-bezier(0.77, 0, 0.175, 1);
transition-property: transform, border;
will-change: transform;
cursor: pointer;
}
#container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
font-size: calc(var(--hello-size, 8rem) * calc(5/11));
}
:host([rank="\u2655"]) {
border: 4px dotted hsl(120, 100%, 50%);
background-color: yellow;
transform: rotateX(15deg) rotateY(15deg);
}
:host([rank="\u2654"]) {
border: 3px solid hsl(270, 100%, 50%);
background-color: magenta;
color: blue;
transform: rotateX(-10deg) rotateY(-15deg);
}
:host(:not([rank])),
:host([rank=""]) {
background-color: #ccc;
}
:host(:hover) {
border: 3px solid hsl(180, 100%, 50%);
transform: translateZ(-25px);
}
:host(:focus) {
border: 12px solid hsl(90, 100%, 50%);
outline: none;
}
#container:empty::before {
content: '\u265F';
}
</style>
<div id="container">${rank}</div>
`;
};
}
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"/index.css"
],
"dependencies": {
"lit-html": "^1.1.1"
"lit-html": "^1.2.1"
},
"devDependencies": {
"@netflix/element-server": "^1.0.12",
"@netflix/x-test": "^1.0.0-rc.11",
"eslint": "^7.0.0",
"puppeteer": "^1.11.0",
"tap-parser": "^9.0.0"
"@netflix/x-test": "^1.0.0-rc.16",
"eslint": "^7.2.0",
"puppeteer": "^4.0.0",
"tap-parser": "^10.0.1"
}
}
Loading

0 comments on commit 358cec0

Please sign in to comment.