@@ -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+ */
711function 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 ( ) {
49- return [ 'name' , 'image' , 'section' , 'desc' , 'linktext' , 'link' , 'linktext2' , 'link2' ] ;
80+ 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 ;
@@ -61,6 +97,9 @@ function createComponent(html) {
6197 const shadow = this . attachShadow ( { mode : 'open' } ) ;
6298 shadow . innerHTML = html ;
6399
100+ const heroSection = shadow . querySelector ( '.hero-section' ) ;
101+ const hero = shadow . querySelector ( '.hero' ) ;
102+
64103 // Set content
65104 setImage ( '.image' , this . image , shadow ) ;
66105 setContent ( '.section' , this . section , shadow ) ;
@@ -73,8 +112,15 @@ function createComponent(html) {
73112 img . classList . add ( 'large-icon' ) ;
74113 }
75114
76- const heroSection = shadow . querySelector ( '.hero-section' ) ;
77- const hero = shadow . querySelector ( '.hero' ) ;
115+ // Display highlight card if content is provided
116+ if ( this . highlight != undefined ) {
117+ setContent ( '.highlight' , this . highlight , shadow ) ;
118+ setLink ( '.highlight-link' , this . highlightlink , shadow , this . highlight ) ;
119+ hero . classList . add ( 'hero-highlight' ) ;
120+ } else {
121+ const highlightCard = shadow . querySelector ( '.highlight-card' ) ;
122+ highlightCard . style . display = 'none' ;
123+ }
78124
79125 // Check if primary button is used
80126 if ( this . link != undefined ) {
0 commit comments