Skip to content
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

- `<PropertyValuePair />`
- fix description and story to point out that `PropertyValueList` need always to be used as wrapper
- `<ApplicationViewability />`
- `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
Expand Down
4 changes: 3 additions & 1 deletion src/components/Application/ApplicationViewability.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 14 additions & 3 deletions src/components/Application/_viewability.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -30,8 +32,11 @@ const TemplateBasicExample: StoryFn<typeof ApplicationViewability> = (args) => <
export const Default = TemplateBasicExample.bind({});
Default.args = {
children: (
<div>
<HtmlContentBlock>
<LoremIpsum random={false} />
</div>
<p>
<Link href={"https://example.com/"}>Link title</Link>
</p>
</HtmlContentBlock>
),
};
120 changes: 102 additions & 18 deletions src/components/Application/tests/ApplicationViewability.test.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,121 @@
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";

import { ApplicationViewability, ApplicationViewabilityProps, CLASSPREFIX as eccgui } from "../../../index";

import { Default as ApplicationViewabilityStory } from "./../stories/ApplicationViewability.stories";

const applyViewabilityAndCheckClass = (props: Omit<ApplicationViewabilityProps, "children">) => {
/**
* 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<ApplicationViewabilityProps, "children">) => {
const { container } = render(<ApplicationViewability {...ApplicationViewabilityStory.args} {...props} />);
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<ApplicationViewabilityProps, "children">[])(
"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<ApplicationViewabilityProps, "children">[])(
"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");
});
});
});
Loading