2727
2828const initialiseSidebar = () => {
2929 // global elements used by the functions.
30- const bodyWrapper = document.getElementsByClassName(" bodywrapper")[0]
31- const sidebar = document.getElementsByClassName(" sphinxsidebar")[0]
32- const sidebarWrapper = document.getElementsByClassName(" sphinxsidebarwrapper")[0]
30+ const bodyWrapper = document.querySelector(". bodywrapper")
31+ const sidebar = document.querySelector(". sphinxsidebar")
32+ const sidebarWrapper = document.querySelector(". sphinxsidebarwrapper")
3333
3434 // exit early if the document has no sidebar for some reason
35- if (typeof sidebar === "undefined" ) {
35+ if (! sidebar) {
3636 return
3737 }
3838
@@ -44,46 +44,43 @@ const initialiseSidebar = () => {
4444 #}
4545{% if sphinx_version_tuple is defined and sphinx_version_tuple[0] >= 5 %}
4646 const sidebarButton = document.getElementById("sidebarbutton")
47- const sidebarArrow = sidebarButton.querySelector('span')
4847{% else %}
4948 // create the sidebar button element
5049 const sidebarButton = document.createElement("div")
5150 sidebarButton.id = "sidebarbutton"
52- // create the sidebar button arrow element
53- const sidebarArrow = document.createElement("span")
54- sidebarArrow.innerText = "«"
55- sidebarButton.appendChild(sidebarArrow)
56- sidebar.appendChild(sidebarButton)
5751{% endif %}
52+ sidebarbutton.innerHTML = ""
53+ sidebarbutton.tabindex = "0" // make it focusable
54+ sidebarbutton.role = "slider"
55+ sidebarbutton.title = _("Resize sidebar")
56+ sidebarbutton.setAttribute("aria-label", _("Resize sidebar by dragging"))
57+ sidebarbutton.setAttribute("aria-valuetext", _("Sidebar width XXX pixels"))
58+ let clientX;
5859
59- const collapse_sidebar = () => {
60- bodyWrapper.style.marginLeft = ".8em"
61- sidebar.style.width = ".8em"
62- sidebarWrapper.style.display = "none"
63- sidebarArrow.innerText = "»"
64- sidebarButton.title = _("Expand sidebar")
65- window.localStorage.setItem("sidebar", "collapsed")
60+ function onMouseMove(e) {
61+ e.preventDefault()
62+ const sidebarWidth = sidebar.offsetWidth
63+ const newWidth = Math.max(0, Math.min(window.innerWidth, sidebarWidth + e.clientX - clientX))
64+ clientX = e.clientX
65+ sidebar.style.width = `${newWidth}px`
66+ bodyWrapper.style.marginLeft = `${newWidth}px`
67+ window.localStorage.setItem("sidebar-width", newWidth)
6668 }
6769
68- const expand_sidebar = () => {
69- bodyWrapper.style.marginLeft = ""
70- sidebar.style.removeProperty("width")
71- sidebarWrapper.style.display = ""
72- sidebarArrow.innerText = "«"
73- sidebarButton.title = _("Collapse sidebar")
74- window.localStorage.setItem("sidebar", "expanded")
75- }
76-
77- sidebarButton.addEventListener("click", () => {
78- (sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar()
70+ sidebarButton.addEventListener("mousedown", e => {
71+ e.preventDefault()
72+ clientX = e.clientX
73+ document.addEventListener("mousemove", onMouseMove)
74+ document.addEventListener("mouseup", () => {
75+ document.removeEventListener("mousemove", onMouseMove)
76+ sidebarbutton.setAttribute("aria-valuetext", _("Sidebar width XXX pixels"))
77+ })
7978 })
8079
81- const sidebar_state = window.localStorage.getItem("sidebar")
82- if (sidebar_state === "collapsed") {
83- collapse_sidebar()
84- }
85- else if (sidebar_state === "expanded") {
86- expand_sidebar()
80+ const sidebarWidth = parseInt(window.localStorage.getItem("sidebar-width"), 10)
81+ if(Number.isFinite(sidebarWidth)) {
82+ sidebar.style.width = `${sidebarWidth}px`
83+ bodyWrapper.style.marginLeft = `${sidebarWidth}px`
8784 }
8885}
8986
0 commit comments