Skip to content

Commit

Permalink
Updated compare example
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDoge committed Nov 18, 2024
1 parent 4e1576e commit 1ffc84b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

### Syntax

[Compare Syntax](https://bafybeigfdi7ylsyo56uz3l5l5h76jsbkjb6jrtyccpsslnnqghquhsowii.ipfs.dweb.link)
[Compare Syntax](https://bafybeihfqjty6ftahk263rc6q66yxzuh24vylahprhodkocbbhvazjtjmi.ipfs.dweb.link)

## Installation 🍙

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fragment, ref, tags } from "@purifyjs/core";
import { useBind } from "./util-bind";
import { useBindText } from "./util-bind";

const { p, input } = tags;

Expand All @@ -8,6 +8,6 @@ export function InputHello() {

return fragment(
p().children(text),
input().effect(useBind(text, "value", "input")),
input().effect(useBindText(text)),
);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { Lifecycle, Signal } from "@purifyjs/core";

export function useBind<
T,
P extends keyof HTMLElement | (string & {}),
>(
signal: Signal.State<T>,
propertyName: P,
eventName: keyof HTMLElementEventMap | (string & {}),
): Lifecycle.OnConnected<HTMLElement & { [_ in P]: T }> {
export function useBindText(
signal: Signal.State<string>,
): Lifecycle.OnConnected<HTMLInputElement> {
return (element) => {
const handler = () => (signal.val = element[propertyName]);
element.addEventListener(eventName, handler);
const listener = () => (signal.val = element.value);
element.addEventListener("input", listener);

const unfollow = signal.follow(
(value) => element[propertyName] === value,
(value) => (element.value = value),
true,
);

return () => {
unfollow();
element.removeEventListener(eventName, handler);
element.removeEventListener("input", listener);
};
};
}
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@purifyjs/core",
"version": "0.0.329",
"version": "0.0.330",
"exports": {
".": "./lib/mod.ts",
"./aria": "./lib/aria.ts"
Expand Down

0 comments on commit 1ffc84b

Please sign in to comment.