-
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the sidebar resizable #214
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,13 +26,14 @@ | |
*/ | ||
|
||
const initialiseSidebar = () => { | ||
const ngettext = Documentation.ngettext | ||
// global elements used by the functions. | ||
const bodyWrapper = document.getElementsByClassName("bodywrapper")[0] | ||
const sidebar = document.getElementsByClassName("sphinxsidebar")[0] | ||
const sidebarWrapper = document.getElementsByClassName("sphinxsidebarwrapper")[0] | ||
const bodyWrapper = document.querySelector(".bodywrapper") | ||
const sidebar = document.querySelector(".sphinxsidebar") | ||
const sidebarWrapper = document.querySelector(".sphinxsidebarwrapper") | ||
|
||
// exit early if the document has no sidebar for some reason | ||
if (typeof sidebar === "undefined") { | ||
if (!sidebar) { | ||
return | ||
} | ||
|
||
|
@@ -44,46 +45,64 @@ const initialiseSidebar = () => { | |
#} | ||
{% if sphinx_version_tuple is defined and sphinx_version_tuple[0] >= 5 %} | ||
const sidebarButton = document.getElementById("sidebarbutton") | ||
const sidebarArrow = sidebarButton.querySelector('span') | ||
{% else %} | ||
// create the sidebar button element | ||
const sidebarButton = document.createElement("div") | ||
sidebarButton.id = "sidebarbutton" | ||
// create the sidebar button arrow element | ||
const sidebarArrow = document.createElement("span") | ||
sidebarArrow.innerText = "«" | ||
sidebarButton.appendChild(sidebarArrow) | ||
sidebar.appendChild(sidebarButton) | ||
{% endif %} | ||
const sidebarMinWidth = 200 | ||
const sidebarMaxWidth = Math.round(0.5 * window.innerWidth) | ||
|
||
const collapse_sidebar = () => { | ||
bodyWrapper.style.marginLeft = ".8em" | ||
sidebar.style.width = ".8em" | ||
sidebarWrapper.style.display = "none" | ||
sidebarArrow.innerText = "»" | ||
sidebarButton.title = _("Expand sidebar") | ||
window.localStorage.setItem("sidebar", "collapsed") | ||
} | ||
sidebarbutton.innerHTML = "" | ||
sidebarbutton.tabindex = "0" // make it focusable | ||
sidebarbutton.role = "slider" | ||
sidebarbutton.title = _("Resize sidebar") | ||
sidebarbutton.style.cursor = "col-resize" // Set the cursor only if JS is enabled | ||
sidebarbutton.setAttribute("aria-label", _("Resize sidebar by dragging")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting that |
||
sidebarbutton.setAttribute( | ||
"aria-valuetext", | ||
ngettext( | ||
"Sidebar width {count} pixel", | ||
"Sidebar width {count} pixels", | ||
sidebar.offsetWidth | ||
).replace("{count}", sidebar.offsetWidth) | ||
) | ||
let clientX; | ||
|
||
const expand_sidebar = () => { | ||
bodyWrapper.style.marginLeft = "" | ||
sidebar.style.removeProperty("width") | ||
sidebarWrapper.style.display = "" | ||
sidebarArrow.innerText = "«" | ||
sidebarButton.title = _("Collapse sidebar") | ||
window.localStorage.setItem("sidebar", "expanded") | ||
function onMouseMove(e) { | ||
e.preventDefault() | ||
const sidebarWidth = sidebar.offsetWidth | ||
const newWidth = Math.max( | ||
sidebarMinWidth, | ||
Math.min(sidebarMaxWidth, sidebarWidth + e.clientX - clientX) | ||
) | ||
clientX = e.clientX | ||
sidebar.style.width = `${newWidth}px` | ||
bodyWrapper.style.marginLeft = `${newWidth}px` | ||
window.localStorage.setItem("sidebar-width", newWidth) | ||
} | ||
|
||
sidebarButton.addEventListener("click", () => { | ||
(sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar() | ||
sidebarButton.addEventListener("mousedown", e => { | ||
e.preventDefault() | ||
clientX = e.clientX | ||
document.addEventListener("mousemove", onMouseMove) | ||
document.addEventListener("mouseup", () => { | ||
document.removeEventListener("mousemove", onMouseMove) | ||
sidebarbutton.setAttribute( | ||
"aria-valuetext", | ||
ngettext( | ||
"Sidebar width {count} pixel", | ||
"Sidebar width {count} pixels", | ||
sidebar.offsetWidth | ||
).replace("{count}", sidebar.offsetWidth) | ||
) | ||
}) | ||
}) | ||
|
||
const sidebar_state = window.localStorage.getItem("sidebar") | ||
if (sidebar_state === "collapsed") { | ||
collapse_sidebar() | ||
} | ||
else if (sidebar_state === "expanded") { | ||
expand_sidebar() | ||
const sidebarWidth = parseInt(window.localStorage.getItem("sidebar-width"), 10) | ||
if(Number.isFinite(sidebarWidth)) { | ||
sidebar.style.width = `${sidebarWidth}px` | ||
bodyWrapper.style.marginLeft = `${sidebarWidth}px` | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting that I could not focus this while navigating with a keyboard nor through screen reader so there might be something tripping this.