Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Accordion Component</title> | ||
<link rel="stylesheet" type="text/css" href="../../src/debug.css"> | ||
</head> | ||
<body> | ||
<element name="accordion" extends="div"> | ||
<template> | ||
<content></content> | ||
</template> | ||
<script> | ||
if (this !== window) { | ||
this.lifecycle({ | ||
created: function(root) { | ||
this.root = root; | ||
var selectedSection = this.selectedSection = this.querySelector('section'); | ||
setTimeout(function() { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cletusw
Author
Owner
|
||
selectedSection.selected = true; | ||
}, 10); | ||
|
||
// Attach listeners | ||
this.addEventListener('click', function(event) { | ||
var clickedSection = event.target.parentElement; | ||
if (clickedSection.selected === false) { | ||
selectedSection.selected = false; | ||
clickedSection.selected = true; | ||
selectedSection = clickedSection; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
</script> | ||
</element> | ||
<element name="accordion-section" extends="section"> | ||
<template> | ||
<style scoped> | ||
This comment has been minimized.
Sorry, something went wrong.
dglazkov
|
||
header { | ||
cursor: pointer; | ||
} | ||
div { | ||
display: none; | ||
} | ||
div.selected { | ||
display: block; | ||
} | ||
</style> | ||
<header> | ||
<content select="h2:first-of-type"></content> | ||
</header> | ||
<div> | ||
<content></content> | ||
</div> | ||
</template> | ||
<script> | ||
if (this !== window) { | ||
this.lifecycle({ | ||
created: function(root) { | ||
// Define instance properties | ||
Object.defineProperties(this, { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cletusw
Author
Owner
|
||
_root: { | ||
value: root | ||
}, | ||
_content: { | ||
value: root.querySelector('div') | ||
}, | ||
_selected: { | ||
value: false, | ||
writable: true | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
this.generatedConstructor.prototype = Object.create(Object.prototype, { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cletusw
Author
Owner
|
||
selected: { | ||
get: function() { | ||
return this._selected; | ||
}, | ||
set: function(selected) { | ||
if (this._selected !== selected) { | ||
if (selected === true) { | ||
this.classList.add('selected'); | ||
this._content.classList.add('selected'); | ||
} | ||
else { | ||
this.classList.remove('selected'); | ||
this._content.classList.remove('selected'); | ||
} | ||
|
||
this._selected = selected; | ||
} | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
} | ||
}); | ||
} | ||
</script> | ||
</element> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Accordion Component Example</title> | ||
<link rel="components" href="accordion-component.html"> | ||
<script src="../../src/components-polyfill.js"></script> | ||
<style> | ||
/* These styles are based on the CSS accordion here: | ||
* http://www.hongkiat.com/blog/css-content-accordion/ | ||
*/ | ||
html { | ||
overflow-y: scroll; | ||
} | ||
body { | ||
font-family: sans-serif; | ||
font-size: 13px; | ||
} | ||
.accordion { | ||
width: 830px; | ||
overflow: hidden; | ||
margin: 10px auto; | ||
padding: 10px; | ||
color: #474747; | ||
background: #414141; | ||
} | ||
.accordion section { | ||
overflow: hidden; | ||
margin-bottom: 6px; | ||
padding: 10px; | ||
color: #333; | ||
background: white; | ||
} | ||
.accordion section:last-of-type { | ||
margin-bottom: 0; | ||
} | ||
.accordion h2 { | ||
margin: -10px; | ||
padding: 8px 10px; | ||
font-size: 16px; | ||
font-weight: normal; | ||
color: #eee; | ||
background: #333; | ||
} | ||
.accordion h2:hover { | ||
background: #444; | ||
} | ||
.accordion .selected h2 { | ||
color: inherit; | ||
background: none; | ||
} | ||
.contact label { | ||
display: block; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Accordion Component Example</h1> | ||
|
||
<div is="accordion" class="accordion"> | ||
<section is="accordion-section"> | ||
<h2>About Us</h2> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id lobortis massa. Nunc viverra velit leo, sit amet elementum mi. Fusce posuere nunc a mi tempus malesuada. Curabitur facilisis rhoncus eros eget placerat. Aliquam semper mauris sit amet justo tempor nec lacinia magna molestie. Etiam placerat congue dolor vitae adipiscing. Aliquam ac erat lorem, ut iaculis justo. Etiam mattis dignissim gravida. Aliquam nec justo ante, non semper mi. Nulla consectetur interdum massa, vel porta enim vulputate sed. Maecenas elit quam, egestas eget placerat non, fringilla vel eros. Nam vehicula elementum nulla sed consequat. Phasellus eu erat enim. Praesent at magna non massa dapibus scelerisque in eu lorem.</p> | ||
</section> | ||
|
||
<section is="accordion-section"> | ||
<h2>Services</h2> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id lobortis massa. Nunc viverra velit leo, sit amet elementum mi. Fusce posuere nunc a mi tempus malesuada. Curabitur facilisis rhoncus eros eget placerat.</p> | ||
<p>Aliquam semper mauris sit amet justo tempor nec lacinia magna molestie. Etiam placerat congue dolor vitae adipiscing. Aliquam ac erat lorem, ut iaculis justo. Etiam mattis dignissim gravida. Aliquam nec justo ante, non semper mi. Nulla consectetur interdum massa, vel porta enim vulputate sed.</p> | ||
<p>Maecenas elit quam, egestas eget placerat non, fringilla vel eros. Nam vehicula elementum nulla sed consequat. Phasellus eu erat enim. Praesent at magna non massa dapibus scelerisque in eu lorem.</p> | ||
</section> | ||
|
||
<section is="accordion-section"> | ||
<h2>Blog</h2> | ||
<article> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id lobortis massa. Nunc viverra velit leo, sit amet elementum mi. Fusce posuere nunc a mi tempus malesuada. Curabitur facilisis rhoncus eros eget placerat.</p> | ||
</article> | ||
<article> | ||
<p>Aliquam semper mauris sit amet justo tempor nec lacinia magna molestie. Etiam placerat congue dolor vitae adipiscing. Aliquam ac erat lorem, ut iaculis justo. Etiam mattis dignissim gravida. Aliquam nec justo ante, non semper mi. Nulla consectetur interdum massa, vel porta enim vulputate sed.</p> | ||
</article> | ||
<article> | ||
<p>Maecenas elit quam, egestas eget placerat non, fringilla vel eros. Nam vehicula elementum nulla sed consequat. Phasellus eu erat enim. Praesent at magna non massa dapibus scelerisque in eu lorem.</p> | ||
</article> | ||
</section> | ||
|
||
<section is="accordion-section"> | ||
<h2>Portfolio</h2> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> | ||
<ul> | ||
<li>Suspendisse id lobortis massa. Nunc viverra velit leo, sit amet elementum mi</li> | ||
<li>Fusce posuere nunc a mi tempus malesuada. Curabitur facilisis rhoncus eros eget placerat</li> | ||
<li>Aliquam semper mauris sit amet justo tempor nec lacinia magna molestie. Etiam placerat congue dolor vitae adipiscing</li> | ||
<li>Aliquam ac erat lorem, ut iaculis justo. Etiam mattis dignissim gravida</li> | ||
<li>Aliquam nec justo ante, non semper mi. Nulla consectetur interdum massa, vel porta enim vulputate sed</li> | ||
<li>Maecenas elit quam, egestas eget placerat non, fringilla vel eros. Nam vehicula elementum nulla sed consequat</li> | ||
<li>Phasellus eu erat enim. Praesent at magna non massa dapibus scelerisque in eu lorem.</li> | ||
</ul> | ||
</section> | ||
|
||
<section is="accordion-section"> | ||
<h2>Contact</h2> | ||
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse id lobortis massa.</p> | ||
<form class="contact"> | ||
<label>Lorem<input type="text" /></label> | ||
<label>Ipsum<input type="password" /></label> | ||
<label>Dolor<input type="radio" /></label> | ||
<label>Sit<input type="checkbox" /></label> | ||
<input type="submit" /> | ||
</form> | ||
</section> | ||
</div> | ||
</body> | ||
</html> |
4 comments
on commit 34d00f4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having sections as separate components is an interesting approach. It let the actual accordion be just a simple controller. I agree, it would be cool to have only is="accordion", and the rest of it "just work", but I think this sample is worth preserving in its present form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for learning purposes, do you know how I could do that? Can I 'upgrade' the section components from within the create method of the accordion component? Or is there some way to use loops/repeated templates in the parent component and not even make the children components at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One way to do this would be to use the same approach as the Tab Manager example and simply distribute contents of the accordion component into the right insertion points. This would mean that you can't use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Thanks!
Not sure why you need a timeout here.