|
1 | 1 | // SPDX-License-Identifier: MIT |
2 | | -// Copyright (c) 2020-2023 The Pybricks Authors |
| 2 | +// Copyright (c) 2020-2026 The Pybricks Authors |
3 | 3 |
|
4 | 4 | import 'react-splitter-layout/lib/index.css'; |
5 | 5 | import './app.scss'; |
@@ -52,76 +52,14 @@ const Docs: React.FunctionComponent = () => { |
52 | 52 |
|
53 | 53 | return ( |
54 | 54 | <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. |
60 | 55 | 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 | | - |
66 | 56 | const target = e.target as HTMLIFrameElement; |
67 | 57 | const contentWindow = target.contentWindow; |
68 | 58 | if (!contentWindow) { |
69 | 59 | console.error('could not get iframe content window'); |
70 | 60 | return; |
71 | 61 | } |
72 | 62 |
|
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 | | - |
125 | 63 | // Override browser default key bindings in iframe. |
126 | 64 | contentWindow.document.addEventListener('keydown', (e) => { |
127 | 65 | // use Ctrl-D/Cmd-D to toggle docs |
|
0 commit comments