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

show-more.js 1.4 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
  1. /*
  2. * This utility component implement a list of items, some of which are hidden initially
  3. * until user clicks "show more".
  4. *
  5. * Example:
  6. *
  7. * <div class="js-show-more-container">
  8. * <div class="js-show-more-item">item</div>
  9. * <div class="js-show-more-item d-none">hidden item</div>
  10. * <div class="js-show-more-item d-none">hidden item</div>
  11. * <button class="js-show-more-button" data-js-show-more-items="1">show one more item</button>
  12. * </div>
  13. */
  14. export default function showMore () {
  15. const buttons = document.querySelectorAll('.js-show-more-button')
  16. for (const btn of buttons) {
  17. btn.addEventListener('click', evt => {
  18. const container = evt.currentTarget.closest('.js-show-more-container')
  19. if (!container) return
  20. const hiddenLinks = container.querySelectorAll('.js-show-more-item.d-none')
  21. // get number of items to show more of, if not set, show all remaining items
  22. const showMoreNum = evt.currentTarget.dataset.jsShowMoreItems || hiddenLinks.length
  23. let count = 0
  24. for (const link of hiddenLinks) {
  25. if (count++ >= showMoreNum) {
  26. break
  27. }
  28. link.classList.remove('d-none')
  29. }
  30. // Remove the button if all items have been shown
  31. if (container.querySelectorAll('.js-show-more-item.d-none').length === 0) {
  32. evt.currentTarget.parentElement.removeChild(evt.currentTarget)
  33. }
  34. })
  35. }
  36. }
Tip!

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

Comments

Loading...