Skip to content

Commit d2ae1c0

Browse files
committed
Make sidebar resizable
1 parent 55754b6 commit d2ae1c0

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

python_docs_theme/static/pydoctheme.css

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ span.pre {
139139
white-space: unset;
140140
}
141141

142+
div.bodywrapper {
143+
margin-left: 250px;
144+
}
145+
142146
div.sphinxsidebar {
147+
display: flex;
148+
width: 250px;
143149
float: none;
144150
position: sticky;
145151
top: 0;
@@ -157,12 +163,12 @@ div.sphinxsidebar h4 {
157163
}
158164

159165
div.sphinxsidebarwrapper {
160-
width: 217px;
161166
box-sizing: border-box;
162167
height: 100%;
163168
overflow-x: hidden;
164169
overflow-y: auto;
165-
float: left;
170+
float: none;
171+
flex-grow: 1;
166172
}
167173

168174
div.sphinxsidebarwrapper > h3:first-child {
@@ -197,12 +203,13 @@ div.sphinxsidebar input[type='text'] {
197203
margin-left: 0;
198204
color: #444444;
199205
font-size: 1.2em;
200-
cursor: pointer;
206+
cursor: col-resize;
201207
padding-top: 1px;
202-
float: right;
208+
float: none;
203209
display: table;
204210
/* after Sphinx 4.x and earlier is dropped, only the below is needed */
205211
width: 12px;
212+
min-width: 12px;
206213
border-radius: 0 5px 5px 0;
207214
border-left: none;
208215
}
@@ -468,7 +475,7 @@ div.genindex-jumpbox a {
468475
font-size: 0.875rem;
469476
}
470477
div.bodywrapper {
471-
margin: 0;
478+
margin: 0 !important;
472479
}
473480
/* Typography */
474481
div.body h1 {
@@ -490,7 +497,7 @@ div.genindex-jumpbox a {
490497
}
491498
/* Remove sidebar and top related bar */
492499
div.related, .sphinxsidebar {
493-
display: none;
500+
display: none !important;
494501
}
495502
/* Anchorlinks are not hidden by fixed-positioned navbar when scrolled to */
496503
html {

python_docs_theme/static/sidebar.js_t

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727

2828
const 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

Comments
 (0)