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.ts 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
39
  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. buttons.forEach((btn) => {
  17. btn.addEventListener('click', (evt) => {
  18. const target = evt.currentTarget as HTMLButtonElement
  19. const container = target.closest('.js-show-more-container')
  20. if (!container) return
  21. const hiddenLinks = container.querySelectorAll('.js-show-more-item.d-none')
  22. // get number of items to show more of, if not set, show all remaining items
  23. const showMoreNum = target.dataset.jsShowMoreItems || hiddenLinks.length
  24. let count = 0
  25. for (const link of Array.from(hiddenLinks)) {
  26. if (count++ >= showMoreNum) {
  27. break
  28. }
  29. link.classList.remove('d-none')
  30. }
  31. // Remove the button if all items have been shown
  32. if (container.querySelectorAll('.js-show-more-item.d-none').length === 0) {
  33. target?.parentElement?.removeChild(target)
  34. }
  35. })
  36. })
  37. }
Tip!

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

Comments

Loading...