Functions and types that ease writing βΊTent components.
bind
- Bind an Input element value to a state propertyclasses
- Generate a class string from multiple class nameson
- Easier event handling based on key codesternary
- Simplified conditional renderingmountAll
- Mount multiple components at onceFormEvent<T>
- A generic type for form events
npm install @tentjs/helpers
# or with JSR (recommended)
npx jsr add @tentjs/helpers
import { type Component, tags } from "@tentjs/tent";
import { bind } from "@tentjs/helpers";
type State = { name: string; details: { age: number } };
const MyComponent: Component<State> = {
state: { name: "", details: { age: 0 } },
view: ({ state }) =>
tags.input("", {
// Bind the input value to the state property `name`
oninput: bind(state, "name"),
// or, for nested properties:
// bind(state, "details.age"),
}),
};
import { tags } from "@tentjs/tent";
import { type FormEvent } from "@tentjs/helpers";
tags.input("", {
oninput: (event: FormEvent<HTMLInputElement>) => {
// event.target is typed as `HTMLInputElement`
state.name = event.target.value;
},
});
import { tags } from "@tentjs/tent";
import { classes } from "@tentjs/helpers";
tags.div("", {
className: classes("container", 2 > 3 && "some-class"),
});
import { tags } from "@tentjs/tent";
import { on } from "@tentjs/helpers";
tags.input("", {
onkeydown: on({
Enter: () => console.log("Enter pressed"),
Escape: () => console.log("Escape pressed"),
}),
});
import { tags } from "@tentjs/tent";
import { ternary } from "@tentjs/helpers";
const MyComponent = {
view: () =>
tags.div(
ternary(3 > 2, "3 is greater than 2", "3 is not greater than 2"),
// => "3 is greater than 2"
),
};
import { mountAll } from "@tentjs/helpers";
mountAll([
{ target: ".target-1", component: Component1 },
{ target: ".target-2", component: Component2 },
{ target: ".target-3", component: Component3 },
]);