Skip to content

Commit 501740e

Browse files
authored
feat(ui5-avatar): implement accessibility spec (#1484)
1 parent ede3635 commit 501740e

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/main/src/Avatar.hbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="ui5-avatar-root">
22
{{#if image}}
3-
<span alt="avatar" class="ui5-avatar-img" style="background-image: url({{image}})"></span>
3+
<span class="ui5-avatar-img" style="background-image: url({{image}})" role="img" aria-label="{{accessibleNameText}}"></span>
44
{{else if icon}}
5-
<ui5-icon class="ui5-avatar-icon" name="{{icon}}"></ui5-icon>
5+
<ui5-icon class="ui5-avatar-icon" name="{{icon}}" accessible-name="{{accessibleNameText}}"></ui5-icon>
66
{{else if initials}}
77
<span class="ui5-avatar-initials">{{validInitials}}</span>
88
{{/if}}

packages/main/src/Avatar.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
3+
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
34

45
// Template
56
import AvatarTemplate from "./generated/templates/AvatarTemplate.lit.js";
67

8+
import { AVATAR_TOOLTIP } from "./generated/i18n/i18n-defaults.js";
9+
710
// Styles
811
import AvatarCss from "./generated/themes/Avatar.css.js";
912

@@ -142,6 +145,19 @@ const metadata = {
142145
type: String,
143146
defaultValue: AvatarBackgroundColor.Accent6,
144147
},
148+
149+
/**
150+
* Defines the text alternative of the <code>ui5-avatar</code>.
151+
* If not provided a default text alternative will be set, if present.
152+
*
153+
* @type {string}
154+
* @defaultvalue ""
155+
* @public
156+
* @since 1.0.0-rc.7
157+
*/
158+
accessibleName: {
159+
type: String,
160+
},
145161
},
146162
slots: /** @lends sap.ui.webcomponents.main.Avatar.prototype */ {
147163
},
@@ -173,6 +189,11 @@ const metadata = {
173189
* @public
174190
*/
175191
class Avatar extends UI5Element {
192+
constructor() {
193+
super();
194+
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
195+
}
196+
176197
static get metadata() {
177198
return metadata;
178199
}
@@ -190,7 +211,10 @@ class Avatar extends UI5Element {
190211
}
191212

192213
static async onDefine() {
193-
await Icon.define();
214+
await Promise.all([
215+
fetchI18nBundle("@ui5/webcomponents"),
216+
Icon.define(),
217+
]);
194218
}
195219

196220
get validInitials() {
@@ -202,6 +226,14 @@ class Avatar extends UI5Element {
202226

203227
return null;
204228
}
229+
230+
get accessibleNameText() {
231+
if (this.accessibleName) {
232+
return this.accessibleName;
233+
}
234+
235+
return this.i18nBundle.getText(AVATAR_TOOLTIP) || undefined;
236+
}
205237
}
206238

207239
Avatar.define();

0 commit comments

Comments
 (0)