Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: scrolltop button #2012

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sphinxawesome-theme"
version = "5.3.0"
version = "5.3.1"
description = "An awesome theme for the Sphinx documentation generator"
readme = "README.md"
authors = ["Kai Welke <[email protected]>"]
Expand Down
7 changes: 6 additions & 1 deletion src/sphinxawesome_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@

{%- block extrahead %}{%- endblock extrahead %}
</head>
<body x-data="{ showSidebar: false }" class="min-h-screen font-sans antialiased bg-background text-foreground" :class="{ 'overflow-hidden': showSidebar }">
<body x-data="{ showSidebar: false, showScrollTop: false }" class="min-h-screen font-sans antialiased bg-background text-foreground" :class="{ 'overflow-hidden': showSidebar }"
{%- if theme_show_scrolltop|tobool -%}@scroll.window="showScrollTop = pageYOffset > 100"{%- endif -%}
>

{#- A blurry background screen for the mobile sidebar -#}
{%- if sidebars|length > 0 %}
Expand Down Expand Up @@ -131,6 +133,9 @@
{%- include "footer.html" %}
{%- endblock footer %}
</div>
{%- if theme_show_scrolltop|tobool %}
{%- include "scrolltop.html" %}
{%- endif %}
{% block scripts %}
{%- for js in script_files %}
{{ js_tag(js) }}
Expand Down
9 changes: 6 additions & 3 deletions src/sphinxawesome_theme/scrolltop.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{#-
A `scroll to top` button. Is shown at the bottom right if `show_scrolltop` is true.
-#}
<button data-scroll-to-top-target="scrollToTop"
data-action="scroll-to-top#scroll"
class="fixed bottom-8 right-8 p-2 z-10 rounded-sm bg-gray-700 text-xs text-white invisible opacity-0 transition-all duration-1000 hover:bg-gray-950 focus:bg-gray-950">
<button id="scrolltop"
x-cloak
@click="window.scrollTo({top: 0, behavior: 'smooth'})"
class="fixed bottom-8 right-8 p-2 z-10 rounded-sm bg-gray-700 text-xs text-white transition-all duration-1000 hover:bg-gray-950 focus:bg-gray-950"
:class="{'opacity-0 invisible' : !showScrollTop}"
>
<svg xmlns="http://www.w3.org/2000/svg"
height="14"
viewBox="0 96 960 960"
Expand Down
2 changes: 1 addition & 1 deletion src/theme-src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sphinxawesome-theme",
"version": "5.3.0",
"version": "5.3.1",
"scripts": {
"build": "NODE_ENV=production webpack",
"dev": "NODE_ENV=development webpack --watch --progress",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def test_returns_version() -> None:
"""It has the correct version."""
assert sphinxawesome_theme.__version__ == "5.3.0"
assert sphinxawesome_theme.__version__ == "5.3.1"


@pytest.mark.sphinx("dummy")
Expand Down
40 changes: 40 additions & 0 deletions tests/test_scrolltop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test the presence or absense of the scroll to top button."""

import os
from pathlib import Path

import pytest
from sphinx.application import Sphinx

from .util import parse_html


@pytest.mark.sphinx(
"html",
confoverrides={
"html_theme": "sphinxawesome_theme",
},
)
def test_no_scroll_top_button(app: Sphinx) -> None:
"""It doesn't have a scroll to top button by default."""
app.build()
assert os.path.exists(Path(app.outdir) / "index.html")
tree = parse_html(Path(app.outdir) / "index.html")
button = tree.select("button#scrolltop")
assert len(button) == 0


@pytest.mark.sphinx(
"html",
confoverrides={
"html_theme": "sphinxawesome_theme",
"html_theme_options": {"show_scrolltop": True},
},
)
def test_scroll_top_button(app: Sphinx) -> None:
"""It shows a scroll to top button."""
app.build()
assert os.path.exists(Path(app.outdir) / "index.html")
tree = parse_html(Path(app.outdir) / "index.html")
button = tree.select("button#scrolltop")
assert len(button) == 1