Skip to content

Commit

Permalink
feat: include ability for progressive enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
mariohamann committed Mar 4, 2024
1 parent 0011866 commit 7656db5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ <h2>Client Side Rendered</h2>
lang="de"
></activity-graph>
/client -->
<h2>Progressive Enhancement / HTML Web Component</h2>
<!-- client
<activity-graph range-start="2024-01-01" range-end="2024-12-31" activity-levels="0,1,2,4">
<ul>
<li data-activity="2024-01-03">Activity on 2024-01-03</li>
<li data-activity="2024-01-04">Activity on 2024-01-04</li>
<li data-activity="2024-01-05">Activity on 2024-01-05</li>
<li data-activity="2024-01-06">Activity on 2024-01-06</li>
</ul>
</activity-graph>
/client -->
<pre>
<code>
&lt;activity-graph
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mariohamann/activity-graph",
"version": "0.0.8",
"version": "0.0.9",
"scripts": {
"dev": "vite",
"build": "node scripts/build.mjs",
Expand Down
12 changes: 12 additions & 0 deletions src/activity-graph.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ class ActivityGraph extends HTMLElement {
"i18n",
].forEach((attr) => (this.attrs[attr] = this.getAttribute(attr)));

if (this.activityDataFromChildren.length > 0) {
this.attrs["activity-data"] =
this.activityDataFromChildren.join(",");
this.setAttribute("activity-data", this.attrs["activity-data"]);
}

this.innerHTML = ActivityGraphElement({
state: { attrs: this.attrs },
});
Expand All @@ -43,6 +49,12 @@ class ActivityGraph extends HTMLElement {
state: { attrs: this.attrs },
});
}

get activityDataFromChildren() {
return Array.from(this.querySelectorAll("[data-activity]")).map((el) =>
el.getAttribute("data-activity")
);
}
}

customElements.define("activity-graph", ActivityGraph);
4 changes: 2 additions & 2 deletions vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const htmlPlugin = () => {
bodyContent: false,
});
return render`${html}`
.replace("<!-- client", "")
.replace("/client -->", "");
.replaceAll("<!-- client", "")
.replaceAll("/client -->", "");
},
};
};
Expand Down

0 comments on commit 7656db5

Please sign in to comment.