Skip to content

Commit b22acd9

Browse files
committed
Fix margin note styles so they dont cause overflow or get cut off.
1 parent 986fdc1 commit b22acd9

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

_quarto.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ format:
3131
include-in-header: www/banner.html
3232
theme:
3333
- www/styles.scss
34+
include-after-body:
35+
- text: |
36+
<script src="/media/js/margin-notes.js"></script>
3437
3538
3639

media/js/margin-notes.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Dynamic margin note width adjustment
2+
(function() {
3+
function updateMarginNoteWidth() {
4+
// Skip on mobile devices (< 760px)
5+
if (window.innerWidth < 760) {
6+
document.documentElement.style.setProperty('--margin-note-width', 'auto');
7+
return;
8+
}
9+
10+
const MAX_MARGIN_NOTE_WIDTH_PX = 350;
11+
// Find the main content container - specifically the Quarto document content
12+
const mainContent = document.querySelector('main.content#quarto-document-content');
13+
14+
console.log("Running the margin note script some more")
15+
if (!mainContent) return;
16+
17+
// Get the body's bounding box as the outer limit
18+
const bodyRect = document.body.getBoundingClientRect();
19+
const bodyRight = bodyRect.right;
20+
21+
// Get the main content's position and dimensions
22+
const contentRect = mainContent.getBoundingClientRect();
23+
const contentRight = contentRect.right;
24+
25+
// Calculate available space (body right edge - content right edge - buffer)
26+
const marginBuffer = 16; // 1rem buffer
27+
const availableSpace = bodyRight - contentRight - marginBuffer;
28+
29+
// Set the CSS variable with a maximum of MAX_MARGIN_NOTE_WIDTH_PX
30+
const marginNoteWidth = Math.min(Math.max(0, availableSpace), MAX_MARGIN_NOTE_WIDTH_PX);
31+
32+
// Apply the CSS variable to the root element
33+
document.documentElement.style.setProperty('--margin-note-width', `${marginNoteWidth}px`);
34+
35+
// Log for debugging
36+
console.log(`Body right: ${bodyRight}px, Content right: ${contentRight}px, Available: ${availableSpace}px, Margin width: ${marginNoteWidth}px`);
37+
38+
// Only manipulate display for Quarto-specific margin notes
39+
const marginNotes = document.querySelectorAll('.quarto-container-page .marginnote');
40+
marginNotes.forEach(note => {
41+
// Remove any inline display style to let CSS handle it
42+
note.style.display = '';
43+
});
44+
}
45+
46+
// Update on load
47+
updateMarginNoteWidth();
48+
49+
// Update on resize with debouncing
50+
let resizeTimeout;
51+
window.addEventListener('resize', function() {
52+
clearTimeout(resizeTimeout);
53+
resizeTimeout = setTimeout(updateMarginNoteWidth, 150);
54+
});
55+
56+
// Also update when DOM content is fully loaded
57+
document.addEventListener('DOMContentLoaded', updateMarginNoteWidth);
58+
})();

www/styles.scss

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ img {
324324
.sidenote, .marginnote {
325325
float: right;
326326
clear: right;
327-
margin-right: -60%;
328-
width: 50%;
327+
margin-right: calc(-1 * var(--margin-note-width, 200px) - 1rem);
328+
width: var(--margin-note-width, 200px);
329329
margin-top: 0;
330330
margin-bottom: 0;
331331
font-size: 1.1rem;
@@ -337,42 +337,22 @@ img {
337337
/* Quarto-specific margin note fixes */
338338
.quarto-container-page .marginnote {
339339
position: absolute;
340-
right: -250px;
341-
width: 200px;
342-
margin-right: 0;
340+
left: 100%;
341+
margin-left: 1rem;
342+
max-width: 200px;
343343
font-size: 0.9rem;
344344
line-height: 1.4;
345345
padding: 0.5rem;
346346
background: transparent;
347347
float: none;
348+
box-sizing: border-box;
348349
}
349350

350351
/* Ensure parent has relative positioning */
351352
.quarto-container-page p:has(.marginnote) {
352353
position: relative;
353354
}
354355

355-
/* Handle overflow and ensure margin notes don't cause horizontal scroll */
356-
.quarto-container-page {
357-
overflow-x: hidden;
358-
}
359-
360-
/* Adjust margin notes for narrower screens */
361-
@media (max-width: 1400px) {
362-
.quarto-container-page .marginnote {
363-
right: -200px;
364-
width: 180px;
365-
}
366-
}
367-
368-
@media (max-width: 1200px) {
369-
.quarto-container-page .marginnote {
370-
right: -180px;
371-
width: 160px;
372-
font-size: 0.85rem;
373-
}
374-
}
375-
376356
.table-caption {
377357
float:right;
378358
clear:right;
@@ -439,10 +419,14 @@ h1 .code, h2 .code, h3 .code {
439419
font-size: 0.80em;
440420
}
441421

442-
.marginnote .code, .sidenote .code {
422+
.code, .sidenote .code {
443423
font-size: 1rem;
444424
}
445425

426+
.marginnote {
427+
font-size: 0.85rem;
428+
}
429+
446430
pre {
447431
background-color: initial;
448432
padding: initial;

0 commit comments

Comments
 (0)