From 98e501b41cc559faf5a30126e534b6fad0378e82 Mon Sep 17 00:00:00 2001 From: Josh Heyer Date: Wed, 30 Oct 2024 05:36:41 +0000 Subject: [PATCH] Expand details elements during printing Also some sensible layout and page break controls for printing --- gatsby-browser.js | 23 +++++++++++++++++++++++ src/components/prev-next.js | 2 +- src/styles/_docs.scss | 26 ++++++++++++++++++++++++++ src/templates/doc.js | 12 +++++++++--- src/templates/learn-doc.js | 14 ++++++++++---- 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/gatsby-browser.js b/gatsby-browser.js index feb9f696645..a91c72c31f3 100644 --- a/gatsby-browser.js +++ b/gatsby-browser.js @@ -19,3 +19,26 @@ function scrollToAnchor(location, mainNavHeight = 0) { return true; } + +exports.onInitialClientRender = () => { + // h/t https://stackoverflow.com/questions/19646684/force-open-the-details-summary-tag-for-print-in-chrome/75260733#75260733 + window.matchMedia("print").addEventListener("change", (evt) => { + if (evt.matches) { + let detailsElements = document.body.querySelectorAll( + "details:not([open])", + ); + for (let e of detailsElements) { + e.toggleAttribute("open", true); + e.dataset.wasclosed = ""; + } + } else { + let detailsElements = document.body.querySelectorAll( + "details[data-wasclosed]", + ); + for (let e of detailsElements) { + e.removeAttribute("open"); + delete e.dataset.wasclosed; + } + } + }); +}; diff --git a/src/components/prev-next.js b/src/components/prev-next.js index 3642fece43c..2a18b32bcd9 100644 --- a/src/components/prev-next.js +++ b/src/components/prev-next.js @@ -7,7 +7,7 @@ const PrevNext = ({ prevNext }) => { let upLink = prevNext.up; return ( -
+
{prevLink && ( { )} -
+
{editTarget !== "none" && ( { ) : null} - + {body} {(!indexCards || indexCards === TileModes.None) && sections && ( @@ -289,7 +295,7 @@ const DocTemplate = ({ data, pageContext }) => { {showToc && ( - + )} diff --git a/src/templates/learn-doc.js b/src/templates/learn-doc.js index 1d2d3d19ccf..c5bcdcd0f0f 100644 --- a/src/templates/learn-doc.js +++ b/src/templates/learn-doc.js @@ -133,7 +133,7 @@ const LearnDocTemplate = ({ data, pageContext }) => { isIndexPage: isPathAnIndexPage(mdx.fileAbsolutePath), productVersions, }; - const { path, depth } = fields; + const { path } = fields; const showToc = !!mdx.tableOfContents.items && !frontmatter.hideToC; const showInteractiveBadge = @@ -212,17 +212,23 @@ const LearnDocTemplate = ({ data, pageContext }) => { )}

{title}

- {editOrFeedbackButton} +
{editOrFeedbackButton}
- + {mdx.body} {showToc && ( - + )}