Skip to content

Commit

Permalink
Add an example of a component with custom js
Browse files Browse the repository at this point in the history
  • Loading branch information
emilos committed May 19, 2024
1 parent 44efa21 commit d80d630
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
26 changes: 14 additions & 12 deletions test/components/accordion/index.js
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}')
})
})
`,
],
}
)
19 changes: 19 additions & 0 deletions test/pages/documentation/index.js
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."
),
]),
]),
]
}
11 changes: 11 additions & 0 deletions test/pages/documentation/index.test.js
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"))
})

0 comments on commit d80d630

Please sign in to comment.