Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Web Components #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
583 changes: 462 additions & 121 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"story:build": "storybook build",
"story:serve": "npm run clean && npm run story:build && http-server ./storybook-static",
"test": "wtr",
"test:tdd": "npm run test -- --watch"
"test:tdd": "npm run test -- --watch",
"postinstall": "patch-package"
},
"devDependencies": {
"@chromatic-com/storybook": "^1.3.1",
Expand All @@ -39,6 +40,7 @@
"@web/test-runner-junit-reporter": "^0.7.1",
"http-server": "^14.1.1",
"lit": "^3.1.2",
"patch-package": "^8.0.0",
"rimraf": "^5.0.5",
"storybook": "^8.0.6",
"vite": "^5.2.8"
Expand Down
87 changes: 87 additions & 0 deletions patches/wc-compiler+0.9.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
diff --git a/node_modules/wc-compiler/src/wcc.js b/node_modules/wc-compiler/src/wcc.js
index f9b3998..005e62e 100644
--- a/node_modules/wc-compiler/src/wcc.js
+++ b/node_modules/wc-compiler/src/wcc.js
@@ -29,16 +29,24 @@ async function renderComponentRoots(tree, definitions) {
const { tagName } = node;

if (definitions[tagName]) {
+ console.log('renderComponentRoots', { tagName });
const { moduleURL } = definitions[tagName];
- const elementInstance = await initializeCustomElement(moduleURL, tagName, node.attrs, definitions);
+ console.log({ node });
+ const elementInstance = await initializeCustomElement(moduleURL, tagName, node, definitions);
const elementHtml = elementInstance.shadowRoot
? elementInstance.getInnerHTML({ includeShadowRoots: true })
: elementInstance.innerHTML;
+ const hasShadow = elementInstance.shadowRoot;
const elementTree = parseFragment(elementHtml);
-
- node.childNodes = node.childNodes.length === 0
+ console.log('elementHtml', { elementHtml });
+ console.log('elementTree', { elementTree });
+ console.log('elementTree.childNodes', elementTree.childNodes);
+ console.log('node.childNodes', node.childNodes)
+ node.childNodes = node.childNodes.length === 0 && elementTree.childNodes > 0 && !hasShadow
? elementTree.childNodes
- : [...elementTree.childNodes, ...node.childNodes];
+ : hasShadow
+ ? [...elementTree.childNodes, ...node.childNodes]
+ : elementTree.childNodes;
} else {
console.warn(`WARNING: customElement <${tagName}> is not defined. You may not have imported it yet.`);
}
@@ -126,7 +134,10 @@ async function getTagName(moduleURL) {
return tagName;
}

-async function initializeCustomElement(elementURL, tagName, attrs = [], definitions = [], isEntry, props = {}) {
+async function initializeCustomElement(elementURL, tagName, node = {}, definitions = [], isEntry, props = {}) {
+ const { attrs = [], childNodes = [] } = node;
+ console.log('initializeCustomElement', { node });
+
if (!tagName) {
const depth = isEntry ? 1 : 0;
registerDependencies(elementURL, definitions, depth);
@@ -146,6 +157,41 @@ async function initializeCustomElement(elementURL, tagName, attrs = [], definiti

if (element) {
const elementInstance = new element(data); // eslint-disable-line new-cap
+ let innerHTML = elementInstance.innerHTML || '';
+
+ // TODO
+ // 1. Needs to be recursive
+ // 2. ~~Needs to handle attributes~~
+ // 3. Needs to handle duplicate content
+ // 4. Needs to handle self closing tags
+ // 5. handle all node types
+ childNodes.forEach((child) => {
+ const { nodeName, attrs = [] } = child;
+
+ if (nodeName !== '#text') {
+ innerHTML += `<${nodeName}`;
+
+ if (attrs.length > 0) {
+ attrs.forEach(attr => {
+ innerHTML += ` ${attr.name}="${attr.value}"`;
+ })
+ }
+
+ innerHTML += '>';
+
+ child.childNodes.forEach((c) => {
+ if (c.nodeName === '#text') {
+ innerHTML += c.value;
+ }
+ })
+
+ innerHTML += `</${nodeName}>`;
+ }
+ })
+
+ console.log({ innerHTML });
+ elementInstance.innerHTML = innerHTML;
+ console.log('=================')

attrs.forEach((attr) => {
elementInstance.setAttribute(attr.name, attr.value);
16 changes: 16 additions & 0 deletions src/components/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// "mock" CSS Modules
const styles = {
card: 'card'
}

export default class Card extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<div class="${styles.card}">
${this.innerHTML}
</div>
`
}
}

customElements.define('app-card', Card);
14 changes: 14 additions & 0 deletions src/components/card/card.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import './card.js';

export default {
title: 'Components/Card'
};

const Template = () => `
<app-card>
<p>This is some feature</p>
<img src="https://www.greenwoodjs.io/assets/greenwood-logo-og.png"/>
</app-card>
`;

export const Primary = Template.bind({});
5 changes: 5 additions & 0 deletions src/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

<head>
<title>Greenwood</title>
<script type="module" src="../components/card/card.js" data-gwd-opt="static"></script>
<script type="module" src="../components/footer/footer.js" data-gwd-opt="static"></script>
<link rel="stylesheet" href="../styles/theme.css"></link>
</head>

<body>
<h1>Welcome to Greenwood!</h1>
<app-card>
<p>This is some feature</p>
<img src="https://www.greenwoodjs.io/assets/greenwood-logo-og.png" height="200px" width="200px"/>
</app-card>
<app-footer></app-footer>
</body>

Expand Down
6 changes: 6 additions & 0 deletions src/styles/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ body {
h1, h2, h3, h4, h5, h6 {
text-align: center;
color: black;
}

.card {
display: block;
text-align: center;
color: red;
}