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.
Files changed (189) hide show
  1. package/dist/declarative-element/custom-tag-name.js +1 -0
  2. package/dist/declarative-element/declarative-element-init.d.ts +56 -0
  3. package/dist/declarative-element/declarative-element-init.js +1 -0
  4. package/dist/declarative-element/declarative-element.d.ts +114 -0
  5. package/dist/declarative-element/declarative-element.js +36 -0
  6. package/dist/declarative-element/define-element.d.ts +41 -0
  7. package/dist/declarative-element/define-element.js +248 -0
  8. package/{src/declarative-element/definition-options.ts → dist/declarative-element/definition-options.d.ts} +2 -7
  9. package/dist/declarative-element/definition-options.js +9 -0
  10. package/dist/declarative-element/directives/assign.directive.d.ts +24 -0
  11. package/dist/declarative-element/directives/assign.directive.js +34 -0
  12. package/dist/declarative-element/directives/async-prop.d.ts +61 -0
  13. package/{src/declarative-element/directives/async-prop.ts → dist/declarative-element/directives/async-prop.js} +8 -42
  14. package/dist/declarative-element/directives/attributes.directive.d.ts +30 -0
  15. package/dist/declarative-element/directives/attributes.directive.js +35 -0
  16. package/dist/declarative-element/directives/create-attribute-directive.d.ts +28 -0
  17. package/dist/declarative-element/directives/create-attribute-directive.js +41 -0
  18. package/dist/declarative-element/directives/directive-helpers.d.ts +27 -0
  19. package/dist/declarative-element/directives/directive-helpers.js +37 -0
  20. package/dist/declarative-element/directives/listen-to-activate.d.ts +15 -0
  21. package/{src/declarative-element/directives/listen-to-activate.ts → dist/declarative-element/directives/listen-to-activate.js} +3 -8
  22. package/dist/declarative-element/directives/listen.directive.d.ts +92 -0
  23. package/dist/declarative-element/directives/listen.directive.js +48 -0
  24. package/dist/declarative-element/directives/mutate.directive.d.ts +38 -0
  25. package/dist/declarative-element/directives/mutate.directive.js +45 -0
  26. package/dist/declarative-element/directives/on-dom-created.directive.d.ts +44 -0
  27. package/dist/declarative-element/directives/on-dom-created.directive.js +51 -0
  28. package/dist/declarative-element/directives/on-dom-rendered.directive.d.ts +41 -0
  29. package/dist/declarative-element/directives/on-dom-rendered.directive.js +45 -0
  30. package/dist/declarative-element/directives/on-intersect.directive.d.ts +64 -0
  31. package/dist/declarative-element/directives/on-intersect.directive.js +89 -0
  32. package/dist/declarative-element/directives/on-resize.directive.d.ts +74 -0
  33. package/dist/declarative-element/directives/on-resize.directive.js +106 -0
  34. package/dist/declarative-element/directives/render-async.directive.d.ts +45 -0
  35. package/dist/declarative-element/directives/render-async.directive.js +33 -0
  36. package/dist/declarative-element/directives/render-if.directive.d.ts +32 -0
  37. package/{src/declarative-element/directives/render-if.directive.ts → dist/declarative-element/directives/render-if.directive.js} +3 -12
  38. package/dist/declarative-element/directives/test-id.directive.d.ts +52 -0
  39. package/{src/declarative-element/directives/test-id.directive.ts → dist/declarative-element/directives/test-id.directive.js} +2 -7
  40. package/dist/declarative-element/has-declarative-element-parent.d.ts +1 -0
  41. package/{src/declarative-element/has-declarative-element-parent.ts → dist/declarative-element/has-declarative-element-parent.js} +4 -7
  42. package/dist/declarative-element/is-declarative-element-definition.d.ts +17 -0
  43. package/{src/declarative-element/is-declarative-element-definition.ts → dist/declarative-element/is-declarative-element-definition.js} +11 -28
  44. package/dist/declarative-element/is-declarative-element.d.ts +15 -0
  45. package/{src/declarative-element/is-declarative-element.ts → dist/declarative-element/is-declarative-element.js} +5 -11
  46. package/dist/declarative-element/properties/assign-inputs.d.ts +1 -0
  47. package/dist/declarative-element/properties/assign-inputs.js +25 -0
  48. package/dist/declarative-element/properties/css-vars.d.ts +16 -0
  49. package/dist/declarative-element/properties/css-vars.js +1 -0
  50. package/dist/declarative-element/properties/element-events.d.ts +65 -0
  51. package/dist/declarative-element/properties/element-events.js +62 -0
  52. package/dist/declarative-element/properties/element-properties.js +1 -0
  53. package/dist/declarative-element/properties/host-classes.d.ts +36 -0
  54. package/dist/declarative-element/properties/host-classes.js +16 -0
  55. package/dist/declarative-element/properties/property-proxy.d.ts +22 -0
  56. package/{src/declarative-element/properties/property-proxy.ts → dist/declarative-element/properties/property-proxy.js} +21 -58
  57. package/dist/declarative-element/properties/string-names.d.ts +28 -0
  58. package/dist/declarative-element/properties/string-names.js +40 -0
  59. package/dist/declarative-element/properties/styles.d.ts +51 -0
  60. package/dist/declarative-element/properties/styles.js +41 -0
  61. package/dist/declarative-element/properties/tag-name.js +1 -0
  62. package/dist/declarative-element/render-callback.d.ts +56 -0
  63. package/dist/declarative-element/render-callback.js +27 -0
  64. package/dist/declarative-element/wrap-define-element.d.ts +36 -0
  65. package/dist/declarative-element/wrap-define-element.js +25 -0
  66. package/{src/index.ts → dist/index.d.ts} +0 -1
  67. package/dist/index.js +43 -0
  68. package/dist/lit-exports/all-lit-exports.js +2 -0
  69. package/{src/lit-exports/base-lit-exports.ts → dist/lit-exports/base-lit-exports.d.ts} +2 -10
  70. package/dist/lit-exports/base-lit-exports.js +24 -0
  71. package/{src/lit-exports/lit-repeat-fix.ts → dist/lit-exports/lit-repeat-fix.d.ts} +16 -45
  72. package/dist/lit-exports/lit-repeat-fix.js +37 -0
  73. package/dist/readme-examples/my-app.element.d.ts +1 -0
  74. package/{src/readme-examples/my-app.element.ts → dist/readme-examples/my-app.element.js} +4 -5
  75. package/dist/readme-examples/my-custom-action.event.d.ts +1 -0
  76. package/dist/readme-examples/my-custom-action.event.js +2 -0
  77. package/dist/readme-examples/my-custom-define.d.ts +4 -0
  78. package/{src/readme-examples/my-custom-define.ts → dist/readme-examples/my-custom-define.js} +4 -9
  79. package/dist/readme-examples/my-simple.element.d.ts +1 -0
  80. package/{src/readme-examples/my-simple.element.ts → dist/readme-examples/my-simple.element.js} +3 -4
  81. package/dist/readme-examples/my-with-assignment.element.d.ts +1 -0
  82. package/dist/readme-examples/my-with-assignment.element.js +15 -0
  83. package/dist/readme-examples/my-with-async-prop.element.d.ts +10 -0
  84. package/{src/readme-examples/my-with-async-prop.element.ts → dist/readme-examples/my-with-async-prop.element.js} +16 -24
  85. package/dist/readme-examples/my-with-cleanup-callback.element.d.ts +3 -0
  86. package/{src/readme-examples/my-with-cleanup-callback.element.ts → dist/readme-examples/my-with-cleanup-callback.element.js} +4 -5
  87. package/dist/readme-examples/my-with-css-vars.element.d.ts +1 -0
  88. package/{src/readme-examples/my-with-css-vars.element.ts → dist/readme-examples/my-with-css-vars.element.js} +4 -5
  89. package/dist/readme-examples/my-with-custom-events.element.d.ts +1 -0
  90. package/dist/readme-examples/my-with-custom-events.element.js +22 -0
  91. package/dist/readme-examples/my-with-event-listening.element.d.ts +3 -0
  92. package/{src/readme-examples/my-with-event-listening.element.ts → dist/readme-examples/my-with-event-listening.element.js} +9 -10
  93. package/dist/readme-examples/my-with-events.element.d.ts +4 -0
  94. package/dist/readme-examples/my-with-events.element.js +20 -0
  95. package/dist/readme-examples/my-with-host-class-definition.element.d.ts +3 -0
  96. package/{src/readme-examples/my-with-host-class-definition.element.ts → dist/readme-examples/my-with-host-class-definition.element.js} +6 -7
  97. package/dist/readme-examples/my-with-host-class-usage.element.d.ts +1 -0
  98. package/{src/readme-examples/my-with-host-class-usage.element.ts → dist/readme-examples/my-with-host-class-usage.element.js} +4 -5
  99. package/dist/readme-examples/my-with-inputs.element.d.ts +4 -0
  100. package/dist/readme-examples/my-with-inputs.element.js +9 -0
  101. package/dist/readme-examples/my-with-on-dom-created.element.d.ts +1 -0
  102. package/{src/readme-examples/my-with-on-dom-created.element.ts → dist/readme-examples/my-with-on-dom-created.element.js} +6 -7
  103. package/dist/readme-examples/my-with-on-resize.element.d.ts +1 -0
  104. package/dist/readme-examples/my-with-on-resize.element.js +18 -0
  105. package/dist/readme-examples/my-with-render-if.element.d.ts +3 -0
  106. package/dist/readme-examples/my-with-render-if.element.js +11 -0
  107. package/dist/readme-examples/my-with-styles-and-interpolated-selector.element.d.ts +1 -0
  108. 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
  109. package/dist/readme-examples/my-with-styles.element.d.ts +1 -0
  110. package/{src/readme-examples/my-with-styles.element.ts → dist/readme-examples/my-with-styles.element.js} +4 -5
  111. package/dist/readme-examples/my-with-update-state.element.d.ts +8 -0
  112. package/{src/readme-examples/my-with-update-state.element.ts → dist/readme-examples/my-with-update-state.element.js} +7 -8
  113. package/dist/readme-examples/require-declarative-element.d.ts +1 -0
  114. package/dist/readme-examples/require-declarative-element.js +2 -0
  115. package/dist/require-declarative-element.d.ts +14 -0
  116. package/{src/require-declarative-element.ts → dist/require-declarative-element.js} +0 -1
  117. package/{src/template-transforms/minimal-element-definition.ts → dist/template-transforms/minimal-element-definition.d.ts} +7 -19
  118. package/dist/template-transforms/minimal-element-definition.js +19 -0
  119. package/dist/template-transforms/nested-mapped-templates.d.ts +6 -0
  120. package/dist/template-transforms/nested-mapped-templates.js +96 -0
  121. package/{src/template-transforms/template-transform-type.ts → dist/template-transforms/template-transform-type.d.ts} +1 -3
  122. package/dist/template-transforms/template-transform-type.js +1 -0
  123. package/dist/template-transforms/transform-template.d.ts +14 -0
  124. package/{src/template-transforms/transform-template.ts → dist/template-transforms/transform-template.js} +22 -70
  125. package/dist/template-transforms/vir-css/css-transform.d.ts +4 -0
  126. package/dist/template-transforms/vir-css/css-transform.js +15 -0
  127. package/dist/template-transforms/vir-css/vir-css.d.ts +12 -0
  128. package/dist/template-transforms/vir-css/vir-css.js +21 -0
  129. package/dist/template-transforms/vir-html/html-interpolation.d.ts +42 -0
  130. package/dist/template-transforms/vir-html/html-interpolation.js +1 -0
  131. package/dist/template-transforms/vir-html/html-transform.d.ts +5 -0
  132. package/dist/template-transforms/vir-html/html-transform.js +96 -0
  133. package/dist/template-transforms/vir-html/tag-name-keys.d.ts +7 -0
  134. package/{src/template-transforms/vir-html/tag-name-keys.ts → dist/template-transforms/vir-html/tag-name-keys.js} +1 -1
  135. package/dist/template-transforms/vir-html/vir-html.d.ts +11 -0
  136. package/{src/template-transforms/vir-html/vir-html.ts → dist/template-transforms/vir-html/vir-html.js} +5 -13
  137. package/dist/typed-event/typed-event.d.ts +55 -0
  138. package/dist/typed-event/typed-event.js +50 -0
  139. package/dist/util/array.d.ts +5 -0
  140. package/{src/util/array.ts → dist/util/array.js} +5 -18
  141. package/{src/util/increment.ts → dist/util/increment.d.ts} +5 -24
  142. package/dist/util/increment.js +1 -0
  143. package/dist/util/lit-template.d.ts +9 -0
  144. package/{src/util/lit-template.ts → dist/util/lit-template.js} +10 -30
  145. package/dist/util/map-async-value.d.ts +7 -0
  146. package/{src/util/map-async-value.ts → dist/util/map-async-value.js} +10 -12
  147. package/dist/util/type.js +1 -0
  148. package/package.json +4 -4
  149. package/src/declarative-element/declarative-element-init.ts +0 -115
  150. package/src/declarative-element/declarative-element.ts +0 -372
  151. package/src/declarative-element/define-element.ts +0 -515
  152. package/src/declarative-element/directives/assign.directive.ts +0 -89
  153. package/src/declarative-element/directives/attributes.directive.ts +0 -63
  154. package/src/declarative-element/directives/create-attribute-directive.ts +0 -47
  155. package/src/declarative-element/directives/directive-helpers.ts +0 -67
  156. package/src/declarative-element/directives/listen.directive.ts +0 -206
  157. package/src/declarative-element/directives/mutate.directive.ts +0 -78
  158. package/src/declarative-element/directives/on-dom-created.directive.ts +0 -68
  159. package/src/declarative-element/directives/on-dom-rendered.directive.ts +0 -61
  160. package/src/declarative-element/directives/on-intersect.directive.ts +0 -139
  161. package/src/declarative-element/directives/on-resize.directive.ts +0 -142
  162. package/src/declarative-element/directives/render-async.directive.ts +0 -111
  163. package/src/declarative-element/properties/assign-inputs.ts +0 -30
  164. package/src/declarative-element/properties/css-vars.ts +0 -24
  165. package/src/declarative-element/properties/element-events.ts +0 -161
  166. package/src/declarative-element/properties/host-classes.ts +0 -63
  167. package/src/declarative-element/properties/string-names.ts +0 -83
  168. package/src/declarative-element/properties/styles.ts +0 -112
  169. package/src/declarative-element/render-callback.ts +0 -196
  170. package/src/declarative-element/wrap-define-element.ts +0 -127
  171. package/src/readme-examples/my-custom-action.event.ts +0 -3
  172. package/src/readme-examples/my-with-assignment.element.ts +0 -16
  173. package/src/readme-examples/my-with-custom-events.element.ts +0 -23
  174. package/src/readme-examples/my-with-events.element.ts +0 -23
  175. package/src/readme-examples/my-with-inputs.element.ts +0 -13
  176. package/src/readme-examples/my-with-on-resize.element.ts +0 -19
  177. package/src/readme-examples/my-with-render-if.element.ts +0 -15
  178. package/src/readme-examples/require-declarative-element.ts +0 -3
  179. package/src/template-transforms/nested-mapped-templates.ts +0 -157
  180. package/src/template-transforms/vir-css/css-transform.ts +0 -30
  181. package/src/template-transforms/vir-css/vir-css.ts +0 -30
  182. package/src/template-transforms/vir-html/html-interpolation.ts +0 -103
  183. package/src/template-transforms/vir-html/html-transform.ts +0 -149
  184. package/src/typed-event/typed-event.ts +0 -90
  185. /package/{src/declarative-element/custom-tag-name.ts → dist/declarative-element/custom-tag-name.d.ts} +0 -0
  186. /package/{src/declarative-element/properties/element-properties.ts → dist/declarative-element/properties/element-properties.d.ts} +0 -0
  187. /package/{src/declarative-element/properties/tag-name.ts → dist/declarative-element/properties/tag-name.d.ts} +0 -0
  188. /package/{src/lit-exports/all-lit-exports.ts → dist/lit-exports/all-lit-exports.d.ts} +0 -0
  189. /package/{src/util/type.ts → dist/util/type.d.ts} +0 -0
@@ -1,157 +0,0 @@
1
- import {check} from '@augment-vir/assert';
2
- import {filterMap} from '@augment-vir/common';
3
- import {hasTagName, isMinimalDefinitionWithInputs} from './minimal-element-definition.js';
4
- import {type TemplateTransform} from './template-transform-type.js';
5
-
6
- type WeakMapElementKey = {
7
- tagName: string;
8
- };
9
-
10
- type TemplateAndNested = {
11
- template: TemplateTransform | undefined;
12
- nested: NestedTemplatesWeakMap | undefined;
13
- };
14
- type NestedTemplatesWeakMap = WeakMap<WeakMapElementKey, TemplateAndNested>;
15
- type TemplatesWeakMap = WeakMap<TemplateStringsArray, TemplateAndNested>;
16
-
17
- function extractElementKeys(values: unknown[]): WeakMapElementKey[] {
18
- return filterMap(
19
- values,
20
- (value): WeakMapElementKey | undefined => {
21
- if (isMinimalDefinitionWithInputs(value)) {
22
- return value.definition;
23
- }
24
- if (hasTagName(value)) {
25
- return value.tagInterpolationKey || value;
26
- }
27
-
28
- return undefined;
29
- },
30
- check.isTruthy,
31
- );
32
- }
33
-
34
- /**
35
- * The transformed templates are written to a map so that we can preserve reference equality between
36
- * calls. Without maintaining reference equality between html`` calls, lit-element reconstructs all
37
- * of its children on every render.
38
- *
39
- * This is a WeakMap because we only care about the transformed array value as long as the original
40
- * template array key exists.
41
- */
42
- const transformedTemplateStrings: TemplatesWeakMap = new WeakMap();
43
-
44
- export function getAlreadyMappedTemplate<PossibleValues>(
45
- templateStringsKey: TemplateStringsArray,
46
- values: PossibleValues[],
47
- ) {
48
- const elementKeys = extractElementKeys(values);
49
- const nestedValue = getNestedValues(transformedTemplateStrings, [
50
- templateStringsKey,
51
- ...elementKeys,
52
- ]);
53
- return nestedValue.value?.template;
54
- }
55
-
56
- export function setMappedTemplate<PossibleValues>(
57
- templateStringsKey: TemplateStringsArray,
58
- values: PossibleValues[],
59
- valueToSet: TemplateTransform,
60
- ) {
61
- const elementKeys = extractElementKeys(values);
62
- return setNestedValues(
63
- transformedTemplateStrings,
64
- [
65
- templateStringsKey,
66
- ...elementKeys,
67
- ],
68
- valueToSet,
69
- );
70
- }
71
-
72
- function getNestedValues(
73
- map: TemplatesWeakMap | NestedTemplatesWeakMap,
74
- keys: (TemplateStringsArray | WeakMapElementKey)[],
75
- index = 0,
76
- ): {value: undefined | TemplateAndNested; reason: string} {
77
- const {currentTemplateAndNested, reason} = getCurrentKeyAndValue(map, keys, index);
78
- if (!currentTemplateAndNested) {
79
- return {value: currentTemplateAndNested, reason};
80
- }
81
-
82
- if (index === keys.length - 1) {
83
- return {value: currentTemplateAndNested, reason: `reached end of keys array`};
84
- }
85
-
86
- if (!currentTemplateAndNested.nested) {
87
- return {value: undefined, reason: `map at key index ${index} did not have nested maps`};
88
- }
89
-
90
- return getNestedValues(currentTemplateAndNested.nested, keys, index + 1);
91
- }
92
-
93
- function getCurrentKeyAndValue(
94
- map: TemplatesWeakMap | NestedTemplatesWeakMap,
95
- keys: (TemplateStringsArray | WeakMapElementKey)[],
96
- index: number,
97
- ): {
98
- currentKey: TemplateStringsArray | WeakMapElementKey | undefined;
99
- currentTemplateAndNested: TemplateAndNested | undefined;
100
- reason: string;
101
- } {
102
- const currentKey = keys[index];
103
- if (currentKey == undefined) {
104
- return {
105
- currentKey: undefined,
106
- currentTemplateAndNested: undefined,
107
- reason: `key at index ${index} not found`,
108
- };
109
- }
110
- if (!map.has(currentKey as any)) {
111
- return {
112
- currentKey,
113
- currentTemplateAndNested: undefined,
114
- reason: `key at index ${index} was not in the map`,
115
- };
116
- }
117
- const currentTemplateAndNested = map.get(currentKey as any);
118
- if (currentTemplateAndNested == undefined) {
119
- return {
120
- currentKey,
121
- currentTemplateAndNested: undefined,
122
- reason: `value at key at index ${index} was undefined`,
123
- };
124
- }
125
-
126
- return {currentKey, currentTemplateAndNested, reason: `key and value exists`};
127
- }
128
-
129
- function setNestedValues(
130
- map: TemplatesWeakMap | NestedTemplatesWeakMap,
131
- keys: (TemplateStringsArray | WeakMapElementKey)[],
132
- valueToSet: TemplateTransform,
133
- index = 0,
134
- ): {result: boolean; reason: string} {
135
- const {currentTemplateAndNested, currentKey, reason} = getCurrentKeyAndValue(map, keys, index);
136
- if (!currentKey) {
137
- return {result: false, reason};
138
- }
139
-
140
- const nestedAndTemplate = currentTemplateAndNested ?? {nested: undefined, template: undefined};
141
- if (!currentTemplateAndNested) {
142
- map.set(currentKey as any, nestedAndTemplate);
143
- }
144
-
145
- if (index === keys.length - 1) {
146
- nestedAndTemplate.template = valueToSet;
147
- return {result: true, reason: `set value at end of keys array`};
148
- }
149
-
150
- const nestedWeakMap = nestedAndTemplate.nested ?? new WeakMap();
151
-
152
- if (!nestedAndTemplate.nested) {
153
- nestedAndTemplate.nested = nestedWeakMap;
154
- }
155
-
156
- return setNestedValues(nestedWeakMap, keys, valueToSet, index + 1);
157
- }
@@ -1,30 +0,0 @@
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
- }
@@ -1,30 +0,0 @@
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
- }
@@ -1,103 +0,0 @@
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;
@@ -1,149 +0,0 @@
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
- }
@@ -1,90 +0,0 @@
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
- }
File without changes