Skip to content

Instantly share code, notes, and snippets.

@jleeson
Last active October 3, 2021 06:08
Code samples for exaltjs.com
import { Component, html } from "@exalt/core";
import { define } from "@exalt/core/decorators";
@define("hello-world")
export class HelloWorld extends Component {
render() {
return html`
<h1>Hello World!</h1>
`;
}
}
export default ({ name, input, toolchainOptions }) => {
return {
dev: () => {},
start: () => {},
build: () => {}
};
};
import { html } from "@exalt/core";
import "@exalt/router/exalt-route";
html`<exalt-route url="/" component="home-page" onresolve=${() => import("@pages/home-page")} />`;
import { Component, html } from "@exalt/core";
import { define } from "@exalt/core/decorators";
@define("say-hello")
export class SayHello extends Component {
render({ name }) {
return html`
<h1>Hello ${name}!</h1>
`;
}
}
import { Component, html } from "@exalt/core";
import { define } from "@exalt/core/decorators";
@define("hello-world")
export class HelloWorld extends Component {
/* render the components html */
render() {}
/* called when the component is added to the DOM */
mount() {}
/* called when the component is removed from the DOM */
unmount() {}
/* called when a component's state or attributes are updated */
onUpdate(key, value) {}
/* called when a component is about to be updated */
shouldUpdate(key, value) {}
}
import { Component, html } from "@exalt/core";
import { define } from "@exalt/core/decorators";
@define("hello-world")
export class HelloWorld extends Component {
header = super.createRef();
render() {
return html`
<h1 ref="header">Hello World!</h1>
`;
}
mount() {
this.header.innerHTML = "Goodbye World!";
}
}
<exalt-redirect url="/redirected-route" />
<exalt-router>
<exalt-route url="/" component="home-page" />
<exalt-route url="/about" component="about-page" />
<exalt-route url="/about/:topic?" component="about-page" />
<exalt-route component="not-found" />
</exalt-router>
import { ExaltRouter } from "@exalt/router";
ExaltRouter.navigate("/about");
import { loadBundle } from "@exalt/ssr";
import path from "path";
const bundlePath = path.join(process.cwd(), "dist", "index.js");
/* loadBundle returns the bundle exports, in this case it returns the App component */
const { App } = loadBundle(bundlePath);
const component = new App();
import { Component, html } from "@exalt/core";
import { define } from "@exalt/core/decorators";
@define("my-clock")
export class Clock extends Component {
date = super.reactive(new Date());
render() {
return html`
<h1>Current Time: ${this.date}</h1>
`;
}
mount() {
this.timer = setInterval(() => this.date = new Date(), 1000);
}
unmount() {
clearInterval(this.timer);
}
}
import { loadBundle, renderToString } from "@exalt/ssr";
import path from "path";
const bundlePath = path.join(process.cwd(), "dist", "index.js");
/* loadBundle returns the bundle exports, in this case it returns the App component */
const { App } = loadBundle(bundlePath);
const html = renderToString(new App(), (currentNode) => {
console.log(`Rendering: ${currentNode.nodeName}`);
});
console.log(html);
import { Component, html, createStore } from "@exalt/core";
import { define } from "@exalt/core/decorators";
/* create the store */
const store = createStore({
count: 0,
increment() {
this.count++
}
});
@define({ tag: "my-container", connect: [store] })
export class Counter extends Component {
render() {
return html`
<button onclick=${() => store.increment()}>Clicked: ${store.count}</button>
`;
}
}
import { Component, html } from "@exalt/core";
import { define } from "@exalt/core/decorators";
import styles from "./hello-world.css";
@define({ tag: "hello-world", styles: [styles] })
export class HelloWorld extends Component {
render() {
return html`
<h1>Hello World!</h1>
`;
}
}
import { html } from "@exalt/core";
const details = {
name: "John Doe",
age: 34
};
html`<user-details ...=${details} />`;
import { html } from "@exalt/core";
/* bind an event */
html`<button onclick=${() => alert("I was clicked!")}>Click Me!</button>`;
/* bind data as attributs */
const items = [
{ id: Math.random(), value: "1" },
{ id: Math.random(), value: "2" }
];
html`<list-view items=${items} />`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment