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.js 1.3 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
38
39
  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. for (const detailsElement of detailsElements) {
  11. anyDetailsOpen
  12. ? detailsElement.removeAttribute('open')
  13. : detailsElement.open = true
  14. }
  15. // toggle the button text on click
  16. anyDetailsOpen
  17. ? expandButton.textContent = expandText
  18. : expandButton.textContent = closeText
  19. })
  20. // also toggle the button text on clicking any of the details elements
  21. for (const detailsElement of detailsElements) {
  22. detailsElement.addEventListener('click', () => {
  23. expandButton.textContent = closeText
  24. // we can only get an accurate count of the open details elements if we wait a fraction after click
  25. setTimeout(() => {
  26. if (!Array.from(detailsElements).find(details => details.open)) {
  27. expandButton.textContent = expandText
  28. }
  29. }, 50)
  30. })
  31. }
  32. }
Tip!

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

Comments

Loading...