Repository navigation
feat(ui): add reusable back-to-top button for long pages, fixed #975 - #976
Flashl3opard wants to merge 5 commits into
Conversation
WalkthroughAdds a floating "Back to Top" button to the base template with scroll-triggered visibility and smooth scroll-to-top behavior, and a new unit test that asserts the button exists on the homepage. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@web/templates/base.html`:
- Around line 1170-1173: The back-to-top button (`#back-to-top-btn`) uses
bg-teal-500/hover:bg-teal-600 and dark variants which don't match the project's
primary palette; update the Tailwind classes on the element with id
"back-to-top-btn" to use the primary button palette (e.g., bg-teal-300
hover:bg-teal-400 and the corresponding dark variants like dark:bg-teal-400
dark:hover:bg-teal-500) while keeping other utility classes (text-white,
rounded-full, shadow-lg, etc.) intact.
- Around line 1133-1154: Extract the visibility toggle into a function (e.g.,
updateBackToTopVisibility) that reads window.scrollY and adds/removes the same
classes used in the scroll handler for the element referenced by backToTopBtn;
call that function once on DOMContentLoaded (immediately after obtaining
backToTopBtn) and also from the existing window.addEventListener('scroll', ...)
so the button state initializes correctly when loading into a scrolled position.
Also update the button's CSS classes to use bg-teal-300 and hover:bg-teal-400
instead of bg-teal-500 and hover:bg-teal-600 (adjust wherever the back-to-top
element or its template sets those classes).
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@web/templates/base.html`:
- Around line 1177-1190: The Back-to-Top button with id "back-to-top-btn" lacks
a visible focus indicator for keyboard users; update its attributes/classes to
include keyboard focus styles (e.g., focus and focus-visible utilities such as
focus:outline-none plus a visible ring like focus:ring-4 and
focus:ring-teal-500/50 or a contrasting outline) so when the element receives
keyboard focus it shows a clear focus ring; ensure the focus styles are applied
regardless of hover and work with the existing opacity/invisible toggling logic
so keyboard users can see the button's focus state when it becomes visible.
- Around line 1131-1174: The new back-to-top script block is misindented;
reformat the entire DOMContentLoaded handler so it uses the same 8-space
indentation style as the rest of the file—indent the
document.addEventListener('DOMContentLoaded', ... ), the const backToTopBtn
declaration, the updateBackToTopVisibility() function body, the
window.addEventListener('scroll', updateBackToTopVisibility, { passive: true }),
and the backToTopBtn.addEventListener('click', ...) handler to match the
existing 8-space indentation used elsewhere.
|
|
||
| // Back to Top Button functionality | ||
| document.addEventListener('DOMContentLoaded', function () { | ||
|
|
||
| const backToTopBtn = document.getElementById('back-to-top-btn'); | ||
| if (!backToTopBtn) return; | ||
|
|
||
| // Extracted visibility toggle | ||
| function updateBackToTopVisibility() { | ||
| if (window.scrollY > 300) { | ||
| backToTopBtn.classList.remove( | ||
| 'opacity-0', | ||
| 'invisible', | ||
| 'pointer-events-none' | ||
| ); | ||
| backToTopBtn.classList.add('opacity-100', 'visible'); | ||
| } else { | ||
| backToTopBtn.classList.add( | ||
| 'opacity-0', | ||
| 'invisible', | ||
| 'pointer-events-none' | ||
| ); | ||
| backToTopBtn.classList.remove('opacity-100', 'visible'); | ||
| } | ||
| } | ||
|
|
||
| // Initialize correctly on page load | ||
| updateBackToTopVisibility(); | ||
|
|
||
| // Reuse same logic on scroll | ||
| window.addEventListener('scroll', updateBackToTopVisibility, { | ||
| passive: true | ||
| }); | ||
|
|
||
| // Scroll action | ||
| backToTopBtn.addEventListener('click', function (e) { | ||
| e.preventDefault(); | ||
| window.scrollTo({ | ||
| top: 0, | ||
| behavior: 'smooth' | ||
| }); | ||
| }); | ||
|
|
||
| }); |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
JavaScript indentation is inconsistent with the rest of the file.
Other script blocks in this file (e.g., lines 25–86, 1086–1130) use 8-space indentation. The new back-to-top code has no indentation, making the file inconsistent.
🔧 Proposed indentation fix
- // Back to Top Button functionality
-document.addEventListener('DOMContentLoaded', function () {
-
- const backToTopBtn = document.getElementById('back-to-top-btn');
- if (!backToTopBtn) return;
-
- // Extracted visibility toggle
- function updateBackToTopVisibility() {
- if (window.scrollY > 300) {
+ // Back to Top Button functionality
+ document.addEventListener('DOMContentLoaded', function () {
+ const backToTopBtn = document.getElementById('back-to-top-btn');
+ if (!backToTopBtn) return;
+
+ // Extracted visibility toggle
+ function updateBackToTopVisibility() {
+ if (window.scrollY > 300) {Apply consistent 8-space indentation throughout the entire block (lines 1132–1174).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/templates/base.html` around lines 1131 - 1174, The new back-to-top script
block is misindented; reformat the entire DOMContentLoaded handler so it uses
the same 8-space indentation style as the rest of the file—indent the
document.addEventListener('DOMContentLoaded', ... ), the const backToTopBtn
declaration, the updateBackToTopVisibility() function body, the
window.addEventListener('scroll', updateBackToTopVisibility, { passive: true }),
and the backToTopBtn.addEventListener('click', ...) handler to match the
existing 8-space indentation used elsewhere.
There was a problem hiding this comment.
♻️ Duplicate comments (1)
web/templates/base.html (1)
1132-1174: 🧹 Nitpick | 🔵 TrivialJavaScript indentation remains inconsistent with the rest of the file.
The comment at line 1132 uses 8-space indentation, but the
document.addEventListenerand its contents (lines 1133-1174) have no indentation. Other script blocks in this file (e.g., lines 25–86, 1086–1130) consistently use 8-space indentation inside<script>tags.🔧 Proposed indentation fix (partial example)
// Back to Top Button functionality -document.addEventListener('DOMContentLoaded', function () { - - const backToTopBtn = document.getElementById('back-to-top-btn'); - if (!backToTopBtn) return; - - // Extracted visibility toggle - function updateBackToTopVisibility() { - if (window.scrollY > 300) { + document.addEventListener('DOMContentLoaded', function () { + const backToTopBtn = document.getElementById('back-to-top-btn'); + if (!backToTopBtn) return; + + // Extracted visibility toggle + function updateBackToTopVisibility() { + if (window.scrollY > 300) {Apply consistent 8-space base indentation throughout the entire block.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/templates/base.html` around lines 1132 - 1174, The JavaScript block starting at document.addEventListener('DOMContentLoaded', ...) has inconsistent indentation; reformat the entire block so every line inside the <script> uses the same 8-space base indentation as other script blocks, including the top-level document.addEventListener line, the const backToTopBtn declaration, the updateBackToTopVisibility function body (and its classList.add/remove calls), the initial call updateBackToTopVisibility(), the window.addEventListener('scroll', updateBackToTopVisibility, { passive: true }), and the backToTopBtn.addEventListener('click', ...) handler; keep all existing identifiers and logic (backToTopBtn, updateBackToTopVisibility, window.addEventListener, window.scrollTo) unchanged—only adjust whitespace to match the file’s 8-space indentation style.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@web/templates/base.html`:
- Around line 1132-1174: The JavaScript block starting at
document.addEventListener('DOMContentLoaded', ...) has inconsistent indentation;
reformat the entire block so every line inside the <script> uses the same
8-space base indentation as other script blocks, including the top-level
document.addEventListener line, the const backToTopBtn declaration, the
updateBackToTopVisibility function body (and its classList.add/remove calls),
the initial call updateBackToTopVisibility(), the
window.addEventListener('scroll', updateBackToTopVisibility, { passive: true }),
and the backToTopBtn.addEventListener('click', ...) handler; keep all existing
identifiers and logic (backToTopBtn, updateBackToTopVisibility,
window.addEventListener, window.scrollTo) unchanged—only adjust whitespace to
match the file’s 8-space indentation style.
|
the homepage is not that long |
Related issues
Fixes #975
Summary
This PR introduces a reusable Back-to-Top button to improve navigation across long pages such as course descriptions, blogs, forums, and virtual labs.
The button appears after scrolling and allows users to quickly return to the top of the page with smooth scrolling behavior.
Changes
base.htmlTesting
Screenshots
Checklist
Summary by CodeRabbit
New Features
Tests