Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

dev-toc.ts 1.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  1. const expandText = 'Expand All'
  2. const closeText = 'Close All'
  3. export default function devToc() {
  4. const expandButton = document.querySelector('.js-expand')
  5. if (!expandButton) return
  6. const detailsElements = document.querySelectorAll('details')
  7. expandButton.addEventListener('click', () => {
  8. // on click, toggle all the details elements open or closed
  9. const anyDetailsOpen = Array.from(detailsElements).find((details) => details.open)
  10. detailsElements.forEach((detailsElement) => {
  11. anyDetailsOpen ? detailsElement.removeAttribute('open') : (detailsElement.open = true)
  12. })
  13. // toggle the button text on click
  14. anyDetailsOpen
  15. ? (expandButton.textContent = expandText)
  16. : (expandButton.textContent = closeText)
  17. })
  18. // also toggle the button text on clicking any of the details elements
  19. detailsElements.forEach((detailsElement) => {
  20. detailsElement.addEventListener('click', () => {
  21. expandButton.textContent = closeText
  22. // we can only get an accurate count of the open details elements if we wait a fraction after click
  23. setTimeout(() => {
  24. if (!Array.from(detailsElements).find((details) => details.open)) {
  25. expandButton.textContent = expandText
  26. }
  27. }, 50)
  28. })
  29. })
  30. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...