Skip to content

Commit

Permalink
enhancement/misc DSD refactoring (#17)
Browse files Browse the repository at this point in the history
* refactor this reference in Card component to use getRootNode

* use DOMParser for injecting HTML with shadow roots from fragments API
  • Loading branch information
thescientist13 authored Jan 6, 2024
1 parent 7a0fe58 commit 615d223
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class Card extends HTMLElement {
<div>
<h3>${title}</h3>
<img src="${thumbnail}" alt="${title}" loading="lazy" width="100%">
<button onclick="this.parentNode.parentNode.host.selectItem()">View Item Details</button>
<button onclick="this.getRootNode().host.selectItem()">View Item Details</button>
</div>
`;
this.attachShadow({ mode: 'open' });
Expand Down
5 changes: 4 additions & 1 deletion src/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
globalThis.document.getElementById('load-products').addEventListener('click', async () => {
offset = offset += page;
const html = await fetch(`/api/fragment?offset=${offset}`).then(resp => resp.text());
const fragment = new DOMParser().parseFromString(html, 'text/html', {
includeShadowRoots: true
});

document.getElementById('load-products-output').insertAdjacentHTML('beforeend', html);
document.getElementById('load-products-output').insertAdjacentHTML('beforeend', fragment.body.innerHTML);
});
});
</script>
Expand Down
5 changes: 4 additions & 1 deletion src/pages/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
'content-type': 'application/x-www-form-urlencoded'
})
}).then(resp => resp.text());
const fragment = new DOMParser().parseFromString(html, 'text/html', {
includeShadowRoots: true
});

document.getElementById('search-products-output').innerHTML = html;
document.getElementById('search-products-output').innerHTML = fragment.body.innerHTML;
});
});
</script>
Expand Down

0 comments on commit 615d223

Please sign in to comment.