Skip to content

Commit 7e65e77

Browse files
authored
feat(ui5-link): add aria-label and aria-labelledby support (#2357)
FIXES: #2356
1 parent a68dfbb commit 7e65e77

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

packages/main/src/Link.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
tabindex="{{tabIndex}}"
88
?disabled="{{disabled}}"
99
aria-disabled="{{ariaDisabled}}"
10+
aria-label="{{ariaLabelText}}"
1011
@focusin={{_onfocusin}}
1112
@click={{_onclick}}
1213
@keydown={{_onkeydown}}

packages/main/src/Link.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
3+
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AriaLabelHelper.js";
34
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
45
import LinkDesign from "./types/LinkDesign.js";
56

7+
68
// Template
79
import LinkRederer from "./generated/templates/LinkTemplate.lit.js";
810

@@ -91,6 +93,31 @@ const metadata = {
9193
type: Boolean,
9294
},
9395

96+
/**
97+
* Defines the aria-label attribute for the link.
98+
*
99+
* @type {String}
100+
* @since 1.0.0-rc.10
101+
* @private
102+
* @defaultvalue ""
103+
*/
104+
ariaLabel: {
105+
type: String,
106+
},
107+
108+
/**
109+
* Receives id(or many ids) of the elements that label the input
110+
*
111+
* @type {String}
112+
* @defaultvalue ""
113+
* @private
114+
* @since 1.0.0-rc.10
115+
*/
116+
ariaLabelledby: {
117+
type: String,
118+
defaultValue: "",
119+
},
120+
94121
_rel: {
95122
type: String,
96123
noAttribute: true,
@@ -211,6 +238,10 @@ class Link extends UI5Element {
211238
return this.disabled ? "true" : undefined;
212239
}
213240

241+
get ariaLabelText() {
242+
return getEffectiveAriaLabelText(this);
243+
}
244+
214245
get hasLinkType() {
215246
return this.design !== LinkDesign.Default;
216247
}

packages/main/test/pages/Link.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ <h2>Link designs</h2>
7979
<ui5-input id="helper-input" value="0"></ui5-input>
8080
</section>
8181

82+
<section class="group">
83+
<label id="lbl">Text from aria-labelledby</label>
84+
<ui5-link id="ariaLbl" aria-label="Text from aria-label">Go to</ui5-link>
85+
<ui5-link id="ariaLblBy" aria-labelledby="lbl">Go to</ui5-link>
86+
</section>
87+
8288
<script>
8389
var disLink = document.querySelector("#disabled-link");
8490
var link = document.querySelector("#link");

packages/main/test/specs/Link.spec.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("General API", () => {
66
it("render initially", () => {
77
const linkRoot = browser.$("ui5-link").shadow$("ui5-link-root");
88

9-
assert.ok(linkRoot, "Link is rendered");
9+
assert.ok(linkRoot, "Link is rendered.");
1010
});
1111

1212
it("tests href attributes", () => {
@@ -16,25 +16,25 @@ describe("General API", () => {
1616
assert.notOk(link.getAttribute("href"), "Render without 'href' by default");
1717

1818
link.setAttribute("href", HREF_ATTRIBUTE);
19-
assert.strictEqual(link.getAttribute("href"), HREF_ATTRIBUTE, "href attribute is changed");
19+
assert.strictEqual(link.getAttribute("href"), HREF_ATTRIBUTE, "The href attribute is changed.");
2020
});
2121

2222
it("tests target attributes", () => {
2323
const link = browser.$("#empty-link-2");
2424
const TARGET_ATTRIBUTE = "_blank";
2525

26-
assert.notOk(link.getAttribute("target"), "Render without 'target' by default");
26+
assert.notOk(link.getAttribute("target"), "Render without 'target' by default.");
2727

2828
link.setAttribute("target", TARGET_ATTRIBUTE);
29-
assert.strictEqual(link.getAttribute("target"), TARGET_ATTRIBUTE, "target attribute is changed");
29+
assert.strictEqual(link.getAttribute("target"), TARGET_ATTRIBUTE, "The target attribute is changed.");
3030
});
3131

3232
it("should wrap the text of the link", () => {
3333
const wrappingLabel = browser.$("#wrapping-link");
3434
const truncatingLabel = browser.$("#non-wrapping-link");
3535

3636
assert.ok(wrappingLabel.getSize().height > truncatingLabel.getSize().height);
37-
assert.strictEqual(truncatingLabel.getSize().height, 16, "truncated label should be single line");
37+
assert.strictEqual(truncatingLabel.getSize().height, 16, "The truncated label should be single line.");
3838
});
3939

4040
it("should prevent clicking on disabled link", () => {
@@ -45,14 +45,14 @@ describe("General API", () => {
4545
disLink.click();
4646
});
4747

48-
assert.strictEqual(input.getValue(), "0", "Click should not be fired and value of input should not be changed");
48+
assert.strictEqual(input.getValue(), "0", "Click should not be fired and value of input should not be changed.");
4949

5050
});
5151

5252
it("disabled link should not be enabled", () => {
5353
const link = browser.$("#disabled-link").shadow$("a").getAttribute("disabled");
5454

55-
assert.ok(link, "Disabled link should not be enabled");
55+
assert.ok(link, "Disabled link should not be enabled.");
5656
});
5757

5858
it("tests prevent default", () => {
@@ -61,4 +61,17 @@ describe("General API", () => {
6161
link.click();
6262
assert.ok(browser.getUrl().indexOf("https://www.google.com") === -1);
6363
});
64+
65+
it("tests acc attributes", () => {
66+
const link1 = browser.$("#ariaLbl").shadow$("a");
67+
const link2 = browser.$("#ariaLblBy").shadow$("a");
68+
69+
const ARIA_LABEL_1 = "Text from aria-label";
70+
const ARIA_LABEL_2 = "Text from aria-labelledby";
71+
72+
assert.strictEqual(link1.getAttribute("aria-label"),
73+
ARIA_LABEL_1, "The aria-label attribute is correct.");
74+
assert.strictEqual(link2.getAttribute("aria-label"),
75+
ARIA_LABEL_2, "The aria-label attribute is correct.");
76+
});
6477
});

0 commit comments

Comments
 (0)