element-vir 26.12.0 → 26.12.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/dist/declarative-element/custom-tag-name.js +1 -0
- package/dist/declarative-element/declarative-element-init.d.ts +56 -0
- package/dist/declarative-element/declarative-element-init.js +1 -0
- package/dist/declarative-element/declarative-element.d.ts +114 -0
- package/dist/declarative-element/declarative-element.js +36 -0
- package/dist/declarative-element/define-element.d.ts +41 -0
- package/dist/declarative-element/define-element.js +248 -0
- package/{src/declarative-element/definition-options.ts → dist/declarative-element/definition-options.d.ts} +2 -7
- package/dist/declarative-element/definition-options.js +9 -0
- package/dist/declarative-element/directives/assign.directive.d.ts +24 -0
- package/dist/declarative-element/directives/assign.directive.js +34 -0
- package/dist/declarative-element/directives/async-prop.d.ts +61 -0
- package/{src/declarative-element/directives/async-prop.ts → dist/declarative-element/directives/async-prop.js} +8 -42
- package/dist/declarative-element/directives/attributes.directive.d.ts +30 -0
- package/dist/declarative-element/directives/attributes.directive.js +35 -0
- package/dist/declarative-element/directives/create-attribute-directive.d.ts +28 -0
- package/dist/declarative-element/directives/create-attribute-directive.js +41 -0
- package/dist/declarative-element/directives/directive-helpers.d.ts +27 -0
- package/dist/declarative-element/directives/directive-helpers.js +37 -0
- package/dist/declarative-element/directives/listen-to-activate.d.ts +15 -0
- package/{src/declarative-element/directives/listen-to-activate.ts → dist/declarative-element/directives/listen-to-activate.js} +3 -8
- package/dist/declarative-element/directives/listen.directive.d.ts +92 -0
- package/dist/declarative-element/directives/listen.directive.js +48 -0
- package/dist/declarative-element/directives/mutate.directive.d.ts +38 -0
- package/dist/declarative-element/directives/mutate.directive.js +45 -0
- package/dist/declarative-element/directives/on-dom-created.directive.d.ts +44 -0
- package/dist/declarative-element/directives/on-dom-created.directive.js +51 -0
- package/dist/declarative-element/directives/on-dom-rendered.directive.d.ts +41 -0
- package/dist/declarative-element/directives/on-dom-rendered.directive.js +45 -0
- package/dist/declarative-element/directives/on-intersect.directive.d.ts +64 -0
- package/dist/declarative-element/directives/on-intersect.directive.js +89 -0
- package/dist/declarative-element/directives/on-resize.directive.d.ts +74 -0
- package/dist/declarative-element/directives/on-resize.directive.js +106 -0
- package/dist/declarative-element/directives/render-async.directive.d.ts +45 -0
- package/dist/declarative-element/directives/render-async.directive.js +33 -0
- package/dist/declarative-element/directives/render-if.directive.d.ts +32 -0
- package/{src/declarative-element/directives/render-if.directive.ts → dist/declarative-element/directives/render-if.directive.js} +3 -12
- package/dist/declarative-element/directives/test-id.directive.d.ts +52 -0
- package/{src/declarative-element/directives/test-id.directive.ts → dist/declarative-element/directives/test-id.directive.js} +2 -7
- package/dist/declarative-element/has-declarative-element-parent.d.ts +1 -0
- package/{src/declarative-element/has-declarative-element-parent.ts → dist/declarative-element/has-declarative-element-parent.js} +4 -7
- package/dist/declarative-element/is-declarative-element-definition.d.ts +17 -0
- package/{src/declarative-element/is-declarative-element-definition.ts → dist/declarative-element/is-declarative-element-definition.js} +11 -28
- package/dist/declarative-element/is-declarative-element.d.ts +15 -0
- package/{src/declarative-element/is-declarative-element.ts → dist/declarative-element/is-declarative-element.js} +5 -11
- package/dist/declarative-element/properties/assign-inputs.d.ts +1 -0
- package/dist/declarative-element/properties/assign-inputs.js +25 -0
- package/dist/declarative-element/properties/css-vars.d.ts +16 -0
- package/dist/declarative-element/properties/css-vars.js +1 -0
- package/dist/declarative-element/properties/element-events.d.ts +65 -0
- package/dist/declarative-element/properties/element-events.js +62 -0
- package/dist/declarative-element/properties/element-properties.js +1 -0
- package/dist/declarative-element/properties/host-classes.d.ts +36 -0
- package/dist/declarative-element/properties/host-classes.js +16 -0
- package/dist/declarative-element/properties/property-proxy.d.ts +22 -0
- package/{src/declarative-element/properties/property-proxy.ts → dist/declarative-element/properties/property-proxy.js} +21 -58
- package/dist/declarative-element/properties/string-names.d.ts +28 -0
- package/dist/declarative-element/properties/string-names.js +40 -0
- package/dist/declarative-element/properties/styles.d.ts +51 -0
- package/dist/declarative-element/properties/styles.js +41 -0
- package/dist/declarative-element/properties/tag-name.js +1 -0
- package/dist/declarative-element/render-callback.d.ts +56 -0
- package/dist/declarative-element/render-callback.js +27 -0
- package/dist/declarative-element/wrap-define-element.d.ts +36 -0
- package/dist/declarative-element/wrap-define-element.js +25 -0
- package/{src/index.ts → dist/index.d.ts} +0 -1
- package/dist/index.js +43 -0
- package/dist/lit-exports/all-lit-exports.js +2 -0
- package/{src/lit-exports/base-lit-exports.ts → dist/lit-exports/base-lit-exports.d.ts} +2 -10
- package/dist/lit-exports/base-lit-exports.js +24 -0
- package/{src/lit-exports/lit-repeat-fix.ts → dist/lit-exports/lit-repeat-fix.d.ts} +16 -45
- package/dist/lit-exports/lit-repeat-fix.js +37 -0
- package/dist/readme-examples/my-app.element.d.ts +1 -0
- package/{src/readme-examples/my-app.element.ts → dist/readme-examples/my-app.element.js} +4 -5
- package/dist/readme-examples/my-custom-action.event.d.ts +1 -0
- package/dist/readme-examples/my-custom-action.event.js +2 -0
- package/dist/readme-examples/my-custom-define.d.ts +4 -0
- package/{src/readme-examples/my-custom-define.ts → dist/readme-examples/my-custom-define.js} +4 -9
- package/dist/readme-examples/my-simple.element.d.ts +1 -0
- package/{src/readme-examples/my-simple.element.ts → dist/readme-examples/my-simple.element.js} +3 -4
- package/dist/readme-examples/my-with-assignment.element.d.ts +1 -0
- package/dist/readme-examples/my-with-assignment.element.js +15 -0
- package/dist/readme-examples/my-with-async-prop.element.d.ts +10 -0
- package/{src/readme-examples/my-with-async-prop.element.ts → dist/readme-examples/my-with-async-prop.element.js} +16 -24
- package/dist/readme-examples/my-with-cleanup-callback.element.d.ts +3 -0
- package/{src/readme-examples/my-with-cleanup-callback.element.ts → dist/readme-examples/my-with-cleanup-callback.element.js} +4 -5
- package/dist/readme-examples/my-with-css-vars.element.d.ts +1 -0
- package/{src/readme-examples/my-with-css-vars.element.ts → dist/readme-examples/my-with-css-vars.element.js} +4 -5
- package/dist/readme-examples/my-with-custom-events.element.d.ts +1 -0
- package/dist/readme-examples/my-with-custom-events.element.js +22 -0
- package/dist/readme-examples/my-with-event-listening.element.d.ts +3 -0
- package/{src/readme-examples/my-with-event-listening.element.ts → dist/readme-examples/my-with-event-listening.element.js} +9 -10
- package/dist/readme-examples/my-with-events.element.d.ts +4 -0
- package/dist/readme-examples/my-with-events.element.js +20 -0
- package/dist/readme-examples/my-with-host-class-definition.element.d.ts +3 -0
- package/{src/readme-examples/my-with-host-class-definition.element.ts → dist/readme-examples/my-with-host-class-definition.element.js} +6 -7
- package/dist/readme-examples/my-with-host-class-usage.element.d.ts +1 -0
- package/{src/readme-examples/my-with-host-class-usage.element.ts → dist/readme-examples/my-with-host-class-usage.element.js} +4 -5
- package/dist/readme-examples/my-with-inputs.element.d.ts +4 -0
- package/dist/readme-examples/my-with-inputs.element.js +9 -0
- package/dist/readme-examples/my-with-on-dom-created.element.d.ts +1 -0
- package/{src/readme-examples/my-with-on-dom-created.element.ts → dist/readme-examples/my-with-on-dom-created.element.js} +6 -7
- package/dist/readme-examples/my-with-on-resize.element.d.ts +1 -0
- package/dist/readme-examples/my-with-on-resize.element.js +18 -0
- package/dist/readme-examples/my-with-render-if.element.d.ts +3 -0
- package/dist/readme-examples/my-with-render-if.element.js +11 -0
- package/dist/readme-examples/my-with-styles-and-interpolated-selector.element.d.ts +1 -0
- package/{src/readme-examples/my-with-styles-and-interpolated-selector.element.ts → dist/readme-examples/my-with-styles-and-interpolated-selector.element.js} +5 -6
- package/dist/readme-examples/my-with-styles.element.d.ts +1 -0
- package/{src/readme-examples/my-with-styles.element.ts → dist/readme-examples/my-with-styles.element.js} +4 -5
- package/dist/readme-examples/my-with-update-state.element.d.ts +8 -0
- package/{src/readme-examples/my-with-update-state.element.ts → dist/readme-examples/my-with-update-state.element.js} +7 -8
- package/dist/readme-examples/require-declarative-element.d.ts +1 -0
- package/dist/readme-examples/require-declarative-element.js +2 -0
- package/dist/require-declarative-element.d.ts +14 -0
- package/{src/require-declarative-element.ts → dist/require-declarative-element.js} +0 -1
- package/{src/template-transforms/minimal-element-definition.ts → dist/template-transforms/minimal-element-definition.d.ts} +7 -19
- package/dist/template-transforms/minimal-element-definition.js +19 -0
- package/dist/template-transforms/nested-mapped-templates.d.ts +6 -0
- package/dist/template-transforms/nested-mapped-templates.js +96 -0
- package/{src/template-transforms/template-transform-type.ts → dist/template-transforms/template-transform-type.d.ts} +1 -3
- package/dist/template-transforms/template-transform-type.js +1 -0
- package/dist/template-transforms/transform-template.d.ts +14 -0
- package/{src/template-transforms/transform-template.ts → dist/template-transforms/transform-template.js} +22 -70
- package/dist/template-transforms/vir-css/css-transform.d.ts +4 -0
- package/dist/template-transforms/vir-css/css-transform.js +15 -0
- package/dist/template-transforms/vir-css/vir-css.d.ts +12 -0
- package/dist/template-transforms/vir-css/vir-css.js +21 -0
- package/dist/template-transforms/vir-html/html-interpolation.d.ts +42 -0
- package/dist/template-transforms/vir-html/html-interpolation.js +1 -0
- package/dist/template-transforms/vir-html/html-transform.d.ts +5 -0
- package/dist/template-transforms/vir-html/html-transform.js +96 -0
- package/dist/template-transforms/vir-html/tag-name-keys.d.ts +7 -0
- package/{src/template-transforms/vir-html/tag-name-keys.ts → dist/template-transforms/vir-html/tag-name-keys.js} +1 -1
- package/dist/template-transforms/vir-html/vir-html.d.ts +11 -0
- package/{src/template-transforms/vir-html/vir-html.ts → dist/template-transforms/vir-html/vir-html.js} +5 -13
- package/dist/typed-event/typed-event.d.ts +55 -0
- package/dist/typed-event/typed-event.js +50 -0
- package/dist/util/array.d.ts +5 -0
- package/{src/util/array.ts → dist/util/array.js} +5 -18
- package/{src/util/increment.ts → dist/util/increment.d.ts} +5 -24
- package/dist/util/increment.js +1 -0
- package/dist/util/lit-template.d.ts +9 -0
- package/{src/util/lit-template.ts → dist/util/lit-template.js} +10 -30
- package/dist/util/map-async-value.d.ts +7 -0
- package/{src/util/map-async-value.ts → dist/util/map-async-value.js} +10 -12
- package/dist/util/type.js +1 -0
- package/package.json +4 -4
- package/src/declarative-element/declarative-element-init.ts +0 -115
- package/src/declarative-element/declarative-element.ts +0 -372
- package/src/declarative-element/define-element.ts +0 -515
- package/src/declarative-element/directives/assign.directive.ts +0 -89
- package/src/declarative-element/directives/attributes.directive.ts +0 -63
- package/src/declarative-element/directives/create-attribute-directive.ts +0 -47
- package/src/declarative-element/directives/directive-helpers.ts +0 -67
- package/src/declarative-element/directives/listen.directive.ts +0 -206
- package/src/declarative-element/directives/mutate.directive.ts +0 -78
- package/src/declarative-element/directives/on-dom-created.directive.ts +0 -68
- package/src/declarative-element/directives/on-dom-rendered.directive.ts +0 -61
- package/src/declarative-element/directives/on-intersect.directive.ts +0 -139
- package/src/declarative-element/directives/on-resize.directive.ts +0 -142
- package/src/declarative-element/directives/render-async.directive.ts +0 -111
- package/src/declarative-element/properties/assign-inputs.ts +0 -30
- package/src/declarative-element/properties/css-vars.ts +0 -24
- package/src/declarative-element/properties/element-events.ts +0 -161
- package/src/declarative-element/properties/host-classes.ts +0 -63
- package/src/declarative-element/properties/string-names.ts +0 -83
- package/src/declarative-element/properties/styles.ts +0 -112
- package/src/declarative-element/render-callback.ts +0 -196
- package/src/declarative-element/wrap-define-element.ts +0 -127
- package/src/readme-examples/my-custom-action.event.ts +0 -3
- package/src/readme-examples/my-with-assignment.element.ts +0 -16
- package/src/readme-examples/my-with-custom-events.element.ts +0 -23
- package/src/readme-examples/my-with-events.element.ts +0 -23
- package/src/readme-examples/my-with-inputs.element.ts +0 -13
- package/src/readme-examples/my-with-on-resize.element.ts +0 -19
- package/src/readme-examples/my-with-render-if.element.ts +0 -15
- package/src/readme-examples/require-declarative-element.ts +0 -3
- package/src/template-transforms/nested-mapped-templates.ts +0 -157
- package/src/template-transforms/vir-css/css-transform.ts +0 -30
- package/src/template-transforms/vir-css/vir-css.ts +0 -30
- package/src/template-transforms/vir-html/html-interpolation.ts +0 -103
- package/src/template-transforms/vir-html/html-transform.ts +0 -149
- package/src/typed-event/typed-event.ts +0 -90
- /package/{src/declarative-element/custom-tag-name.ts → dist/declarative-element/custom-tag-name.d.ts} +0 -0
- /package/{src/declarative-element/properties/element-properties.ts → dist/declarative-element/properties/element-properties.d.ts} +0 -0
- /package/{src/declarative-element/properties/tag-name.ts → dist/declarative-element/properties/tag-name.d.ts} +0 -0
- /package/{src/lit-exports/all-lit-exports.ts → dist/lit-exports/all-lit-exports.d.ts} +0 -0
- /package/{src/util/type.ts → dist/util/type.d.ts} +0 -0
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import {type MaybePromise} from '@augment-vir/common';
|
|
2
|
-
import {directive, Directive, type PartInfo} from '../../lit-exports/all-lit-exports.js';
|
|
3
|
-
import {assertIsElementPartInfo} from './directive-helpers.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Callback called by the {@link onResize} directive.
|
|
7
|
-
*
|
|
8
|
-
* @category Internal
|
|
9
|
-
*/
|
|
10
|
-
export type OnResizeCallback = (
|
|
11
|
-
size: Readonly<
|
|
12
|
-
Pick<
|
|
13
|
-
ResizeObserverEntry,
|
|
14
|
-
/** Only these two properties are supported in all major modern browsers */
|
|
15
|
-
'target' | 'contentRect'
|
|
16
|
-
>
|
|
17
|
-
>,
|
|
18
|
-
element: Element,
|
|
19
|
-
) => MaybePromise<void>;
|
|
20
|
-
|
|
21
|
-
const directiveName = 'onResize';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A directive that fires its listener any time the element that it's attached to is resized. This
|
|
25
|
-
* uses the [built-in `ResizeObserver`
|
|
26
|
-
* API](https://developer.mozilla.org/docs/Web/API/ResizeObserver), so it is very efficient.
|
|
27
|
-
*
|
|
28
|
-
* @category Directives
|
|
29
|
-
* @example
|
|
30
|
-
*
|
|
31
|
-
* ```ts
|
|
32
|
-
* import {html, defineElement, onResize} from 'element-vir';
|
|
33
|
-
*
|
|
34
|
-
* const MyElement = defineElement()({
|
|
35
|
-
* tagName: 'my-element',
|
|
36
|
-
* render() {
|
|
37
|
-
* return html`
|
|
38
|
-
* <div
|
|
39
|
-
* ${onResize((size, element) => {
|
|
40
|
-
* console.log('resized!', element, size);
|
|
41
|
-
* })}
|
|
42
|
-
* >
|
|
43
|
-
* Some div
|
|
44
|
-
* </div>
|
|
45
|
-
* `;
|
|
46
|
-
* },
|
|
47
|
-
* });
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export const onResize = directive(
|
|
51
|
-
class extends Directive {
|
|
52
|
-
public element: Element | undefined;
|
|
53
|
-
public readonly resizeObserver = new ResizeObserver((entries) => {
|
|
54
|
-
if (this.element && this.callback) {
|
|
55
|
-
handleOnResizeCallback(this.element, this.callback, entries);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
public callback: OnResizeCallback | undefined;
|
|
59
|
-
|
|
60
|
-
constructor(partInfo: PartInfo) {
|
|
61
|
-
super(partInfo);
|
|
62
|
-
|
|
63
|
-
assertIsElementPartInfo(partInfo, directiveName);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public override update(partInfo: PartInfo, [callback]: [OnResizeCallback]) {
|
|
67
|
-
assertIsElementPartInfo(partInfo, directiveName);
|
|
68
|
-
this.callback = callback;
|
|
69
|
-
const newElement = partInfo.element;
|
|
70
|
-
const oldElement = this.element;
|
|
71
|
-
// if the element changes we need to observe the new one
|
|
72
|
-
if (newElement !== oldElement) {
|
|
73
|
-
this.element = newElement;
|
|
74
|
-
if (oldElement) {
|
|
75
|
-
this.resizeObserver.unobserve(oldElement);
|
|
76
|
-
}
|
|
77
|
-
this.resizeObserver.observe(newElement);
|
|
78
|
-
}
|
|
79
|
-
return this.render(callback);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
83
|
-
public render(callback: OnResizeCallback) {
|
|
84
|
-
return undefined;
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
function handleOnResizeCallback(
|
|
90
|
-
element: Element,
|
|
91
|
-
callback: OnResizeCallback,
|
|
92
|
-
entries: ResizeObserverEntry[],
|
|
93
|
-
) {
|
|
94
|
-
const resizeEntry = entries[0];
|
|
95
|
-
if (!resizeEntry) {
|
|
96
|
-
console.error(entries);
|
|
97
|
-
throw new Error(`Resize observation triggered but the first entry was empty.`);
|
|
98
|
-
}
|
|
99
|
-
void callback(
|
|
100
|
-
{
|
|
101
|
-
target: resizeEntry.target,
|
|
102
|
-
contentRect: resizeEntry.contentRect,
|
|
103
|
-
},
|
|
104
|
-
element,
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* A function that attaches a
|
|
110
|
-
* [`ResizeObserver`](https://developer.mozilla.org/docs/Web/API/ResizeObserver) to any given
|
|
111
|
-
* element, so it is very efficient.
|
|
112
|
-
*
|
|
113
|
-
* @category Directives
|
|
114
|
-
* @example
|
|
115
|
-
*
|
|
116
|
-
* ```ts
|
|
117
|
-
* import {html, defineElement, attachOnResize} from 'element-vir';
|
|
118
|
-
*
|
|
119
|
-
* const MyElement = defineElement()({
|
|
120
|
-
* tagName: 'my-element',
|
|
121
|
-
* render({host}) {
|
|
122
|
-
* attachOnResize(host, (size, element) => {
|
|
123
|
-
* console.log('resized!', element, size);
|
|
124
|
-
* });
|
|
125
|
-
*
|
|
126
|
-
* return '';
|
|
127
|
-
* },
|
|
128
|
-
* });
|
|
129
|
-
* ```
|
|
130
|
-
*/
|
|
131
|
-
export function attachOnResize(element: Element, callback: OnResizeCallback) {
|
|
132
|
-
const resizeObserver = new ResizeObserver((entries) => {
|
|
133
|
-
handleOnResizeCallback(element, callback, entries);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
resizeObserver.observe(element);
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
resizeObserver,
|
|
140
|
-
element,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import {check} from '@augment-vir/assert';
|
|
2
|
-
import {extractErrorMessage} from '@augment-vir/common';
|
|
3
|
-
import {type AsyncProp} from './async-prop.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
|
|
7
|
-
* once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
|
|
8
|
-
* parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
|
|
9
|
-
* cases.
|
|
10
|
-
*
|
|
11
|
-
* This is the overload for when `resolutionRender` and `errorRender` are both provided.
|
|
12
|
-
*
|
|
13
|
-
* @category Async
|
|
14
|
-
*/
|
|
15
|
-
export function renderAsync<
|
|
16
|
-
T,
|
|
17
|
-
FallbackResult,
|
|
18
|
-
ResolutionRenderResult = never,
|
|
19
|
-
ErrorRenderResult = never,
|
|
20
|
-
>(
|
|
21
|
-
asyncProp: Pick<AsyncProp<T, any>, 'value'>,
|
|
22
|
-
fallback: FallbackResult,
|
|
23
|
-
resolutionRender: (resolved: Awaited<T>) => ResolutionRenderResult,
|
|
24
|
-
errorRender: (error: Error) => ErrorRenderResult,
|
|
25
|
-
): FallbackResult | ResolutionRenderResult | ErrorRenderResult;
|
|
26
|
-
/**
|
|
27
|
-
* Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
|
|
28
|
-
* once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
|
|
29
|
-
* parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
|
|
30
|
-
* cases.
|
|
31
|
-
*
|
|
32
|
-
* This is the overload for when `resolutionRender` is provided but `errorRender` is not.
|
|
33
|
-
*
|
|
34
|
-
* @category Async
|
|
35
|
-
*/
|
|
36
|
-
export function renderAsync<T, FallbackResult, ResolutionRenderResult = never>(
|
|
37
|
-
asyncProp: Pick<AsyncProp<T, any>, 'value'>,
|
|
38
|
-
fallback: FallbackResult,
|
|
39
|
-
resolutionRender: (resolved: Awaited<T>) => ResolutionRenderResult,
|
|
40
|
-
errorRender?: undefined,
|
|
41
|
-
): FallbackResult | ResolutionRenderResult | string;
|
|
42
|
-
/**
|
|
43
|
-
* Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
|
|
44
|
-
* once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
|
|
45
|
-
* parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
|
|
46
|
-
* cases.
|
|
47
|
-
*
|
|
48
|
-
* This is the overload for when `resolutionRender` is not provided but `errorRender` is.
|
|
49
|
-
*
|
|
50
|
-
* @category Async
|
|
51
|
-
*/
|
|
52
|
-
export function renderAsync<T, FallbackResult, ErrorRenderResult = never>(
|
|
53
|
-
asyncProp: Pick<AsyncProp<T, any>, 'value'>,
|
|
54
|
-
fallback: FallbackResult,
|
|
55
|
-
resolutionRender: undefined,
|
|
56
|
-
errorRender: (error: Error) => ErrorRenderResult,
|
|
57
|
-
): FallbackResult | Awaited<T> | ErrorRenderResult;
|
|
58
|
-
/**
|
|
59
|
-
* Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
|
|
60
|
-
* once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
|
|
61
|
-
* parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
|
|
62
|
-
* cases.
|
|
63
|
-
*
|
|
64
|
-
* This is the overload for when neither `resolutionRender` or `errorRender` are provided.
|
|
65
|
-
*
|
|
66
|
-
* @category Async
|
|
67
|
-
*/
|
|
68
|
-
export function renderAsync<T, FallbackResult>(
|
|
69
|
-
asyncProp: Pick<AsyncProp<T, any>, 'value'>,
|
|
70
|
-
fallback: FallbackResult,
|
|
71
|
-
resolutionRender?: undefined,
|
|
72
|
-
errorRender?: undefined,
|
|
73
|
-
): FallbackResult | Awaited<T> | string;
|
|
74
|
-
/**
|
|
75
|
-
* Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
|
|
76
|
-
* once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
|
|
77
|
-
* parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
|
|
78
|
-
* cases.
|
|
79
|
-
*
|
|
80
|
-
* This is the full function definition and implementation.
|
|
81
|
-
*
|
|
82
|
-
* @category Async
|
|
83
|
-
*/
|
|
84
|
-
export function renderAsync<
|
|
85
|
-
T,
|
|
86
|
-
FallbackResult,
|
|
87
|
-
ResolutionRenderResult = never,
|
|
88
|
-
ErrorRenderResult = never,
|
|
89
|
-
>(
|
|
90
|
-
asyncProp: Pick<AsyncProp<T, any>, 'value'>,
|
|
91
|
-
/** This value will be rendered if the async prop has not settled yet. */
|
|
92
|
-
fallback: FallbackResult,
|
|
93
|
-
resolutionRender?: ((resolved: Awaited<T>) => ResolutionRenderResult) | undefined,
|
|
94
|
-
errorRender?: ((error: Error) => ErrorRenderResult) | undefined,
|
|
95
|
-
): FallbackResult | Awaited<T> | ResolutionRenderResult | string | ErrorRenderResult {
|
|
96
|
-
const asyncPropValue = asyncProp.value;
|
|
97
|
-
if (asyncPropValue instanceof Error) {
|
|
98
|
-
const errorResult: string | ErrorRenderResult = errorRender
|
|
99
|
-
? errorRender(asyncPropValue)
|
|
100
|
-
: extractErrorMessage(asyncPropValue);
|
|
101
|
-
return errorResult as any;
|
|
102
|
-
} else if (check.isPromiseLike(asyncPropValue)) {
|
|
103
|
-
const fallbackResult: FallbackResult = fallback;
|
|
104
|
-
return fallbackResult as any;
|
|
105
|
-
} else {
|
|
106
|
-
const resolutionResult: ResolutionRenderResult | Awaited<T> = resolutionRender
|
|
107
|
-
? resolutionRender(asyncPropValue)
|
|
108
|
-
: asyncPropValue;
|
|
109
|
-
return resolutionResult as any;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {getObjectTypedKeys} from '@augment-vir/common';
|
|
2
|
-
import {type DeclarativeElement} from '../declarative-element.js';
|
|
3
|
-
|
|
4
|
-
export function assignInputs(element: Element, inputs: object): void {
|
|
5
|
-
const instanceState = (element as Partial<DeclarativeElement>).instanceState;
|
|
6
|
-
|
|
7
|
-
getObjectTypedKeys(inputs).forEach((newInputKey) => {
|
|
8
|
-
if (instanceState && newInputKey in instanceState) {
|
|
9
|
-
throw new Error(
|
|
10
|
-
`Cannot set input '${String(newInputKey)}' on '${element.tagName}'. '${element.tagName}' already has a state property with the same name.`,
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
if ('instanceInputs' in element) {
|
|
14
|
-
(element.instanceInputs as DeclarativeElement['instanceInputs'])[newInputKey] =
|
|
15
|
-
inputs[newInputKey];
|
|
16
|
-
} else {
|
|
17
|
-
element[newInputKey] = inputs[newInputKey];
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
/** Wipe out all inputs that weren't set to undefined (as expected) */
|
|
22
|
-
if ('instanceInputs' in element) {
|
|
23
|
-
getObjectTypedKeys(element.instanceInputs).forEach((existingKey) => {
|
|
24
|
-
if (!(existingKey in inputs)) {
|
|
25
|
-
(element.instanceInputs as DeclarativeElement['instanceInputs'])[existingKey] =
|
|
26
|
-
undefined;
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import {type Values} from '@augment-vir/common';
|
|
2
|
-
import {type CssVarDefinitions, type CssVarsSetup} from 'lit-css-vars';
|
|
3
|
-
import {type CustomElementTagName} from '../custom-tag-name.js';
|
|
4
|
-
import {type BaseStringName} from './string-names.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Base type for a declarative element definition's CSS vars.
|
|
8
|
-
*
|
|
9
|
-
* @category Internal
|
|
10
|
-
*/
|
|
11
|
-
export type CssVarsInitMap<
|
|
12
|
-
ElementTagName extends CustomElementTagName,
|
|
13
|
-
CssVarKeys extends BaseStringName<ElementTagName>,
|
|
14
|
-
> = Readonly<Record<CssVarKeys, Values<CssVarsSetup>>>;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* CSS vars ready for use within a declarative element's `render` or `styles` method.
|
|
18
|
-
*
|
|
19
|
-
* @category Internal
|
|
20
|
-
*/
|
|
21
|
-
export type CssVars<
|
|
22
|
-
ElementTagName extends CustomElementTagName,
|
|
23
|
-
CssVarKeys extends BaseStringName<ElementTagName>,
|
|
24
|
-
> = CssVarDefinitions<CssVarsInitMap<ElementTagName, CssVarKeys>>;
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DefinedTypedEvent,
|
|
3
|
-
type DefineEvent,
|
|
4
|
-
defineTypedEvent,
|
|
5
|
-
type TypedEvent,
|
|
6
|
-
} from '../../typed-event/typed-event.js';
|
|
7
|
-
import {type NonEmptyString} from '../../util/type.js';
|
|
8
|
-
import {type CustomElementTagName} from '../custom-tag-name.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Base type for defining element events inside of an element definition.
|
|
12
|
-
*
|
|
13
|
-
* @category Internal
|
|
14
|
-
*/
|
|
15
|
-
export type EventsInitMap = Record<string, DefineEvent<any>>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Used to define element events, with a type.
|
|
19
|
-
*
|
|
20
|
-
* @category Element Definition
|
|
21
|
-
* @example
|
|
22
|
-
*
|
|
23
|
-
* ```ts
|
|
24
|
-
* import {html, defineElement, defineElementEvent, listen} from 'element-vir';
|
|
25
|
-
*
|
|
26
|
-
* const MyElement = defineElement()({
|
|
27
|
-
* tagName: 'my-element',
|
|
28
|
-
* events: {
|
|
29
|
-
* myOutput: defineElementEvent<number>(),
|
|
30
|
-
* },
|
|
31
|
-
* render({events, dispatch}) {
|
|
32
|
-
* return html`
|
|
33
|
-
* <div
|
|
34
|
-
* ${listen('click', () => {
|
|
35
|
-
* dispatch(new events.myOutput(1));
|
|
36
|
-
* })}
|
|
37
|
-
* >
|
|
38
|
-
* Some div
|
|
39
|
-
* </div>
|
|
40
|
-
* `;
|
|
41
|
-
* },
|
|
42
|
-
* });
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export function defineElementEvent<EventDetailGeneric>(): DefineEvent<EventDetailGeneric> {
|
|
46
|
-
return defineTypedEvent<EventDetailGeneric>();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Extracts the detail type of the given event name from the given event init map.
|
|
51
|
-
*
|
|
52
|
-
* @category Internal
|
|
53
|
-
*/
|
|
54
|
-
export type EventInitMapEventDetailExtractor<
|
|
55
|
-
EventTypeNameGeneric extends keyof EventsInitGeneric,
|
|
56
|
-
EventsInitGeneric extends EventsInitMap,
|
|
57
|
-
> = EventsInitGeneric[EventTypeNameGeneric] extends DefineEvent<infer Detail> ? Detail : never;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Maps the given element tag name and map of event names to their run-time event type strings.
|
|
61
|
-
*
|
|
62
|
-
* @category Internal
|
|
63
|
-
*/
|
|
64
|
-
export type EventDescriptorMap<
|
|
65
|
-
TagName extends CustomElementTagName,
|
|
66
|
-
EventsInitGeneric extends EventsInitMap,
|
|
67
|
-
> = {
|
|
68
|
-
[CurrentEventTypeName in keyof EventsInitGeneric]: DefinedTypedEvent<
|
|
69
|
-
CurrentEventTypeName extends string ? `${TagName}-${CurrentEventTypeName}` : never,
|
|
70
|
-
EventInitMapEventDetailExtractor<CurrentEventTypeName, EventsInitGeneric>
|
|
71
|
-
>;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Extract the event detail type from a {@link TypedEvent}, {@link DefinedTypedEvent}, or
|
|
76
|
-
* `CustomEvent.
|
|
77
|
-
*
|
|
78
|
-
* @category Internal
|
|
79
|
-
*/
|
|
80
|
-
export type EventDetail<
|
|
81
|
-
ElementEvent extends TypedEvent<any, any> | DefinedTypedEvent<any, any> | CustomEvent,
|
|
82
|
-
> =
|
|
83
|
-
ElementEvent extends TypedEvent<string, infer Detail>
|
|
84
|
-
? Detail
|
|
85
|
-
: ElementEvent extends DefinedTypedEvent<any, infer Detail>
|
|
86
|
-
? Detail
|
|
87
|
-
: ElementEvent extends CustomEvent<infer Detail>
|
|
88
|
-
? Detail
|
|
89
|
-
: 'TYPE ERROR: failed to extract event detail type';
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Maps an element definition initialization's tag name and event map to a map of ready-to-construct
|
|
93
|
-
* event classes. This also verifies that the input event init map has valid keys.
|
|
94
|
-
*
|
|
95
|
-
* @category Internal
|
|
96
|
-
*/
|
|
97
|
-
export function createEventDescriptorMap<
|
|
98
|
-
TagName extends CustomElementTagName,
|
|
99
|
-
EventsInitGeneric extends EventsInitMap,
|
|
100
|
-
>(
|
|
101
|
-
tagName: CustomElementTagName,
|
|
102
|
-
eventsInit: EventsInitGeneric | undefined,
|
|
103
|
-
): EventDescriptorMap<TagName, EventsInitGeneric> {
|
|
104
|
-
if (!eventsInit) {
|
|
105
|
-
return {} as EventDescriptorMap<TagName, EventsInitGeneric>;
|
|
106
|
-
}
|
|
107
|
-
return (
|
|
108
|
-
Object.keys(eventsInit)
|
|
109
|
-
/** Verify event keys. */
|
|
110
|
-
.filter(
|
|
111
|
-
(
|
|
112
|
-
currentElementEventKey: keyof EventsInitGeneric,
|
|
113
|
-
): currentElementEventKey is NonEmptyString<keyof EventsInitGeneric> => {
|
|
114
|
-
if (typeof currentElementEventKey !== 'string') {
|
|
115
|
-
throw new TypeError(
|
|
116
|
-
`Expected event key of type string but got type '${typeof currentElementEventKey}' for key ${String(
|
|
117
|
-
currentElementEventKey,
|
|
118
|
-
)}`,
|
|
119
|
-
);
|
|
120
|
-
} else if (currentElementEventKey === '') {
|
|
121
|
-
throw new Error(`Got empty string for events key.`);
|
|
122
|
-
}
|
|
123
|
-
return true;
|
|
124
|
-
},
|
|
125
|
-
)
|
|
126
|
-
.reduce(
|
|
127
|
-
(
|
|
128
|
-
accum: EventDescriptorMap<TagName, EventsInitGeneric>,
|
|
129
|
-
currentElementEventKey: keyof EventsInitGeneric,
|
|
130
|
-
): EventDescriptorMap<TagName, EventsInitGeneric> => {
|
|
131
|
-
const eventObject: DefinedTypedEvent<
|
|
132
|
-
typeof currentElementEventKey extends string
|
|
133
|
-
? typeof currentElementEventKey
|
|
134
|
-
: never,
|
|
135
|
-
EventInitMapEventDetailExtractor<
|
|
136
|
-
typeof currentElementEventKey,
|
|
137
|
-
EventsInitGeneric
|
|
138
|
-
>
|
|
139
|
-
> = defineTypedEvent<
|
|
140
|
-
EventInitMapEventDetailExtractor<
|
|
141
|
-
typeof currentElementEventKey,
|
|
142
|
-
EventsInitGeneric
|
|
143
|
-
>
|
|
144
|
-
>()<
|
|
145
|
-
typeof currentElementEventKey extends string
|
|
146
|
-
? typeof currentElementEventKey
|
|
147
|
-
: never
|
|
148
|
-
>(
|
|
149
|
-
[
|
|
150
|
-
tagName,
|
|
151
|
-
currentElementEventKey,
|
|
152
|
-
].join('-') as any,
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
accum[currentElementEventKey] = eventObject as any;
|
|
156
|
-
return accum;
|
|
157
|
-
},
|
|
158
|
-
{} as EventDescriptorMap<TagName, EventsInitGeneric>,
|
|
159
|
-
)
|
|
160
|
-
);
|
|
161
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import {mapObjectValues} from '@augment-vir/common';
|
|
2
|
-
import {type CustomElementTagName} from '../custom-tag-name.js';
|
|
3
|
-
import {type PropertyInitMapBase} from './element-properties.js';
|
|
4
|
-
import {type BaseStringName} from './string-names.js';
|
|
5
|
-
import {type WithTagName} from './tag-name.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Base init map for defining host classes in an element definition.
|
|
9
|
-
*
|
|
10
|
-
* @category Internal
|
|
11
|
-
*/
|
|
12
|
-
export type HostClassesInitMap<
|
|
13
|
-
TagName extends CustomElementTagName,
|
|
14
|
-
HostClassKeys extends BaseStringName<TagName>,
|
|
15
|
-
Inputs extends PropertyInitMapBase,
|
|
16
|
-
State extends PropertyInitMapBase,
|
|
17
|
-
> = Record<
|
|
18
|
-
HostClassKeys,
|
|
19
|
-
/**
|
|
20
|
-
* Callback to determine when host class should be enabled (based on current inputs and state),
|
|
21
|
-
* or just undefined to mark that this host class name will only be manually applied.
|
|
22
|
-
*/
|
|
23
|
-
((inputs: {state: Readonly<State>; inputs: Readonly<Inputs>}) => boolean) | false
|
|
24
|
-
>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Creates a mapping of host class keys (as defined in an element definition) to their runtime host
|
|
28
|
-
* class names.
|
|
29
|
-
*
|
|
30
|
-
* @category Internal
|
|
31
|
-
*/
|
|
32
|
-
export type HostClassNamesMap<TagName extends string, HostClassKeys extends string> = Record<
|
|
33
|
-
HostClassKeys,
|
|
34
|
-
WithTagName<TagName, string>
|
|
35
|
-
>;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Maps element definition host class definitions to their runtime host class name equivalents.
|
|
39
|
-
*
|
|
40
|
-
* @category Internal
|
|
41
|
-
*/
|
|
42
|
-
export function createHostClassNamesMap<
|
|
43
|
-
TagName extends CustomElementTagName,
|
|
44
|
-
HostClassKeys extends BaseStringName<TagName>,
|
|
45
|
-
HostClassesInit extends HostClassesInitMap<
|
|
46
|
-
TagName,
|
|
47
|
-
HostClassKeys,
|
|
48
|
-
/**
|
|
49
|
-
* We can use any here because we don't care what the state or input names are, we just care
|
|
50
|
-
* what the host class names are
|
|
51
|
-
*/
|
|
52
|
-
any,
|
|
53
|
-
any
|
|
54
|
-
>,
|
|
55
|
-
>(hostClassesInit?: HostClassesInit): HostClassNamesMap<TagName, HostClassKeys> {
|
|
56
|
-
if (hostClassesInit) {
|
|
57
|
-
return mapObjectValues(hostClassesInit, (key) => {
|
|
58
|
-
return key;
|
|
59
|
-
}) as HostClassNamesMap<TagName, HostClassKeys>;
|
|
60
|
-
} else {
|
|
61
|
-
return {} as HostClassNamesMap<TagName, HostClassKeys>;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import {arrayToObject, type ArrayElement} from '@augment-vir/common';
|
|
2
|
-
import {type CustomElementTagName} from '../custom-tag-name.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Base requirement for all string names with the element tag name prepended.
|
|
6
|
-
*
|
|
7
|
-
* @category Internal
|
|
8
|
-
*/
|
|
9
|
-
export type BaseStringName<ElementTagName extends CustomElementTagName> =
|
|
10
|
-
`${ElementTagName}-${string}`;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Asserts that all the given string names for the given element are valid.
|
|
14
|
-
*
|
|
15
|
-
* @category Internal
|
|
16
|
-
*/
|
|
17
|
-
export function assertValidStringNames(
|
|
18
|
-
elementTagName: CustomElementTagName,
|
|
19
|
-
stringNames: Record<BaseStringName<CustomElementTagName>, any>,
|
|
20
|
-
): void {
|
|
21
|
-
const requiredNameStart = [
|
|
22
|
-
elementTagName,
|
|
23
|
-
'-',
|
|
24
|
-
].join('');
|
|
25
|
-
|
|
26
|
-
Object.keys(stringNames).forEach((stringName) => {
|
|
27
|
-
if (!stringName.startsWith(requiredNameStart)) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`Invalid element string name '${stringName}' in '${elementTagName}': element string names must begin with the element's tag name.`,
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Type safe mapping of string names to themself with the element tag name inserted.
|
|
37
|
-
*
|
|
38
|
-
* @category Internal
|
|
39
|
-
*/
|
|
40
|
-
export type StringNameMap<
|
|
41
|
-
ElementTagName extends CustomElementTagName,
|
|
42
|
-
NameType extends string,
|
|
43
|
-
StringNames extends ReadonlyArray<string>,
|
|
44
|
-
> = Readonly<{
|
|
45
|
-
[StringName in ArrayElement<StringNames>]: `${ElementTagName}-${NameType}-${StringName}`;
|
|
46
|
-
}>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Converts an array of string names into a `StringNameMap`.
|
|
50
|
-
*
|
|
51
|
-
* @category Internal
|
|
52
|
-
*/
|
|
53
|
-
export function createStringNameMap<
|
|
54
|
-
ElementTagName extends CustomElementTagName,
|
|
55
|
-
NameType extends string,
|
|
56
|
-
StringNames extends ReadonlyArray<string>,
|
|
57
|
-
>(
|
|
58
|
-
elementTagName: ElementTagName,
|
|
59
|
-
nameType: NameType,
|
|
60
|
-
stringNames: StringNames | undefined,
|
|
61
|
-
): StringNameMap<ElementTagName, NameType, StringNames> {
|
|
62
|
-
if (!stringNames) {
|
|
63
|
-
return {} as StringNameMap<ElementTagName, NameType, StringNames>;
|
|
64
|
-
}
|
|
65
|
-
const stringNameMap: Record<string, string> = arrayToObject(
|
|
66
|
-
stringNames,
|
|
67
|
-
(stringName) => {
|
|
68
|
-
return {
|
|
69
|
-
key: stringName,
|
|
70
|
-
value: [
|
|
71
|
-
elementTagName,
|
|
72
|
-
nameType,
|
|
73
|
-
stringName,
|
|
74
|
-
].join('-'),
|
|
75
|
-
};
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
useRequired: true,
|
|
79
|
-
},
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
return stringNameMap as StringNameMap<ElementTagName, NameType, StringNames>;
|
|
83
|
-
}
|