@@ -9,9 +9,33 @@ fetch("web-components/jump-links/jump-links.html")
99 * @returns {void }
1010 */
1111function createComponent ( html ) {
12+
13+ function createLink ( link , shadow ) {
14+ let newListItem = document . createElement ( 'li' ) ;
15+ let newImage = document . createElement ( 'img' ) ;
16+ newImage . src = "wp-content/icons/icon_jump-link.svg" ;
17+ newImage . alt = "jump link" ;
18+ newListItem . appendChild ( newImage ) ;
19+ let newLink = document . createElement ( 'a' ) ;
20+ newLink . textContent = link ;
21+ let linkSrc = "#" + ( link . toLowerCase ( ) . replaceAll ( ' ' , '-' ) ) ;
22+ newLink . href = linkSrc ;
23+ newListItem . appendChild ( newLink ) ;
24+ let list = shadow . querySelector ( '.list' ) ;
25+ list . appendChild ( newListItem ) ;
26+ }
27+
1228 // Web component class
1329 class JumpLinks extends HTMLElement {
1430
31+ /**
32+ * Returns an array of properties to observe.
33+ * @returns {Array } An array of property names.
34+ */
35+ static get observedAttributes ( ) {
36+ return [ 'links' ] ;
37+ }
38+
1539 /**
1640 * Called when an attribute is defined or changed.
1741 * @param {string } property - The name of the attribute.
@@ -26,8 +50,13 @@ function createComponent(html) {
2650 // Invoked when element is added to document
2751 connectedCallback ( ) {
2852 // Create shadow root for element
29- const shadow = this . attachShadow ( { mode : 'closed ' } ) ;
53+ const shadow = this . attachShadow ( { mode : 'open ' } ) ;
3054 shadow . innerHTML = html ;
55+
56+ const linksArr = ( this . links ) . split ( ', ' ) ;
57+ linksArr . forEach ( link => {
58+ createLink ( link , shadow ) ;
59+ } ) ;
3160 }
3261 }
3362
0 commit comments