diff --git a/CHANGELOG.md b/CHANGELOG.md index e8cc9f91e..c10851fc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - `` - fix description and story to point out that `PropertyValueList` need always to be used as wrapper +- `` + - `show={"print"}` and `hide={"screen"}` content is still accessible by screen readers - BOM issue on compressed stylesheet - first rule `selector` becomes `BOM:selector` that is valid but will never apply - we fixed this problem by adding a dummy rule as first rule diff --git a/src/components/Application/ApplicationViewability.tsx b/src/components/Application/ApplicationViewability.tsx index c9dafcc34..82296076a 100644 --- a/src/components/Application/ApplicationViewability.tsx +++ b/src/components/Application/ApplicationViewability.tsx @@ -35,8 +35,10 @@ export type ApplicationViewabilityProps = ApplicationViewabilityUndecided & (ApplicationViewabilityShow | ApplicationViewabilityHide); /** - * Sets the viewability of the the contained element regarding media. + * Sets the viewability of the contained element regarding media. * Can be used to hide elements, e.g. when the page is printed. + * For accessibility elements are still accessible to screen readers if they are hidden on screen. + * Additionally, elements are displayed on screen if they contain a focused elements. */ export const ApplicationViewability = ({ children, show, hide }: ApplicationViewabilityProps) => { if (!show && !hide) { diff --git a/src/components/Application/_viewability.scss b/src/components/Application/_viewability.scss index d944eba59..cea376c3b 100644 --- a/src/components/Application/_viewability.scss +++ b/src/components/Application/_viewability.scss @@ -6,8 +6,19 @@ } @media screen { - .#{eccgui}-application__hide--screen, - .#{eccgui}-application__show--print { - display: none !important; + .#{eccgui}-application__show--print, + .#{eccgui}-application__hide--screen { + &:not(:has(:focus)) { + position: absolute !important; + right: 110vw !important; + bottom: 110vh !important; + width: 1px; + padding: 0; + margin: 0; + overflow: hidden; + border: none; + opacity: 0; + clip-path: rect(0 0 0 0); + } } } diff --git a/src/components/Application/stories/ApplicationViewability.stories.tsx b/src/components/Application/stories/ApplicationViewability.stories.tsx index 3290ee89d..a57649385 100644 --- a/src/components/Application/stories/ApplicationViewability.stories.tsx +++ b/src/components/Application/stories/ApplicationViewability.stories.tsx @@ -3,6 +3,8 @@ import { LoremIpsum } from "react-lorem-ipsum"; import { Meta, StoryFn } from "@storybook/react"; import { ApplicationViewability } from "../../../index"; +import { Link } from "../../Link/Link"; +import { HtmlContentBlock } from "../../Typography/HtmlContentBlock"; export default { title: "Components/Application/Viewability", component: ApplicationViewability, @@ -30,8 +32,11 @@ const TemplateBasicExample: StoryFn = (args) => < export const Default = TemplateBasicExample.bind({}); Default.args = { children: ( -
+ -
+

+ Link title +

+ ), }; diff --git a/src/components/Application/tests/ApplicationViewability.test.tsx b/src/components/Application/tests/ApplicationViewability.test.tsx index 88f0d1553..aa6098e9f 100644 --- a/src/components/Application/tests/ApplicationViewability.test.tsx +++ b/src/components/Application/tests/ApplicationViewability.test.tsx @@ -1,5 +1,7 @@ import React from "react"; -import { render } from "@testing-library/react"; +import { render, screen } from "@testing-library/react"; +import path from "path"; +import * as sass from "sass"; import "@testing-library/jest-dom"; @@ -7,31 +9,113 @@ import { ApplicationViewability, ApplicationViewabilityProps, CLASSPREFIX as ecc import { Default as ApplicationViewabilityStory } from "./../stories/ApplicationViewability.stories"; -const applyViewabilityAndCheckClass = (props: Omit) => { +/** + * The viewability behaviour is defined by S/CSS rules only, so the styles need to be + * compiled and injected into the test document to be testable. + * `jsdom` evaluates `@media screen` rules when computing styles, `@media print` rules are ignored. + */ +const compiledViewabilityStyles = sass.compile(path.resolve(__dirname, "./../_viewability.scss")).css; + +let viewabilityStyleElement: HTMLStyleElement; + +const renderViewability = (props: Omit) => { const { container } = render(); - const element = container.getElementsByClassName( + const elements = container.getElementsByClassName( props.hide ? `${eccgui}-application__hide--${props.hide}` : `${eccgui}-application__show--${props.show}`, ); - expect(element.length).toBe(1); - return element; + expect(elements.length).toBe(1); + return elements[0] as HTMLElement; }; describe("ApplicationViewability", () => { - it("should be visible on `show=screen`", () => { - applyViewabilityAndCheckClass({ show: "screen" }); - /** - * Currently we cannot really test visibility via jest if it is defined by S/CSS rules because those styles are not known. - * Looks like it is not too easy to include and test them. - * So we only test for the correct CSS class. - */ + beforeAll(() => { + viewabilityStyleElement = document.createElement("style"); + viewabilityStyleElement.textContent = compiledViewabilityStyles; + document.head.appendChild(viewabilityStyleElement); + }); + + describe("sets the correct CSS class", () => { + it("on `show=screen`", () => { + renderViewability({ show: "screen" }); + }); + it("on `hide=screen`", () => { + renderViewability({ hide: "screen" }); + }); + it("on `hide=print`", () => { + renderViewability({ hide: "print" }); + }); + it("on `show=print`", () => { + renderViewability({ show: "print" }); + }); }); - it("should not be visible on `hide=screen`", () => { - applyViewabilityAndCheckClass({ hide: "screen" }); + + describe("is visible on screen", () => { + it("on `show=screen`", () => { + expect(renderViewability({ show: "screen" })).toBeVisible(); + }); + it("on `hide=print`", () => { + expect(renderViewability({ hide: "print" })).toBeVisible(); + }); + }); + + /** + * Elements hidden on screen must not be removed from the rendering, otherwise they + * would be removed from the accessibility tree, too. + */ + describe("is hidden on screen but still accessible", () => { + it.each([{ hide: "screen" }, { show: "print" }] as Omit[])( + "on `%s`", + (props) => { + const element = renderViewability(props); + const styles = getComputedStyle(element); + + // moved out of the viewport instead of being removed from the rendering + expect(styles.display).not.toBe("none"); + expect(styles.visibility).not.toBe("hidden"); + expect(styles.position).toBe("absolute"); + expect(styles.opacity).toBe("0"); + expect(element).not.toBeVisible(); + + // still part of the accessibility tree, `getByRole` ignores inaccessible elements + expect(screen.getByRole("link", { name: "Link title" })).toBeInTheDocument(); + }, + ); }); - it("should be visible on `hide=print`", () => { - applyViewabilityAndCheckClass({ hide: "print" }); + + /** + * Computed styles are cached per element by `jsdom`, so the focus needs to be set + * on a freshly rendered element before the styles are read. + */ + describe("is displayed on screen if it contains a focused element", () => { + it.each([{ hide: "screen" }, { show: "print" }] as Omit[])( + "on `%s`", + (props) => { + const element = renderViewability(props); + expect(element.matches(":not(:has(:focus))")).toBe(true); + + element.querySelector("a")!.focus(); + + expect(element.matches(":not(:has(:focus))")).toBe(false); + expect(getComputedStyle(element).position).not.toBe("absolute"); + expect(element).toBeVisible(); + }, + ); }); - it("should not be visible on `show=print`", () => { - applyViewabilityAndCheckClass({ show: "print" }); + + /** + * `@media print` rules are not applied by `jsdom`, so only the rule itself can be checked. + */ + describe("is removed from the rendering on print", () => { + it("on `hide=print` and `show=screen`", () => { + const printRules = Array.from(viewabilityStyleElement.sheet!.cssRules) + .filter((rule): rule is CSSMediaRule => (rule as CSSMediaRule).conditionText === "print") + .flatMap((rule) => Array.from(rule.cssRules) as CSSStyleRule[]); + + expect(printRules.length).toBe(1); + expect(printRules[0].selectorText.replace(/\s+/g, " ")).toBe( + `.${eccgui}-application__hide--print, .${eccgui}-application__show--screen`, + ); + expect(printRules[0].style.display).toBe("none"); + }); }); });