-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collapse): add collapse component
- Loading branch information
1 parent
21d6a42
commit 8254ece
Showing
6 changed files
with
87 additions
and
3 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
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
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,54 @@ | ||
export class Collapse extends HTMLElement { | ||
NAME = "collapse"; | ||
|
||
static register() { | ||
customElements.define("bl-collapse", this); | ||
document.querySelectorAll("[data-toggle='collapse']").forEach((element) => { | ||
element.addEventListener("click", () => { | ||
const targetString = element.getAttribute("data-target"); | ||
if (targetString) { | ||
const target = document.querySelector<Collapse>(targetString); | ||
if (target) { | ||
target.toggle(); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
connectedCallback() { | ||
this.classList.add("bl-collapse"); | ||
this.setAttribute("aria-hidden", "true"); | ||
const wrapper = document.createElement("div"); | ||
wrapper.classList.add("bl-collapse-wrapper"); | ||
wrapper.append(...this.childNodes); | ||
this.append(wrapper); | ||
this.hide(); | ||
} | ||
|
||
show() { | ||
this.ariaHidden = "false"; | ||
this.classList.add("show"); | ||
} | ||
|
||
hide() { | ||
this.ariaHidden = "true"; | ||
this.classList.remove("show"); | ||
} | ||
|
||
toggle() { | ||
if (this.expanded) { | ||
this.hide(); | ||
} else { | ||
this.show(); | ||
} | ||
} | ||
|
||
get expanded() { | ||
return this.classList.contains("show"); | ||
} | ||
} |
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,13 @@ | ||
.bl-collapse { | ||
display: grid; | ||
grid-template-rows: 0fr; | ||
transition: grid-template-rows 0.3s ease-in-out; | ||
|
||
& > .bl-collapse-wrapper { | ||
overflow: hidden; | ||
} | ||
|
||
&.show { | ||
grid-template-rows: 1fr; | ||
} | ||
} |
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
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