element-vir 23.4.2 → 24.0.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 (35) hide show
  1. package/README.md +34 -31
  2. package/dist/declarative-element/declarative-element-init.d.ts +17 -15
  3. package/dist/declarative-element/declarative-element.d.ts +14 -17
  4. package/dist/declarative-element/declarative-element.js +0 -1
  5. package/dist/declarative-element/define-element-no-inputs.d.ts +1 -8
  6. package/dist/declarative-element/define-element-no-inputs.js +13 -11
  7. package/dist/declarative-element/define-element.d.ts +12 -6
  8. package/dist/declarative-element/define-element.js +8 -2
  9. package/dist/declarative-element/directives/async-prop.d.ts +1 -2
  10. package/dist/declarative-element/directives/async-prop.js +1 -6
  11. package/dist/declarative-element/directives/is-resolved.directive.d.ts +12 -6
  12. package/dist/declarative-element/directives/is-resolved.directive.js +12 -6
  13. package/dist/declarative-element/is-declarative-element-definition.js +0 -1
  14. package/dist/declarative-element/properties/element-properties.d.ts +0 -17
  15. package/dist/declarative-element/properties/host-classes.d.ts +2 -3
  16. package/dist/declarative-element/properties/property-proxy.js +1 -5
  17. package/dist/declarative-element/properties/styles.d.ts +3 -4
  18. package/dist/declarative-element/render-callback.d.ts +10 -11
  19. package/dist/declarative-element/wrap-define-element.d.ts +7 -8
  20. package/dist/declarative-element/wrap-define-element.js +2 -2
  21. package/dist/index.d.ts +0 -2
  22. package/dist/index.js +0 -2
  23. package/dist/readme-examples/my-custom-define.d.ts +3 -3
  24. package/dist/readme-examples/my-with-async-prop.element.d.ts +3 -2
  25. package/dist/readme-examples/my-with-async-prop.element.js +9 -6
  26. package/dist/readme-examples/my-with-cleanup-callback.element.d.ts +1 -1
  27. package/dist/readme-examples/my-with-cleanup-callback.element.js +4 -10
  28. package/dist/readme-examples/my-with-event-listening.element.js +4 -2
  29. package/dist/readme-examples/my-with-host-class-definition.element.js +4 -2
  30. package/dist/readme-examples/my-with-update-state.element.js +9 -7
  31. package/package.json +11 -11
  32. package/dist/declarative-element/properties/element-vir-state-setup.d.ts +0 -54
  33. package/dist/declarative-element/properties/element-vir-state-setup.js +0 -22
  34. package/dist/declarative-element/properties/per-instance.d.ts +0 -26
  35. package/dist/declarative-element/properties/per-instance.js +0 -32
package/README.md CHANGED
@@ -169,13 +169,15 @@ import {defineElementNoInputs, html, listen} from 'element-vir';
169
169
 
170
170
  export const MyWithUpdateState = defineElementNoInputs({
171
171
  tagName: 'my-with-update-state',
172
- stateInitStatic: {
173
- username: 'dev',
174
- /**
175
- * Use "as" to create state properties that can be types other than the initial value's
176
- * type. This is particularly useful when, as below, the initial value is undefined.
177
- */
178
- email: undefined as string | undefined,
172
+ state() {
173
+ return {
174
+ username: 'dev',
175
+ /**
176
+ * Use "as" to create state properties that can be types other than the initial value's
177
+ * type. This is particularly useful when, as below, the initial value is undefined.
178
+ */
179
+ email: undefined as string | undefined,
180
+ };
179
181
  },
180
182
  render({state, updateState}) {
181
183
  return html`
@@ -219,8 +221,8 @@ export const MyWithAssignment = defineElementNoInputs({
219
221
 
220
222
  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
223
 
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
+ - `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.)
225
+ - `cleanup`: called when an element is removed from the DOM. (This is the same as the `disconnectedCallback` in standard HTMLElement classes.)
224
226
 
225
227
  <!-- example-link: src/readme-examples/my-with-cleanup-callback.element.ts -->
226
228
 
@@ -229,24 +231,18 @@ import {defineElementNoInputs, html} from 'element-vir';
229
231
 
230
232
  export const MyWithAssignmentCleanupCallback = defineElementNoInputs({
231
233
  tagName: 'my-with-cleanup-callback',
232
- stateInitStatic: {
233
- intervalId: undefined as undefined | number,
234
- },
235
- init: ({updateState}) => {
236
- updateState({
234
+ state() {
235
+ return {
237
236
  intervalId: window.setInterval(() => console.info('hi'), 1000),
238
- });
237
+ };
239
238
  },
240
239
  render() {
241
240
  return html`
242
241
  <h1>My App</h1>
243
242
  `;
244
243
  },
245
- cleanup: ({state, updateState}) => {
244
+ cleanup({state}) {
246
245
  window.clearInterval(state.intervalId);
247
- updateState({
248
- intervalId: undefined,
249
- });
250
246
  },
251
247
  });
252
248
  ```
@@ -296,8 +292,10 @@ import {MyWithEvents} from './my-with-events.element.js';
296
292
 
297
293
  export const MyWithEventListening = defineElementNoInputs({
298
294
  tagName: 'my-with-event-listening',
299
- stateInitStatic: {
300
- myNumber: -1,
295
+ state() {
296
+ return {
297
+ myNumber: -1,
298
+ };
301
299
  },
302
300
  render({state, updateState}) {
303
301
  return html`
@@ -369,8 +367,8 @@ Host classes can be defined and used with type safety. Host classes are used to
369
367
 
370
368
  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:
371
369
 
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.
370
+ - if the value is a callback, that host class will automatically be applied if the callback returns true after a render is executed.
371
+ - if the value is `false`, the host class is never automatically applied, it must be manually applied by consumers.
374
372
 
375
373
  Apply host classes in the element's stylesheet by using a callback for the styles property:
376
374
 
@@ -381,8 +379,10 @@ import {css, defineElementNoInputs, html} from 'element-vir';
381
379
 
382
380
  export const MyWithHostClassDefinition = defineElementNoInputs({
383
381
  tagName: 'my-with-host-class-definition',
384
- stateInitStatic: {
385
- myProp: 'hello there',
382
+ state() {
383
+ return {
384
+ myProp: 'hello there',
385
+ };
386
386
  },
387
387
  hostClasses: {
388
388
  /**
@@ -629,12 +629,15 @@ async function loadSomething(endpoint: string): Promise<EndpointData> {
629
629
 
630
630
  export const MyWithAsyncProp = defineElement<{endpoint: string}>()({
631
631
  tagName: 'my-with-async-prop',
632
- stateInitStatic: {
633
- data: asyncProp({
634
- async updateCallback({endpoint}: {endpoint: string}) {
635
- return loadSomething(endpoint);
636
- },
637
- }),
632
+ state() {
633
+ return {
634
+ data: asyncProp({
635
+ async updateCallback({endpoint}: {endpoint: string}) {
636
+ return loadSomething(endpoint);
637
+ },
638
+ }),
639
+ hi: '',
640
+ };
638
641
  },
639
642
  render({inputs, state}) {
640
643
  /**
@@ -7,36 +7,30 @@ import { type EventsInitMap } from './properties/element-events.js';
7
7
  import { type PropertyInitMapBase } from './properties/element-properties.js';
8
8
  import { type HostClassesInitMap } from './properties/host-classes.js';
9
9
  import { type StylesCallback } from './properties/styles.js';
10
- import { type InitCallback, type RenderCallback } from './render-callback.js';
10
+ import { type InitCallback, type RenderCallback, type RenderParams } from './render-callback.js';
11
11
  /**
12
12
  * Initialization for an element-vir declarative element. This defines all the pieces required for
13
13
  * rendering the element.
14
14
  *
15
15
  * @category Internal
16
16
  */
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>> = {
17
+ export type DeclarativeElementInit<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = {
18
18
  /**
19
19
  * HTML tag name. This should not be used directly, as interpolating it with the html tagged
20
20
  * template from this package is preferred.
21
21
  */
22
22
  tagName: TagName;
23
23
  /** Static styles. These should not and cannot change. */
24
- styles?: CSSResult | StylesCallback<TagName, HostClassKeys, CssVarKeys>;
25
- /**
26
- * The definition of and initial values for the element's internal state. Note that this is
27
- * defined statically: the init value will be the same for all instances of this element because
28
- * it is only defined once.
29
- */
30
- stateInitStatic?: StateInit;
24
+ styles?: CSSResult | StylesCallback<TagName, HostClassKeys, CssVarKeys> | undefined;
31
25
  /** Events that the element can dispatch. (These can be thought of as "outputs".) */
32
- events?: EventsInit;
33
- slotNames?: SlotNames;
26
+ events?: EventsInit | undefined;
27
+ slotNames?: SlotNames | undefined;
34
28
  /**
35
29
  * HTML host classes. Values can be callbacks to determine when a host class should be defined,
36
30
  * based on current instance state or inputs, or just false to indicate that the host class will
37
31
  * only be manually set.
38
32
  */
39
- hostClasses?: HostClassesInitMap<TagName, HostClassKeys, Inputs, StateInit>;
33
+ hostClasses?: HostClassesInitMap<TagName, HostClassKeys, Inputs, State> | undefined;
40
34
  /**
41
35
  * CSS Vars for the component. Keys of this object should be kebab-case and start with the
42
36
  * element's tag name.
@@ -45,9 +39,17 @@ export type DeclarativeElementInit<TagName extends CustomElementTagName, Inputs
45
39
  * then passed to the styles property, which must be a callback to take advantage of these.
46
40
  */
47
41
  cssVars?: CssVarsInitMap<TagName, CssVarKeys>;
42
+ /**
43
+ * Make sure to define this at the top of your element init object or TypeScript will fail to
44
+ * infer the element's state type.
45
+ *
46
+ * Setup the element's initial state. This is only called once per element instance, before the
47
+ * first render. The return type of this method becomes the element's state type.
48
+ */
49
+ state?: (params: Omit<RenderParams<TagName, Inputs, any, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, 'state' | 'updateState'>) => Extract<keyof State, keyof HTMLElement> extends never ? Extract<keyof State, keyof Inputs> extends never ? State : `ERROR: Cannot define an element state property that clashes with input properties: ${Extract<keyof State, keyof Inputs> extends string | number | bigint | boolean | null | undefined ? Extract<keyof State, keyof Inputs> : ''}` : `ERROR: Cannot define an element state property that clashes with native HTMLElement properties: ${Extract<keyof State, keyof HTMLElement>}`;
48
50
  /** 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>;
51
+ init?: InitCallback<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames> | undefined;
52
+ render: RenderCallback<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
53
+ cleanup?: InitCallback<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames> | undefined;
52
54
  options?: Partial<DeclarativeElementDefinitionOptions> | undefined;
53
55
  };
@@ -8,8 +8,7 @@ import { type DeclarativeElementDefinitionOptions } from './definition-options.j
8
8
  import { type BaseCssPropertyName } from './properties/css-properties.js';
9
9
  import { type CssVars } from './properties/css-vars.js';
10
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';
11
+ import { type PropertyInitMapBase } from './properties/element-properties.js';
13
12
  import { type HostClassNamesMap } from './properties/host-classes.js';
14
13
  import { type ObservableListenerMap } from './properties/property-proxy.js';
15
14
  import { type RenderCallback, type RenderParams, type UpdateStateCallback } from './render-callback.js';
@@ -20,21 +19,21 @@ import { type SlotNameMap } from './slot-names.js';
20
19
  *
21
20
  * @category Internal
22
21
  */
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'>;
22
+ export type DeclarativeElementHost<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, State 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, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, Exclude<keyof StaticDeclarativeElementProperties<any, any, any, any, any, any, any>, keyof HTMLElement>>, 'shadowRoot'>;
24
23
  /**
25
24
  * The full definition for a declarative element.
26
25
  *
27
26
  * @category Internal
28
27
  */
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> & {
30
- instanceType: DeclarativeElementHost<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
28
+ export type DeclarativeElementDefinition<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, State 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, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) & StaticDeclarativeElementProperties<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames> & {
29
+ instanceType: DeclarativeElementHost<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
31
30
  };
32
31
  /**
33
32
  * Abstract class base for all declarative elements.
34
33
  *
35
34
  * @category Internal
36
35
  */
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 {
36
+ export declare abstract class DeclarativeElement<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, State extends PropertyInitMapBase = any, EventsInit extends EventsInitMap = any, HostClassKeys extends BaseCssPropertyName<TagName> = any, CssVarKeys extends BaseCssPropertyName<TagName> = any, SlotNames extends ReadonlyArray<string> = any> extends LitElement {
38
37
  /**
39
38
  * Assign inputs to an element instantiation. Use only on the opening tag.
40
39
  *
@@ -57,7 +56,6 @@ export declare abstract class DeclarativeElement<TagName extends CustomElementTa
57
56
  static readonly stateType: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['stateType'];
58
57
  static readonly updateStateType: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['updateStateType'];
59
58
  static readonly events: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['events'];
60
- static readonly stateInitStatic: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['stateInitStatic'];
61
59
  static readonly init: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['init'];
62
60
  static readonly elementOptions: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['elementOptions'];
63
61
  static readonly hostClasses: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['hostClasses'];
@@ -65,15 +63,15 @@ export declare abstract class DeclarativeElement<TagName extends CustomElementTa
65
63
  static readonly slotNames: StaticDeclarativeElementProperties<CustomElementTagName, PropertyInitMapBase, PropertyInitMapBase, EventsInitMap, BaseCssPropertyName<CustomElementTagName>, BaseCssPropertyName<CustomElementTagName>, ReadonlyArray<string>>['slotNames'];
66
64
  abstract _lastRenderError: Error | undefined;
67
65
  abstract _internalRenderCount: number;
68
- abstract _lastRenderedProps: Readonly<Pick<RenderParams<any, Inputs, StateInit, any, any, any, any>, 'inputs' | 'state'>>;
66
+ abstract _lastRenderedProps: Readonly<Pick<RenderParams<any, Inputs, State, any, any, any, any>, 'inputs' | 'state'>>;
69
67
  /**
70
68
  * Calls all destroy methods on all state properties, if they exist. This is automatically
71
69
  * called whenever the element is detached.
72
70
  */
73
71
  abstract destroy(): void;
74
72
  abstract render(): unknown;
75
- abstract readonly instanceState: FlattenElementVirStateSetup<StateInit>;
76
- abstract readonly observablePropertyListenerMap: ObservableListenerMap<StateInit & Inputs>;
73
+ abstract readonly instanceState: State;
74
+ abstract readonly observablePropertyListenerMap: ObservableListenerMap<State & Inputs>;
77
75
  abstract readonly instanceInputs: Inputs;
78
76
  /**
79
77
  * Used to assign inputs to the given element. This can be externally called as an API for
@@ -83,7 +81,7 @@ export declare abstract class DeclarativeElement<TagName extends CustomElementTa
83
81
  abstract assignInputs(inputs: EmptyObject extends Required<Inputs> ? never : Partial<Inputs>): void;
84
82
  abstract _haveInputsBeenSet: boolean;
85
83
  /** The element definition for this element instance. */
86
- abstract readonly definition: DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
84
+ abstract readonly definition: DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
87
85
  }
88
86
  /**
89
87
  * The assign inputs method of a declarative element.
@@ -96,20 +94,19 @@ export type AssignMethod<Inputs extends PropertyInitMapBase> = IsAny<Inputs> ext
96
94
  *
97
95
  * @category Internal
98
96
  */
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>> = {
97
+ export type StaticDeclarativeElementProperties<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = {
100
98
  /** Assign inputs to an element directly on its interpolated tag. */
101
99
  readonly assign: AssignMethod<Inputs>;
102
100
  assignedInputs: Inputs | undefined;
103
101
  /** Pass through the render callback for direct unit testability */
104
- readonly render: RenderCallback<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
102
+ readonly render: RenderCallback<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
105
103
  readonly events: EventDescriptorMap<TagName, EventsInit>;
106
- readonly stateInitStatic: ElementPropertyDescriptorMap<StateInit>;
107
104
  readonly slotNames: SlotNameMap<SlotNames>;
108
- readonly init: DeclarativeElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
105
+ readonly init: DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
109
106
  readonly elementOptions: DeclarativeElementDefinitionOptions;
110
107
  readonly inputsType: Inputs;
111
- readonly stateType: Readonly<FlattenElementVirStateSetup<StateInit>>;
112
- readonly updateStateType: UpdateStateCallback<StateInit>;
108
+ readonly stateType: Readonly<State>;
109
+ readonly updateStateType: UpdateStateCallback<State>;
113
110
  readonly hostClasses: HostClassNamesMap<string, HostClassKeys>;
114
111
  readonly cssVars: CssVars<TagName, CssVarKeys>;
115
112
  readonly tagName: string;
@@ -27,7 +27,6 @@ export class DeclarativeElement extends LitElement {
27
27
  static stateType;
28
28
  static updateStateType;
29
29
  static events;
30
- static stateInitStatic;
31
30
  static init;
32
31
  static elementOptions;
33
32
  static hostClasses;
@@ -4,13 +4,6 @@ import { DeclarativeElementDefinition } from './declarative-element.js';
4
4
  import { BaseCssPropertyName } from './properties/css-properties.js';
5
5
  import { EventsInitMap } from './properties/element-events.js';
6
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
- */
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
7
  /**
15
8
  * Defines an element without any inputs.
16
9
  *
@@ -30,4 +23,4 @@ export type VerifiedElementNoInputsInit<TagName extends CustomElementTagName, In
30
23
  * });
31
24
  * ```
32
25
  */
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>;
26
+ export declare function defineElementNoInputs<const TagName extends CustomElementTagName = '-', Inputs extends PropertyInitMapBase = {}, State extends PropertyInitMapBase = {}, EventsInit extends EventsInitMap = {}, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(init: DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>): DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
@@ -33,9 +33,7 @@ import { createSlotNamesMap } from './slot-names.js';
33
33
  * });
34
34
  * ```
35
35
  */
36
- export function defineElementNoInputs(initInput) {
37
- /** This as cast is safe only because of the following run-time type check. */
38
- const init = initInput;
36
+ export function defineElementNoInputs(init) {
39
37
  if (!check.isObject(init)) {
40
38
  throw new TypeError('Cannot define element with non-object init: ${init}');
41
39
  }
@@ -94,7 +92,6 @@ export function defineElementNoInputs(initInput) {
94
92
  static cssVars = cssVars;
95
93
  static init = init;
96
94
  static slotNames = slotNamesMap;
97
- static stateInitStatic = init.stateInitStatic;
98
95
  get instanceType() {
99
96
  throw new Error(`"instanceType" was called on ${init.tagName} as a value but it is only for types.`);
100
97
  }
@@ -105,6 +102,7 @@ export function defineElementNoInputs(initInput) {
105
102
  throw new Error(`"stateType" was called on ${init.tagName} as a value but it is only for types.`);
106
103
  }
107
104
  _initCalled = false;
105
+ _stateCalled = false;
108
106
  _hasRendered = false;
109
107
  _lastRenderedProps = undefined;
110
108
  _haveInputsBeenSet = false;
@@ -121,6 +119,17 @@ export function defineElementNoInputs(initInput) {
121
119
  }
122
120
  this._hasRendered = true;
123
121
  const renderParams = this.createRenderParams();
122
+ if (!this._stateCalled && init.state) {
123
+ this._stateCalled = true;
124
+ const stateInit = init.state(renderParams);
125
+ if (stateInit instanceof Promise) {
126
+ throw new TypeError('init cannot be asynchronous');
127
+ }
128
+ getObjectTypedKeys(stateInit).forEach((stateKey) => {
129
+ bindReactiveProperty(this, stateKey);
130
+ this.instanceState[stateKey] = stateInit[stateKey];
131
+ });
132
+ }
124
133
  if (!this._initCalled && init.init) {
125
134
  this._initCalled = true;
126
135
  if (init.init(renderParams) instanceof Promise) {
@@ -189,13 +198,6 @@ export function defineElementNoInputs(initInput) {
189
198
  instanceState = createElementPropertyProxy(this, !elementOptions.allowPolymorphicState);
190
199
  constructor() {
191
200
  super();
192
- const stateInitStatic =
193
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
194
- init.stateInitStatic || {};
195
- getObjectTypedKeys(stateInitStatic).forEach((stateKey) => {
196
- bindReactiveProperty(this, stateKey);
197
- this.instanceState[stateKey] = stateInitStatic[stateKey];
198
- });
199
201
  this.definition = anonymousClass;
200
202
  }
201
203
  };
@@ -1,17 +1,18 @@
1
1
  import { CustomElementTagName } from './custom-tag-name.js';
2
+ import { DeclarativeElementInit } from './declarative-element-init.js';
2
3
  import { DeclarativeElementDefinition } from './declarative-element.js';
3
- import { VerifiedElementNoInputsInit } from './define-element-no-inputs.js';
4
4
  import { BaseCssPropertyName } from './properties/css-properties.js';
5
5
  import { EventsInitMap } from './properties/element-events.js';
6
6
  import { PropertyInitMapBase } from './properties/element-properties.js';
7
7
  /**
8
- * Verifies that the given {@link DeclarativeElementInit} for an element definition with inputs does
9
- * not have any state or input properties that clash with built-in HTML element properties, or state
10
- * / input properties that clash with each other.
8
+ * Verifies that the given `Inputs` type does not clash with built-in HTMLElement properties. This
9
+ * is used within {@link defineElement}.
11
10
  *
12
11
  * @category Internal
13
12
  */
14
- export type VerifiedElementInit<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 Inputs> extends never ? Extract<keyof Inputs, keyof HTMLElement> extends never ? VerifiedElementNoInputsInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames> : 'ERROR: Cannot define an element input property that clashes with native HTMLElement properties.' : "ERROR: Cannot define an element state property that clashes with the element's input properties.";
13
+ export type DeclarativeElementInputErrorParams<Inputs extends PropertyInitMapBase> = Extract<keyof Inputs, keyof HTMLElement> extends never ? [] : [
14
+ 'ERROR: Cannot define an element input property that clashes with native HTMLElement properties.'
15
+ ];
15
16
  /**
16
17
  * Defines an element with inputs. If the element actually has no inputs, use
17
18
  * {@link defineElementNoInputs} instead. Note that this function must be called twice, due to
@@ -33,4 +34,9 @@ export type VerifiedElementInit<TagName extends CustomElementTagName, Inputs ext
33
34
  * });
34
35
  * ```
35
36
  */
36
- export declare function defineElement<Inputs extends PropertyInitMapBase = {}>(): <const TagName extends CustomElementTagName, 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: VerifiedElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
37
+ export declare function defineElement<Inputs extends PropertyInitMapBase = {}>(
38
+ /**
39
+ * These `errorParams` is present when there are problems with the `Inputs` type. If it is
40
+ * present, the error should be fixed. This should always be empty.
41
+ */
42
+ ...errorParams: DeclarativeElementInputErrorParams<Inputs>): <const TagName extends CustomElementTagName, State extends PropertyInitMapBase = {}, EventsInit extends EventsInitMap = {}, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(initInput: DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-empty-object-type */
2
- import { check } from '@augment-vir/assert';
2
+ import { assert, check } from '@augment-vir/assert';
3
3
  import { defineElementNoInputs } from './define-element-no-inputs.js';
4
4
  /**
5
5
  * Defines an element with inputs. If the element actually has no inputs, use
@@ -22,7 +22,13 @@ import { defineElementNoInputs } from './define-element-no-inputs.js';
22
22
  * });
23
23
  * ```
24
24
  */
25
- export function defineElement() {
25
+ export function defineElement(
26
+ /**
27
+ * These `errorParams` is present when there are problems with the `Inputs` type. If it is
28
+ * present, the error should be fixed. This should always be empty.
29
+ */
30
+ ...errorParams) {
31
+ assert.isEmpty(errorParams);
26
32
  return (initInput) => {
27
33
  const init = initInput;
28
34
  if (!check.isObject(init)) {
@@ -1,6 +1,5 @@
1
1
  import { CallbackObservable, CallbackObservableInit } from 'observavir';
2
2
  import { Constructor } from 'type-fest';
3
- import { ElementVirStateSetup } from '../properties/element-vir-state-setup.js';
4
3
  export type { AsyncValue } from 'observavir';
5
4
  /** Class for constructing async props. Should not be referenced directly, use `AsyncProp` instead. */
6
5
  declare class InternalAsyncPropClass<Value, Params> extends CallbackObservable<Value, Params> {
@@ -24,4 +23,4 @@ export declare const AsyncProp: Constructor<AsyncProp<unknown, unknown>, Constru
24
23
  *
25
24
  * @category Async
26
25
  */
27
- export declare function asyncProp<Value, Params = void>(init?: CallbackObservableInit<Value, Params>): ElementVirStateSetup<AsyncProp<Value, Params>>;
26
+ export declare function asyncProp<Value, Params = void>(init?: CallbackObservableInit<Value, Params>): AsyncProp<Value, Params>;
@@ -1,5 +1,4 @@
1
1
  import { CallbackObservable } from 'observavir';
2
- import { stateSetupKey } from '../properties/element-vir-state-setup.js';
3
2
  /** Class for constructing async props. Should not be referenced directly, use `AsyncProp` instead. */
4
3
  class InternalAsyncPropClass extends CallbackObservable {
5
4
  }
@@ -15,9 +14,5 @@ export const AsyncProp = InternalAsyncPropClass;
15
14
  * @category Async
16
15
  */
17
16
  export function asyncProp(init) {
18
- return {
19
- [stateSetupKey]() {
20
- return new InternalAsyncPropClass(init);
21
- },
22
- };
17
+ return new InternalAsyncPropClass(init);
23
18
  }
@@ -13,8 +13,10 @@ import { AsyncProp, AsyncValue } from './async-prop.js';
13
13
  *
14
14
  * const MyElement = defineElementNoInputs({
15
15
  * tagName: 'my-element',
16
- * stateInitStatic: {
17
- * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
16
+ * state() {
17
+ * return {
18
+ * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
19
+ * };
18
20
  * },
19
21
  * render({state}) {
20
22
  * if (isAsyncError(state.myProp.value)) {
@@ -49,8 +51,10 @@ export declare function isResolved<Value extends AsyncValue<any>>(asyncValue: Va
49
51
  *
50
52
  * const MyElement = defineElementNoInputs({
51
53
  * tagName: 'my-element',
52
- * stateInitStatic: {
53
- * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
54
+ * state() {
55
+ * return {
56
+ * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
57
+ * };
54
58
  * },
55
59
  * render({state}) {
56
60
  * if (isAsyncError(state.myProp.value)) {
@@ -84,8 +88,10 @@ export declare function isAsyncError<Value extends AsyncValue<any>>(asyncValue:
84
88
  *
85
89
  * const MyElement = defineElementNoInputs({
86
90
  * tagName: 'my-element',
87
- * stateInitStatic: {
88
- * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
91
+ * state() {
92
+ * return {
93
+ * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
94
+ * };
89
95
  * },
90
96
  * render({state}) {
91
97
  * const resolvedValue = resolvedOrUndefined(state.myProp.value);
@@ -13,8 +13,10 @@ import { AsyncProp } from './async-prop.js';
13
13
  *
14
14
  * const MyElement = defineElementNoInputs({
15
15
  * tagName: 'my-element',
16
- * stateInitStatic: {
17
- * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
16
+ * state() {
17
+ * return {
18
+ * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
19
+ * };
18
20
  * },
19
21
  * render({state}) {
20
22
  * if (isAsyncError(state.myProp.value)) {
@@ -54,8 +56,10 @@ export function isResolved(asyncValue) {
54
56
  *
55
57
  * const MyElement = defineElementNoInputs({
56
58
  * tagName: 'my-element',
57
- * stateInitStatic: {
58
- * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
59
+ * state() {
60
+ * return {
61
+ * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
62
+ * };
59
63
  * },
60
64
  * render({state}) {
61
65
  * if (isAsyncError(state.myProp.value)) {
@@ -94,8 +98,10 @@ export function isAsyncError(asyncValue) {
94
98
  *
95
99
  * const MyElement = defineElementNoInputs({
96
100
  * tagName: 'my-element',
97
- * stateInitStatic: {
98
- * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
101
+ * state() {
102
+ * return {
103
+ * myProp: asyncProp({defaultValue: waitValue({seconds: 10}, 'value')}),
104
+ * };
99
105
  * },
100
106
  * render({state}) {
101
107
  * const resolvedValue = resolvedOrUndefined(state.myProp.value);
@@ -11,7 +11,6 @@ const expectedStaticProperties = getObjectTypedKeys({
11
11
  inputsType: '',
12
12
  render: '',
13
13
  slotNames: '',
14
- stateInitStatic: '',
15
14
  stateType: '',
16
15
  styles: '',
17
16
  tagName: '',
@@ -4,20 +4,3 @@
4
4
  * @category Internal
5
5
  */
6
6
  export type PropertyInitMapBase = Record<PropertyKey, unknown>;
7
- /**
8
- * Describes a static element property.
9
- *
10
- * @category Internal
11
- */
12
- export type StaticElementPropertyDescriptor<PropName extends string, PropValue> = {
13
- propName: PropName;
14
- initValue: PropValue;
15
- };
16
- /**
17
- * Maps a property init map into static element descriptions.
18
- *
19
- * @category Internal
20
- */
21
- export type ElementPropertyDescriptorMap<PropertyInitGeneric extends PropertyInitMapBase> = {
22
- [Property in keyof PropertyInitGeneric]: StaticElementPropertyDescriptor<string, PropertyInitGeneric[Property]>;
23
- };
@@ -1,20 +1,19 @@
1
1
  import { CustomElementTagName } from '../custom-tag-name.js';
2
2
  import { BaseCssPropertyName } from './css-properties.js';
3
3
  import { PropertyInitMapBase } from './element-properties.js';
4
- import { FlattenElementVirStateSetup } from './element-vir-state-setup.js';
5
4
  import { WithTagName } from './tag-name.js';
6
5
  /**
7
6
  * Base init map for defining host classes in an element definition.
8
7
  *
9
8
  * @category Internal
10
9
  */
11
- export type HostClassesInitMap<TagName extends CustomElementTagName, HostClassKeys extends BaseCssPropertyName<TagName>, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase> = Record<HostClassKeys,
10
+ export type HostClassesInitMap<TagName extends CustomElementTagName, HostClassKeys extends BaseCssPropertyName<TagName>, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase> = Record<HostClassKeys,
12
11
  /**
13
12
  * Callback to determine when host class should be enabled (based on current inputs and state),
14
13
  * or just undefined to mark that this host class name will only be manually applied.
15
14
  */
16
15
  ((inputs: {
17
- state: Readonly<FlattenElementVirStateSetup<StateInit>>;
16
+ state: Readonly<State>;
18
17
  inputs: Readonly<Inputs>;
19
18
  }) => boolean) | false>;
20
19
  /**
@@ -1,6 +1,5 @@
1
1
  import { isObservableBase } from 'observavir';
2
2
  import { property } from '../../lit-exports/base-lit-exports.js';
3
- import { isElementVirStateSetup, stateSetupKey } from './element-vir-state-setup.js';
4
3
  /**
5
4
  * Binds the given property key as a reactive property on the given element.
6
5
  *
@@ -46,10 +45,7 @@ export function createElementPropertyProxy(element, shouldAlreadyExist) {
46
45
  }
47
46
  const propsProxy = new Proxy({}, {
48
47
  get: valueGetter,
49
- set(target, propertyKey, rawNewValue) {
50
- const newValue = isElementVirStateSetup(rawNewValue)
51
- ? rawNewValue[stateSetupKey]()
52
- : rawNewValue;
48
+ set(target, propertyKey, newValue) {
53
49
  verifyProperty(propertyKey);
54
50
  const oldValue = elementAsProps[propertyKey];
55
51
  function setValueOnElement(value) {
@@ -3,7 +3,6 @@ import { type CustomElementTagName } from '../custom-tag-name.js';
3
3
  import { type BaseCssPropertyName } from './css-properties.js';
4
4
  import { type CssVars } from './css-vars.js';
5
5
  import { type PropertyInitMapBase } from './element-properties.js';
6
- import { type FlattenElementVirStateSetup } from './element-vir-state-setup.js';
7
6
  import { type HostClassNamesMap, type HostClassesInitMap } from './host-classes.js';
8
7
  /**
9
8
  * A host class instance to be referenced inside of an element definition's `styles` callback.
@@ -43,10 +42,10 @@ export declare function createStylesCallbackInput<TagName extends CustomElementT
43
42
  *
44
43
  * @category Internal
45
44
  */
46
- export declare function applyHostClasses<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, HostClassKeys extends BaseCssPropertyName<TagName>>({ host, hostClassesInit, hostClassNames, state, inputs, }: {
45
+ export declare function applyHostClasses<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase, HostClassKeys extends BaseCssPropertyName<TagName>>({ host, hostClassesInit, hostClassNames, state, inputs, }: {
47
46
  host: HTMLElement;
48
- hostClassesInit: Readonly<HostClassesInitMap<TagName, HostClassKeys, Inputs, StateInit>> | undefined;
47
+ hostClassesInit: Readonly<HostClassesInitMap<TagName, HostClassKeys, Inputs, State>> | undefined;
49
48
  hostClassNames: HostClassNamesMap<string, HostClassKeys>;
50
- state: Readonly<FlattenElementVirStateSetup<StateInit>>;
49
+ state: Readonly<State>;
51
50
  inputs: Readonly<Inputs>;
52
51
  }): void;
@@ -6,37 +6,36 @@ import { type BaseCssPropertyName } from './properties/css-properties.js';
6
6
  import { type CssVars } from './properties/css-vars.js';
7
7
  import { type EventDescriptorMap, type EventInitMapEventDetailExtractor, type EventsInitMap } from './properties/element-events.js';
8
8
  import { type PropertyInitMapBase } from './properties/element-properties.js';
9
- import { type AllowElementVirStateSetup, type FlattenElementVirStateSetup } from './properties/element-vir-state-setup.js';
10
9
  import { type SlotNameMap } from './slot-names.js';
11
10
  /**
12
11
  * Type for the `render` element definition method.
13
12
  *
14
13
  * @category Internal
15
14
  */
16
- export type RenderCallback<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> = (params: RenderParams<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => HtmlInterpolation;
15
+ export type RenderCallback<TagName extends CustomElementTagName = any, Inputs extends PropertyInitMapBase = any, State extends PropertyInitMapBase = any, EventsInit extends EventsInitMap = any, HostClassKeys extends BaseCssPropertyName<TagName> = any, CssVarKeys extends BaseCssPropertyName<TagName> = any, SlotNames extends ReadonlyArray<string> = any> = (params: RenderParams<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => HtmlInterpolation;
17
16
  /**
18
17
  * Type for the `init` and `cleanup` element definition methods.
19
18
  *
20
19
  * @category Internal
21
20
  */
22
- export type InitCallback<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = (params: RenderParams<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => undefined | void;
21
+ export type InitCallback<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = (params: RenderParams<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => undefined | void;
23
22
  /**
24
23
  * Type for the `updateState` render parameter.
25
24
  *
26
25
  * @category Internal
27
26
  */
28
- export type UpdateStateCallback<StateInit extends PropertyInitMapBase> = (newState: Partial<AllowElementVirStateSetup<StateInit>>) => void;
27
+ export type UpdateStateCallback<State extends PropertyInitMapBase> = (newState: Partial<State>) => void;
29
28
  /**
30
29
  * The full parameters object passed to `render`, `init`, and `cleanup` element definition methods.
31
30
  *
32
31
  * @category Internal
33
32
  */
34
- export type RenderParams<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = {
35
- state: Readonly<FlattenElementVirStateSetup<StateInit>>;
33
+ export type RenderParams<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>> = {
34
+ state: Readonly<State>;
36
35
  cssVars: Readonly<CssVars<TagName, CssVarKeys>>;
37
- updateState: UpdateStateCallback<StateInit>;
36
+ updateState: UpdateStateCallback<State>;
38
37
  events: EventDescriptorMap<TagName, EventsInit>;
39
- host: DeclarativeElementHost<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
38
+ host: DeclarativeElementHost<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
40
39
  slotNames: SlotNameMap<SlotNames>;
41
40
  /** Dispatch an event from the current element. */
42
41
  dispatch: <EventTypeName extends keyof EventsInit>(event: TypedEvent<EventTypeName extends string ? EventTypeName : never, EventInitMapEventDetailExtractor<EventTypeName, EventsInit>> | Event) => boolean;
@@ -48,9 +47,9 @@ export type RenderParams<TagName extends CustomElementTagName, Inputs extends Pr
48
47
  *
49
48
  * @category Internal
50
49
  */
51
- export declare function createRenderParams<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, StateInit extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>>({ element, eventsMap, cssVars, slotNamesMap, }: {
52
- element: DeclarativeElement<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
50
+ export declare function createRenderParams<TagName extends CustomElementTagName, Inputs extends PropertyInitMapBase, State extends PropertyInitMapBase, EventsInit extends EventsInitMap, HostClassKeys extends BaseCssPropertyName<TagName>, CssVarKeys extends BaseCssPropertyName<TagName>, SlotNames extends ReadonlyArray<string>>({ element, eventsMap, cssVars, slotNamesMap, }: {
51
+ element: DeclarativeElement<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
53
52
  eventsMap: EventDescriptorMap<TagName, EventsInit>;
54
53
  cssVars: Readonly<CssVars<TagName, CssVarKeys>>;
55
54
  slotNamesMap: SlotNameMap<SlotNames>;
56
- }): RenderParams<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
55
+ }): RenderParams<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
@@ -1,8 +1,7 @@
1
1
  import { PartialWithNullable } from '@augment-vir/common';
2
2
  import { CustomElementTagName } from './custom-tag-name.js';
3
3
  import { DeclarativeElementInit } from './declarative-element-init.js';
4
- import { VerifiedElementNoInputsInit } from './define-element-no-inputs.js';
5
- import { VerifiedElementInit } from './define-element.js';
4
+ import { DeclarativeElementInputErrorParams } from './define-element.js';
6
5
  import { BaseCssPropertyName } from './properties/css-properties.js';
7
6
  import { EventsInitMap } from './properties/element-events.js';
8
7
  import { PropertyInitMapBase } from './properties/element-properties.js';
@@ -11,17 +10,17 @@ import { PropertyInitMapBase } from './properties/element-properties.js';
11
10
  *
12
11
  * @category Internal
13
12
  */
14
- export type WrapDefineElementOptions<TagNameRequirement extends CustomElementTagName = CustomElementTagName, InputsRequirement extends PropertyInitMapBase = {}, StateInitRequirement extends PropertyInitMapBase = {}, EventsInitRequirement extends EventsInitMap = {}> = PartialWithNullable<{
13
+ export type WrapDefineElementOptions<TagNameRequirement extends CustomElementTagName = CustomElementTagName, InputsRequirement extends PropertyInitMapBase = {}, StateRequirement extends PropertyInitMapBase = {}, EventsInitRequirement extends EventsInitMap = {}> = PartialWithNullable<{
15
14
  /**
16
15
  * An optional callback which asserts that an element definition init object given to the
17
16
  * wrapped element definition functions is valid.
18
17
  */
19
- assertInputs: (inputInit: DeclarativeElementInit<TagNameRequirement, InputsRequirement, StateInitRequirement, EventsInitRequirement, BaseCssPropertyName<TagNameRequirement>, BaseCssPropertyName<TagNameRequirement>, ReadonlyArray<string>>) => void;
18
+ assertInputs: (inputInit: DeclarativeElementInit<TagNameRequirement, InputsRequirement, StateRequirement, EventsInitRequirement, BaseCssPropertyName<TagNameRequirement>, BaseCssPropertyName<TagNameRequirement>, ReadonlyArray<string>>) => void;
20
19
  /**
21
20
  * An optional callback which transforms a element definition init object given to the wrapped
22
21
  * element definition.
23
22
  */
24
- transformInputs: (inputInit: DeclarativeElementInit<TagNameRequirement, InputsRequirement, StateInitRequirement, EventsInitRequirement, BaseCssPropertyName<TagNameRequirement>, BaseCssPropertyName<TagNameRequirement>, ReadonlyArray<string>>) => DeclarativeElementInit<TagNameRequirement, InputsRequirement, StateInitRequirement, EventsInitRequirement, BaseCssPropertyName<TagNameRequirement>, BaseCssPropertyName<TagNameRequirement>, ReadonlyArray<string>>;
23
+ transformInputs: (inputInit: DeclarativeElementInit<TagNameRequirement, InputsRequirement, StateRequirement, EventsInitRequirement, BaseCssPropertyName<TagNameRequirement>, BaseCssPropertyName<TagNameRequirement>, ReadonlyArray<string>>) => DeclarativeElementInit<TagNameRequirement, InputsRequirement, StateRequirement, EventsInitRequirement, BaseCssPropertyName<TagNameRequirement>, BaseCssPropertyName<TagNameRequirement>, ReadonlyArray<string>>;
25
24
  }>;
26
25
  /**
27
26
  * Wraps {@link defineElement} and {@link defineElementNoInputs} in a superset of requirements. For
@@ -35,9 +34,9 @@ export type WrapDefineElementOptions<TagNameRequirement extends CustomElementTag
35
34
  *
36
35
  * @category Element Definition
37
36
  */
38
- export declare function wrapDefineElement<TagNameRequirement extends CustomElementTagName = CustomElementTagName, InputsRequirement extends PropertyInitMapBase = {}, StateInitRequirement extends PropertyInitMapBase = {}, EventsInitRequirement extends EventsInitMap = {}>(options?: WrapDefineElementOptions | undefined): {
37
+ export declare function wrapDefineElement<TagNameRequirement extends CustomElementTagName = CustomElementTagName, InputsRequirement extends PropertyInitMapBase = {}, StateRequirement extends PropertyInitMapBase = {}, EventsInitRequirement extends EventsInitMap = {}>(options?: WrapDefineElementOptions | undefined): {
39
38
  /** A wrapped function for defining an element with inputs. */
40
- defineElement: <Inputs extends InputsRequirement>() => <const TagName extends TagNameRequirement, StateInit extends StateInitRequirement, EventsInit extends EventsInitRequirement, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: VerifiedElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("./declarative-element.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
39
+ defineElement: <Inputs extends InputsRequirement>(...errorParams: DeclarativeElementInputErrorParams<Inputs>) => <const TagName extends TagNameRequirement, State extends StateRequirement, EventsInit extends EventsInitRequirement, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(inputs: DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("./declarative-element.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
41
40
  /** A wrapped function for defining an element without inputs. */
42
- defineElementNoInputs: <const TagName extends TagNameRequirement, Inputs extends InputsRequirement, StateInit extends StateInitRequirement, EventsInit extends EventsInitRequirement, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: VerifiedElementNoInputsInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("./declarative-element.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
41
+ defineElementNoInputs: <const TagName extends TagNameRequirement, Inputs extends InputsRequirement, State extends StateRequirement, EventsInit extends EventsInitRequirement, const HostClassKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("./declarative-element.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
43
42
  };
@@ -20,10 +20,10 @@ export function wrapDefineElement(options) {
20
20
  };
21
21
  return {
22
22
  /** A wrapped function for defining an element with inputs. */
23
- defineElement: () => {
23
+ defineElement: (...errorParams) => {
24
24
  return (inputs) => {
25
25
  assertInputs(inputs);
26
- return defineElement()(transformInputs(inputs));
26
+ return defineElement(...errorParams)(transformInputs(inputs));
27
27
  };
28
28
  },
29
29
  /** A wrapped function for defining an element without inputs. */
package/dist/index.d.ts CHANGED
@@ -23,9 +23,7 @@ export * from './declarative-element/properties/css-properties.js';
23
23
  export * from './declarative-element/properties/css-vars.js';
24
24
  export * from './declarative-element/properties/element-events.js';
25
25
  export * from './declarative-element/properties/element-properties.js';
26
- export * from './declarative-element/properties/element-vir-state-setup.js';
27
26
  export * from './declarative-element/properties/host-classes.js';
28
- export * from './declarative-element/properties/per-instance.js';
29
27
  export * from './declarative-element/properties/styles.js';
30
28
  export * from './declarative-element/properties/tag-name.js';
31
29
  export * from './declarative-element/render-callback.js';
package/dist/index.js CHANGED
@@ -23,9 +23,7 @@ export * from './declarative-element/properties/css-properties.js';
23
23
  export * from './declarative-element/properties/css-vars.js';
24
24
  export * from './declarative-element/properties/element-events.js';
25
25
  export * from './declarative-element/properties/element-properties.js';
26
- export * from './declarative-element/properties/element-vir-state-setup.js';
27
26
  export * from './declarative-element/properties/host-classes.js';
28
- export * from './declarative-element/properties/per-instance.js';
29
27
  export * from './declarative-element/properties/styles.js';
30
28
  export * from './declarative-element/properties/tag-name.js';
31
29
  export * from './declarative-element/render-callback.js';
@@ -1,4 +1,4 @@
1
1
  export type VirTagName = `vir-${string}`;
2
- export declare const defineVirElement: <Inputs extends {}>() => <const TagName extends `vir-${string}`, StateInit extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").VerifiedElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, defineVirElementNoInputs: <const TagName extends `vir-${string}`, Inputs extends {}, StateInit extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").VerifiedElementNoInputsInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
3
- export declare const defineVerifiedVirElement: <Inputs extends {}>() => <const TagName extends `vir-${string}`, StateInit extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").VerifiedElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, defineVerifiedVirElementNoInputs: <const TagName extends `vir-${string}`, Inputs extends {}, StateInit extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").VerifiedElementNoInputsInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
4
- export declare const defineTransformedVirElement: <Inputs extends {}>() => <const TagName extends `vir-${string}`, StateInit extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").VerifiedElementInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, defineTransformedVirElementNoInputs: <const TagName extends `vir-${string}`, Inputs extends {}, StateInit extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").VerifiedElementNoInputsInit<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, StateInit, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
2
+ export declare const defineVirElement: <Inputs extends {}>(...errorParams: import("../index.js").DeclarativeElementInputErrorParams<Inputs>) => <const TagName extends `vir-${string}`, State extends {}, EventsInit extends {}, const HostClassKeys extends import("../index.js").BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends import("../index.js").BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(inputs: import("../index.js").DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, defineVirElementNoInputs: <const TagName extends `vir-${string}`, Inputs extends {}, State extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
3
+ export declare const defineVerifiedVirElement: <Inputs extends {}>(...errorParams: import("../index.js").DeclarativeElementInputErrorParams<Inputs>) => <const TagName extends `vir-${string}`, State extends {}, EventsInit extends {}, const HostClassKeys extends import("../index.js").BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends import("../index.js").BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(inputs: import("../index.js").DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, defineVerifiedVirElementNoInputs: <const TagName extends `vir-${string}`, Inputs extends {}, State extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
4
+ export declare const defineTransformedVirElement: <Inputs extends {}>(...errorParams: import("../index.js").DeclarativeElementInputErrorParams<Inputs>) => <const TagName extends `vir-${string}`, State extends {}, EventsInit extends {}, const HostClassKeys extends import("../index.js").BaseCssPropertyName<TagName> = `${TagName}-`, const CssVarKeys extends import("../index.js").BaseCssPropertyName<TagName> = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = Readonly<[]>>(inputs: import("../index.js").DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>, defineTransformedVirElementNoInputs: <const TagName extends `vir-${string}`, Inputs extends {}, State extends {}, EventsInit extends {}, const HostClassKeys extends `${TagName}-${string}` = `${TagName}-`, const CssVarKeys extends `${TagName}-${string}` = `${TagName}-`, const SlotNames extends ReadonlyArray<string> = readonly []>(inputs: import("../index.js").DeclarativeElementInit<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>) => import("../index.js").DeclarativeElementDefinition<TagName, Inputs, State, EventsInit, HostClassKeys, CssVarKeys, SlotNames>;
@@ -2,8 +2,9 @@ type EndpointData = number[];
2
2
  export declare const MyWithAsyncProp: import("../index.js").DeclarativeElementDefinition<"my-with-async-prop", {
3
3
  endpoint: string;
4
4
  }, {
5
- data: import("../index.js").ElementVirStateSetup<import("../index.js").AsyncProp<EndpointData, {
5
+ data: import("../index.js").AsyncProp<EndpointData, {
6
6
  endpoint: string;
7
- }>>;
7
+ }>;
8
+ hi: string;
8
9
  }, {}, "my-with-async-prop-", "my-with-async-prop-", readonly []>;
9
10
  export {};
@@ -10,12 +10,15 @@ async function loadSomething(endpoint) {
10
10
  }
11
11
  export const MyWithAsyncProp = defineElement()({
12
12
  tagName: 'my-with-async-prop',
13
- stateInitStatic: {
14
- data: asyncProp({
15
- async updateCallback({ endpoint }) {
16
- return loadSomething(endpoint);
17
- },
18
- }),
13
+ state() {
14
+ return {
15
+ data: asyncProp({
16
+ async updateCallback({ endpoint }) {
17
+ return loadSomething(endpoint);
18
+ },
19
+ }),
20
+ hi: '',
21
+ };
19
22
  },
20
23
  render({ inputs, state }) {
21
24
  /**
@@ -1,3 +1,3 @@
1
1
  export declare const MyWithAssignmentCleanupCallback: import("../index.js").DeclarativeElementDefinition<"my-with-cleanup-callback", {}, {
2
- intervalId: undefined | number;
2
+ intervalId: number;
3
3
  }, {}, "my-with-cleanup-callback-", "my-with-cleanup-callback-", readonly []>;
@@ -1,23 +1,17 @@
1
1
  import { defineElementNoInputs, html } from '../index.js';
2
2
  export const MyWithAssignmentCleanupCallback = defineElementNoInputs({
3
3
  tagName: 'my-with-cleanup-callback',
4
- stateInitStatic: {
5
- intervalId: undefined,
6
- },
7
- init: ({ updateState }) => {
8
- updateState({
4
+ state() {
5
+ return {
9
6
  intervalId: window.setInterval(() => console.info('hi'), 1000),
10
- });
7
+ };
11
8
  },
12
9
  render() {
13
10
  return html `
14
11
  <h1>My App</h1>
15
12
  `;
16
13
  },
17
- cleanup: ({ state, updateState }) => {
14
+ cleanup({ state }) {
18
15
  window.clearInterval(state.intervalId);
19
- updateState({
20
- intervalId: undefined,
21
- });
22
16
  },
23
17
  });
@@ -2,8 +2,10 @@ import { defineElementNoInputs, html, listen } from '../index.js';
2
2
  import { MyWithEvents } from './my-with-events.element.js';
3
3
  export const MyWithEventListening = defineElementNoInputs({
4
4
  tagName: 'my-with-event-listening',
5
- stateInitStatic: {
6
- myNumber: -1,
5
+ state() {
6
+ return {
7
+ myNumber: -1,
8
+ };
7
9
  },
8
10
  render({ state, updateState }) {
9
11
  return html `
@@ -1,8 +1,10 @@
1
1
  import { css, defineElementNoInputs, html } from '../index.js';
2
2
  export const MyWithHostClassDefinition = defineElementNoInputs({
3
3
  tagName: 'my-with-host-class-definition',
4
- stateInitStatic: {
5
- myProp: 'hello there',
4
+ state() {
5
+ return {
6
+ myProp: 'hello there',
7
+ };
6
8
  },
7
9
  hostClasses: {
8
10
  /**
@@ -1,13 +1,15 @@
1
1
  import { defineElementNoInputs, html, listen } from '../index.js';
2
2
  export const MyWithUpdateState = defineElementNoInputs({
3
3
  tagName: 'my-with-update-state',
4
- stateInitStatic: {
5
- username: 'dev',
6
- /**
7
- * Use "as" to create state properties that can be types other than the initial value's
8
- * type. This is particularly useful when, as below, the initial value is undefined.
9
- */
10
- email: undefined,
4
+ state() {
5
+ return {
6
+ username: 'dev',
7
+ /**
8
+ * Use "as" to create state properties that can be types other than the initial value's
9
+ * type. This is particularly useful when, as below, the initial value is undefined.
10
+ */
11
+ email: undefined,
12
+ };
11
13
  },
12
14
  render({ state, updateState }) {
13
15
  return html `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "element-vir",
3
- "version": "23.4.2",
3
+ "version": "24.0.0",
4
4
  "keywords": [
5
5
  "custom",
6
6
  "web",
@@ -38,19 +38,19 @@
38
38
  "test:docs": "virmator docs check"
39
39
  },
40
40
  "dependencies": {
41
- "@augment-vir/assert": "^31.9.2",
42
- "@augment-vir/common": "^31.9.2",
43
- "date-vir": "^7.2.1",
41
+ "@augment-vir/assert": "^31.10.0",
42
+ "@augment-vir/common": "^31.10.0",
43
+ "date-vir": "^7.3.1",
44
44
  "lit": "^3.2.1",
45
45
  "lit-css-vars": "^3.0.11",
46
46
  "lit-html": "^3.2.1",
47
- "object-shape-tester": "^5.1.4",
47
+ "object-shape-tester": "^5.1.5",
48
48
  "observavir": "^2.0.5",
49
- "typed-event-target": "^4.0.2"
49
+ "typed-event-target": "^4.0.3"
50
50
  },
51
51
  "devDependencies": {
52
- "@augment-vir/test": "^31.9.2",
53
- "@augment-vir/web": "^31.9.2",
52
+ "@augment-vir/test": "^31.10.0",
53
+ "@augment-vir/web": "^31.10.0",
54
54
  "@web/dev-server-esbuild": "^1.0.4",
55
55
  "@web/test-runner": "^0.20.0",
56
56
  "@web/test-runner-commands": "^0.9.0",
@@ -59,10 +59,10 @@
59
59
  "html-spec-tags": "^2.2.2",
60
60
  "istanbul-smart-text-reporter": "^1.1.5",
61
61
  "markdown-code-example-inserter": "^3.0.3",
62
- "type-fest": "^4.35.0",
62
+ "type-fest": "^4.37.0",
63
63
  "typedoc": "^0.27.9",
64
- "typescript": "5.7.3",
65
- "vite": "^6.2.0",
64
+ "typescript": "5.8.2",
65
+ "vite": "^6.2.1",
66
66
  "vite-tsconfig-paths": "^5.1.4"
67
67
  },
68
68
  "engines": {
@@ -1,54 +0,0 @@
1
- import { PropertyInitMapBase } from './element-properties.js';
2
- /**
3
- * Use this key with a function value on any object set in `stateInitStatic` to, rather than using
4
- * the object itself, call the function during each element's initialization and use the output of
5
- * that function rather than the object itself.
6
- *
7
- * See `perInstance` for an example usage.
8
- *
9
- * @category Util
10
- */
11
- export declare const stateSetupKey: unique symbol;
12
- /**
13
- * The type for an object that uses {@link stateSetupKey}.
14
- *
15
- * @category Internal
16
- */
17
- export type ElementVirStateSetup<InnerValue> = {
18
- [key in typeof stateSetupKey]: () => InnerValue;
19
- };
20
- /**
21
- * Check if the given value is a value {@link ElementVirStateSetup} implementation.
22
- *
23
- * @category Internal
24
- */
25
- export declare function isElementVirStateSetup<T = unknown>(input: unknown): input is ElementVirStateSetup<T>;
26
- /**
27
- * If the type parameter is an instance of {@link ElementVirStateSetup}, this unwraps that instance
28
- * to its type parameter. Otherwise, the original type parameter is used.
29
- *
30
- * @category Internal
31
- */
32
- export type UnwrapElementVirStateSetup<T> = T extends ElementVirStateSetup<infer U> ? U : T;
33
- /**
34
- * Allow wrapped state setup objects.
35
- *
36
- * @category Internal
37
- */
38
- export type MaybeElementVirStateSetup<T> = UnwrapElementVirStateSetup<T> | ElementVirStateSetup<UnwrapElementVirStateSetup<T>>;
39
- /**
40
- * Unwraps all {@link ElementVirStateSetup} properties in the given state init object type parameter.
41
- *
42
- * @category Internal
43
- */
44
- export type FlattenElementVirStateSetup<StateInit extends PropertyInitMapBase> = {
45
- [Prop in keyof StateInit]: Extract<StateInit[Prop], ElementVirStateSetup<any>> extends never ? StateInit[Prop] : Extract<StateInit[Prop], ElementVirStateSetup<any>> extends ElementVirStateSetup<infer InnerValue> ? InnerValue | Exclude<StateInit[Prop], ElementVirStateSetup<any>> : StateInit[Prop];
46
- };
47
- /**
48
- * Allows setting stat with {@link ElementVirStateSetup} properties.
49
- *
50
- * @category Internal
51
- */
52
- export type AllowElementVirStateSetup<OriginalObject extends PropertyInitMapBase> = {
53
- [Prop in keyof OriginalObject]: MaybeElementVirStateSetup<OriginalObject[Prop]>;
54
- };
@@ -1,22 +0,0 @@
1
- import { check } from '@augment-vir/assert';
2
- /**
3
- * Use this key with a function value on any object set in `stateInitStatic` to, rather than using
4
- * the object itself, call the function during each element's initialization and use the output of
5
- * that function rather than the object itself.
6
- *
7
- * See `perInstance` for an example usage.
8
- *
9
- * @category Util
10
- */
11
- export const stateSetupKey = Symbol('element-vir-state-setup');
12
- /**
13
- * Check if the given value is a value {@link ElementVirStateSetup} implementation.
14
- *
15
- * @category Internal
16
- */
17
- export function isElementVirStateSetup(input) {
18
- if (!check.isObject(input)) {
19
- return false;
20
- }
21
- return stateSetupKey in input && check.isFunction(input[stateSetupKey]);
22
- }
@@ -1,26 +0,0 @@
1
- /**
2
- * For use within `stateInitStatic`: use this as the value of a state prop to call the given
3
- * callback on each element's creation rather than creating a static state init value.
4
- *
5
- * @category Directives
6
- * @example
7
- *
8
- * ```ts
9
- * import {waitValue, extractErrorMessage} from '@augment-vir/common';
10
- * import {isResolved, html, defineElementNoInputs, perInstance} from 'element-vir';
11
- *
12
- * const MyElement = defineElementNoInputs({
13
- * tagName: 'my-element',
14
- * stateInitStatic: {
15
- * // each instance of `MyElement` will have a different `randomValue`
16
- * randomValue: perInstance(() => Math.random()),
17
- * },
18
- * render({state}) {
19
- * return html`
20
- * Value: ${state.randomValue}
21
- * `;
22
- * },
23
- * });
24
- * ```
25
- */
26
- export declare function perInstance<T>(creationCallback: () => T): T;
@@ -1,32 +0,0 @@
1
- import { stateSetupKey } from './element-vir-state-setup.js';
2
- /**
3
- * For use within `stateInitStatic`: use this as the value of a state prop to call the given
4
- * callback on each element's creation rather than creating a static state init value.
5
- *
6
- * @category Directives
7
- * @example
8
- *
9
- * ```ts
10
- * import {waitValue, extractErrorMessage} from '@augment-vir/common';
11
- * import {isResolved, html, defineElementNoInputs, perInstance} from 'element-vir';
12
- *
13
- * const MyElement = defineElementNoInputs({
14
- * tagName: 'my-element',
15
- * stateInitStatic: {
16
- * // each instance of `MyElement` will have a different `randomValue`
17
- * randomValue: perInstance(() => Math.random()),
18
- * },
19
- * render({state}) {
20
- * return html`
21
- * Value: ${state.randomValue}
22
- * `;
23
- * },
24
- * });
25
- * ```
26
- */
27
- export function perInstance(creationCallback) {
28
- const stateSetup = {
29
- [stateSetupKey]: creationCallback,
30
- };
31
- return stateSetup;
32
- }