Skip to content

Commit 0cd442e

Browse files
committed
add optional aria-label to hero links and comments to hero-section js
1 parent 9be6913 commit 0cd442e

1 file changed

Lines changed: 44 additions & 8 deletions

File tree

web-components/hero-section/hero-section.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ fetch("web-components/hero-section/hero-section.html")
33
.then(stream => stream.text())
44
.then(text => createComponent(text))
55

6-
// Create web component
6+
/**
7+
* Creates a web component using a given HTML template.
8+
* @param {string} html - The HTML template.
9+
* @returns {void}
10+
*/
711
function createComponent(html) {
812

9-
// Set text content
13+
/**
14+
* Sets the content of an element based on a CSS selector.
15+
* @param {string} cssSelector - The CSS selector of the element to set the content for.
16+
* @param {string} content - The content to set for the element.
17+
* @param {boolean} shadow - A boolean indicating whether to use the shadow DOM for the element.
18+
* @returns {void}
19+
*/
1020
function setContent(cssSelector, content, shadow) {
1121
const selector = shadow.querySelector(cssSelector);
1222
// Check if span tag is used in desc
@@ -19,17 +29,35 @@ function createComponent(html) {
1929
}
2030
}
2131

22-
// Set link
23-
function setLink(cssSelector, url, shadow) {
32+
/**
33+
* Sets the link of an element based on a CSS selector
34+
* @param {string} cssSelector - The CSS selector of the element to set the link for
35+
* @param {string} url - The link url
36+
* @param {boolean} shadow - A boolean indicating whether to use the shadow DOM for the element
37+
* @param {string} name - The name of the element containing the link
38+
* @returns {void}
39+
*/
40+
function setLink(cssSelector, url, shadow, name) {
41+
name = name || "";
2442
const link = shadow.querySelector(cssSelector);
2543
link.href = url;
2644
// Open new tab for external links
2745
if (url.includes('https://')) {
2846
link.setAttribute('target', '_blank');
2947
}
48+
// Set aria-label if needed
49+
if (name != "") {
50+
link.setAttribute('aria-label', `See details on ${name}`);
51+
}
3052
}
3153

32-
// Set image
54+
/**
55+
* Sets the image of an element based on a CSS selector.
56+
* @param {string} cssSelector - The CSS selector of the element to set the content for.
57+
* @param {string} src - The image to set for the element.
58+
* @param {boolean} shadow - A boolean indicating whether to use the shadow DOM for the element.
59+
* @returns {void}
60+
*/
3361
function setImage(cssSelector, src, shadow) {
3462
const image = shadow.querySelector(cssSelector);
3563
image.src = src;
@@ -44,12 +72,20 @@ function createComponent(html) {
4472
super();
4573
}
4674

47-
// Return array of properties to observe
75+
/**
76+
* Returns an array of properties to observe.
77+
* @returns {Array} An array of property names.
78+
*/
4879
static get observedAttributes() {
4980
return ['name', 'image', 'section', 'desc', 'linktext', 'link', 'linktext2', 'link2', 'highlight', 'highlightlink'];
5081
}
5182

52-
// Called when an attribute is defined or changed
83+
/**
84+
* Called when an attribute is defined or changed.
85+
* @param {string} property - The name of the attribute.
86+
* @param {string} oldValue - The old value of the attribute.
87+
* @param {string} newValue - The new value of the attribute.
88+
*/
5389
attributeChangedCallback(property, oldValue, newValue) {
5490
if (oldValue === newValue) return;
5591
this[property] = newValue;
@@ -79,7 +115,7 @@ function createComponent(html) {
79115
// Display highlight card if content is provided
80116
if (this.highlight != undefined) {
81117
setContent('.highlight', this.highlight, shadow);
82-
setLink('.highlight-link', this.highlightlink, shadow);
118+
setLink('.highlight-link', this.highlightlink, shadow, this.highlight);
83119
hero.classList.add('hero-highlight');
84120
} else {
85121
const highlightCard = shadow.querySelector('.highlight-card');

0 commit comments

Comments
 (0)