clapsit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +77 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# clapsit
|
|
2
|
+
|
|
3
|
+
**clapsit** is a simple and declarative JavaScript framework for building user interfaces, inspired by Flutter's component-based architecture. It allows you to build a web application's UI using a declarative, function-based syntax.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install clapsit, you can use npm or yarn:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install clapsit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add clapsit
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Here is a simple example of a clapsit component:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { div, h1, p, button } from 'clapsit';
|
|
23
|
+
|
|
24
|
+
function MyComponent() {
|
|
25
|
+
return div(
|
|
26
|
+
h1('Hello, clapsit!'),
|
|
27
|
+
p('This is a simple component.'),
|
|
28
|
+
button('Click me!')
|
|
29
|
+
.onClick(() => alert('Button clicked!'))
|
|
30
|
+
.style({ backgroundColor: 'blue', color: 'white' })
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
To render a component to the DOM, use the `render` function:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { render } from 'clapsit';
|
|
39
|
+
|
|
40
|
+
render('#app', MyComponent());
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Example
|
|
44
|
+
|
|
45
|
+
This repository contains an example application in the `/example` directory. To run it:
|
|
46
|
+
|
|
47
|
+
1. Navigate to the `example` directory:
|
|
48
|
+
```bash
|
|
49
|
+
cd example
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
2. Install the dependencies:
|
|
53
|
+
```bash
|
|
54
|
+
npm install
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
3. Start the development server:
|
|
58
|
+
```bash
|
|
59
|
+
npm run start
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This will start a Parcel development server and open the example application in your browser.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare class DomElement {
|
|
2
|
+
element: HTMLElement;
|
|
3
|
+
constructor(tagName: string, ...children: (DomElement | HTMLElement | string)[]);
|
|
4
|
+
private addChildren;
|
|
5
|
+
style(styles: Partial<CSSStyleDeclaration>): this;
|
|
6
|
+
settings(attributes: {
|
|
7
|
+
[key: string]: string;
|
|
8
|
+
}): this;
|
|
9
|
+
onClick(handler: (e: MouseEvent) => void): this;
|
|
10
|
+
on(event: string, handler: (e: Event) => void): this;
|
|
11
|
+
render(): HTMLElement;
|
|
12
|
+
}
|
|
13
|
+
type Child = DomElement | HTMLElement | string;
|
|
14
|
+
export declare const div: (...children: Child[]) => DomElement;
|
|
15
|
+
export declare const h1: (...children: Child[]) => DomElement;
|
|
16
|
+
export declare const p: (...children: Child[]) => DomElement;
|
|
17
|
+
export declare const a: (...children: Child[]) => DomElement;
|
|
18
|
+
export declare const span: (...children: Child[]) => DomElement;
|
|
19
|
+
export declare const img: () => DomElement;
|
|
20
|
+
export declare const input: () => DomElement;
|
|
21
|
+
export declare const button: (...children: Child[]) => DomElement;
|
|
22
|
+
export declare function render(selector: string, component: DomElement): void;
|
|
23
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.render = exports.button = exports.input = exports.img = exports.span = exports.a = exports.p = exports.h1 = exports.div = void 0;
|
|
4
|
+
// A simple DOM element wrapper
|
|
5
|
+
class DomElement {
|
|
6
|
+
constructor(tagName, ...children) {
|
|
7
|
+
this.element = document.createElement(tagName);
|
|
8
|
+
this.addChildren(children);
|
|
9
|
+
}
|
|
10
|
+
addChildren(children) {
|
|
11
|
+
for (const child of children) {
|
|
12
|
+
if (typeof child === 'string') {
|
|
13
|
+
this.element.appendChild(document.createTextNode(child));
|
|
14
|
+
}
|
|
15
|
+
else if (child instanceof DomElement) {
|
|
16
|
+
this.element.appendChild(child.render());
|
|
17
|
+
}
|
|
18
|
+
else if (child instanceof HTMLElement) {
|
|
19
|
+
this.element.appendChild(child);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
style(styles) {
|
|
24
|
+
for (const key in styles) {
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(styles, key)) {
|
|
26
|
+
const styleKey = key;
|
|
27
|
+
this.element.style[styleKey] = styles[styleKey];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
settings(attributes) {
|
|
33
|
+
for (const key in attributes) {
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
|
|
35
|
+
this.element.setAttribute(key, attributes[key]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
onClick(handler) {
|
|
41
|
+
this.element.onclick = handler;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
on(event, handler) {
|
|
45
|
+
this.element.addEventListener(event, handler);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
render() {
|
|
49
|
+
return this.element;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Factory functions for creating elements
|
|
53
|
+
const div = (...children) => new DomElement('div', ...children);
|
|
54
|
+
exports.div = div;
|
|
55
|
+
const h1 = (...children) => new DomElement('h1', ...children);
|
|
56
|
+
exports.h1 = h1;
|
|
57
|
+
const p = (...children) => new DomElement('p', ...children);
|
|
58
|
+
exports.p = p;
|
|
59
|
+
const a = (...children) => new DomElement('a', ...children);
|
|
60
|
+
exports.a = a;
|
|
61
|
+
const span = (...children) => new DomElement('span', ...children);
|
|
62
|
+
exports.span = span;
|
|
63
|
+
const img = () => new DomElement('img');
|
|
64
|
+
exports.img = img;
|
|
65
|
+
const input = () => new DomElement('input');
|
|
66
|
+
exports.input = input;
|
|
67
|
+
const button = (...children) => new DomElement('button', ...children);
|
|
68
|
+
exports.button = button;
|
|
69
|
+
// The top-level render function
|
|
70
|
+
function render(selector, component) {
|
|
71
|
+
const app = document.querySelector(selector);
|
|
72
|
+
if (app) {
|
|
73
|
+
app.innerHTML = '';
|
|
74
|
+
app.appendChild(component.render());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.render = render;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clapsit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A simple and declarative JavaScript framework for building user interfaces, inspired by Flutter.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"framework",
|
|
14
|
+
"ui",
|
|
15
|
+
"declarative",
|
|
16
|
+
"frontend"
|
|
17
|
+
],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "^4.5.0"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
]
|
|
26
|
+
}
|