@20baht/angular 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +158 -2
- package/dist/cell.d.ts +44 -0
- package/dist/cell.js +101 -0
- package/dist/elements.d.ts +2323 -0
- package/dist/elements.js +5082 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/runtime.d.ts +21 -0
- package/dist/runtime.js +49 -0
- package/dist/stickers.d.ts +77 -0
- package/dist/stickers.js +146 -0
- package/dist/store.d.ts +12 -0
- package/dist/store.js +37 -0
- package/package.json +81 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nattapong Promthong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,159 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @20baht/angular
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Angular adapter for [@20baht/ui](https://www.npmjs.com/package/@20baht/ui). One directive per `<baht-*>`
|
|
4
|
+
element, sitting on the real element (no wrapper, no extra DOM), so templates get:
|
|
5
|
+
|
|
6
|
+
- **typed inputs**: `[field]`, `[form]`, `[items]` are checked against the element's own property types, and a
|
|
7
|
+
misspelled property fails the build (`Can't bind to 'varient'`);
|
|
8
|
+
- **typed outputs** that emit the event `detail`: `(bahtChange)="save($event.data)"`, no `$any($event).detail`;
|
|
9
|
+
- **`[(value)]`** on every element that edits a value (fields, tabs, rating, range, otp, …), with signals;
|
|
10
|
+
- **the binding gotchas handled**: `readonly` reaches the element (Angular maps a plain `[readonly]` to the
|
|
11
|
+
native `readOnly`), and boolean/number attributes (`<baht-form readonly>`, `debounce="300"`) are coerced;
|
|
12
|
+
- **no `CUSTOM_ELEMENTS_SCHEMA`**: the directives make the tags known to the compiler.
|
|
13
|
+
|
|
14
|
+
The directives are generated from the elements' `custom-elements.json`, so every element, property and event
|
|
15
|
+
in the matching @20baht/ui minor is covered.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @20baht/ui @20baht/angular
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Use
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// main.ts: register the elements once, as before
|
|
25
|
+
import '@20baht/ui';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { Component, signal } from '@angular/core';
|
|
30
|
+
import type { FormDefinition } from '@20baht/ui';
|
|
31
|
+
import { BAHT_UI } from '@20baht/angular';
|
|
32
|
+
|
|
33
|
+
@Component({
|
|
34
|
+
selector: 'app-request',
|
|
35
|
+
imports: [BAHT_UI],
|
|
36
|
+
template: `
|
|
37
|
+
<baht-tabs [tabs]="tabs" [(value)]="tab" />
|
|
38
|
+
<baht-form #f="bahtForm" [form]="form" [readonly]="locked()" (bahtChange)="draft = $event.data" />
|
|
39
|
+
<baht-button (click)="f.element.validate().valid && save()">Save</baht-button>
|
|
40
|
+
`,
|
|
41
|
+
})
|
|
42
|
+
export class RequestComponent {
|
|
43
|
+
readonly tab = signal('details');
|
|
44
|
+
readonly locked = signal(false);
|
|
45
|
+
form!: FormDefinition;
|
|
46
|
+
draft: Record<string, unknown> = {};
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
- `BAHT_UI` is every directive; import single ones (`BahtFormDirective`, `BahtSelectDirective`, …) if you prefer.
|
|
51
|
+
- Outputs are the event name in camelCase: `baht-row-click` → `(bahtRowClick)`, emitting the detail. The DOM
|
|
52
|
+
event still fires, so existing `(baht-row-click)="…$event.detail…"` bindings keep working while you migrate.
|
|
53
|
+
- `#ref="bahtForm"` (the tag in camelCase) gives the directive; `.element` is the element, typed, for its methods
|
|
54
|
+
(`validate()`, `getData()`, `reset()`, `replay()`, …). `viewChild(BahtFormDirective)` works the same way.
|
|
55
|
+
- Outputs only fire for the element's own events; the same event bubbling from an element inside it is ignored.
|
|
56
|
+
- A **cancelable** event (one the element documents as "Cancelable: `preventDefault()` to route yourself") has a
|
|
57
|
+
second output, `<name>Event`, that emits the event itself, typed `CustomEvent<Detail>`: `baht-nav` on
|
|
58
|
+
`<baht-app-shell>`, `baht-back` on `<baht-page-header>` and `<baht-record-bar>`, `baht-open` on
|
|
59
|
+
`<baht-catalog-card>`. It fires during the dispatch, so `preventDefault()` keeps the browser on the page:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// <baht-app-shell [items]="items" (bahtNavEvent)="navigate($event)" />
|
|
63
|
+
navigate(event: CustomEvent<{ key: string; href: string | undefined }>): void {
|
|
64
|
+
if (event.detail.href === undefined) return;
|
|
65
|
+
event.preventDefault(); // route in place instead of loading the link
|
|
66
|
+
void this.router.navigateByUrl(event.detail.href);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
- `<baht-form>` and `<baht-workflow>` have no `[(value)]`: their `value` is the starting data. Listen to
|
|
70
|
+
`(bahtChange)` / `(bahtDataChange)`, or bind a store (below).
|
|
71
|
+
|
|
72
|
+
## Table cells as templates
|
|
73
|
+
|
|
74
|
+
A `<baht-table>` column already renders text, a badge (`cell: 'badge'`, `tone`, `labels`) or a code chip from its
|
|
75
|
+
column spec, and `rowMenu` gives each row a menu. For anything else, write the cell as a template inside the table:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { BAHT_UI, BahtCellDirective } from '@20baht/angular';
|
|
79
|
+
|
|
80
|
+
@Component({
|
|
81
|
+
imports: [BAHT_UI, BahtCellDirective],
|
|
82
|
+
template: `
|
|
83
|
+
<baht-table [items]="servers()" [columns]="columns">
|
|
84
|
+
<span *bahtCell="'name' of servers(); let row">
|
|
85
|
+
<a [routerLink]="['/servers', row.id]">{{ row.name }}</a>
|
|
86
|
+
@if (row.locked) { <baht-badge label="Locked" /> }
|
|
87
|
+
</span>
|
|
88
|
+
</baht-table>
|
|
89
|
+
`,
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- `'name'` is the column key (a dotted key works too). `let row` is the row; `let v = value` the cell's raw value.
|
|
94
|
+
- `of servers()` is optional and only types `row` as your row type; without it `row` is `Record<string, unknown>`.
|
|
95
|
+
- It goes through the table's `renderers`: the template wins over that column's `cell` / `tone` / `labels`, and
|
|
96
|
+
replaces a `[renderers]` entry of your own for the same key. Remove the template and the column renders as before.
|
|
97
|
+
- One embedded view per row object: a row object that stays keeps its view (and its state); a row that leaves the
|
|
98
|
+
page destroys its view. The views are checked with the rest of the app.
|
|
99
|
+
- Row types for `[items]` must be `type` aliases, not interfaces: `items` is `Record<string, unknown>[]`, which an
|
|
100
|
+
interface does not satisfy.
|
|
101
|
+
|
|
102
|
+
## @20baht/store as signals
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { createBahtStore } from '@20baht/store';
|
|
106
|
+
import { bahtSignal, bahtStore } from '@20baht/angular/store';
|
|
107
|
+
|
|
108
|
+
readonly store = createBahtStore();
|
|
109
|
+
readonly days = bahtSignal<number>(this.store, 'Request.days'); // WritableSignal: set() writes through
|
|
110
|
+
readonly all = bahtStore(this.store); // the whole tree, read-only
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Call them in an injection context; they unsubscribe on destroy. `@20baht/store` is an optional peer.
|
|
114
|
+
|
|
115
|
+
## Stickers
|
|
116
|
+
|
|
117
|
+
`@20baht/angular/stickers` has the directives for [@20baht/stickers](https://www.npmjs.com/package/@20baht/stickers)'
|
|
118
|
+
`<baht-sticker>` and `<baht-sticker-graph>`, in the same shape. `@20baht/stickers` is an optional peer: an app that
|
|
119
|
+
does not import this entry pulls nothing of it in.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
// main.ts: register the element and the stickers you use, once
|
|
123
|
+
import '@20baht/stickers/element';
|
|
124
|
+
import { registerSticker } from '@20baht/stickers';
|
|
125
|
+
import { piggyBank } from '@20baht/stickers/icons/piggy-bank';
|
|
126
|
+
registerSticker(piggyBank); // or import '@20baht/stickers/all' for every sticker and the graph
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import { BAHT_STICKERS } from '@20baht/angular/stickers';
|
|
131
|
+
|
|
132
|
+
@Component({
|
|
133
|
+
imports: [BAHT_STICKERS],
|
|
134
|
+
template: `
|
|
135
|
+
<baht-sticker name="piggy-bank" [playOnce]="!loop()" enter="0.1" replay-on-hover />
|
|
136
|
+
<baht-sticker [icon]="icon" />
|
|
137
|
+
<baht-sticker-graph [graph]="graph" (bahtStep)="step = $event.index" />
|
|
138
|
+
<baht-sticker-graph [graph]="graph" interactive (bahtNode)="open($event.id)" />
|
|
139
|
+
`,
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
- `[playOnce]` is the element's `once`. Angular reads any name starting with "on" as an event handler and refuses
|
|
144
|
+
`[once]`; a static `once` attribute still works.
|
|
145
|
+
- `name` is typed as `StickerName` (the collection's names), so a misspelled sticker fails the build. A sticker of
|
|
146
|
+
your own goes in `[icon]`.
|
|
147
|
+
- `#s="bahtSticker"` gives `s.element.replay()`.
|
|
148
|
+
|
|
149
|
+
## Versions and federation
|
|
150
|
+
|
|
151
|
+
- This package has the same version number as `@20baht/ui` and peers on its minor (`>=0.20.0 <0.21.0`): the
|
|
152
|
+
directives are generated from that minor's elements. Optional peer `@20baht/store`, same range. Angular `>=20`. Optional peer `@20baht/stickers` (`>=0.3.0 <0.4.0`), for `/stickers` only.
|
|
153
|
+
- The package ships partial-compiled (Ivy) code and imports only types from @20baht/ui at runtime, so it adds
|
|
154
|
+
no second copy of the elements. With Native Federation, share it like any other library.
|
|
155
|
+
- With Native Federation, also list `@20baht/engine` in each app's `dependencies` (same version as
|
|
156
|
+
`@20baht/ui`): the elements import it by name, and federation shares only what an app lists. Without it the
|
|
157
|
+
build passes and the browser cannot load the elements. Scaffolds from `bahtui create --angular` list it.
|
|
158
|
+
|
|
159
|
+
MIT.
|
package/dist/cell.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type DoCheck, type OnDestroy } from '@angular/core';
|
|
2
|
+
import * as i0 from "@angular/core";
|
|
3
|
+
type Row = Record<string, unknown>;
|
|
4
|
+
/** What a `*bahtCell` template gets: the row (`let row`) and the cell's raw value (`let v = value`). */
|
|
5
|
+
export interface BahtCellContext<R extends object = Row> {
|
|
6
|
+
$implicit: R;
|
|
7
|
+
row: R;
|
|
8
|
+
value: unknown;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A `<baht-table>` column's cells as an Angular template, placed inside the table:
|
|
12
|
+
*
|
|
13
|
+
* <baht-table [items]="servers()" [columns]="columns">
|
|
14
|
+
* <baht-badge *bahtCell="'status' of servers(); let row" [tone]="tone(row)" [label]="row.status" />
|
|
15
|
+
* </baht-table>
|
|
16
|
+
*
|
|
17
|
+
* `'status'` is the column key (dotted keys too). `of rows` is optional and only types `row` as
|
|
18
|
+
* your row type. The template wins over the column's `cell` / `tone` / `labels`, like any
|
|
19
|
+
* `renderers` entry; an entry of your own `[renderers]` for the same key is replaced.
|
|
20
|
+
*/
|
|
21
|
+
export declare class BahtCellDirective<R extends object = Row> implements DoCheck, OnDestroy {
|
|
22
|
+
/** The column key the template renders. */
|
|
23
|
+
bahtCell: string;
|
|
24
|
+
/** The table's rows, only to type `let row` (`*bahtCell="'status' of servers(); let row"`). */
|
|
25
|
+
bahtCellOf: readonly R[] | null | undefined;
|
|
26
|
+
private readonly template;
|
|
27
|
+
private readonly table;
|
|
28
|
+
private readonly app;
|
|
29
|
+
private readonly document;
|
|
30
|
+
private readonly cells;
|
|
31
|
+
private key;
|
|
32
|
+
private sweeping;
|
|
33
|
+
static ngTemplateContextGuard<R extends object>(_dir: BahtCellDirective<R>, _ctx: unknown): _ctx is BahtCellContext<R>;
|
|
34
|
+
private readonly render;
|
|
35
|
+
/** Keep the renderer in the table's `renderers`, even when the host rebinds `[renderers]`. */
|
|
36
|
+
ngDoCheck(): void;
|
|
37
|
+
ngOnDestroy(): void;
|
|
38
|
+
private detach;
|
|
39
|
+
/** Once the table has rendered, destroy the views of rows it no longer shows. */
|
|
40
|
+
private sweepAfterUpdate;
|
|
41
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<BahtCellDirective<any>, never>;
|
|
42
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<BahtCellDirective<any>, "[bahtCell]", never, { "bahtCell": { "alias": "bahtCell"; "required": true; }; "bahtCellOf": { "alias": "bahtCellOf"; "required": false; }; }, {}, never, never, true, never>;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
package/dist/cell.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// *bahtCell: an Angular template as a <baht-table> cell, through the table's own `renderers`
|
|
2
|
+
// property. The renderer returns an element holding an embedded view of the template; Lit puts
|
|
3
|
+
// that element in the cell like any other node, and the same element comes back while the row
|
|
4
|
+
// object is the same, so Lit leaves it where it is. Views whose element left the table are
|
|
5
|
+
// destroyed after the table's update.
|
|
6
|
+
import { ApplicationRef, Directive, DOCUMENT, inject, Input, TemplateRef, } from '@angular/core';
|
|
7
|
+
import { BahtTableDirective } from './elements.js';
|
|
8
|
+
import * as i0 from "@angular/core";
|
|
9
|
+
/**
|
|
10
|
+
* A `<baht-table>` column's cells as an Angular template, placed inside the table:
|
|
11
|
+
*
|
|
12
|
+
* <baht-table [items]="servers()" [columns]="columns">
|
|
13
|
+
* <baht-badge *bahtCell="'status' of servers(); let row" [tone]="tone(row)" [label]="row.status" />
|
|
14
|
+
* </baht-table>
|
|
15
|
+
*
|
|
16
|
+
* `'status'` is the column key (dotted keys too). `of rows` is optional and only types `row` as
|
|
17
|
+
* your row type. The template wins over the column's `cell` / `tone` / `labels`, like any
|
|
18
|
+
* `renderers` entry; an entry of your own `[renderers]` for the same key is replaced.
|
|
19
|
+
*/
|
|
20
|
+
export class BahtCellDirective {
|
|
21
|
+
constructor() {
|
|
22
|
+
/** The column key the template renders. */
|
|
23
|
+
this.bahtCell = '';
|
|
24
|
+
this.template = inject(TemplateRef);
|
|
25
|
+
this.table = inject(BahtTableDirective).element;
|
|
26
|
+
this.app = inject(ApplicationRef);
|
|
27
|
+
this.document = inject(DOCUMENT);
|
|
28
|
+
this.cells = new Map();
|
|
29
|
+
this.key = '';
|
|
30
|
+
this.sweeping = false;
|
|
31
|
+
this.render = (value, row) => {
|
|
32
|
+
let cell = this.cells.get(row);
|
|
33
|
+
if (cell) {
|
|
34
|
+
Object.assign(cell.view.context, { $implicit: row, row, value });
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const view = this.template.createEmbeddedView({ $implicit: row, row: row, value });
|
|
38
|
+
this.app.attachView(view);
|
|
39
|
+
const node = this.document.createElement('span');
|
|
40
|
+
node.style.display = 'contents';
|
|
41
|
+
node.append(...view.rootNodes);
|
|
42
|
+
cell = { view, node };
|
|
43
|
+
this.cells.set(row, cell);
|
|
44
|
+
}
|
|
45
|
+
cell.view.detectChanges();
|
|
46
|
+
this.sweepAfterUpdate();
|
|
47
|
+
return cell.node;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
static ngTemplateContextGuard(_dir, _ctx) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/** Keep the renderer in the table's `renderers`, even when the host rebinds `[renderers]`. */
|
|
54
|
+
ngDoCheck() {
|
|
55
|
+
if (this.key !== this.bahtCell) {
|
|
56
|
+
this.detach();
|
|
57
|
+
this.key = this.bahtCell;
|
|
58
|
+
}
|
|
59
|
+
if (this.table.renderers[this.key] !== this.render) {
|
|
60
|
+
this.table.renderers = { ...this.table.renderers, [this.key]: this.render };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
ngOnDestroy() {
|
|
64
|
+
this.detach();
|
|
65
|
+
for (const cell of this.cells.values())
|
|
66
|
+
cell.view.destroy();
|
|
67
|
+
this.cells.clear();
|
|
68
|
+
}
|
|
69
|
+
detach() {
|
|
70
|
+
if (this.key === '' || this.table.renderers[this.key] !== this.render)
|
|
71
|
+
return;
|
|
72
|
+
const key = this.key;
|
|
73
|
+
this.table.renderers = Object.fromEntries(Object.entries(this.table.renderers).filter(([k]) => k !== key));
|
|
74
|
+
}
|
|
75
|
+
/** Once the table has rendered, destroy the views of rows it no longer shows. */
|
|
76
|
+
sweepAfterUpdate() {
|
|
77
|
+
if (this.sweeping)
|
|
78
|
+
return;
|
|
79
|
+
this.sweeping = true;
|
|
80
|
+
void this.table.updateComplete.then(() => {
|
|
81
|
+
this.sweeping = false;
|
|
82
|
+
for (const [row, cell] of this.cells) {
|
|
83
|
+
if (this.table.contains(cell.node))
|
|
84
|
+
continue;
|
|
85
|
+
cell.view.destroy();
|
|
86
|
+
this.cells.delete(row);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.5", ngImport: i0, type: BahtCellDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
|
|
91
|
+
static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "22.2.5", type: BahtCellDirective, isStandalone: true, selector: "[bahtCell]", inputs: { bahtCell: "bahtCell", bahtCellOf: "bahtCellOf" }, ngImport: i0 }); }
|
|
92
|
+
}
|
|
93
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.5", ngImport: i0, type: BahtCellDirective, decorators: [{
|
|
94
|
+
type: Directive,
|
|
95
|
+
args: [{ selector: '[bahtCell]' }]
|
|
96
|
+
}], propDecorators: { bahtCell: [{
|
|
97
|
+
type: Input,
|
|
98
|
+
args: [{ required: true }]
|
|
99
|
+
}], bahtCellOf: [{
|
|
100
|
+
type: Input
|
|
101
|
+
}] } });
|