Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork66
Make the sidebar resizable#214
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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.querySelector(".bodywrapper") | ||
const sidebar = document.querySelector(".sphinxsidebar") | ||
const sidebarWrapper = document.querySelector(".sphinxsidebarwrapper") | ||
// exit early if the document has no sidebar for some reason | ||
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") | ||
{% else %} | ||
// create the sidebar button element | ||
const sidebarButton = document.createElement("div") | ||
sidebarButton.id = "sidebarbutton" | ||
{% endif %} | ||
const sidebarMinWidth = 200 | ||
const sidebarMaxWidth = Math.round(0.5 * window.innerWidth) | ||
sidebarbutton.innerHTML = "" | ||
sidebarbutton.tabindex = "0" // make it focusable | ||
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 I could not focus this while navigating with a keyboard nor through screen reader so there might be something tripping this. | ||
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; | ||
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("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 sidebarWidth = parseInt(window.localStorage.getItem("sidebar-width"), 10) | ||
if(Number.isFinite(sidebarWidth)) { | ||
sidebar.style.width = `${sidebarWidth}px` | ||
bodyWrapper.style.marginLeft = `${sidebarWidth}px` | ||
} | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.