sidebar.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { getDeviceState } from '../core/device'
  2. // Toggle sidebar on click. Here, class "hide" open the sidebar
  3. function toggleSidebar () {
  4. const sidebar = document.getElementById('sidebar-section')
  5. if (sidebar == null) {
  6. return
  7. }
  8. if (sidebar.classList.contains('expanded')) {
  9. sidebar.classList.remove('expanded')
  10. } else {
  11. // if toc-section is open, then close it first
  12. const toc = document.getElementById('toc-section')
  13. if (toc != null && toc.classList.contains('hide')) {
  14. toc.classList.remove('hide')
  15. }
  16. // add "expanded" class
  17. sidebar.classList.add('expanded')
  18. // if it is mobile device. then scroll to top.
  19. const { isMobile } = getDeviceState()
  20. if (isMobile && sidebar.classList.contains('expanded')) {
  21. document.body.scrollTop = 0
  22. document.documentElement.scrollTop = 0
  23. if (document.getElementById('hero-area') != null) {
  24. document.getElementById('hero-area').classList.toggle('hide')
  25. }
  26. }
  27. }
  28. if (document.getElementById('content-section') != null) {
  29. document.getElementById('content-section').classList.toggle('hide')
  30. }
  31. }
  32. window.addEventListener('DOMContentLoaded', () => {
  33. // bind click event to #sidebar-toggler in navbar-2.html
  34. const toggle = document.getElementById('sidebar-toggler')
  35. if (toggle) toggle.addEventListener('click', toggleSidebar)
  36. })