Skip to content

Commit 1e1bf83

Browse files
committed
App: Drop hacks that preserved docs scroll.
The iframe automatically scrolled to 0 when CSS `display: none` is set, so we had several workarounds. Hide it instead.
1 parent 98d2e82 commit 1e1bf83

File tree

2 files changed

+11
-66
lines changed

2 files changed

+11
-66
lines changed

src/app/App.tsx

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2020-2023 The Pybricks Authors
2+
// Copyright (c) 2020-2026 The Pybricks Authors
33

44
import 'react-splitter-layout/lib/index.css';
55
import './app.scss';
@@ -52,76 +52,14 @@ const Docs: React.FunctionComponent = () => {
5252

5353
return (
5454
<iframe
55-
// REVISIT: some of this could be moved to the docs repo
56-
// so that it runs earlier to prevent flashing in the UI.
57-
// The load event doesn't run until after the page is fully
58-
// loaded and there doesn't seem to be a reasonable way to
59-
// hook into the iframe to know when it has a new document.
6055
onLoad={(e) => {
61-
// HACK: this mess restores the scroll position when
62-
// the documentation iframe visibility is toggled.
63-
// The iframe will be automatically scrolled to 0 when
64-
// CSS `display: none` is set.
65-
6656
const target = e.target as HTMLIFrameElement;
6757
const contentWindow = target.contentWindow;
6858
if (!contentWindow) {
6959
console.error('could not get iframe content window');
7060
return;
7161
}
7262

73-
// the last "good" scrollY value of the iframe
74-
let iframeScroll = 0;
75-
76-
// This bit monitors the visibility.
77-
// https://stackoverflow.com/a/44670818/1976323
78-
const observer = new IntersectionObserver(
79-
(entries) => {
80-
entries.forEach((entry) => {
81-
// Restore the scroll position when the iframe is
82-
// shown. Toggling the visibility prevents flashing
83-
// the contents from the top of the page before the
84-
// scroll is done.
85-
if (entry.intersectionRatio > 0) {
86-
contentWindow.scrollTo(0, iframeScroll);
87-
contentWindow.document.documentElement.style.visibility =
88-
'visible';
89-
} else {
90-
contentWindow.document.documentElement.style.visibility =
91-
'hidden';
92-
}
93-
});
94-
},
95-
{
96-
root: target.parentElement,
97-
},
98-
);
99-
100-
observer.observe(target);
101-
102-
// Have to remove he observer, otherwise we end up with
103-
// conflicting values when a new page is loaded in the iframe.
104-
contentWindow.addEventListener('unload', () => {
105-
observer.unobserve(target);
106-
});
107-
108-
// And this keeps track of the scroll position.
109-
contentWindow.addEventListener('scroll', () => {
110-
if (contentWindow.scrollY !== 0) {
111-
// Record the current scroll position. If it is 0, it
112-
// could be that the iframe has been hidden or the user
113-
// scrolled there. So we have to ignore 0. But we don't
114-
// want to be one pixel off if the user really did
115-
// scroll there, so we assume that if the last scroll
116-
// is 1, then the user probably went all the way to 0.
117-
if (contentWindow.scrollY === 1) {
118-
iframeScroll = 0;
119-
} else {
120-
iframeScroll = contentWindow.scrollY;
121-
}
122-
}
123-
});
124-
12563
// Override browser default key bindings in iframe.
12664
contentWindow.document.addEventListener('keydown', (e) => {
12765
// use Ctrl-D/Cmd-D to toggle docs

src/app/app.scss

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2020-2022 The Pybricks Authors
2+
// Copyright (c) 2020-2026 The Pybricks Authors
33

44
// Custom styling for the App control.
55

@@ -184,7 +184,14 @@ $dark-splitter-color-hover: color.adjust(
184184
}
185185

186186
// hide the docs and resize separator
187-
187+
// Use visibility rather than display:none so the iframe keeps its layout and
188+
// scroll position when the docs panel is toggled.
189+
// Collapse the secondary pane and splitter to zero width so they don't
190+
// consume space, while keeping the iframe in the DOM.
188191
div.pb-hide-docs > :not(.layout-pane-primary) {
189-
display: none;
192+
visibility: hidden;
193+
pointer-events: none;
194+
flex-basis: 0 !important;
195+
width: 0 !important;
196+
min-width: 0 !important;
190197
}

0 commit comments

Comments
 (0)