Skip to content

A platform for community discussion. Free, open, simple.

A11Y: ensure skip links are hidden for locales with longer words (#37066)

Kris203cf5b8c1f6
diff --git a/app/assets/stylesheets/common/base/discourse.scss b/app/assets/stylesheets/common/base/discourse.scss
index 433385dd423..fefdadf61f5 100644
--- a/app/assets/stylesheets/common/base/discourse.scss
+++ b/app/assets/stylesheets/common/base/discourse.scss
@@ -1093,20 +1093,13 @@ form {
}
}
-#skip-links__container {
- position: fixed;
- top: -50px;
- left: var(--space-4);
- z-index: z("header") + 1;
-}
-
#skip-links__container a,
a#user-nav-skip-link,
a#skip-link,
.skip-link {
padding: var(--space-1) var(--space-2);
position: fixed;
- top: -50px;
+ top: -75px;
left: var(--space-4);
color: var(--secondary);
background: var(--tertiary);
diff --git a/frontend/discourse/tests/integration/components/a11y/skip-links-test.gjs b/frontend/discourse/tests/integration/components/a11y/skip-links-test.gjs
new file mode 100644
index 00000000000..a183b44e6e7
--- /dev/null
+++ b/frontend/discourse/tests/integration/components/a11y/skip-links-test.gjs
@@ -0,0 +1,87 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { i18n } from "discourse-i18n";
+
+module("Integration | Component | a11y/skip-links", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("skip link height does not exceed -75px offset on narrow 320px viewports", async function (assert) {
+ const testContainer = document.querySelector("#ember-testing");
+ const originalWidth = testContainer?.style.width;
+
+ if (testContainer) {
+ testContainer.style.width = "320px";
+ }
+
+ try {
+ // Russian has the longest translation for this skip link
+ const skipLinkText = i18n("skip_to_where_you_left_off_last", {
+ post_number: 999,
+ locale: "ru",
+ });
+
+ await render(
+ <template>
+ <a
+ href="#main-outlet"
+ id="skip-link"
+ class="skip-link"
+ >{{skipLinkText}}</a>
+ </template>
+ );
+
+ const skipLink = document.querySelector("#skip-link");
+ const computedStyle = window.getComputedStyle(skipLink);
+
+ assert.strictEqual(
+ computedStyle.top,
+ "-75px",
+ "skip link has -75px offset on narrow viewport"
+ );
+
+ const rect = skipLink.getBoundingClientRect();
+
+ assert.true(
+ rect.height <= 75,
+ `skip link height (${rect.height}px) must be <= 75px to work with -75px offset`
+ );
+ } finally {
+ if (testContainer) {
+ testContainer.style.width = originalWidth || "";
+ }
+ }
+ });
+
+ test("skip links get updated CSS when focused", async function (assert) {
+ const skipLinkText = i18n("skip_to_main_content");
+
+ await render(
+ <template>
+ <a
+ href="#main-outlet"
+ id="skip-link"
+ class="skip-link"
+ >{{skipLinkText}}</a>
+ </template>
+ );
+
+ const skipLink = document.querySelector("#skip-link");
+
+ let computedStyle = window.getComputedStyle(skipLink);
+ assert.strictEqual(
+ computedStyle.top,
+ "-75px",
+ "skip link is hidden with -75px offset before focus"
+ );
+
+ skipLink.focus();
+
+ computedStyle = window.getComputedStyle(skipLink);
+ assert.strictEqual(
+ computedStyle.top,
+ "0px",
+ "skip link gets top: 0 when focused"
+ );
+ });
+});