element-vir 22.2.2 → 23.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +44 -38
  2. package/dist/declarative-element/custom-tag-name.d.ts +6 -0
  3. package/dist/declarative-element/declarative-element-init.d.ts +20 -14
  4. package/dist/declarative-element/declarative-element.d.ts +70 -24
  5. package/dist/declarative-element/declarative-element.js +34 -1
  6. package/dist/declarative-element/define-element-no-inputs.d.ts +31 -6
  7. package/dist/declarative-element/define-element-no-inputs.js +154 -257
  8. package/dist/declarative-element/define-element.d.ts +35 -7
  9. package/dist/declarative-element/define-element.js +26 -4
  10. package/dist/declarative-element/definition-options.d.ts +10 -0
  11. package/dist/declarative-element/definition-options.js +5 -0
  12. package/dist/declarative-element/directives/assign.directive.d.ts +8 -11
  13. package/dist/declarative-element/directives/assign.directive.js +6 -13
  14. package/dist/declarative-element/directives/async-prop.d.ts +19 -4
  15. package/dist/declarative-element/directives/async-prop.js +14 -4
  16. package/dist/declarative-element/directives/create-attribute-directive.d.ts +19 -4
  17. package/dist/declarative-element/directives/create-attribute-directive.js +18 -8
  18. package/dist/declarative-element/directives/directive-helpers.d.ts +19 -4
  19. package/dist/declarative-element/directives/directive-helpers.js +14 -2
  20. package/dist/declarative-element/directives/is-resolved.directive.d.ts +107 -1
  21. package/dist/declarative-element/directives/is-resolved.directive.js +107 -1
  22. package/dist/declarative-element/directives/listen.directive.d.ts +84 -10
  23. package/dist/declarative-element/directives/listen.directive.js +5 -15
  24. package/dist/declarative-element/directives/on-dom-created.directive.d.ts +37 -4
  25. package/dist/declarative-element/directives/on-dom-created.directive.js +32 -12
  26. package/dist/declarative-element/directives/on-dom-rendered.directive.d.ts +33 -3
  27. package/dist/declarative-element/directives/on-dom-rendered.directive.js +29 -6
  28. package/dist/declarative-element/directives/on-resize.directive.d.ts +37 -4
  29. package/dist/declarative-element/directives/on-resize.directive.js +37 -24
  30. package/dist/declarative-element/directives/render-async.directive.d.ts +44 -4
  31. package/dist/declarative-element/directives/render-async.directive.js +13 -3
  32. package/dist/declarative-element/directives/render-if.directive.d.ts +31 -0
  33. package/dist/declarative-element/directives/render-if.directive.js +32 -1
  34. package/dist/declarative-element/directives/test-id.directive.d.ts +48 -4
  35. package/dist/declarative-element/directives/test-id.directive.js +47 -2
  36. package/dist/declarative-element/has-declarative-element-parent.js +1 -1
  37. package/dist/declarative-element/is-declarative-element-definition.d.ts +15 -1
  38. package/dist/declarative-element/is-declarative-element-definition.js +21 -18
  39. package/dist/declarative-element/is-declarative-element.d.ts +5 -1
  40. package/dist/declarative-element/is-declarative-element.js +7 -3
  41. package/dist/declarative-element/properties/assign-inputs.js +1 -1
  42. package/dist/declarative-element/properties/css-properties.d.ts +11 -1
  43. package/dist/declarative-element/properties/css-properties.js +5 -0
  44. package/dist/declarative-element/properties/css-vars.d.ts +14 -4
  45. package/dist/declarative-element/properties/element-events.d.ts +61 -7
  46. package/dist/declarative-element/properties/element-events.js +40 -5
  47. package/dist/declarative-element/properties/element-properties.d.ts +15 -6
  48. package/dist/declarative-element/properties/element-properties.js +1 -21
  49. package/dist/declarative-element/properties/element-vir-state-setup.d.ts +43 -3
  50. package/dist/declarative-element/properties/element-vir-state-setup.js +17 -3
  51. package/dist/declarative-element/properties/host-classes.d.ts +25 -12
  52. package/dist/declarative-element/properties/host-classes.js +5 -0
  53. package/dist/declarative-element/properties/per-instance.d.ts +23 -2
  54. package/dist/declarative-element/properties/per-instance.js +24 -3
  55. package/dist/declarative-element/properties/property-proxy.d.ts +19 -4
  56. package/dist/declarative-element/properties/property-proxy.js +14 -4
  57. package/dist/declarative-element/properties/styles.d.ts +33 -8
  58. package/dist/declarative-element/properties/styles.js +12 -2
  59. package/dist/declarative-element/properties/tag-name.d.ts +5 -0
  60. package/dist/declarative-element/render-callback.d.ts +38 -11
  61. package/dist/declarative-element/render-callback.js +6 -0
  62. package/dist/declarative-element/slot-names.d.ts +10 -2
  63. package/dist/declarative-element/slot-names.js +5 -1
  64. package/dist/declarative-element/wrap-define-element.d.ts +38 -11
  65. package/dist/declarative-element/wrap-define-element.js +17 -2
  66. package/dist/index.d.ts +40 -40
  67. package/dist/index.js +40 -39
  68. package/dist/lit-exports/all-lit-exports.d.ts +2 -2
  69. package/dist/lit-exports/all-lit-exports.js +2 -2
  70. package/dist/lit-exports/lit-repeat-fix.d.ts +60 -4
  71. package/dist/lit-exports/lit-repeat-fix.js +35 -1
  72. package/dist/readme-examples/my-app.element.d.ts +1 -0
  73. package/dist/readme-examples/my-app.element.js +11 -0
  74. package/dist/readme-examples/my-custom-action.event.d.ts +1 -0
  75. package/dist/readme-examples/my-custom-action.event.js +2 -0
  76. package/dist/readme-examples/my-custom-define.d.ts +4 -0
  77. package/dist/readme-examples/my-custom-define.js +19 -0
  78. package/dist/readme-examples/my-simple.element.d.ts +1 -0
  79. package/dist/readme-examples/my-simple.element.js +9 -0
  80. package/dist/readme-examples/my-with-assignment.element.d.ts +1 -0
  81. package/dist/readme-examples/my-with-assignment.element.js +14 -0
  82. package/dist/readme-examples/my-with-async-prop.element.d.ts +9 -0
  83. package/dist/readme-examples/my-with-async-prop.element.js +46 -0
  84. package/dist/readme-examples/my-with-cleanup-callback.element.d.ts +3 -0
  85. package/dist/readme-examples/my-with-cleanup-callback.element.js +23 -0
  86. package/dist/readme-examples/my-with-css-vars.element.d.ts +1 -0
  87. package/dist/readme-examples/my-with-css-vars.element.js +24 -0
  88. package/dist/readme-examples/my-with-custom-events.element.d.ts +1 -0
  89. package/dist/readme-examples/my-with-custom-events.element.js +21 -0
  90. package/dist/readme-examples/my-with-event-listening.element.d.ts +3 -0
  91. package/dist/readme-examples/my-with-event-listening.element.js +22 -0
  92. package/dist/readme-examples/my-with-events.element.d.ts +4 -0
  93. package/dist/readme-examples/my-with-events.element.js +19 -0
  94. package/dist/readme-examples/my-with-host-class-definition.element.d.ts +3 -0
  95. package/dist/readme-examples/my-with-host-class-definition.element.js +39 -0
  96. package/dist/readme-examples/my-with-host-class-usage.element.d.ts +1 -0
  97. package/dist/readme-examples/my-with-host-class-usage.element.js +12 -0
  98. package/dist/readme-examples/my-with-inputs.element.d.ts +4 -0
  99. package/dist/readme-examples/my-with-inputs.element.js +9 -0
  100. package/dist/readme-examples/my-with-on-dom-created.element.d.ts +1 -0
  101. package/dist/readme-examples/my-with-on-dom-created.element.js +16 -0
  102. package/dist/readme-examples/my-with-on-resize.element.d.ts +1 -0
  103. package/dist/readme-examples/my-with-on-resize.element.js +17 -0
  104. package/dist/readme-examples/my-with-render-if.element.d.ts +3 -0
  105. package/dist/readme-examples/my-with-render-if.element.js +11 -0
  106. package/dist/readme-examples/my-with-styles-and-interpolated-selector.element.d.ts +1 -0
  107. package/dist/readme-examples/my-with-styles-and-interpolated-selector.element.js +15 -0
  108. package/dist/readme-examples/my-with-styles.element.d.ts +1 -0
  109. package/dist/readme-examples/my-with-styles.element.js +21 -0
  110. package/dist/readme-examples/my-with-update-state.element.d.ts +8 -0
  111. package/dist/readme-examples/my-with-update-state.element.js +23 -0
  112. package/dist/readme-examples/require-declarative-element.d.ts +1 -0
  113. package/dist/readme-examples/require-declarative-element.js +2 -0
  114. package/dist/require-declarative-element.d.ts +12 -0
  115. package/dist/require-declarative-element.js +12 -0
  116. package/dist/template-transforms/minimal-element-definition.d.ts +30 -2
  117. package/dist/template-transforms/minimal-element-definition.js +14 -3
  118. package/dist/template-transforms/nested-mapped-templates.d.ts +1 -1
  119. package/dist/template-transforms/nested-mapped-templates.js +4 -3
  120. package/dist/template-transforms/template-transform-type.d.ts +1 -1
  121. package/dist/template-transforms/transform-template.d.ts +1 -1
  122. package/dist/template-transforms/transform-template.js +17 -21
  123. package/dist/template-transforms/vir-css/css-transform.d.ts +3 -3
  124. package/dist/template-transforms/vir-css/css-transform.js +3 -2
  125. package/dist/template-transforms/vir-css/vir-css.d.ts +11 -2
  126. package/dist/template-transforms/vir-css/vir-css.js +11 -2
  127. package/dist/template-transforms/vir-html/html-interpolation.d.ts +9 -5
  128. package/dist/template-transforms/vir-html/html-transform.d.ts +3 -3
  129. package/dist/template-transforms/vir-html/html-transform.js +14 -12
  130. package/dist/template-transforms/vir-html/vir-html.d.ts +4 -2
  131. package/dist/template-transforms/vir-html/vir-html.js +4 -2
  132. package/dist/typed-event/typed-event.d.ts +42 -6
  133. package/dist/typed-event/typed-event.js +31 -28
  134. package/dist/util/lit-template.d.ts +6 -1
  135. package/dist/util/lit-template.js +10 -7
  136. package/dist/util/type.d.ts +5 -0
  137. package/package.json +37 -35
package/README.md CHANGED
@@ -36,7 +36,7 @@ All of [`lit`](https://lit.dev)'s syntax and functionality is available for use
36
36
 
37
37
  ## Simple element definition
38
38
 
39
- Use `defineElementNoInputs` to define your element if it's not going to accept any inputs (or if you're just getting started). It's only input is an object with at least `tagName` and `renderCallback` properties (the types enforce this). Here is a bare-minimum example custom element:
39
+ Use `defineElementNoInputs` to define your element if it's not going to accept any inputs (or if you're just getting started). It's only input is an object with at least `tagName` and `render` properties (the types enforce this). Here is a bare-minimum example custom element:
40
40
 
41
41
  <!-- example-link: src/readme-examples/my-simple.element.ts -->
42
42
 
@@ -45,7 +45,7 @@ import {defineElementNoInputs, html} from 'element-vir';
45
45
 
46
46
  export const MySimple = defineElementNoInputs({
47
47
  tagName: 'my-simple',
48
- renderCallback() {
48
+ render() {
49
49
  return html`
50
50
  <span>Hello there!</span>
51
51
  `;
@@ -63,11 +63,11 @@ To use already defined elements (like the example above), they must be interpola
63
63
 
64
64
  ```TypeScript
65
65
  import {defineElementNoInputs, html} from 'element-vir';
66
- import {MySimple} from './my-simple.element';
66
+ import {MySimple} from './my-simple.element.js';
67
67
 
68
68
  export const MyApp = defineElementNoInputs({
69
69
  tagName: 'my-app',
70
- renderCallback() {
70
+ render() {
71
71
  return html`
72
72
  <h1>My App</h1>
73
73
  <${MySimple}></${MySimple}>
@@ -100,7 +100,7 @@ export const MyWithStyles = defineElementNoInputs({
100
100
  margin-top: 16px;
101
101
  }
102
102
  `,
103
- renderCallback() {
103
+ render() {
104
104
  return html`
105
105
  <span>Hello there!</span>
106
106
  <span>How are you doing?</span>
@@ -117,7 +117,7 @@ Declarative element definitions can be used in the `css` tagged template just li
117
117
 
118
118
  ```TypeScript
119
119
  import {css, defineElementNoInputs, html} from 'element-vir';
120
- import {MySimple} from './my-simple.element';
120
+ import {MySimple} from './my-simple.element.js';
121
121
 
122
122
  export const MyWithStylesAndInterpolatedSelector = defineElementNoInputs({
123
123
  tagName: 'my-with-styles-and-interpolated-selector',
@@ -126,7 +126,7 @@ export const MyWithStylesAndInterpolatedSelector = defineElementNoInputs({
126
126
  background-color: blue;
127
127
  }
128
128
  `,
129
- renderCallback() {
129
+ render() {
130
130
  return html`
131
131
  <${MySimple}></${MySimple}>
132
132
  `;
@@ -138,7 +138,7 @@ export const MyWithStylesAndInterpolatedSelector = defineElementNoInputs({
138
138
 
139
139
  Define element inputs by using `defineElement` to define a declarative element. Pass your input type as a generic to the `defineElement` call. Then call _that_ with the normal definition input (like when using `defineElementNoInputs`).
140
140
 
141
- To use an element's inputs for use in its template, grab `inputs` from `renderCallback`'s parameters and interpolate it into your HTML template:
141
+ To use an element's inputs for use in its template, grab `inputs` from `render`'s parameters and interpolate it into your HTML template:
142
142
 
143
143
  <!-- example-link: src/readme-examples/my-with-inputs.element.ts -->
144
144
 
@@ -150,7 +150,7 @@ export const MyWithInputs = defineElement<{
150
150
  email: string;
151
151
  }>()({
152
152
  tagName: 'my-with-inputs',
153
- renderCallback({inputs}) {
153
+ render({inputs}) {
154
154
  return html`
155
155
  <span>Hello there ${inputs.username}!</span>
156
156
  `;
@@ -160,7 +160,7 @@ export const MyWithInputs = defineElement<{
160
160
 
161
161
  ## Defining internal state
162
162
 
163
- Define initial internal state values and types with the `stateInit` property when defining an element. Grab it with `state` in `renderCallback` to use state. Grab `updateState` in `renderCallback` to update state:
163
+ Define initial internal state values and types with the `stateInit` property when defining an element. Grab it with `state` in `render` to use state. Grab `updateState` in `render` to update state:
164
164
 
165
165
  <!-- example-link: src/readme-examples/my-with-update-state.element.ts -->
166
166
 
@@ -177,7 +177,7 @@ export const MyWithUpdateState = defineElementNoInputs({
177
177
  */
178
178
  email: undefined as string | undefined,
179
179
  },
180
- renderCallback({state, updateState}) {
180
+ render({state, updateState}) {
181
181
  return html`
182
182
  <span
183
183
  ${listen('click', () => {
@@ -199,11 +199,11 @@ Use the `assign` directive to assign values to child custom elements inputs:
199
199
 
200
200
  ```TypeScript
201
201
  import {defineElementNoInputs, html} from 'element-vir';
202
- import {MyWithInputs} from './my-with-inputs.element';
202
+ import {MyWithInputs} from './my-with-inputs.element.js';
203
203
 
204
204
  export const MyWithAssignment = defineElementNoInputs({
205
205
  tagName: 'my-with-assignment',
206
- renderCallback() {
206
+ render() {
207
207
  return html`
208
208
  <h1>My App</h1>
209
209
  <${MyWithInputs.assign({
@@ -219,8 +219,8 @@ export const MyWithAssignment = defineElementNoInputs({
219
219
 
220
220
  There are two other callbacks you can define that are sort of similar to lifecycle callbacks. They are much simpler than lifecycle callbacks however.
221
221
 
222
- - `initCallback`: called right before the first render and has all state and inputs setup. (This is similar to `connectedCallback` in standard HTMLElement classes but is fired much later, after inputs are assigned, to avoid race conditions.)
223
- - `cleanupCallback`: called when an element is removed from the DOM. (This is the same as the `disconnectedCallback` in standard HTMLElement classes.)
222
+ - `init`: called right before the first render and has all state and inputs setup. (This is similar to `connectedCallback` in standard HTMLElement classes but is fired much later, after inputs are assigned, to avoid race conditions.)
223
+ - `cleanup`: called when an element is removed from the DOM. (This is the same as the `disconnectedCallback` in standard HTMLElement classes.)
224
224
 
225
225
  <!-- example-link: src/readme-examples/my-with-cleanup-callback.element.ts -->
226
226
 
@@ -232,17 +232,17 @@ export const MyWithAssignmentCleanupCallback = defineElementNoInputs({
232
232
  stateInitStatic: {
233
233
  intervalId: undefined as undefined | number,
234
234
  },
235
- initCallback: ({updateState}) => {
235
+ init: ({updateState}) => {
236
236
  updateState({
237
237
  intervalId: window.setInterval(() => console.info('hi'), 1000),
238
238
  });
239
239
  },
240
- renderCallback() {
240
+ render() {
241
241
  return html`
242
242
  <h1>My App</h1>
243
243
  `;
244
244
  },
245
- cleanupCallback: ({state, updateState}) => {
245
+ cleanup: ({state, updateState}) => {
246
246
  window.clearInterval(state.intervalId);
247
247
  updateState({
248
248
  intervalId: undefined,
@@ -255,11 +255,12 @@ export const MyWithAssignmentCleanupCallback = defineElementNoInputs({
255
255
 
256
256
  When defining a declarative element, use `events` to setup event names and types. Each event must be initialized with `defineElementEvent` and a type parameter but no run-time inputs.
257
257
 
258
- To dispatch an event, grab `dispatch` and `events` from `renderCallback`'s parameters.
258
+ To dispatch an event, grab `dispatch` and `events` from `render`'s parameters.
259
259
 
260
260
  <!-- example-link: src/readme-examples/my-with-events.element.ts -->
261
261
 
262
262
  ```TypeScript
263
+ import {randomInteger} from '@augment-vir/common';
263
264
  import {defineElementEvent, defineElementNoInputs, html, listen} from 'element-vir';
264
265
 
265
266
  export const MyWithEvents = defineElementNoInputs({
@@ -268,10 +269,14 @@ export const MyWithEvents = defineElementNoInputs({
268
269
  logoutClick: defineElementEvent<void>(),
269
270
  randomNumber: defineElementEvent<number>(),
270
271
  },
271
- renderCallback({dispatch, events}) {
272
+ render({dispatch, events}) {
272
273
  return html`
273
274
  <button ${listen('click', () => dispatch(new events.logoutClick()))}>log out</button>
274
- <button ${listen('click', () => dispatch(new events.randomNumber(Math.random())))}>
275
+ <button
276
+ ${listen('click', () =>
277
+ dispatch(new events.randomNumber(randomInteger({min: 0, max: 1_000_000}))),
278
+ )}
279
+ >
275
280
  generate random number
276
281
  </button>
277
282
  `;
@@ -287,14 +292,14 @@ Use the `listen` directive to listen to events emitted by your custom elements:
287
292
 
288
293
  ```TypeScript
289
294
  import {defineElementNoInputs, html, listen} from 'element-vir';
290
- import {MyWithEvents} from './my-with-events.element';
295
+ import {MyWithEvents} from './my-with-events.element.js';
291
296
 
292
297
  export const MyWithEventListening = defineElementNoInputs({
293
298
  tagName: 'my-with-event-listening',
294
299
  stateInitStatic: {
295
300
  myNumber: -1,
296
301
  },
297
- renderCallback({state, updateState}) {
302
+ render({state, updateState}) {
298
303
  return html`
299
304
  <h1>My App</h1>
300
305
  <${MyWithEvents}
@@ -332,12 +337,13 @@ Dispatching a custom event and listening to a custom event is the same as doing
332
337
  <!-- example-link: src/readme-examples/my-with-custom-events.element.ts -->
333
338
 
334
339
  ```TypeScript
340
+ import {randomInteger} from '@augment-vir/common';
335
341
  import {defineElementNoInputs, html, listen} from 'element-vir';
336
- import {MyCustomActionEvent} from './my-custom-action.event';
342
+ import {MyCustomActionEvent} from './my-custom-action.event.js';
337
343
 
338
344
  export const MyWithCustomEvents = defineElementNoInputs({
339
345
  tagName: 'my-with-custom-events',
340
- renderCallback({dispatch}) {
346
+ render({dispatch}) {
341
347
  return html`
342
348
  <div
343
349
  ${listen(MyCustomActionEvent, (event) => {
@@ -346,7 +352,7 @@ export const MyWithCustomEvents = defineElementNoInputs({
346
352
  >
347
353
  <div
348
354
  ${listen('click', () => {
349
- dispatch(new MyCustomActionEvent(Math.random()));
355
+ dispatch(new MyCustomActionEvent(randomInteger({min: 0, max: 1_000_000})));
350
356
  })}
351
357
  ></div>
352
358
  </div>
@@ -361,10 +367,10 @@ export const MyWithCustomEvents = defineElementNoInputs({
361
367
 
362
368
  Host classes can be defined and used with type safety. Host classes are used to provide alternative styles for custom elements. They are purely driven by CSS and are thus applied to the the `class` HTML attribute.
363
369
 
364
- Host classes are defined by passing an object to `hostClasses` at element definition time. Each property name in the `hostClasses` object creates a host class name (note that host class names must start with the element's tag name). Each value in the `hostClasses` object defines behavior for teh host class:
370
+ Host classes are defined by passing an object to `hostClasses` at element definition time. Each property name in the `hostClasses` object creates a host class name (note that host class names must start with the element's tag name). Each value in the `hostClasses` object defines behavior for the host class:
365
371
 
366
- - if the value is a callback, that host class will automatically be applied if the callback returns true after a render is executed.
367
- - if the value is `false`, the host class is never automatically applied, it must be manually applied by consumers.
372
+ - if the value is a callback, that host class will automatically be applied if the callback returns true after a render is executed.
373
+ - if the value is `false`, the host class is never automatically applied, it must be manually applied by consumers.
368
374
 
369
375
  Apply host classes in the element's stylesheet by using a callback for the styles property:
370
376
 
@@ -386,7 +392,7 @@ export const MyWithHostClassDefinition = defineElementNoInputs({
386
392
  'my-with-host-class-definition-a': false,
387
393
  /**
388
394
  * This host class will be automatically applied if the given callback is evaluated to true
389
- * after a call to renderCallback.
395
+ * after a call to render.
390
396
  */
391
397
  'my-with-host-class-definition-automatic': ({state}) => {
392
398
  return state.myProp === 'foo';
@@ -405,7 +411,7 @@ export const MyWithHostClassDefinition = defineElementNoInputs({
405
411
  color: red;
406
412
  }
407
413
  `,
408
- renderCallback({state}) {
414
+ render({state}) {
409
415
  return html`
410
416
  ${state.myProp}
411
417
  `;
@@ -421,11 +427,11 @@ To apply a host class in a consumer, access the child element's `.hostClasses` p
421
427
 
422
428
  ```TypeScript
423
429
  import {defineElementNoInputs, html} from 'element-vir';
424
- import {MyWithHostClassDefinition} from './my-with-host-class-definition.element';
430
+ import {MyWithHostClassDefinition} from './my-with-host-class-definition.element.js';
425
431
 
426
432
  export const MyWithHostClassUsage = defineElementNoInputs({
427
433
  tagName: 'my-with-host-class-usage',
428
- renderCallback() {
434
+ render() {
429
435
  return html`
430
436
  <${MyWithHostClassDefinition}
431
437
  class=${MyWithHostClassDefinition.hostClasses['my-with-host-class-definition-a']}
@@ -463,7 +469,7 @@ export const MyWithCssVars = defineElementNoInputs({
463
469
  color: ${cssVars['my-with-css-vars-my-var'].value};
464
470
  }
465
471
  `,
466
- renderCallback() {
472
+ render() {
467
473
  return html``;
468
474
  },
469
475
  });
@@ -526,7 +532,7 @@ import {defineElementNoInputs, html, onDomCreated} from 'element-vir';
526
532
 
527
533
  export const MyWithOnDomCreated = defineElementNoInputs({
528
534
  tagName: 'my-with-on-dom-created',
529
- renderCallback() {
535
+ render() {
530
536
  return html`
531
537
  <span
532
538
  ${onDomCreated((element) => {
@@ -552,7 +558,7 @@ import {defineElementNoInputs, html, onResize} from 'element-vir';
552
558
 
553
559
  export const MyWithOnResize = defineElementNoInputs({
554
560
  tagName: 'my-with-on-resize',
555
- renderCallback() {
561
+ render() {
556
562
  return html`
557
563
  <span
558
564
  ${onResize((entry) => {
@@ -583,7 +589,7 @@ import {defineElement, html, renderIf} from 'element-vir';
583
589
 
584
590
  export const MyWithRenderIf = defineElement<{shouldRender: boolean}>()({
585
591
  tagName: 'my-with-render-if',
586
- renderCallback({inputs}) {
592
+ render({inputs}) {
587
593
  return html`
588
594
  ${renderIf(
589
595
  inputs.shouldRender,
@@ -630,7 +636,7 @@ export const MyWithAsyncProp = defineElement<{endpoint: string}>()({
630
636
  },
631
637
  }),
632
638
  },
633
- renderCallback({inputs, state}) {
639
+ render({inputs, state}) {
634
640
  /**
635
641
  * This causes the a promise which automatically updates the state.data prop once the
636
642
  * promise resolves. It only creates a new promise if the first input, the trigger, value
@@ -1 +1,7 @@
1
+ /**
2
+ * The spec-required type for all custom tag names (they must contain a hyphen).
3
+ *
4
+ * @category Internal
5
+ * @see https://developer.mozilla.org/docs/Web/API/Web_components/Using_custom_elements#name
6
+ */
1
7
  export type CustomElementTagName = `${string}-${string}`;
@@ -1,13 +1,19 @@
1
- import { CSSResult } from '../lit-exports/all-lit-exports';
2
- import { CustomElementTagName } from './custom-tag-name';
3
- import { DeclarativeElementDefinitionOptions } from './definition-options';
4
- import { BaseCssPropertyName } from './properties/css-properties';
5
- import { CssVarsInitMap } from './properties/css-vars';
6
- import { EventsInitMap } from './properties/element-events';
7
- import { PropertyInitMapBase } from './properties/element-properties';
8
- import { HostClassesInitMap } from './properties/host-classes';
9
- import { StylesCallback } from './properties/styles';
10
- import { InitCallback, RenderCallback } from './render-callback';
1
+ import { type CSSResult } from '../lit-exports/base-lit-exports.js';
2
+ import { type CustomElementTagName } from './custom-tag-name.js';
3
+ import { type DeclarativeElementDefinitionOptions } from './definition-options.js';
4
+ import { type BaseCssPropertyName } from './properties/css-properties.js';
5
+ import { type CssVarsInitMap } from './properties/css-vars.js';
6
+ import { type EventsInitMap } from './properties/element-events.js';
7
+ import { type PropertyInitMapBase } from './properties/element-properties.js';
8
+ import { type HostClassesInitMap } from './properties/host-classes.js';
9
+ import { type StylesCallback } from './properties/styles.js';
10
+ import { type InitCallback, type RenderCallback } from './render-callback.js';
11
+ /**
12
+ * Initialization for an element-vir declarative element. This defines all the pieces required for
13
+ * rendering the element.
14
+ *
15
+ * @category Internal
16
+ */
11
17
  export type DeclarativeElementInit<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = {
12
18
  /**
13
19
  * HTML tag name. This should not be used directly, as interpolating it with the html tagged
@@ -39,9 +45,9 @@ export type DeclarativeElementInit<TagName extends CustomElementTagName, Inputs
39
45
  * then passed to the styles property, which must be a callback to take advantage of these.
40
46
  */
41
47
  cssVars?: CssVarsInitMap<TagName, CssVarKeys>;
42
- /** Called as part of the first renderCallback call, before the first renderCallback call. */
43
- initCallback?: InitCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
44
- renderCallback: RenderCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
45
- cleanupCallback?: InitCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
48
+ /** Called as part of the first render call, before the first render call. */
49
+ init?: InitCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
50
+ render: RenderCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
51
+ cleanup?: InitCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
46
52
  options?: Partial<DeclarativeElementDefinitionOptions> | undefined;
47
53
  };
@@ -1,30 +1,58 @@
1
- import { RequiredAndNotNullBy } from '@augment-vir/common';
2
- import { IsAny, IsEmptyObject } from 'type-fest';
3
- import { CSSResult, LitElement } from '../lit-exports/all-lit-exports';
4
- import { MinimalDefinitionWithInputs } from '../template-transforms/minimal-element-definition';
5
- import { CustomElementTagName } from './custom-tag-name';
6
- import { DeclarativeElementInit } from './declarative-element-init';
7
- import { DeclarativeElementDefinitionOptions } from './definition-options';
8
- import { BaseCssPropertyName } from './properties/css-properties';
9
- import { CssVars } from './properties/css-vars';
10
- import { EventDescriptorMap, EventsInitMap } from './properties/element-events';
11
- import { ElementPropertyDescriptorMap, PropertyInitMapBase } from './properties/element-properties';
12
- import { FlattenElementVirStateSetup } from './properties/element-vir-state-setup';
13
- import { HostClassNamesMap } from './properties/host-classes';
14
- import { ObservableListenerMap } from './properties/property-proxy';
15
- import type { RenderCallback, RenderParams, UpdateStateCallback } from './render-callback';
16
- import { SlotNameMap } from './slot-names';
17
- export type DeclarativeElementHost<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, StateInit extends PropertyInitMapBase = any, EventsInit extends EventsInitMap = any, HostClassKeys extends BaseCssPropertyName<TagName> = any, CssVarKeys extends BaseCssPropertyName<TagName> = any, SlotNames extends ReadonlyArray<string> = any> = RequiredAndNotNullBy<Omit<DeclarativeElement<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, Exclude<keyof StaticDeclarativeElementProperties<any, any, any, any, any, any, any>, keyof HTMLElement>>, 'shadowRoot'>;
1
+ import { type SetRequiredAndNotNull } from '@augment-vir/common';
2
+ import { type EmptyObject, type IsAny, type IsEmptyObject } from 'type-fest';
3
+ import { LitElement, type CSSResult } from '../lit-exports/base-lit-exports.js';
4
+ import { type MinimalDefinitionWithInputs } from '../template-transforms/minimal-element-definition.js';
5
+ import { type CustomElementTagName } from './custom-tag-name.js';
6
+ import { type DeclarativeElementInit } from './declarative-element-init.js';
7
+ import { type DeclarativeElementDefinitionOptions } from './definition-options.js';
8
+ import { type BaseCssPropertyName } from './properties/css-properties.js';
9
+ import { type CssVars } from './properties/css-vars.js';
10
+ import { type EventDescriptorMap, type EventsInitMap } from './properties/element-events.js';
11
+ import { type ElementPropertyDescriptorMap, type PropertyInitMapBase } from './properties/element-properties.js';
12
+ import { type FlattenElementVirStateSetup } from './properties/element-vir-state-setup.js';
13
+ import { type HostClassNamesMap } from './properties/host-classes.js';
14
+ import { type ObservableListenerMap } from './properties/property-proxy.js';
15
+ import { type RenderCallback, type RenderParams, type UpdateStateCallback } from './render-callback.js';
16
+ import { type SlotNameMap } from './slot-names.js';
17
+ /**
18
+ * The `host` type for a declarative element. This references a declarative element instance's
19
+ * top-level HTML element and always contains a shadow root (wherein the element is rendered).
20
+ *
21
+ * @category Internal
22
+ */
23
+ export type DeclarativeElementHost<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, StateInit extends PropertyInitMapBase = any, EventsInit extends EventsInitMap = any, HostClassKeys extends BaseCssPropertyName<TagName> = any, CssVarKeys extends BaseCssPropertyName<TagName> = any, SlotNames extends ReadonlyArray<string> = any> = SetRequiredAndNotNull<Omit<DeclarativeElement<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, Exclude<keyof StaticDeclarativeElementProperties<any, any, any, any, any, any, any>, keyof HTMLElement>>, 'shadowRoot'>;
24
+ /**
25
+ * The full definition for a declarative element.
26
+ *
27
+ * @category Internal
28
+ */
18
29
  export type DeclarativeElementDefinition<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, StateInit extends PropertyInitMapBase = any, EventsInit extends EventsInitMap = any, HostClassKeys extends BaseCssPropertyName<TagName> = any, CssVarKeys extends BaseCssPropertyName<TagName> = any, SlotNames extends ReadonlyArray<string> = any> = (new () => DeclarativeElementHost<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) & StaticDeclarativeElementProperties<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames> & {
19
30
  instanceType: DeclarativeElementHost<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
20
31
  };
32
+ /**
33
+ * Abstract class base for all declarative elements.
34
+ *
35
+ * @category Internal
36
+ */
21
37
  export declare abstract class DeclarativeElement<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, StateInit extends PropertyInitMapBase = any, EventsInit extends EventsInitMap = any, HostClassKeys extends BaseCssPropertyName<TagName> = any, CssVarKeys extends BaseCssPropertyName<TagName> = any, SlotNames extends ReadonlyArray<string> = any> extends LitElement {
22
- static assign: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['assign'];
23
- static assignedInputs: PropertyInitMapBase | undefined;
38
+ /**
39
+ * Assign inputs to an element instantiation. Use only on the opening tag.
40
+ *
41
+ * @example
42
+ *
43
+ * ```ts
44
+ * import {html} from 'element-vir';
45
+ *
46
+ * const myTemplate = html`
47
+ * <${MyElement.assign({input1: 'a', input2: 'b'})}></${MyElement}>
48
+ * `;
49
+ * ```
50
+ */
51
+ static readonly assign: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['assign'];
52
+ static readonly assignedInputs: PropertyInitMapBase | undefined;
24
53
  static readonly tagName: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['tagName'];
25
54
  static readonly styles: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['styles'];
26
- static readonly isStrictInstance: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['isStrictInstance'];
27
- static readonly renderCallback: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['renderCallback'];
55
+ static readonly render: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['render'];
28
56
  static readonly inputsType: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['inputsType'];
29
57
  static readonly stateType: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['stateType'];
30
58
  static readonly updateStateType: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['updateStateType'];
@@ -38,23 +66,42 @@ export declare abstract class DeclarativeElement<TagName extends CustomElementTa
38
66
  abstract _lastRenderError: Error | undefined;
39
67
  abstract _internalRenderCount: number;
40
68
  abstract _lastRenderedProps: Readonly<Pick<RenderParams<any, Inputs, StateInit, any, any, any, any>, 'inputs' | 'state'>>;
69
+ /**
70
+ * Calls all destroy methods on all state properties, if they exist. This is automatically
71
+ * called whenever the element is detached.
72
+ */
41
73
  abstract destroy(): void;
42
74
  abstract render(): unknown;
43
75
  abstract readonly instanceState: FlattenElementVirStateSetup<StateInit>;
44
76
  abstract readonly observablePropertyListenerMap: ObservableListenerMap<StateInit & Inputs>;
45
77
  abstract readonly instanceInputs: Inputs;
46
- abstract assignInputs(inputs: {} extends Required<Inputs> ? never : Partial<Inputs>): void;
78
+ /**
79
+ * Used to assign inputs to the given element. This can be externally called as an API for
80
+ * setting inputs on an element reference, though this is discouraged. Inputs should typically
81
+ * be called using the `.assign()` method on an element definition inside of an HTML template.
82
+ */
83
+ abstract assignInputs(inputs: EmptyObject extends Required<Inputs> ? never : Partial<Inputs>): void;
47
84
  abstract _haveInputsBeenSet: boolean;
48
85
  /** The element definition for this element instance. */
49
86
  abstract readonly definition: DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
50
87
  }
88
+ /**
89
+ * The assign inputs method of a declarative element.
90
+ *
91
+ * @category Internal
92
+ */
51
93
  export type AssignMethod<Inputs extends PropertyInitMapBase> = IsAny<Inputs> extends true ? any : IsEmptyObject<Required<Inputs>> extends true ? (inputsObject: never) => never : (inputsObject: IsEmptyObject<Required<Inputs>> extends true ? never : Inputs) => MinimalDefinitionWithInputs;
94
+ /**
95
+ * All static properties on a declarative element. These all come from the element's definition.
96
+ *
97
+ * @category Internal
98
+ */
52
99
  export type StaticDeclarativeElementProperties<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = {
53
100
  /** Assign inputs to an element directly on its interpolated tag. */
54
101
  readonly assign: AssignMethod<Inputs>;
55
102
  assignedInputs: Inputs | undefined;
56
103
  /** Pass through the render callback for direct unit testability */
57
- readonly renderCallback: RenderCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
104
+ readonly render: RenderCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
58
105
  readonly events: EventDescriptorMap<TagName, EventsInit>;
59
106
  readonly stateInitStatic: ElementPropertyDescriptorMap<StateInit>;
60
107
  readonly slotNames: SlotNameMap<SlotNames>;
@@ -63,7 +110,6 @@ export type StaticDeclarativeElementProperties<TagName extends CustomElementTagN
63
110
  readonly inputsType: Inputs;
64
111
  readonly stateType: Readonly<FlattenElementVirStateSetup<StateInit>>;
65
112
  readonly updateStateType: UpdateStateCallback<StateInit>;
66
- readonly isStrictInstance: (element: unknown) => element is DeclarativeElement<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
67
113
  readonly hostClasses: HostClassNamesMap<string, HostClassKeys>;
68
114
  readonly cssVars: CssVars<TagName, CssVarKeys>;
69
115
  readonly tagName: string;
@@ -1,3 +1,36 @@
1
- import { LitElement } from '../lit-exports/all-lit-exports';
1
+ import { LitElement } from '../lit-exports/base-lit-exports.js';
2
+ /**
3
+ * Abstract class base for all declarative elements.
4
+ *
5
+ * @category Internal
6
+ */
2
7
  export class DeclarativeElement extends LitElement {
8
+ /**
9
+ * Assign inputs to an element instantiation. Use only on the opening tag.
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * import {html} from 'element-vir';
15
+ *
16
+ * const myTemplate = html`
17
+ * <${MyElement.assign({input1: 'a', input2: 'b'})}></${MyElement}>
18
+ * `;
19
+ * ```
20
+ */
21
+ static assign;
22
+ static assignedInputs;
23
+ static tagName;
24
+ static styles;
25
+ static render;
26
+ static inputsType;
27
+ static stateType;
28
+ static updateStateType;
29
+ static events;
30
+ static stateInitStatic;
31
+ static init;
32
+ static elementOptions;
33
+ static hostClasses;
34
+ static cssVars;
35
+ static slotNames;
3
36
  }
@@ -1,8 +1,33 @@
1
- import { CustomElementTagName } from './custom-tag-name';
2
- import { DeclarativeElementDefinition } from './declarative-element';
3
- import { DeclarativeElementInit } from './declarative-element-init';
4
- import { BaseCssPropertyName } from './properties/css-properties';
5
- import { EventsInitMap } from './properties/element-events';
6
- import { PropertyInitMapBase } from './properties/element-properties';
1
+ import { CustomElementTagName } from './custom-tag-name.js';
2
+ import { DeclarativeElementInit } from './declarative-element-init.js';
3
+ import { DeclarativeElementDefinition } from './declarative-element.js';
4
+ import { BaseCssPropertyName } from './properties/css-properties.js';
5
+ import { EventsInitMap } from './properties/element-events.js';
6
+ import { PropertyInitMapBase } from './properties/element-properties.js';
7
+ /**
8
+ * Verifies that the given {@link DeclarativeElementInit} for an element definition without inputs
9
+ * does not have any state properties that clash with built-in HTML element properties.
10
+ *
11
+ * @category Internal
12
+ */
7
13
  export type VerifiedElementNoInputsInit<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = Extract<keyof StateInit, keyof HTMLElement> extends never ? DeclarativeElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames> : 'ERROR: Cannot define an element state property that clashes with native HTMLElement properties.';
14
+ /**
15
+ * Defines an element without any inputs.
16
+ *
17
+ * @category Element Definition
18
+ * @example
19
+ *
20
+ * ```ts
21
+ * import {defineElementNoInputs, html} from 'element-vir';
22
+ *
23
+ * const MyElement = defineElementNoInputs({
24
+ * tagName: 'my-element',
25
+ * render() {
26
+ * return html`
27
+ * <p>hi</p>
28
+ * `;
29
+ * },
30
+ * });
31
+ * ```
32
+ */
8
33
  export declare function defineElementNoInputs<const TagName extends CustomElementTagName = '-', Inputs extends PropertyInitMapBase = {}, StateInit extends PropertyInitMapBase = {}, EventsInit extends EventsInitMap = {}, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(initInput: VerifiedElementNoInputsInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>): DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;