Skip to content

Only make scrollable code blocks into tab stops #1777

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

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,29 @@ function setupMobileSidebarKeyboardHandlers() {
});
}

/**
* When the page loads or the window resizes check all elements with
* [data-tabindex="0"], and if they have scrollable overflow, set tabIndex = 0.
*/
function setupLiteralBlockTabStops() {
const updateTabStops = () => {
document.querySelectorAll('[data-tabindex="0"]').forEach((el) => {
el.tabIndex = el.scrollWidth > el.clientWidth ? 0 : -1;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right now, I'm only checking for scrollable content in the x, or horizontal, direction because that's all I've ever seen. But do we know if we ever have code blocks that scroll in the y direction?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we do, or I don't see any reason to have some. The only thing I ce see that would be vertically scrollable would be inclusions like widgets/iframe from things like jupyter-lite-sphinx.

Copy link
Collaborator Author

@gabalafou gabalafou Apr 24, 2024

Choose a reason for hiding this comment

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

I decided to extend this line to check for y-overflow too. My reasoning is that the theme should work robustly—it would be fairly easy for some documentation site to create a class like .height-10em that they apply to code blocks.

});
};
window.addEventListener("resize", debounce(updateTabStops, 300));
updateTabStops();
}
function debounce(callback, wait) {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
callback(...args);
}, wait);
};
}
Comment on lines +711 to +719
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is so general-purpose I'm surprised there isn't a JS built-in for doing it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Heh, I had the same thought!


/*******************************************************************************
* Call functions after document loading.
*/
Expand All @@ -703,3 +726,4 @@ documentReady(setupSearchButtons);
documentReady(initRTDObserver);
documentReady(setupMobileSidebarKeyboardHandlers);
documentReady(fixMoreLinksInMobileSidebar);
documentReady(setupLiteralBlockTabStops);
4 changes: 2 additions & 2 deletions src/pydata_sphinx_theme/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
kwargs["ARIA-LEVEL"] = "2"

if "pre" in args:
kwargs["tabindex"] = "0"
kwargs["data-tabindex"] = "0"

Check warning on line 36 in src/pydata_sphinx_theme/translator.py

View check run for this annotation

Codecov / codecov/patch

src/pydata_sphinx_theme/translator.py#L36

Added line #L36 was not covered by tests

return super().starttag(*args, **kwargs)

Expand All @@ -50,7 +50,7 @@
# executed successfully and appended to self.body a string of HTML
# representing the code block, which we then modify.
html_string = self.body[-1]
self.body[-1] = html_string.replace("<pre", '<pre tabindex="0"')
self.body[-1] = html_string.replace("<pre", '<pre data-tabindex="0"')
raise nodes.SkipNode

def visit_table(self, node):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_a11y.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,25 @@ def test_version_switcher_highlighting(page: Page, url_base: str) -> None:
light_mode = "rgb(10, 125, 145)" # pst-color-primary
# dark_mode = "rgb(63, 177, 197)"
expect(entry).to_have_css("color", light_mode)


def test_code_block_tab_stop(page: Page, url_base: str) -> None:
"""Code blocks that have scrollable content should be tab stops."""
page.set_viewport_size({"width": 1440, "height": 720})
page.goto(urljoin(url_base, "/examples/kitchen-sink/blocks.html"))
code_block = page.locator(
'css=#code-block pre[data-tabindex="0"]', has_text="from typing import Iterator"
)

# Viewport is wide, so code block content fits, no overflow, no tab stop
assert code_block.evaluate("el => el.scrollWidth > el.clientWidth") is False
assert code_block.evaluate("el => el.tabIndex") != 0

page.set_viewport_size({"width": 400, "height": 720})

# Resize handler is debounced with 300 ms wait time
page.wait_for_timeout(301)

# Narrow viewport, content overflows and code block should be a tab stop
assert code_block.evaluate("el => el.scrollWidth > el.clientWidth") is True
assert code_block.evaluate("el => el.tabIndex") == 0
Loading