Skip to content

Commit

Permalink
feat(visually-hidden): implement a utility component to visually hide…
Browse files Browse the repository at this point in the history
… content

fix(pagination): add a visually hidden label for the page input field

refactor(breadcrumb): use visually hidden element
  • Loading branch information
daenub authored Mar 28, 2024
1 parent cd84b4e commit 98143c6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/components/breadcrumb/Breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Icon } from "../icon/icon.js"
import "../menu/leu-menu.js"
import "../menu/leu-menu-item.js"
import "../popup/leu-popup.js"
import "../visually-hidden/leu-visually-hidden.js"
import { debounce } from "../../lib/utils.js"

/**
Expand Down Expand Up @@ -276,7 +277,7 @@ export class LeuBreadcrumb extends LitElement {

return html`
<nav class=${classMap(wrapperClasses)}>
<h2 class="visuallyhidden">Sie sind hier:</h2>
<leu-visually-hidden><h2>Sie sind hier:</h2></leu-visually-hidden>
<ol class="breadcrumbs__list" ref=${ref(this._containerRef)}>
${showBackOnly
? html` <li class="breadcrumbs__item breadcrumbs__item--back">
Expand Down
11 changes: 0 additions & 11 deletions src/components/breadcrumb/breadcrumb.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@
box-shadow: var(--leu-box-shadow-short);
}

.visuallyhidden {
clip: rect(0 0 0 0);
border: 0;
height: 1px;
margin: -1px !important;
overflow: hidden;
padding: 0 !important;
position: absolute;
width: 1px;
}

.breadcrumbs {
font-size: 1rem;
}
Expand Down
6 changes: 6 additions & 0 deletions src/components/pagination/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { live } from "lit/directives/live.js"
import "../button/leu-button.js"
import styles from "./pagination.css"

import "../visually-hidden/leu-visually-hidden.js"

const MIN_PAGE = 1

/**
Expand Down Expand Up @@ -133,7 +135,11 @@ export class LeuPagination extends LitElement {

render() {
return html`
<leu-visually-hidden>
<label for="page-input">Aktuelle Seite</label>
</leu-visually-hidden>
<input
id="page-input"
class="input"
min=${MIN_PAGE}
max=${this._maxPage}
Expand Down
13 changes: 13 additions & 0 deletions src/components/visually-hidden/VisuallyHidden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { html, LitElement } from "lit"
import styles from "./visually-hidden.css"

/**
* @tagname leu-visually-hidden
*/
export class LeuVisuallyHidden extends LitElement {
static styles = styles

render() {
return html`<slot></slot>`
}
}
6 changes: 6 additions & 0 deletions src/components/visually-hidden/leu-visually-hidden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineElement } from "../../lib/defineElement.js"
import { LeuVisuallyHidden } from "./VisuallyHidden.js"

export { LeuVisuallyHidden }

defineElement("visually-hidden", LeuVisuallyHidden)
22 changes: 22 additions & 0 deletions src/components/visually-hidden/stories/visually-hidden.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { html } from "lit"
import "../leu-visually-hidden.js"

export default {
title: "VisuallyHidden",
component: "leu-visually-hidden",
argTypes: {
content: {
control: "text",
},
},
}

function Template({ content }) {
return html` <leu-visually-hidden>${content}</leu-visually-hidden>`
}

export const Regular = Template.bind({})
Regular.args = {
content:
"This is a text that isn't visible but still accessible for screenreaders.",
}
36 changes: 36 additions & 0 deletions src/components/visually-hidden/test/visually-hidden.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { html } from "lit"
import { fixture, expect } from "@open-wc/testing"

import "../leu-visually-hidden.js"

async function defaultFixture() {
return fixture(
html`
<leu-visually-hidden>
This is a text that shouldn't be visible but still accessible.
</leu-visually-hidden>
`
)
}

describe("LeuVisuallyHidden", () => {
it("is a defined element", async () => {
const el = await customElements.get("leu-visually-hidden")

await expect(el).not.to.be.undefined
})

it("passes the a11y audit", async () => {
const el = await defaultFixture()

await expect(el).to.be.accessible()
})

it("renders the default slot content", async () => {
const el = await defaultFixture()

expect(el).dom.to.equal(
"<leu-visually-hidden>This is a text that shouldn't be visible but still accessible.</leu-visually-hidden>"
)
})
})
10 changes: 10 additions & 0 deletions src/components/visually-hidden/visually-hidden.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
:host {
clip: rect(0 0 0 0);
border: 0;
height: 1px;
margin: -1px !important;
overflow: hidden;
padding: 0 !important;
position: absolute;
width: 1px;
}

0 comments on commit 98143c6

Please sign in to comment.