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
@@ -0,0 +1,30 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+
3
+ import {type CSSResultGroup} from '../../lit-exports/all-lit-exports.js';
4
+ import {type MinimalElementDefinition, hasTagName} from '../minimal-element-definition.js';
5
+ import {type TemplateTransform} from '../template-transform-type.js';
6
+ import {type ValueTransformCallback, transformTemplate} from '../transform-template.js';
7
+
8
+ function transformCss(
9
+ ...[
10
+ lastNewString,
11
+ currentLitString,
12
+ currentValue,
13
+ ]: Parameters<ValueTransformCallback>
14
+ ): ReturnType<ValueTransformCallback> {
15
+ if (!hasTagName(currentValue)) {
16
+ return undefined;
17
+ }
18
+
19
+ return {
20
+ replacement: currentValue.tagName,
21
+ getExtraValues: undefined,
22
+ };
23
+ }
24
+
25
+ export function transformCssTemplate(
26
+ inputTemplateStrings: TemplateStringsArray,
27
+ inputValues: (number | CSSResultGroup | MinimalElementDefinition)[],
28
+ ): TemplateTransform {
29
+ return transformTemplate(inputTemplateStrings, inputValues, transformCss);
30
+ }
@@ -0,0 +1,30 @@
1
+ import {css as litCss} from 'lit';
2
+ import {type CSSResult, type CSSResultGroup} from '../../lit-exports/all-lit-exports.js';
3
+ import {type MinimalElementDefinition} from '../minimal-element-definition.js';
4
+ import {getTransformedTemplate} from '../transform-template.js';
5
+ import {transformCssTemplate} from './css-transform.js';
6
+
7
+ /**
8
+ * A template literal tag used to define styles for element definitions. In particular, this is used
9
+ * for the `styles` property in an element definitions init object.
10
+ *
11
+ * For security reasons, only literal string or number values may be interpolated inside of this
12
+ * taggedTemplate. To incorporate non-literal values, wrap the value in a call to `unsafeCSS`.
13
+ *
14
+ * @category Element Definition
15
+ */
16
+ export function css(
17
+ inputTemplateStrings: TemplateStringsArray,
18
+ ...inputValues: (number | CSSResultGroup | MinimalElementDefinition | CSSResult)[]
19
+ ): CSSResult {
20
+ const transformedTemplate = getTransformedTemplate(inputTemplateStrings, inputValues, () => {
21
+ return transformCssTemplate(inputTemplateStrings, inputValues);
22
+ });
23
+
24
+ const cssResult = litCss(
25
+ transformedTemplate.strings,
26
+ /** The filter will remove the DeclarativeElement elements */
27
+ ...(transformedTemplate.values as (number | CSSResultGroup)[]),
28
+ );
29
+ return cssResult;
30
+ }
@@ -0,0 +1,103 @@
1
+ import {type AnyFunction, type Overwrite} from '@augment-vir/common';
2
+ import {type CSSResult, type TemplateResult, type nothing} from 'lit';
3
+ import {type EmptyObject, type HasRequiredKeys, type IsNever} from 'type-fest';
4
+ import {type DeclarativeElementDefinition} from '../../declarative-element/declarative-element.js';
5
+ import {type Decrement, type Increment} from '../../util/increment.js';
6
+ import {
7
+ type MinimalDefinitionWithInputs,
8
+ type MinimalElementDefinition,
9
+ } from '../minimal-element-definition.js';
10
+
11
+ /**
12
+ * Unfortunately the type for `DirectiveResult` means it's just an empty object. So in order to
13
+ * block actual objects, we have to narrow `DirectiveResult` further to this empty object type.
14
+ *
15
+ * @category Internal
16
+ */
17
+ export type DirectiveOutput = EmptyObject;
18
+
19
+ /**
20
+ * This is used in order to block accidental object interpolations into HTML, which get stringified
21
+ * into `'[object Object]'`, which nobody ever wants that.
22
+ *
23
+ * @category Internal
24
+ */
25
+ export type HtmlInterpolation =
26
+ | null
27
+ | undefined
28
+ | string
29
+ | number
30
+ | boolean
31
+ | bigint
32
+ | CSSResult
33
+ | Readonly<CSSResult>
34
+ | Element
35
+ | Readonly<Element>
36
+ | TemplateResult
37
+ | Readonly<TemplateResult>
38
+ | MinimalElementDefinition
39
+ | Readonly<MinimalElementDefinition>
40
+ | MinimalDefinitionWithInputs
41
+ | Readonly<MinimalDefinitionWithInputs>
42
+ | DeclarativeElementDefinition
43
+ | Readonly<DeclarativeElementDefinition>
44
+ | DirectiveOutput
45
+ | Readonly<DirectiveOutput>
46
+ | AnyFunction
47
+ | typeof nothing
48
+ | HtmlInterpolation[]
49
+ | ReadonlyArray<HtmlInterpolation>
50
+ | Iterable<HtmlInterpolation>
51
+ | Readonly<Iterable<HtmlInterpolation>>;
52
+
53
+ /**
54
+ * This type ensures that interpolated element definitions are not missing their inputs, when inputs
55
+ * are required.
56
+ *
57
+ * @category Internal
58
+ */
59
+ export type VerifyHtmlValues<
60
+ Values extends HtmlInterpolation[],
61
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
62
+ WaitingForEndTags extends Record<string, number> = {},
63
+ > = Values extends [
64
+ infer CurrentDefinition extends DeclarativeElementDefinition,
65
+ ...infer Rest extends HtmlInterpolation[],
66
+ ]
67
+ ? CurrentDefinition extends DeclarativeElementDefinition<infer TagName, infer Inputs>
68
+ ? HasRequiredKeys<Inputs> extends true
69
+ ? IsNever<Decrement<WaitingForEndTags[TagName]>> extends true
70
+ ? [
71
+ `ERROR: This element is missing its inputs.`,
72
+ ...VerifyHtmlValues<Rest, WaitingForEndTags>,
73
+ ]
74
+ : [
75
+ CurrentDefinition,
76
+ ...VerifyHtmlValues<
77
+ Rest,
78
+ Overwrite<
79
+ WaitingForEndTags,
80
+ Record<TagName, Decrement<WaitingForEndTags[TagName]>>
81
+ >
82
+ >,
83
+ ]
84
+ : [CurrentDefinition, ...VerifyHtmlValues<Rest, WaitingForEndTags>]
85
+ : [CurrentDefinition, ...VerifyHtmlValues<Rest, WaitingForEndTags>]
86
+ : Values extends [
87
+ infer CurrentDefinition extends MinimalDefinitionWithInputs,
88
+ ...infer Rest extends HtmlInterpolation[],
89
+ ]
90
+ ? [
91
+ CurrentDefinition,
92
+ ...VerifyHtmlValues<
93
+ Rest,
94
+ Overwrite<
95
+ WaitingForEndTags,
96
+ Record<
97
+ CurrentDefinition['definition']['tagName'],
98
+ Increment<WaitingForEndTags[CurrentDefinition['definition']['tagName']]>
99
+ >
100
+ >
101
+ >,
102
+ ]
103
+ : Values;
@@ -0,0 +1,149 @@
1
+ /* eslint-disable @typescript-eslint/no-deprecated */
2
+
3
+ import {check} from '@augment-vir/assert';
4
+ import {collapseWhiteSpace, getOrSet, safeMatch} from '@augment-vir/common';
5
+ import {assign} from '../../declarative-element/directives/assign.directive.js';
6
+ import {type HTMLTemplateResult} from '../../lit-exports/all-lit-exports.js';
7
+ import {declarativeElementRequired} from '../../require-declarative-element.js';
8
+ import {
9
+ type MinimalElementDefinition,
10
+ hasTagName,
11
+ isMinimalDefinitionWithInputs,
12
+ } from '../minimal-element-definition.js';
13
+ import {type TemplateTransform} from '../template-transform-type.js';
14
+ import {type ValueTransformCallback, transformTemplate} from '../transform-template.js';
15
+ import {type HtmlInterpolation} from './html-interpolation.js';
16
+ import {tagNameKeys} from './tag-name-keys.js';
17
+
18
+ export function mapHtmlValues(
19
+ inputTemplateStrings: TemplateStringsArray,
20
+ inputValues: HtmlInterpolation[],
21
+ ): HtmlInterpolation[] {
22
+ return inputValues.map((currentValue, currentValueIndex): HtmlInterpolation => {
23
+ const lastString = inputTemplateStrings[currentValueIndex];
24
+ const nextString = inputTemplateStrings[currentValueIndex + 1];
25
+
26
+ if (lastString && nextString) {
27
+ const {shouldHaveTagNameHere} = classifyValue(lastString, nextString);
28
+
29
+ if (shouldHaveTagNameHere && check.isString(currentValue)) {
30
+ const replacement: MinimalElementDefinition = {
31
+ tagName: currentValue,
32
+ tagInterpolationKey: getOrSet(tagNameKeys, currentValue, () => {
33
+ return {tagName: currentValue};
34
+ }),
35
+ };
36
+
37
+ return replacement;
38
+ }
39
+ }
40
+
41
+ return currentValue;
42
+ });
43
+ }
44
+
45
+ function classifyValue(lastNewString: string, currentTemplateString: string) {
46
+ const isOpeningTag =
47
+ lastNewString.trim().endsWith('<') && !!currentTemplateString.match(/^[\s>]/);
48
+ const isClosingTag =
49
+ lastNewString.trim().endsWith('</') && currentTemplateString.trim().startsWith('>');
50
+ const shouldHaveTagNameHere: boolean = isOpeningTag || isClosingTag;
51
+
52
+ return {
53
+ isOpeningTag,
54
+ shouldHaveTagNameHere,
55
+ };
56
+ }
57
+
58
+ function transformHtml(
59
+ ...[
60
+ lastNewString,
61
+ currentTemplateString,
62
+ rawCurrentValue,
63
+ ]: Parameters<ValueTransformCallback>
64
+ ): ReturnType<ValueTransformCallback> {
65
+ const currentValue = isMinimalDefinitionWithInputs(rawCurrentValue)
66
+ ? rawCurrentValue.definition
67
+ : rawCurrentValue;
68
+
69
+ const {isOpeningTag, shouldHaveTagNameHere} = classifyValue(
70
+ lastNewString,
71
+ currentTemplateString,
72
+ );
73
+ const isTagNameWrapper = hasTagName(currentValue);
74
+
75
+ if (isTagNameWrapper && shouldHaveTagNameHere && currentValue.tagInterpolationKey) {
76
+ return {
77
+ replacement: currentValue.tagName,
78
+ getExtraValues: undefined,
79
+ };
80
+ }
81
+
82
+ if (shouldHaveTagNameHere && !isTagNameWrapper) {
83
+ console.error({
84
+ lastNewString,
85
+ currentTemplateString,
86
+ currentValue,
87
+ });
88
+ throw new Error(
89
+ `Got interpolated tag name but found no tag name on the given value: '${
90
+ (currentValue as any)?.tagName ||
91
+ (currentValue as any)?.prototype?.constructor?.name ||
92
+ (currentValue as any)?.constructor?.name
93
+ }'`,
94
+ );
95
+ }
96
+
97
+ if (!shouldHaveTagNameHere || !isTagNameWrapper) {
98
+ return undefined;
99
+ }
100
+
101
+ const replacement = currentValue.tagName;
102
+
103
+ return {
104
+ replacement,
105
+ getExtraValues(extraValueCurrentValue) {
106
+ const assignedInputs = isMinimalDefinitionWithInputs(extraValueCurrentValue)
107
+ ? extraValueCurrentValue.inputs
108
+ : undefined;
109
+
110
+ return [
111
+ isOpeningTag && assignedInputs ? assign(assignedInputs) : undefined,
112
+ ].filter(check.isTruthy);
113
+ },
114
+ };
115
+ }
116
+
117
+ function extractCustomElementTags(input: string): string[] {
118
+ const tagNameMatches = safeMatch(input, /<\/\s*[^\s><]+\s*>/g);
119
+ return tagNameMatches.reduce((accum: string[], match) => {
120
+ const tagName = collapseWhiteSpace(match.replace(/\n/g, ' ')).replace(/<\/|>/g, '');
121
+ // custom elements always have a dash in them
122
+ if (tagName.includes('-')) {
123
+ return accum.concat(tagName);
124
+ }
125
+ return accum;
126
+ }, []);
127
+ }
128
+
129
+ function stringValidator(input: string): void {
130
+ if (declarativeElementRequired) {
131
+ const customElementTagNames = extractCustomElementTags(input);
132
+ if (customElementTagNames.length) {
133
+ console.error(
134
+ `Custom element tags must be interpolated from declarative elements: ${customElementTagNames.join(
135
+ ', ',
136
+ )}`,
137
+ );
138
+ }
139
+ }
140
+ }
141
+
142
+ export function transformHtmlTemplate(litTemplate: HTMLTemplateResult): TemplateTransform {
143
+ return transformTemplate(
144
+ litTemplate.strings,
145
+ litTemplate.values,
146
+ transformHtml,
147
+ stringValidator,
148
+ );
149
+ }
@@ -2,4 +2,4 @@
2
2
  * Used to create objects for each interpolated tag name string (tag names that aren't wrapped in a
3
3
  * `DeclarativeElementDefinition`) so that we can use them as keys for the mapped template weakmap.
4
4
  */
5
- export const tagNameKeys = {};
5
+ export const tagNameKeys: Record<string, {tagName: string}> = {};
@@ -1,6 +1,9 @@
1
- import { html as litHtml } from 'lit';
2
- import { getTransformedTemplate } from '../transform-template.js';
3
- import { mapHtmlValues, transformHtmlTemplate } from './html-transform.js';
1
+ import {html as litHtml} from 'lit';
2
+ import {type HTMLTemplateResult} from '../../lit-exports/all-lit-exports.js';
3
+ import {getTransformedTemplate} from '../transform-template.js';
4
+ import {type HtmlInterpolation, type VerifyHtmlValues} from './html-interpolation.js';
5
+ import {mapHtmlValues, transformHtmlTemplate} from './html-transform.js';
6
+
4
7
  /**
5
8
  * Interprets a template literal as an HTML template which is lazily rendered to the DOM.
6
9
  *
@@ -9,13 +12,18 @@ import { mapHtmlValues, transformHtmlTemplate } from './html-transform.js';
9
12
  *
10
13
  * @category Element Definition
11
14
  */
12
- export function html(inputTemplateStrings, ...inputValues) {
15
+ export function html<const Values extends HtmlInterpolation[]>(
16
+ inputTemplateStrings: TemplateStringsArray,
17
+ ...inputValues: VerifyHtmlValues<Values>
18
+ ): HTMLTemplateResult {
13
19
  const mappedValues = mapHtmlValues(inputTemplateStrings, inputValues);
20
+
14
21
  const litTemplate = litHtml(inputTemplateStrings, ...mappedValues);
15
22
  const transformedTemplate = getTransformedTemplate(inputTemplateStrings, mappedValues, () => {
16
23
  return transformHtmlTemplate(litTemplate);
17
24
  });
18
- const htmlTemplate = {
25
+
26
+ const htmlTemplate: HTMLTemplateResult = {
19
27
  ...litTemplate,
20
28
  strings: transformedTemplate.strings,
21
29
  values: transformedTemplate.values,
@@ -0,0 +1,90 @@
1
+ import {type NonEmptyString} from '../util/type.js';
2
+
3
+ /**
4
+ * A custom event with strict types for details and the event's `type` property.
5
+ *
6
+ * @category Internal
7
+ */
8
+ export class TypedEvent<
9
+ EventTypeNameGeneric extends string = '',
10
+ EventDetailGeneric = undefined,
11
+ > extends CustomEvent<EventDetailGeneric> {
12
+ public readonly _type: EventTypeNameGeneric = '' as EventTypeNameGeneric;
13
+ public override get type(): EventTypeNameGeneric {
14
+ return this._type;
15
+ }
16
+
17
+ constructor(
18
+ type: EventTypeNameGeneric | {type: EventTypeNameGeneric},
19
+ value: EventDetailGeneric,
20
+ ) {
21
+ super(typeof type === 'string' ? type : type.type, {
22
+ detail: value,
23
+ bubbles: true,
24
+ composed: true,
25
+ });
26
+ }
27
+ }
28
+
29
+ /**
30
+ * A function that defines a typed event, used for defining element events in an element's
31
+ * definition.
32
+ *
33
+ * @category Internal
34
+ */
35
+ export type DefineEvent<EventDetailGeneric> = <EventTypeNameGeneric extends string>(
36
+ eventType: NonEmptyString<EventTypeNameGeneric>,
37
+ ) => DefinedTypedEvent<EventTypeNameGeneric, EventDetailGeneric>;
38
+
39
+ /**
40
+ * A concrete typed event definition.
41
+ *
42
+ * @category Internal
43
+ */
44
+ export type DefinedTypedEvent<EventTypeNameGeneric extends string, EventDetailGeneric> = (new (
45
+ eventValue: EventDetailGeneric,
46
+ ) => TypedEvent<EventTypeNameGeneric, EventDetailGeneric>) & {
47
+ type: EventTypeNameGeneric;
48
+ };
49
+
50
+ /**
51
+ * Define a stand-alone typed event that can be emitted and listened to inside of HTML templates.
52
+ *
53
+ * Make sure to use currying and call this function twice! (This is required by TypeScript's type
54
+ * parameter inference system.)
55
+ *
56
+ * @category Element Definition
57
+ * @example
58
+ *
59
+ * ```ts
60
+ * import {defineTypedEvent} from 'element-vir';
61
+ *
62
+ * const myCustomEvent = defineTypedEvent<number>()('my-custom-event');
63
+ *
64
+ * const myCustomEvent2 = defineTypedEvent<// the event's `.detail` type
65
+ * number>()(
66
+ * // the event's `.type` string
67
+ * 'my-custom-event2',
68
+ * );
69
+ * ```
70
+ */
71
+ export function defineTypedEvent<EventDetailGeneric>() {
72
+ return <
73
+ /**
74
+ * EventTypeNameGeneric is used for the event type property but not for the event value
75
+ * type... so it's named "name" instead of "type" cause type is overloaded here.
76
+ */
77
+ EventTypeNameGeneric extends string,
78
+ >(
79
+ eventType: NonEmptyString<EventTypeNameGeneric>,
80
+ ): DefinedTypedEvent<EventTypeNameGeneric, EventDetailGeneric> => {
81
+ return class extends TypedEvent<EventTypeNameGeneric, EventDetailGeneric> {
82
+ public static readonly type = eventType;
83
+ public override readonly _type = eventType;
84
+
85
+ constructor(value: EventDetailGeneric) {
86
+ super(eventType, value);
87
+ }
88
+ };
89
+ };
90
+ }
@@ -1,23 +1,34 @@
1
- export function insertAndRemoveValues(originalArray, valuesToInsert, indexesToRemove) {
2
- const noOperations = !valuesToInsert.length && !indexesToRemove.length;
3
- const nothingToDoBecauseEmptyArray = originalArray.length
1
+ export type ArrayInsertion<T> = {index: number; values: ReadonlyArray<T>};
2
+
3
+ export function insertAndRemoveValues<Entry>(
4
+ originalArray: ReadonlyArray<Entry>,
5
+ valuesToInsert: ReadonlyArray<ArrayInsertion<Entry>>,
6
+ indexesToRemove: ReadonlyArray<number>,
7
+ ): Entry[] {
8
+ const noOperations: boolean = !valuesToInsert.length && !indexesToRemove.length;
9
+ const nothingToDoBecauseEmptyArray: boolean = originalArray.length
4
10
  ? false
5
11
  : !valuesToInsert.filter((value) => !!value.index).length;
12
+
6
13
  if (noOperations || nothingToDoBecauseEmptyArray) {
7
14
  // there's nothing to do
8
15
  return [...originalArray];
9
16
  }
10
- const arrayValues = originalArray.map((entry) => {
17
+
18
+ const arrayValues: Entry[][] = originalArray.map((entry) => {
11
19
  return [entry];
12
20
  });
21
+
13
22
  if (!arrayValues.length) {
14
23
  arrayValues[0] = [];
15
24
  }
25
+
16
26
  indexesToRemove.forEach((index) => {
17
27
  if (index >= 0 && index < originalArray.length) {
18
28
  arrayValues[index] = [];
19
29
  }
20
30
  });
31
+
21
32
  valuesToInsert.forEach((valueWrapper) => {
22
33
  const indexEntry = arrayValues[valueWrapper.index];
23
34
  if (indexEntry) {
@@ -25,6 +36,8 @@ export function insertAndRemoveValues(originalArray, valuesToInsert, indexesToRe
25
36
  indexEntry.splice(0, 0, ...valueWrapper.values);
26
37
  }
27
38
  });
28
- const finalArray = arrayValues.flat();
39
+
40
+ const finalArray: Entry[] = arrayValues.flat();
41
+
29
42
  return finalArray;
30
43
  }
@@ -1,5 +1,6 @@
1
- import { type ArrayElement } from '@augment-vir/common';
2
- import { type IsNever } from 'type-fest';
1
+ import {type ArrayElement} from '@augment-vir/common';
2
+ import {type IsNever} from 'type-fest';
3
+
3
4
  /**
4
5
  * The largest number in this tuple is the largest number of nestings of the same element that
5
6
  * `element-vir` will support with types (in runtime it'll support any level of nesting the same
@@ -38,18 +39,36 @@ export type Incrementable = [
38
39
  27,
39
40
  28,
40
41
  29,
41
- 30
42
+ 30,
42
43
  ];
44
+
43
45
  /**
44
46
  * Increments the given number. If an invalid type parameter is given, it is treated as `0` and this
45
47
  * will output `1`. If the upper bound of increments has been reached, this will output `never`.
46
48
  *
47
49
  * @category Internal
48
50
  */
49
- export type Increment<T> = IsNever<Extract<ArrayElement<Incrementable>, T>> extends true ? IsNever<T> extends true ? 1 : never : Incrementable extends [any, ...infer Rest] ? T extends keyof Rest ? Rest[T] extends undefined ? never : Rest[T] : 1 : 1;
51
+ export type Increment<T> =
52
+ IsNever<Extract<ArrayElement<Incrementable>, T>> extends true
53
+ ? IsNever<T> extends true
54
+ ? 1
55
+ : never
56
+ : Incrementable extends [any, ...infer Rest]
57
+ ? T extends keyof Rest
58
+ ? Rest[T] extends undefined
59
+ ? never
60
+ : Rest[T]
61
+ : 1
62
+ : 1;
63
+
50
64
  /**
51
65
  * Decrements the given number. If an invalid type parameter is given, `never` is the output.
52
66
  *
53
67
  * @category Internal
54
68
  */
55
- export type Decrement<T> = IsNever<Extract<ArrayElement<Incrementable>, T>> extends true ? never : T extends keyof Incrementable ? [never, ...Incrementable][T] : never;
69
+ export type Decrement<T> =
70
+ IsNever<Extract<ArrayElement<Incrementable>, T>> extends true
71
+ ? never
72
+ : T extends keyof Incrementable
73
+ ? [never, ...Incrementable][T]
74
+ : never;
@@ -1,42 +1,62 @@
1
- import { collapseWhiteSpace } from '@augment-vir/common';
1
+ import {collapseWhiteSpace} from '@augment-vir/common';
2
+ import {
3
+ type TemplateResult,
4
+ type unsafeHTML,
5
+ type unsafeSVG,
6
+ } from '../lit-exports/all-lit-exports.js';
7
+
2
8
  /**
3
9
  * Converts an HTML or SVG template into a raw string.
4
10
  *
5
11
  * @category Util
6
12
  */
7
- export function convertTemplateToString(template) {
13
+ export function convertTemplateToString(
14
+ template:
15
+ | TemplateResult
16
+ | ReturnType<typeof unsafeSVG>
17
+ | ReturnType<typeof unsafeHTML>
18
+ | {templateString: string},
19
+ ): string {
8
20
  if ('templateString' in template) {
9
21
  return template.templateString;
10
22
  }
11
- const { strings, values } = template;
23
+
24
+ const {strings, values} = template as
25
+ | TemplateResult
26
+ | {
27
+ strings: undefined | string[];
28
+ values: undefined | unknown[];
29
+ };
30
+
12
31
  if (!strings?.length && !values?.length) {
13
32
  return '';
14
33
  }
34
+
15
35
  const valueList = [
16
36
  ...(values || []),
17
37
  '', // this last empty string is so it's easier to deal with indexes
18
38
  ];
19
39
  const stringsList = strings ?? [''];
40
+
20
41
  const all = stringsList.map((stringValue, index) => {
21
42
  const value = extractValue(stringValue, valueList[index]);
22
43
  return `${stringValue}${value}`;
23
44
  });
45
+
24
46
  return collapseWhiteSpace(all.join(''));
25
47
  }
26
- function extractValue(previousString, value) {
48
+
49
+ function extractValue(previousString: string, value: any) {
27
50
  if (value._$litType$ != undefined || value._$litDirective$ != undefined) {
28
51
  // nested templates
29
52
  return convertTemplateToString(value);
30
- }
31
- else if (Array.isArray(value)) {
53
+ } else if (Array.isArray(value)) {
32
54
  // array of strings or templates.
33
55
  const values = value.map((innerValue) => convertTemplateToString(innerValue));
34
56
  return values.join('');
35
- }
36
- else if (previousString.endsWith('=')) {
57
+ } else if (previousString.endsWith('=')) {
37
58
  return `"${value}"`;
38
- }
39
- else {
59
+ } else {
40
60
  return value;
41
61
  }
42
62
  }
@@ -0,0 +1,33 @@
1
+ import {isNotNoUpdate, type AsyncValue} from 'observavir';
2
+
3
+ /**
4
+ * Maps an async value to a new async value.
5
+ *
6
+ * @category Util
7
+ */
8
+ export function mapAsyncValue<const OriginalValue, const MappedValue>(
9
+ asyncValue: AsyncValue<OriginalValue>,
10
+ mapper: (value: NoInfer<OriginalValue>) => MappedValue,
11
+ ): AsyncValue<MappedValue> {
12
+ if (asyncValue instanceof Error) {
13
+ return asyncValue;
14
+ } else if (asyncValue instanceof Promise) {
15
+ return asyncValue.then((value) => {
16
+ const mapped = mapper(value);
17
+
18
+ if (isNotNoUpdate(mapped)) {
19
+ return mapped;
20
+ } else {
21
+ throw new Error('Cannot map to no update.');
22
+ }
23
+ });
24
+ } else {
25
+ const mapped = mapper(asyncValue);
26
+
27
+ if (isNotNoUpdate(mapped)) {
28
+ return mapped;
29
+ } else {
30
+ throw new Error('Cannot map to no update.');
31
+ }
32
+ }
33
+ }
@@ -1 +0,0 @@
1
- export {};