-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example of a component with custom js
- Loading branch information
Showing
3 changed files
with
44 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
const { component, css, div, js, h3 } = require("../../..") | ||
|
||
const styles = css.load(__dirname, "index.css") | ||
|
||
const code = js` | ||
const accordions = document.querySelector('.${styles.accordion}') | ||
accordions.forEach(accordion => { | ||
accordion.addEventListener('click', function () { | ||
const sibling = this.nextElement | ||
sibling.classList.toggle('${styles.content}') | ||
}) | ||
}) | ||
` | ||
|
||
module.exports = component( | ||
({ title }, children) => { | ||
return [ | ||
h3({ class: styles.accordion }, title), | ||
div({ class: [styles.content, styles.hidden] }, children), | ||
] | ||
}, | ||
{ styles, code } | ||
{ | ||
styles, | ||
scripts: [ | ||
js` | ||
const accordions = document.querySelector('.${styles.accordion}') | ||
accordions.forEach(accordion => { | ||
accordion.addEventListener('click', function () { | ||
const sibling = this.nextElement | ||
sibling.classList.toggle('${styles.content}') | ||
}) | ||
}) | ||
`, | ||
], | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { doctype, html, head, body, title, h1, p } = require("boxwood") | ||
const accordion = require("../../components/accordion") | ||
|
||
module.exports = () => { | ||
return [ | ||
doctype(), | ||
html([ | ||
head([title("Documentation")]), | ||
body([ | ||
h1("Documentation"), | ||
p("This is the documentation page."), | ||
accordion( | ||
{ title: "Accordion" }, | ||
"This is the content of the accordion." | ||
), | ||
]), | ||
]), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const test = require("node:test") | ||
const assert = require("node:assert") | ||
const { join } = require("path") | ||
const { compile } = require("../../..") | ||
|
||
test("#pages/documentation: it returns a page with an accordion", async () => { | ||
const { template } = await compile(join(__dirname, "./index.js")) | ||
const html = template() | ||
assert(html.includes("Accordion")) | ||
assert(html.includes("const sibling = this.nextElement")) | ||
}) |