@amateras/widget 0.0.1

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 ADDED
@@ -0,0 +1,14 @@
1
+ # Amateras Widget
2
+ Use `$.widget` for writing component templates.
3
+
4
+ ## Usage
5
+ ```ts
6
+ import 'amateras';
7
+ import 'amateras/widget';
8
+
9
+ const App = $.widget((props) => {
10
+ $('h1', () => $`Hello, World!`)
11
+ })
12
+
13
+ $.render(App, 'body');
14
+ ```
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@amateras/widget",
3
+ "peerDependencies": {
4
+ "@amateras/core": "workspace:*",
5
+ "@amateras/utils": "workspace:*"
6
+ },
7
+ "imports": {
8
+ "#structure/*": "./src/structure/*.ts",
9
+ "#lib/*": "./src/lib/*.ts"
10
+ },
11
+ "exports": {
12
+ ".": "./src/index.ts"
13
+ },
14
+ "version": "0.0.1"
15
+ }
package/src/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { WidgetConstructor } from '#structure/Widget';
2
+ import { symbol_ProtoType, type Proto } from '@amateras/core';
3
+ import { Utils } from '@amateras/utils';
4
+
5
+ declare global {
6
+ export namespace $ {
7
+ export function widget<Props = {}, Parent extends Proto = never>(builder: WidgetBuilder<Props, Parent>): WidgetConstructor<Props, Parent>;
8
+
9
+ export interface Overload<I> {
10
+ widget: [
11
+ input: [WidgetConstructor<any, any>],
12
+ output: I[0] extends WidgetConstructor ? InstanceType<I[0]> : never,
13
+ args: I[0] extends WidgetConstructor<infer Props, infer Parent>
14
+ ? WidgetConstructorArguments<Props, Parent>
15
+ : never
16
+ ]
17
+ }
18
+ }
19
+
20
+ // export function $<Props, Parent extends Proto>(widget: WidgetConstructor<Props, Parent>, ...args: WidgetConstructorArguments<Props, Parent>): Widget<Props, Parent>;
21
+ }
22
+
23
+ export type WidgetBuilder<Props = any, Parent extends Proto = any> = (props: $.Props<Props>, children: $.Layout<Parent>) => void;
24
+
25
+ type WidgetConstructorArguments<Props, Parent extends Proto> =
26
+ RequiredKeys<Props> extends never
27
+ ? [props?: $.Props<Props>, children?: $.Layout<Parent>] | [children?: $.Layout<Parent>]
28
+ : [props: $.Props<Props>, children?: $.Layout<Parent>];
29
+
30
+ Utils.assign($, {
31
+ widget(builder: WidgetBuilder) {
32
+ return WidgetConstructor(builder);
33
+ }
34
+ })
35
+
36
+ $.middleware.craft.add((widget, ...args) => {
37
+ if (widget[symbol_ProtoType] === 'Widget') {
38
+ let [arg1] = args;
39
+ args = Utils.isFunction(arg1) ? [{}, arg1] : args;
40
+ return new widget(...args)
41
+ }
42
+ })
43
+
44
+ export * from '#structure/Widget';
@@ -0,0 +1,25 @@
1
+ import { Proto, ProxyProto, symbol_ProtoType } from "@amateras/core";
2
+ import type { WidgetBuilder } from "..";
3
+
4
+ export const WidgetConstructor = (builder: WidgetBuilder) => {
5
+ const Widget = class extends ProxyProto {
6
+ static override readonly [symbol_ProtoType] = 'Widget' as any;
7
+ static override name = 'Widget';
8
+ static builder = builder;
9
+ constructor(props: $.Props, layout?: $.Layout) {
10
+ super(() => Widget.builder(props, (proto) => layout?.(proto)));
11
+ }
12
+ } as unknown as WidgetConstructor
13
+ return Widget;
14
+ }
15
+
16
+ export interface WidgetConstructor<Props = {}, Parent extends Proto = any> {
17
+ [symbol_ProtoType]: 'Widget';
18
+ builder: WidgetBuilder;
19
+ new (builder: WidgetBuilder): Widget<Props, Parent>;
20
+ }
21
+
22
+ export interface Widget<Props = {}, Parent extends Proto = any> extends Proto {
23
+ props: Props;
24
+ parent: Parent;
25
+ }