Skip to content

Instantly share code, notes, and snippets.

@IrisCZ
Created September 20, 2019 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IrisCZ/a6907cf0cebd9e1d3be77d1c58dfcda0 to your computer and use it in GitHub Desktop.
Save IrisCZ/a6907cf0cebd9e1d3be77d1c58dfcda0 to your computer and use it in GitHub Desktop.
class NavBar extends HTMLElement {
constructor() {
super();
this.render = this.render.bind(this);
this.render();
this.optionButtons = this.shadowRoot.querySelectorAll('.option_button');
this.optionButtons.forEach(optionButton =>
optionButton.addEventListener('click', this.handleClick)
);
}
handleClick(e) {
const selected = e.target.textContent;
const container = document.querySelector('#display-field');
container.setAttribute('selected', selected);
}
options() {
let options = data;
return Object.keys(options).map(option => {
return `
<li value="${option}" class="nav_option">
<button class="option_button" onclick="this.handleClick">${option}</button>
</li>`;
}).join('');
}
render() {
const shadowRoot = this.shadowRoot || this.attachShadow({ mode: 'open'});
shadowRoot.innerHTML = `
<ol id="${this.id}" class="nav_bar">
${this.options()}
</ol>
`;
}
}
customElements.define('nav-bar', NavBar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment