Skip to content

Commit 2978f18

Browse files
Stop embedded codelabs scrolling the parent page on load
codelab-elements.js focuses the current step, which scrolls an embedding page to the iframe. When framed, force preventScroll on focus() calls.
1 parent 3a9f205 commit 2978f18

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎build.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ set -euo pipefail
66
PATH="/tmp/claat:${PATH}" python3 ./build.py
77

88
sed -e 's#https://storage.googleapis.com/claat-public#/static#g' -i build/*/index.html
9+
# Stop focus() inside an embedding iframe from scrolling the parent page (see static/iframe-focus-fix.js)
10+
sed -e 's#<script src="/static/codelab-elements.js"></script>#<script src="/static/iframe-focus-fix.js"></script>&#' -i build/*/index.html
11+
if grep -L 'iframe-focus-fix.js' build/*/index.html | grep -q .; then
12+
echo "iframe-focus-fix.js was not injected into every page - has claat's script tag changed?" >&2
13+
exit 1
14+
fi
915

1016
# The public static hosting seems offline (e.g. https://storage.googleapis.com/claat-public/codelab-elements.css) so we vendor these assets:
1117
# Built by running `USE_BAZEL_VERSION=0.19.1 bazel build :bundle` with patches to:

‎static/iframe-focus-fix.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// When a codelab is embedded in an iframe (e.g. on the CYF curriculum site),
2+
// codelab-elements.js focuses the current step on load. Focusing an element
3+
// inside an iframe scrolls the parent page to the iframe, losing the reader's
4+
// place. Force preventScroll on every focus() call, only when embedded.
5+
// Must load before codelab-elements.js.
6+
(function () {
7+
if (window.self === window.top) return;
8+
var originalFocus = HTMLElement.prototype.focus;
9+
HTMLElement.prototype.focus = function (options) {
10+
var opts = Object.assign({}, options, { preventScroll: true });
11+
return originalFocus.call(this, opts);
12+
};
13+
})();

0 commit comments

Comments
 (0)