element-vir 26.11.2 → 26.12.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.
Files changed (188) hide show
  1. package/package.json +12 -12
  2. package/src/declarative-element/declarative-element-init.ts +115 -0
  3. package/src/declarative-element/declarative-element.ts +372 -0
  4. package/src/declarative-element/define-element.ts +515 -0
  5. package/{dist/declarative-element/definition-options.d.ts → src/declarative-element/definition-options.ts} +7 -2
  6. package/src/declarative-element/directives/assign.directive.ts +89 -0
  7. package/{dist/declarative-element/directives/async-prop.js → src/declarative-element/directives/async-prop.ts} +42 -8
  8. package/src/declarative-element/directives/attributes.directive.ts +63 -0
  9. package/src/declarative-element/directives/create-attribute-directive.ts +47 -0
  10. package/src/declarative-element/directives/directive-helpers.ts +67 -0
  11. package/{dist/declarative-element/directives/listen-to-activate.js → src/declarative-element/directives/listen-to-activate.ts} +8 -3
  12. package/src/declarative-element/directives/listen.directive.ts +206 -0
  13. package/src/declarative-element/directives/mutate.directive.ts +78 -0
  14. package/src/declarative-element/directives/on-dom-created.directive.ts +68 -0
  15. package/src/declarative-element/directives/on-dom-rendered.directive.ts +61 -0
  16. package/src/declarative-element/directives/on-intersect.directive.ts +139 -0
  17. package/src/declarative-element/directives/on-resize.directive.ts +142 -0
  18. package/src/declarative-element/directives/render-async.directive.ts +111 -0
  19. package/{dist/declarative-element/directives/render-if.directive.js → src/declarative-element/directives/render-if.directive.ts} +12 -3
  20. package/{dist/declarative-element/directives/test-id.directive.js → src/declarative-element/directives/test-id.directive.ts} +7 -2
  21. package/{dist/declarative-element/has-declarative-element-parent.js → src/declarative-element/has-declarative-element-parent.ts} +7 -4
  22. package/{dist/declarative-element/is-declarative-element-definition.js → src/declarative-element/is-declarative-element-definition.ts} +28 -11
  23. package/{dist/declarative-element/is-declarative-element.js → src/declarative-element/is-declarative-element.ts} +11 -5
  24. package/src/declarative-element/properties/assign-inputs.ts +30 -0
  25. package/src/declarative-element/properties/css-vars.ts +24 -0
  26. package/src/declarative-element/properties/element-events.ts +161 -0
  27. package/src/declarative-element/properties/host-classes.ts +63 -0
  28. package/{dist/declarative-element/properties/property-proxy.js → src/declarative-element/properties/property-proxy.ts} +58 -21
  29. package/src/declarative-element/properties/string-names.ts +83 -0
  30. package/src/declarative-element/properties/styles.ts +112 -0
  31. package/src/declarative-element/render-callback.ts +196 -0
  32. package/src/declarative-element/wrap-define-element.ts +127 -0
  33. package/{dist/index.d.ts → src/index.ts} +2 -0
  34. package/{dist/lit-exports/base-lit-exports.js → src/lit-exports/base-lit-exports.ts} +10 -1
  35. package/{dist/lit-exports/lit-repeat-fix.d.ts → src/lit-exports/lit-repeat-fix.ts} +45 -16
  36. package/{dist/readme-examples/my-app.element.js → src/readme-examples/my-app.element.ts} +5 -4
  37. package/src/readme-examples/my-custom-action.event.ts +3 -0
  38. package/{dist/readme-examples/my-custom-define.js → src/readme-examples/my-custom-define.ts} +9 -4
  39. package/{dist/readme-examples/my-simple.element.js → src/readme-examples/my-simple.element.ts} +4 -3
  40. package/src/readme-examples/my-with-assignment.element.ts +16 -0
  41. package/{dist/readme-examples/my-with-async-prop.element.js → src/readme-examples/my-with-async-prop.element.ts} +24 -16
  42. package/{dist/readme-examples/my-with-cleanup-callback.element.js → src/readme-examples/my-with-cleanup-callback.element.ts} +5 -4
  43. package/{dist/readme-examples/my-with-css-vars.element.js → src/readme-examples/my-with-css-vars.element.ts} +5 -4
  44. package/src/readme-examples/my-with-custom-events.element.ts +23 -0
  45. package/{dist/readme-examples/my-with-event-listening.element.js → src/readme-examples/my-with-event-listening.element.ts} +10 -9
  46. package/src/readme-examples/my-with-events.element.ts +23 -0
  47. package/{dist/readme-examples/my-with-host-class-definition.element.js → src/readme-examples/my-with-host-class-definition.element.ts} +7 -6
  48. package/{dist/readme-examples/my-with-host-class-usage.element.js → src/readme-examples/my-with-host-class-usage.element.ts} +5 -4
  49. package/src/readme-examples/my-with-inputs.element.ts +13 -0
  50. package/{dist/readme-examples/my-with-on-dom-created.element.js → src/readme-examples/my-with-on-dom-created.element.ts} +7 -6
  51. package/src/readme-examples/my-with-on-resize.element.ts +19 -0
  52. package/src/readme-examples/my-with-render-if.element.ts +15 -0
  53. package/{dist/readme-examples/my-with-styles-and-interpolated-selector.element.js → src/readme-examples/my-with-styles-and-interpolated-selector.element.ts} +6 -5
  54. package/{dist/readme-examples/my-with-styles.element.js → src/readme-examples/my-with-styles.element.ts} +5 -4
  55. package/{dist/readme-examples/my-with-update-state.element.js → src/readme-examples/my-with-update-state.element.ts} +8 -7
  56. package/src/readme-examples/require-declarative-element.ts +3 -0
  57. package/{dist/require-declarative-element.js → src/require-declarative-element.ts} +1 -0
  58. package/{dist/template-transforms/minimal-element-definition.d.ts → src/template-transforms/minimal-element-definition.ts} +19 -7
  59. package/src/template-transforms/nested-mapped-templates.ts +157 -0
  60. package/{dist/template-transforms/template-transform-type.d.ts → src/template-transforms/template-transform-type.ts} +3 -1
  61. package/{dist/template-transforms/transform-template.js → src/template-transforms/transform-template.ts} +70 -22
  62. package/src/template-transforms/vir-css/css-transform.ts +30 -0
  63. package/src/template-transforms/vir-css/vir-css.ts +30 -0
  64. package/src/template-transforms/vir-html/html-interpolation.ts +103 -0
  65. package/src/template-transforms/vir-html/html-transform.ts +149 -0
  66. package/{dist/template-transforms/vir-html/tag-name-keys.js → src/template-transforms/vir-html/tag-name-keys.ts} +1 -1
  67. package/{dist/template-transforms/vir-html/vir-html.js → src/template-transforms/vir-html/vir-html.ts} +13 -5
  68. package/src/typed-event/typed-event.ts +90 -0
  69. package/{dist/util/array.js → src/util/array.ts} +18 -5
  70. package/{dist/util/increment.d.ts → src/util/increment.ts} +24 -5
  71. package/{dist/util/lit-template.js → src/util/lit-template.ts} +30 -10
  72. package/src/util/map-async-value.ts +33 -0
  73. package/dist/declarative-element/custom-tag-name.js +0 -1
  74. package/dist/declarative-element/declarative-element-init.d.ts +0 -56
  75. package/dist/declarative-element/declarative-element-init.js +0 -1
  76. package/dist/declarative-element/declarative-element.d.ts +0 -114
  77. package/dist/declarative-element/declarative-element.js +0 -36
  78. package/dist/declarative-element/define-element.d.ts +0 -41
  79. package/dist/declarative-element/define-element.js +0 -248
  80. package/dist/declarative-element/definition-options.js +0 -9
  81. package/dist/declarative-element/directives/assign.directive.d.ts +0 -24
  82. package/dist/declarative-element/directives/assign.directive.js +0 -34
  83. package/dist/declarative-element/directives/async-prop.d.ts +0 -61
  84. package/dist/declarative-element/directives/attributes.directive.d.ts +0 -30
  85. package/dist/declarative-element/directives/attributes.directive.js +0 -35
  86. package/dist/declarative-element/directives/create-attribute-directive.d.ts +0 -28
  87. package/dist/declarative-element/directives/create-attribute-directive.js +0 -41
  88. package/dist/declarative-element/directives/directive-helpers.d.ts +0 -27
  89. package/dist/declarative-element/directives/directive-helpers.js +0 -37
  90. package/dist/declarative-element/directives/listen-to-activate.d.ts +0 -15
  91. package/dist/declarative-element/directives/listen.directive.d.ts +0 -92
  92. package/dist/declarative-element/directives/listen.directive.js +0 -48
  93. package/dist/declarative-element/directives/mutate.directive.d.ts +0 -38
  94. package/dist/declarative-element/directives/mutate.directive.js +0 -45
  95. package/dist/declarative-element/directives/on-dom-created.directive.d.ts +0 -44
  96. package/dist/declarative-element/directives/on-dom-created.directive.js +0 -51
  97. package/dist/declarative-element/directives/on-dom-rendered.directive.d.ts +0 -41
  98. package/dist/declarative-element/directives/on-dom-rendered.directive.js +0 -45
  99. package/dist/declarative-element/directives/on-intersect.directive.d.ts +0 -64
  100. package/dist/declarative-element/directives/on-intersect.directive.js +0 -89
  101. package/dist/declarative-element/directives/on-resize.directive.d.ts +0 -74
  102. package/dist/declarative-element/directives/on-resize.directive.js +0 -106
  103. package/dist/declarative-element/directives/render-async.directive.d.ts +0 -45
  104. package/dist/declarative-element/directives/render-async.directive.js +0 -33
  105. package/dist/declarative-element/directives/render-if.directive.d.ts +0 -32
  106. package/dist/declarative-element/directives/test-id.directive.d.ts +0 -52
  107. package/dist/declarative-element/has-declarative-element-parent.d.ts +0 -1
  108. package/dist/declarative-element/is-declarative-element-definition.d.ts +0 -17
  109. package/dist/declarative-element/is-declarative-element.d.ts +0 -15
  110. package/dist/declarative-element/properties/assign-inputs.d.ts +0 -1
  111. package/dist/declarative-element/properties/assign-inputs.js +0 -25
  112. package/dist/declarative-element/properties/css-vars.d.ts +0 -16
  113. package/dist/declarative-element/properties/css-vars.js +0 -1
  114. package/dist/declarative-element/properties/element-events.d.ts +0 -65
  115. package/dist/declarative-element/properties/element-events.js +0 -62
  116. package/dist/declarative-element/properties/element-properties.js +0 -1
  117. package/dist/declarative-element/properties/host-classes.d.ts +0 -36
  118. package/dist/declarative-element/properties/host-classes.js +0 -16
  119. package/dist/declarative-element/properties/property-proxy.d.ts +0 -22
  120. package/dist/declarative-element/properties/string-names.d.ts +0 -28
  121. package/dist/declarative-element/properties/string-names.js +0 -40
  122. package/dist/declarative-element/properties/styles.d.ts +0 -51
  123. package/dist/declarative-element/properties/styles.js +0 -41
  124. package/dist/declarative-element/properties/tag-name.js +0 -1
  125. package/dist/declarative-element/render-callback.d.ts +0 -56
  126. package/dist/declarative-element/render-callback.js +0 -27
  127. package/dist/declarative-element/wrap-define-element.d.ts +0 -36
  128. package/dist/declarative-element/wrap-define-element.js +0 -25
  129. package/dist/index.js +0 -42
  130. package/dist/lit-exports/all-lit-exports.js +0 -2
  131. package/dist/lit-exports/base-lit-exports.d.ts +0 -25
  132. package/dist/lit-exports/lit-repeat-fix.js +0 -37
  133. package/dist/readme-examples/my-app.element.d.ts +0 -1
  134. package/dist/readme-examples/my-custom-action.event.d.ts +0 -1
  135. package/dist/readme-examples/my-custom-action.event.js +0 -2
  136. package/dist/readme-examples/my-custom-define.d.ts +0 -4
  137. package/dist/readme-examples/my-simple.element.d.ts +0 -1
  138. package/dist/readme-examples/my-with-assignment.element.d.ts +0 -1
  139. package/dist/readme-examples/my-with-assignment.element.js +0 -15
  140. package/dist/readme-examples/my-with-async-prop.element.d.ts +0 -10
  141. package/dist/readme-examples/my-with-cleanup-callback.element.d.ts +0 -3
  142. package/dist/readme-examples/my-with-css-vars.element.d.ts +0 -1
  143. package/dist/readme-examples/my-with-custom-events.element.d.ts +0 -1
  144. package/dist/readme-examples/my-with-custom-events.element.js +0 -22
  145. package/dist/readme-examples/my-with-event-listening.element.d.ts +0 -3
  146. package/dist/readme-examples/my-with-events.element.d.ts +0 -4
  147. package/dist/readme-examples/my-with-events.element.js +0 -20
  148. package/dist/readme-examples/my-with-host-class-definition.element.d.ts +0 -3
  149. package/dist/readme-examples/my-with-host-class-usage.element.d.ts +0 -1
  150. package/dist/readme-examples/my-with-inputs.element.d.ts +0 -4
  151. package/dist/readme-examples/my-with-inputs.element.js +0 -9
  152. package/dist/readme-examples/my-with-on-dom-created.element.d.ts +0 -1
  153. package/dist/readme-examples/my-with-on-resize.element.d.ts +0 -1
  154. package/dist/readme-examples/my-with-on-resize.element.js +0 -18
  155. package/dist/readme-examples/my-with-render-if.element.d.ts +0 -3
  156. package/dist/readme-examples/my-with-render-if.element.js +0 -11
  157. package/dist/readme-examples/my-with-styles-and-interpolated-selector.element.d.ts +0 -1
  158. package/dist/readme-examples/my-with-styles.element.d.ts +0 -1
  159. package/dist/readme-examples/my-with-update-state.element.d.ts +0 -8
  160. package/dist/readme-examples/require-declarative-element.d.ts +0 -1
  161. package/dist/readme-examples/require-declarative-element.js +0 -2
  162. package/dist/require-declarative-element.d.ts +0 -14
  163. package/dist/template-transforms/minimal-element-definition.js +0 -19
  164. package/dist/template-transforms/nested-mapped-templates.d.ts +0 -6
  165. package/dist/template-transforms/nested-mapped-templates.js +0 -96
  166. package/dist/template-transforms/template-transform-type.js +0 -1
  167. package/dist/template-transforms/transform-template.d.ts +0 -14
  168. package/dist/template-transforms/vir-css/css-transform.d.ts +0 -4
  169. package/dist/template-transforms/vir-css/css-transform.js +0 -15
  170. package/dist/template-transforms/vir-css/vir-css.d.ts +0 -12
  171. package/dist/template-transforms/vir-css/vir-css.js +0 -21
  172. package/dist/template-transforms/vir-html/html-interpolation.d.ts +0 -42
  173. package/dist/template-transforms/vir-html/html-interpolation.js +0 -1
  174. package/dist/template-transforms/vir-html/html-transform.d.ts +0 -5
  175. package/dist/template-transforms/vir-html/html-transform.js +0 -96
  176. package/dist/template-transforms/vir-html/tag-name-keys.d.ts +0 -7
  177. package/dist/template-transforms/vir-html/vir-html.d.ts +0 -11
  178. package/dist/typed-event/typed-event.d.ts +0 -55
  179. package/dist/typed-event/typed-event.js +0 -50
  180. package/dist/util/array.d.ts +0 -5
  181. package/dist/util/increment.js +0 -1
  182. package/dist/util/lit-template.d.ts +0 -9
  183. package/dist/util/type.js +0 -1
  184. /package/{dist/declarative-element/custom-tag-name.d.ts → src/declarative-element/custom-tag-name.ts} +0 -0
  185. /package/{dist/declarative-element/properties/element-properties.d.ts → src/declarative-element/properties/element-properties.ts} +0 -0
  186. /package/{dist/declarative-element/properties/tag-name.d.ts → src/declarative-element/properties/tag-name.ts} +0 -0
  187. /package/{dist/lit-exports/all-lit-exports.d.ts → src/lit-exports/all-lit-exports.ts} +0 -0
  188. /package/{dist/util/type.d.ts → src/util/type.ts} +0 -0
@@ -1,64 +0,0 @@
1
- import { type MaybePromise } from '@augment-vir/common';
2
- import { type PartInfo } from '../../lit-exports/all-lit-exports.js';
3
- /**
4
- * Callback called by the {@link onIntersect} directive.
5
- *
6
- * @category Internal
7
- */
8
- export type OnIntersectCallback = (params: {
9
- entry: IntersectionObserverEntry;
10
- allEntries: IntersectionObserverEntry[];
11
- observer: IntersectionObserver;
12
- element: Element;
13
- }) => MaybePromise<void>;
14
- /**
15
- * Options used for the {@link onIntersect} directive.
16
- *
17
- * @category Internal
18
- */
19
- export type OnIntersectOptions = IntersectionObserverInit;
20
- /**
21
- * A directive that fires its listener any time the element's configured "intersection" is crossed.
22
- * This is commonly use for detecting when an element has scrolled into view. This uses the
23
- * [built-in `IntersectionObserver`
24
- * API](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/IntersectionObserver), so it
25
- * is very efficient.
26
- *
27
- * @category Directives
28
- * @example
29
- *
30
- * ```ts
31
- * import {html, defineElement, onIntersect} from 'element-vir';
32
- *
33
- * const MyElement = defineElement()({
34
- * tagName: 'my-element',
35
- * render() {
36
- * return html`
37
- * <div
38
- * ${onIntersect({threshold: 1}, ({element, entry}) => {
39
- * if (entry.isIntersecting) {
40
- * console.log('is intersecting!');
41
- * } else {
42
- * console.log('is not intersecting');
43
- * }
44
- * })}
45
- * >
46
- * Some div
47
- * </div>
48
- * `;
49
- * },
50
- * });
51
- * ```
52
- */
53
- export declare const onIntersect: (options: IntersectionObserverInit, callback: OnIntersectCallback) => import("lit-html/directive.js").DirectiveResult<{
54
- new (partInfo: PartInfo): {
55
- element: Element | undefined;
56
- options: OnIntersectOptions | undefined;
57
- intersectionObserver: undefined | IntersectionObserver;
58
- callback: OnIntersectCallback | undefined;
59
- fireCallback(entries: IntersectionObserverEntry[], observer: IntersectionObserver): void;
60
- update(partInfo: PartInfo, [options, callback,]: [OnIntersectOptions, OnIntersectCallback]): undefined;
61
- render(options: OnIntersectOptions, callback: OnIntersectCallback): undefined;
62
- get _$isConnected(): boolean;
63
- };
64
- }>;
@@ -1,89 +0,0 @@
1
- import { assert, assertWrap, check } from '@augment-vir/assert';
2
- import { directive, Directive } from '../../lit-exports/all-lit-exports.js';
3
- import { assertIsElementPartInfo } from './directive-helpers.js';
4
- const directiveName = 'onIntersect';
5
- /**
6
- * A directive that fires its listener any time the element's configured "intersection" is crossed.
7
- * This is commonly use for detecting when an element has scrolled into view. This uses the
8
- * [built-in `IntersectionObserver`
9
- * API](https://developer.mozilla.org/docs/Web/API/IntersectionObserver/IntersectionObserver), so it
10
- * is very efficient.
11
- *
12
- * @category Directives
13
- * @example
14
- *
15
- * ```ts
16
- * import {html, defineElement, onIntersect} from 'element-vir';
17
- *
18
- * const MyElement = defineElement()({
19
- * tagName: 'my-element',
20
- * render() {
21
- * return html`
22
- * <div
23
- * ${onIntersect({threshold: 1}, ({element, entry}) => {
24
- * if (entry.isIntersecting) {
25
- * console.log('is intersecting!');
26
- * } else {
27
- * console.log('is not intersecting');
28
- * }
29
- * })}
30
- * >
31
- * Some div
32
- * </div>
33
- * `;
34
- * },
35
- * });
36
- * ```
37
- */
38
- export const onIntersect = directive(class extends Directive {
39
- element;
40
- options;
41
- intersectionObserver;
42
- callback;
43
- constructor(partInfo) {
44
- super(partInfo);
45
- assertIsElementPartInfo(partInfo, directiveName);
46
- }
47
- fireCallback(entries, observer) {
48
- assert.isLengthAtLeast(entries, 1);
49
- void this.callback?.({
50
- element: assertWrap.isDefined(this.element),
51
- allEntries: entries,
52
- observer,
53
- entry: entries[0],
54
- });
55
- }
56
- update(partInfo, [options, callback,]) {
57
- assertIsElementPartInfo(partInfo, directiveName);
58
- this.callback = callback;
59
- let needsObserving = false;
60
- const newOptions = options;
61
- const oldOptions = this.options;
62
- if (!this.intersectionObserver ||
63
- !oldOptions ||
64
- !check.entriesEqual(newOptions, oldOptions)) {
65
- this.options = options;
66
- this.intersectionObserver?.disconnect();
67
- this.intersectionObserver = new IntersectionObserver((entries, observer) => this.fireCallback(entries, observer), options);
68
- needsObserving = true;
69
- }
70
- const newElement = partInfo.element;
71
- const oldElement = this.element;
72
- // if the element changes we need to observe the new one
73
- if (newElement !== oldElement) {
74
- this.element = newElement;
75
- if (oldElement) {
76
- this.intersectionObserver.unobserve(oldElement);
77
- }
78
- needsObserving = true;
79
- }
80
- if (needsObserving) {
81
- this.intersectionObserver.observe(newElement);
82
- }
83
- return this.render(options, callback);
84
- }
85
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
86
- render(options, callback) {
87
- return undefined;
88
- }
89
- });
@@ -1,74 +0,0 @@
1
- import { type MaybePromise } from '@augment-vir/common';
2
- import { type PartInfo } from '../../lit-exports/all-lit-exports.js';
3
- /**
4
- * Callback called by the {@link onResize} directive.
5
- *
6
- * @category Internal
7
- */
8
- export type OnResizeCallback = (size: Readonly<Pick<ResizeObserverEntry,
9
- /** Only these two properties are supported in all major modern browsers */
10
- 'target' | 'contentRect'>>, element: Element) => MaybePromise<void>;
11
- /**
12
- * A directive that fires its listener any time the element that it's attached to is resized. This
13
- * uses the [built-in `ResizeObserver`
14
- * API](https://developer.mozilla.org/docs/Web/API/ResizeObserver), so it is very efficient.
15
- *
16
- * @category Directives
17
- * @example
18
- *
19
- * ```ts
20
- * import {html, defineElement, onResize} from 'element-vir';
21
- *
22
- * const MyElement = defineElement()({
23
- * tagName: 'my-element',
24
- * render() {
25
- * return html`
26
- * <div
27
- * ${onResize((size, element) => {
28
- * console.log('resized!', element, size);
29
- * })}
30
- * >
31
- * Some div
32
- * </div>
33
- * `;
34
- * },
35
- * });
36
- * ```
37
- */
38
- export declare const onResize: (callback: OnResizeCallback) => import("lit-html/directive.js").DirectiveResult<{
39
- new (partInfo: PartInfo): {
40
- element: Element | undefined;
41
- readonly resizeObserver: ResizeObserver;
42
- callback: OnResizeCallback | undefined;
43
- update(partInfo: PartInfo, [callback]: [OnResizeCallback]): undefined;
44
- render(callback: OnResizeCallback): undefined;
45
- get _$isConnected(): boolean;
46
- };
47
- }>;
48
- /**
49
- * A function that attaches a
50
- * [`ResizeObserver`](https://developer.mozilla.org/docs/Web/API/ResizeObserver) to any given
51
- * element, so it is very efficient.
52
- *
53
- * @category Directives
54
- * @example
55
- *
56
- * ```ts
57
- * import {html, defineElement, attachOnResize} from 'element-vir';
58
- *
59
- * const MyElement = defineElement()({
60
- * tagName: 'my-element',
61
- * render({host}) {
62
- * attachOnResize(host, (size, element) => {
63
- * console.log('resized!', element, size);
64
- * });
65
- *
66
- * return '';
67
- * },
68
- * });
69
- * ```
70
- */
71
- export declare function attachOnResize(element: Element, callback: OnResizeCallback): {
72
- resizeObserver: ResizeObserver;
73
- element: Element;
74
- };
@@ -1,106 +0,0 @@
1
- import { directive, Directive } from '../../lit-exports/all-lit-exports.js';
2
- import { assertIsElementPartInfo } from './directive-helpers.js';
3
- const directiveName = 'onResize';
4
- /**
5
- * A directive that fires its listener any time the element that it's attached to is resized. This
6
- * uses the [built-in `ResizeObserver`
7
- * API](https://developer.mozilla.org/docs/Web/API/ResizeObserver), so it is very efficient.
8
- *
9
- * @category Directives
10
- * @example
11
- *
12
- * ```ts
13
- * import {html, defineElement, onResize} from 'element-vir';
14
- *
15
- * const MyElement = defineElement()({
16
- * tagName: 'my-element',
17
- * render() {
18
- * return html`
19
- * <div
20
- * ${onResize((size, element) => {
21
- * console.log('resized!', element, size);
22
- * })}
23
- * >
24
- * Some div
25
- * </div>
26
- * `;
27
- * },
28
- * });
29
- * ```
30
- */
31
- export const onResize = directive(class extends Directive {
32
- element;
33
- resizeObserver = new ResizeObserver((entries) => {
34
- if (this.element && this.callback) {
35
- handleOnResizeCallback(this.element, this.callback, entries);
36
- }
37
- });
38
- callback;
39
- constructor(partInfo) {
40
- super(partInfo);
41
- assertIsElementPartInfo(partInfo, directiveName);
42
- }
43
- update(partInfo, [callback]) {
44
- assertIsElementPartInfo(partInfo, directiveName);
45
- this.callback = callback;
46
- const newElement = partInfo.element;
47
- const oldElement = this.element;
48
- // if the element changes we need to observe the new one
49
- if (newElement !== oldElement) {
50
- this.element = newElement;
51
- if (oldElement) {
52
- this.resizeObserver.unobserve(oldElement);
53
- }
54
- this.resizeObserver.observe(newElement);
55
- }
56
- return this.render(callback);
57
- }
58
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
59
- render(callback) {
60
- return undefined;
61
- }
62
- });
63
- function handleOnResizeCallback(element, callback, entries) {
64
- const resizeEntry = entries[0];
65
- if (!resizeEntry) {
66
- console.error(entries);
67
- throw new Error(`Resize observation triggered but the first entry was empty.`);
68
- }
69
- void callback({
70
- target: resizeEntry.target,
71
- contentRect: resizeEntry.contentRect,
72
- }, element);
73
- }
74
- /**
75
- * A function that attaches a
76
- * [`ResizeObserver`](https://developer.mozilla.org/docs/Web/API/ResizeObserver) to any given
77
- * element, so it is very efficient.
78
- *
79
- * @category Directives
80
- * @example
81
- *
82
- * ```ts
83
- * import {html, defineElement, attachOnResize} from 'element-vir';
84
- *
85
- * const MyElement = defineElement()({
86
- * tagName: 'my-element',
87
- * render({host}) {
88
- * attachOnResize(host, (size, element) => {
89
- * console.log('resized!', element, size);
90
- * });
91
- *
92
- * return '';
93
- * },
94
- * });
95
- * ```
96
- */
97
- export function attachOnResize(element, callback) {
98
- const resizeObserver = new ResizeObserver((entries) => {
99
- handleOnResizeCallback(element, callback, entries);
100
- });
101
- resizeObserver.observe(element);
102
- return {
103
- resizeObserver,
104
- element,
105
- };
106
- }
@@ -1,45 +0,0 @@
1
- import { type AsyncProp } from './async-prop.js';
2
- /**
3
- * Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
4
- * once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
5
- * parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
6
- * cases.
7
- *
8
- * This is the overload for when `resolutionRender` and `errorRender` are both provided.
9
- *
10
- * @category Async
11
- */
12
- export declare function renderAsync<T, FallbackResult, ResolutionRenderResult = never, ErrorRenderResult = never>(asyncProp: Pick<AsyncProp<T, any>, 'value'>, fallback: FallbackResult, resolutionRender: (resolved: Awaited<T>) => ResolutionRenderResult, errorRender: (error: Error) => ErrorRenderResult): FallbackResult | ResolutionRenderResult | ErrorRenderResult;
13
- /**
14
- * Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
15
- * once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
16
- * parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
17
- * cases.
18
- *
19
- * This is the overload for when `resolutionRender` is provided but `errorRender` is not.
20
- *
21
- * @category Async
22
- */
23
- export declare function renderAsync<T, FallbackResult, ResolutionRenderResult = never>(asyncProp: Pick<AsyncProp<T, any>, 'value'>, fallback: FallbackResult, resolutionRender: (resolved: Awaited<T>) => ResolutionRenderResult, errorRender?: undefined): FallbackResult | ResolutionRenderResult | string;
24
- /**
25
- * Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
26
- * once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
27
- * parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
28
- * cases.
29
- *
30
- * This is the overload for when `resolutionRender` is not provided but `errorRender` is.
31
- *
32
- * @category Async
33
- */
34
- export declare function renderAsync<T, FallbackResult, ErrorRenderResult = never>(asyncProp: Pick<AsyncProp<T, any>, 'value'>, fallback: FallbackResult, resolutionRender: undefined, errorRender: (error: Error) => ErrorRenderResult): FallbackResult | Awaited<T> | ErrorRenderResult;
35
- /**
36
- * Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
37
- * once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
38
- * parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
39
- * cases.
40
- *
41
- * This is the overload for when neither `resolutionRender` or `errorRender` are provided.
42
- *
43
- * @category Async
44
- */
45
- export declare function renderAsync<T, FallbackResult>(asyncProp: Pick<AsyncProp<T, any>, 'value'>, fallback: FallbackResult, resolutionRender?: undefined, errorRender?: undefined): FallbackResult | Awaited<T> | string;
@@ -1,33 +0,0 @@
1
- import { check } from '@augment-vir/assert';
2
- import { extractErrorMessage } from '@augment-vir/common';
3
- /**
4
- * Given a {@link AsyncProp} instance, call and return the output of the `resolutionRender` parameter
5
- * once the {@link AsyncProp} has been resolved, call and return the output of the `errorRender`
6
- * parameter if the {@link AsyncProp} errored out, return the `fallback` parameter in all other
7
- * cases.
8
- *
9
- * This is the full function definition and implementation.
10
- *
11
- * @category Async
12
- */
13
- export function renderAsync(asyncProp,
14
- /** This value will be rendered if the async prop has not settled yet. */
15
- fallback, resolutionRender, errorRender) {
16
- const asyncPropValue = asyncProp.value;
17
- if (asyncPropValue instanceof Error) {
18
- const errorResult = errorRender
19
- ? errorRender(asyncPropValue)
20
- : extractErrorMessage(asyncPropValue);
21
- return errorResult;
22
- }
23
- else if (check.isPromiseLike(asyncPropValue)) {
24
- const fallbackResult = fallback;
25
- return fallbackResult;
26
- }
27
- else {
28
- const resolutionResult = resolutionRender
29
- ? resolutionRender(asyncPropValue)
30
- : asyncPropValue;
31
- return resolutionResult;
32
- }
33
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * A directive that, given a condition, chooses between two inputs: `ifTrue` and `ifFalse`. If
3
- * `ifFalse` is omitted (which is allowed), the false case defaults to `undefined`, which won't get
4
- * rendered at all inside of an HTML template.
5
- *
6
- * @category Directives
7
- * @example
8
- *
9
- * ```ts
10
- * import {html, defineElement, renderIf} from 'element-vir';
11
- *
12
- * const MyElement = defineElement()({
13
- * tagName: 'my-element',
14
- * render() {
15
- * return html`
16
- * <div>
17
- * ${renderIf(
18
- * Math.random() > 0.5,
19
- * html`
20
- * True!
21
- * `,
22
- * html`
23
- * False!
24
- * `,
25
- * )}
26
- * </div>
27
- * `;
28
- * },
29
- * });
30
- * ```
31
- */
32
- export declare function renderIf<TrueCondition = unknown, FalseCondition = undefined>(condition: boolean, ifTrue: TrueCondition, ifFalse?: FalseCondition): TrueCondition | FalseCondition;
@@ -1,52 +0,0 @@
1
- /**
2
- * Assigns the given id as a test id attribute to the attached element.
3
- *
4
- * @category Test
5
- * @example
6
- *
7
- * ```ts
8
- * import {html, defineElement, testId} from 'element-vir';
9
- *
10
- * const MyElement = defineElement()({
11
- * tagName: 'my-element',
12
- * render() {
13
- * return html`
14
- * <div ${testId('my-test')}>Some div</div>
15
- * `;
16
- * },
17
- * });
18
- * ```
19
- */
20
- export declare const testId: (this: void, attributeValue: string) => import("lit-html/directive.js").DirectiveResult<{
21
- new (partInfo: import("lit-html/directive.js").PartInfo): {
22
- readonly element: Element;
23
- render(attributeValue: string): symbol;
24
- get _$isConnected(): boolean;
25
- update(_part: import("lit-html").Part, props: Array<unknown>): unknown;
26
- };
27
- }>;
28
- /**
29
- * Construct an attribute selector for the given test id.
30
- *
31
- * @category Test
32
- * @example
33
- *
34
- * ```ts
35
- * import {html, selectTestId} from 'element-vir';
36
- * import {testWeb} from '@augment-vir/test';
37
- * import {assert} from '@augment-vir/assert';
38
- *
39
- * const instance = testWeb.render(html`
40
- * <${MyElement}></${MyElement}>
41
- * `);
42
- * assert.instanceOf(instance, MyElement);
43
- * instance.shadowRoot.querySelector(selectTestId('my-test'));
44
- * ```
45
- */
46
- export declare const testIdSelector: (this: void, attributeValue: string) => string;
47
- /**
48
- * The test id attribute name.
49
- *
50
- * @category Test
51
- */
52
- export declare const testIdAttributeName: string;
@@ -1 +0,0 @@
1
- export declare function hasDeclarativeElementParent(input: Element): boolean;
@@ -1,17 +0,0 @@
1
- import { type DeclarativeElementDefinition } from './declarative-element.js';
2
- /**
3
- * Asserts that the given input is a declarative element definition.
4
- *
5
- * @category Util
6
- * @see
7
- * - {@link isDeclarativeElementDefinition}
8
- */
9
- export declare function assertDeclarativeElementDefinition(input: unknown, failMessage?: string | undefined): asserts input is DeclarativeElementDefinition;
10
- /**
11
- * Checks that the given input is a declarative element definition.
12
- *
13
- * @category Util
14
- * @see
15
- * - {@link assertDeclarativeElementDefinition}
16
- */
17
- export declare function isDeclarativeElementDefinition(input: unknown): input is DeclarativeElementDefinition;
@@ -1,15 +0,0 @@
1
- import { type DeclarativeElement } from './declarative-element.js';
2
- /**
3
- * Checks if the input is an instance of a DeclarativeElement, the super class of all custom
4
- * elements defined with element-vir.
5
- *
6
- * @category Util
7
- */
8
- export declare function isDeclarativeElement(input: unknown): input is DeclarativeElement;
9
- /**
10
- * Checks if the input is an instance of a DeclarativeElement, the super class of all custom
11
- * elements defined with element-vir.
12
- *
13
- * @category Util
14
- */
15
- export declare function assertIsDeclarativeElement(input: unknown): asserts input is DeclarativeElement;
@@ -1 +0,0 @@
1
- export declare function assignInputs(element: Element, inputs: object): void;
@@ -1,25 +0,0 @@
1
- import { getObjectTypedKeys } from '@augment-vir/common';
2
- export function assignInputs(element, inputs) {
3
- const instanceState = element.instanceState;
4
- getObjectTypedKeys(inputs).forEach((newInputKey) => {
5
- if (instanceState && newInputKey in instanceState) {
6
- throw new Error(`Cannot set input '${String(newInputKey)}' on '${element.tagName}'. '${element.tagName}' already has a state property with the same name.`);
7
- }
8
- if ('instanceInputs' in element) {
9
- element.instanceInputs[newInputKey] =
10
- inputs[newInputKey];
11
- }
12
- else {
13
- element[newInputKey] = inputs[newInputKey];
14
- }
15
- });
16
- /** Wipe out all inputs that weren't set to undefined (as expected) */
17
- if ('instanceInputs' in element) {
18
- getObjectTypedKeys(element.instanceInputs).forEach((existingKey) => {
19
- if (!(existingKey in inputs)) {
20
- element.instanceInputs[existingKey] =
21
- undefined;
22
- }
23
- });
24
- }
25
- }
@@ -1,16 +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
- * Base type for a declarative element definition's CSS vars.
7
- *
8
- * @category Internal
9
- */
10
- export type CssVarsInitMap<ElementTagName extends CustomElementTagName, CssVarKeys extends BaseStringName<ElementTagName>> = Readonly<Record<CssVarKeys, Values<CssVarsSetup>>>;
11
- /**
12
- * CSS vars ready for use within a declarative element's `render` or `styles` method.
13
- *
14
- * @category Internal
15
- */
16
- export type CssVars<ElementTagName extends CustomElementTagName, CssVarKeys extends BaseStringName<ElementTagName>> = CssVarDefinitions<CssVarsInitMap<ElementTagName, CssVarKeys>>;
@@ -1 +0,0 @@
1
- export {};
@@ -1,65 +0,0 @@
1
- import { type DefinedTypedEvent, type DefineEvent, type TypedEvent } from '../../typed-event/typed-event.js';
2
- import { type CustomElementTagName } from '../custom-tag-name.js';
3
- /**
4
- * Base type for defining element events inside of an element definition.
5
- *
6
- * @category Internal
7
- */
8
- export type EventsInitMap = Record<string, DefineEvent<any>>;
9
- /**
10
- * Used to define element events, with a type.
11
- *
12
- * @category Element Definition
13
- * @example
14
- *
15
- * ```ts
16
- * import {html, defineElement, defineElementEvent, listen} from 'element-vir';
17
- *
18
- * const MyElement = defineElement()({
19
- * tagName: 'my-element',
20
- * events: {
21
- * myOutput: defineElementEvent<number>(),
22
- * },
23
- * render({events, dispatch}) {
24
- * return html`
25
- * <div
26
- * ${listen('click', () => {
27
- * dispatch(new events.myOutput(1));
28
- * })}
29
- * >
30
- * Some div
31
- * </div>
32
- * `;
33
- * },
34
- * });
35
- * ```
36
- */
37
- export declare function defineElementEvent<EventDetailGeneric>(): DefineEvent<EventDetailGeneric>;
38
- /**
39
- * Extracts the detail type of the given event name from the given event init map.
40
- *
41
- * @category Internal
42
- */
43
- export type EventInitMapEventDetailExtractor<EventTypeNameGeneric extends keyof EventsInitGeneric, EventsInitGeneric extends EventsInitMap> = EventsInitGeneric[EventTypeNameGeneric] extends DefineEvent<infer Detail> ? Detail : never;
44
- /**
45
- * Maps the given element tag name and map of event names to their run-time event type strings.
46
- *
47
- * @category Internal
48
- */
49
- export type EventDescriptorMap<TagName extends CustomElementTagName, EventsInitGeneric extends EventsInitMap> = {
50
- [CurrentEventTypeName in keyof EventsInitGeneric]: DefinedTypedEvent<CurrentEventTypeName extends string ? `${TagName}-${CurrentEventTypeName}` : never, EventInitMapEventDetailExtractor<CurrentEventTypeName, EventsInitGeneric>>;
51
- };
52
- /**
53
- * Extract the event detail type from a {@link TypedEvent}, {@link DefinedTypedEvent}, or
54
- * `CustomEvent.
55
- *
56
- * @category Internal
57
- */
58
- export type EventDetail<ElementEvent extends TypedEvent<any, any> | DefinedTypedEvent<any, any> | CustomEvent> = ElementEvent extends TypedEvent<string, infer Detail> ? Detail : ElementEvent extends DefinedTypedEvent<any, infer Detail> ? Detail : ElementEvent extends CustomEvent<infer Detail> ? Detail : 'TYPE ERROR: failed to extract event detail type';
59
- /**
60
- * Maps an element definition initialization's tag name and event map to a map of ready-to-construct
61
- * event classes. This also verifies that the input event init map has valid keys.
62
- *
63
- * @category Internal
64
- */
65
- export declare function createEventDescriptorMap<TagName extends CustomElementTagName, EventsInitGeneric extends EventsInitMap>(tagName: CustomElementTagName, eventsInit: EventsInitGeneric | undefined): EventDescriptorMap<TagName, EventsInitGeneric>;