Skip to content

Commit

Permalink
extract x-forecast-item component
Browse files Browse the repository at this point in the history
  • Loading branch information
kherrick committed Nov 11, 2018
1 parent 102eba3 commit 74b8970
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 125 deletions.
113 changes: 53 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "x-weather",
"version": "0.1.1",
"version": "1.0.0",
"description": "A collection of web components implementing portions of the OpenWeatherMap API.",
"files": [
"lib",
Expand Down
77 changes: 77 additions & 0 deletions src/components/x-forecast-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { loadComponent } from '../utilities.js'

const template = `
<style>
:host {
display: block;
}
ul[data-x-forecast-item] {
list-style-type: none;
padding: 0 0 1rem 1.5rem;
}
ul[data-x-forecast] > li {
padding: 0.5rem 0 0 0;
}
</style>
<ul data-x-forecast-item>
<li>
<u id="forecastDate"></u>
</li>
<li>
<img id="icon" />
</li>
<li>Day: <span id="day"></span>°<span data-scale></span></li>
<li>Night: <span id="night"></span>°<span data-scale></span></li>
</ul>
`

const XForecastItem = class extends HTMLElement {
constructor() {
super()

const container = document.createElement('div')
container.innerHTML = template

this.attachShadow({ mode: 'open' }).appendChild(container)
}

attributeChangedCallback(attrName, oldVal, newVal) {
switch (attrName) {
case 'day':
this.shadowRoot.getElementById('day').textContent = newVal
break;
case 'description':
this.shadowRoot.getElementById('icon').alt = newVal
break;
case 'forecast-date':
this.shadowRoot.getElementById('forecastDate').textContent = newVal
break;
case 'icon':
this.shadowRoot.getElementById('icon').src = newVal
break;
case 'night':
this.shadowRoot.getElementById('night').textContent = newVal
break;
case 'scale':
this.shadowRoot.querySelectorAll('[data-scale]').forEach(element => {
element.textContent = newVal
})
break;
}
}

static get observedAttributes() {
return [ 'day', 'description', 'forecast-date', 'icon', 'night', 'scale' ]
}
}

export const load = () => loadComponent({
customElements: customElements,
tagName: 'x-forecast-item',
element: XForecastItem
})

export default XForecastItem
Loading

0 comments on commit 74b8970

Please sign in to comment.