Skip to content

feat(ui): add reusable back-to-top button for long pages, fixed #975 - #976

Closed
Flashl3opard wants to merge 5 commits into
alphaonelabs:mainfrom
Flashl3opard:Add/Back-to-Top-btn
Closed

Flashl3opard wants to merge 5 commits into
alphaonelabs:mainfrom
Flashl3opard:Add/Back-to-Top-btn

Conversation

@Flashl3opard

@Flashl3opard Flashl3opard commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Added Back-to-Top button to base.html
  • Implemented scroll detection logic
  • Added smooth scrolling behavior
  • Applied responsive Tailwind styling
  • Ensured visibility above overlays using proper z-index

Testing

  • Verified button renders across pages inheriting base template
  • Tested scrolling behavior on desktop and mobile views
  • Confirmed no layout shifts or JS console errors

Screenshots

image

Checklist

  • Did you run the pre-commit? (If not, your PR will most likely not pass — please ensure it passes pre-commit)
  • Did you test the change? (Ensure you didn’t just prompt the AI and blindly commit — test the code and confirm it works)
  • Added screenshots to the PR description (if applicable)

Summary by CodeRabbit

  • New Features

    • Added a "Back to Top" button that appears after scrolling and lets users smoothly return to the top with one click; positioned in the bottom-right for easy access.
  • Tests

    • Added automated tests that verify the back-to-top button is present and accessible on the homepage.

@github-actions github-actions Bot added the files-changed: 2 PR changes 2 files label Feb 27, 2026
@coderabbitai

coderabbitai Bot commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds 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

Cohort / File(s) Summary
Template & Frontend
web/templates/base.html
Adds a fixed bottom-right button with id="back-to-top-btn", Tailwind utility classes for styling/visibility, and a DOMContentLoaded script that toggles visibility after 300px scroll and smooth-scrolls to top on click.
Tests
tests/test_back_to_top.py
Adds BackToTopButtonTests (TestCase) with setUp and test_button_present_on_homepage that GETs the index URL, asserts 200, and checks the response contains the button element.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The PR addresses issue #975 requirements: button renders on base.html (all pages), smooth scrolling implemented, Tailwind styling applied, scroll event listener controls visibility, but aria-label accessibility attribute is not evident in the provided code summary. Verify that the back-to-top button includes the aria-label attribute for accessibility as specified in issue #975 requirements.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding a reusable back-to-top button for long pages, which directly aligns with the primary modifications in base.html and the test file.
Out of Scope Changes check ✅ Passed All changes are directly related to issue #975 objectives: back-to-top button in base.html, scroll detection, smooth scrolling, Tailwind styling, and unit test for button presence, with no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

ℹ️ Review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 733e879 and 557f32e.

📒 Files selected for processing (2)
  • tests/test_back_to_top.py
  • web/templates/base.html

Comment thread web/templates/base.html Resolved
Comment thread web/templates/base.html Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ℹ️ Review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 557f32e and f300a83.

📒 Files selected for processing (1)
  • web/templates/base.html

Comment thread web/templates/base.html
Comment on lines +1131 to +1174

// 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'
});
});

});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 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.

Comment thread web/templates/base.html Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
web/templates/base.html (1)

1132-1174: 🧹 Nitpick | 🔵 Trivial

JavaScript indentation remains inconsistent with the rest of the file.

The comment at line 1132 uses 8-space indentation, but the document.addEventListener and 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.

ℹ️ Review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f300a83 and 3fb3474.

📒 Files selected for processing (1)
  • web/templates/base.html

@A1L13N

A1L13N commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

the homepage is not that long

@A1L13N A1L13N closed this Mar 1, 2026
@Flashl3opard
Flashl3opard deleted the Add/Back-to-Top-btn branch March 4, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

files-changed: 2 PR changes 2 files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Add Back-to-Top button for improved navigation on long pages

2 participants