Skip to content

Commit

Permalink
create hook to use dollars.js
Browse files Browse the repository at this point in the history
  • Loading branch information
UpperCod committed Oct 26, 2021
1 parent d84c0f7 commit 6143cad
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
29 changes: 27 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@atomico/hooks",
"description": "Series of utilities in hooks format to extend the operation of Atomico",
"version": "3.19.2",
"version": "3.20.0",
"type": "module",
"workspaces": [
"src/**/*"
Expand Down
5 changes: 5 additions & 0 deletions src/use-dollars/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"dollars.js": "^0.6.1"
}
}
31 changes: 31 additions & 0 deletions src/use-dollars/use-dollars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useHost, useEffect } from "atomico";
import { useSlot } from "../use-slot/use-slot";
import { State, Host } from "dollars.js";

class AtomicoState extends State {
init(state) {
return state;
}
}

export function useDollars(ref, config = { prefix: "$" }) {
const childNodes = useSlot(ref);
const host = useHost();

useEffect(() => {
ref.states = childNodes
.filter((el) => el instanceof Element)
.map((element) => {
const state = new AtomicoState(host.current);
const isTemplate = element instanceof HTMLTemplateElement;
new Host(isTemplate ? element.content : element, state, config);
if (isTemplate)
element.parentElement.insertBefore(element.content, element);
return state;
});
}, childNodes);

ref?.states?.forEach((state) => state.update());

return ref;
}

0 comments on commit 6143cad

Please sign in to comment.