ilha 0.0.1 → 0.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 (4) hide show
  1. package/README.md +387 -176
  2. package/dist/index.d.ts +79 -32
  3. package/dist/index.js +375 -205
  4. package/package.json +4 -20
package/dist/index.d.ts CHANGED
@@ -37,18 +37,18 @@ interface RawHtml {
37
37
  [RAW]: true;
38
38
  value: string;
39
39
  }
40
- interface SlotAccessor {
41
- (props?: Record<string, unknown>): RawHtml;
40
+ interface SlotAccessor<TProps = Record<string, unknown>> {
41
+ (props?: TProps): RawHtml;
42
42
  toString(): string;
43
43
  [SLOT_ACCESSOR]: true;
44
44
  }
45
45
  interface MarkedSignalAccessor<T> {
46
46
  (): T;
47
- (value: T): void;
47
+ (...args: [value: T]): void;
48
48
  [SIGNAL_ACCESSOR]: true;
49
49
  }
50
50
  declare function ilhaRaw(value: string): RawHtml;
51
- declare function ilhaHtml(strings: TemplateStringsArray, ...values: unknown[]): string;
51
+ declare function ilhaHtml(strings: TemplateStringsArray, ...values: unknown[]): RawHtml;
52
52
  type ContextSignal<T> = {
53
53
  (): T;
54
54
  (value: T): void;
@@ -70,20 +70,36 @@ interface DerivedEntry<TInput, TStateMap extends Record<string, unknown>> {
70
70
  fn: DerivedFn<TInput, TStateMap, unknown>;
71
71
  }
72
72
  type IslandDerived<TDerivedMap extends Record<string, unknown>> = { readonly [K in keyof TDerivedMap]: DerivedValue<TDerivedMap[K]> };
73
+ type ExternalSignal<T = unknown> = {
74
+ (): T;
75
+ (value: T): void;
76
+ };
73
77
  interface BindEntry<TStateMap extends Record<string, unknown>> {
74
78
  selector: string;
75
- stateKey: keyof TStateMap & string;
79
+ target: (keyof TStateMap & string) | ExternalSignal;
76
80
  }
77
81
  type SignalAccessor<T> = MarkedSignalAccessor<T>;
78
- type IslandState<TStateMap extends Record<string, unknown>> = { [K in keyof TStateMap]: SignalAccessor<TStateMap[K]> };
79
- interface Island<TInput, TStateMap extends Record<string, unknown>> {
82
+ type MergeState<TStateMap extends Record<string, unknown>, K extends string, V> = { [P in keyof TStateMap as P extends K ? never : P]-?: TStateMap[P] } & Record<K, V>;
83
+ type IslandState<TStateMap extends Record<string, unknown>> = { readonly [K in keyof TStateMap]-?: SignalAccessor<TStateMap[K]> };
84
+ interface HydratableOptions {
85
+ name: string;
86
+ as?: string;
87
+ snapshot?: boolean | {
88
+ state?: boolean;
89
+ derived?: boolean;
90
+ };
91
+ skipOnMount?: boolean;
92
+ }
93
+ interface Island<TInput = Record<string, unknown>, _TStateMap extends Record<string, unknown> = Record<string, unknown>> {
80
94
  (props?: Partial<TInput>): string | Promise<string>;
81
95
  toString(props?: Partial<TInput>): string;
82
- mount(el: Element, props?: Partial<TInput>): () => void;
96
+ mount(host: Element, props?: Partial<TInput>): () => void;
97
+ hydratable(props: Partial<TInput>, options: HydratableOptions): Promise<string>;
83
98
  }
84
- type AnyIsland = Island<Record<string, unknown>, Record<string, unknown>>;
99
+ type InferIslandInput<T> = T extends Island<infer TInput, any> ? TInput : Record<string, unknown>;
100
+ type AnyIsland = Island<any, any>;
85
101
  type SlotMap = Record<string, AnyIsland>;
86
- type SlotsProxy<TSlots extends SlotMap> = { readonly [K in keyof TSlots]: SlotAccessor };
102
+ type SlotsProxy<TSlots extends SlotMap> = { readonly [K in keyof TSlots]: SlotAccessor<Partial<InferIslandInput<TSlots[K]>>> };
87
103
  type RenderContext<TInput, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown>, TSlots extends SlotMap> = {
88
104
  state: IslandState<TStateMap>;
89
105
  derived: IslandDerived<TDerivedMap>;
@@ -93,59 +109,89 @@ type RenderContext<TInput, TStateMap extends Record<string, unknown>, TDerivedMa
93
109
  type EffectContext<TInput, TStateMap extends Record<string, unknown>> = {
94
110
  state: IslandState<TStateMap>;
95
111
  input: TInput;
96
- el: Element;
112
+ host: Element;
97
113
  };
98
- type HandlerContext<TInput, TStateMap extends Record<string, unknown>> = {
114
+ type OnMountContext<TInput, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown> = Record<string, never>> = {
99
115
  state: IslandState<TStateMap>;
116
+ derived: IslandDerived<TDerivedMap>;
100
117
  input: TInput;
101
- el: Element;
118
+ host: Element;
119
+ hydrated: boolean;
120
+ };
121
+ type HandlerContext<TInput, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown> = Record<string, never>> = {
122
+ state: IslandState<TStateMap>;
123
+ derived: IslandDerived<TDerivedMap>;
124
+ input: TInput;
125
+ host: Element;
126
+ target: Element;
102
127
  event: Event;
103
128
  };
129
+ type HTMLEventName = keyof HTMLElementEventMap & string;
130
+ type Modifier = "once" | "capture" | "passive";
131
+ type WithModifiers<E extends string> = E | `${E}:${Modifier}` | `${E}:${Modifier}:${Modifier}` | `${E}:${Modifier}:${Modifier}:${Modifier}`;
132
+ type OnSelectorString = `@${WithModifiers<HTMLEventName>}` | `${string}@${WithModifiers<HTMLEventName>}`;
133
+ type HandlerContextFor<TInput, TStateMap extends Record<string, unknown>, TEventName extends string, TDerivedMap extends Record<string, unknown> = Record<string, never>> = {
134
+ state: IslandState<TStateMap>;
135
+ derived: IslandDerived<TDerivedMap>;
136
+ input: TInput;
137
+ host: Element;
138
+ target: TEventName extends keyof HTMLElementEventMap ? HTMLElementEventMap[TEventName]["target"] extends Element | null ? NonNullable<HTMLElementEventMap[TEventName]["target"]> : Element : Element;
139
+ event: TEventName extends keyof HTMLElementEventMap ? HTMLElementEventMap[TEventName] : Event;
140
+ };
104
141
  type StateInit<TInput, V> = V | ((input: TInput) => V);
105
142
  interface StateEntry<TInput> {
106
143
  key: string;
107
144
  init: StateInit<TInput, unknown>;
108
145
  }
109
- interface OnEntry<TInput, TStateMap extends Record<string, unknown>> {
146
+ interface OnEntry<TInput, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown> = Record<string, never>> {
110
147
  selector: string;
111
148
  event: string;
112
149
  options: AddEventListenerOptions;
113
- handler: (ctx: HandlerContext<TInput, TStateMap>) => void | Promise<void>;
150
+ handler: (ctx: HandlerContext<TInput, TStateMap, TDerivedMap>) => void | Promise<void>;
114
151
  }
115
152
  interface EffectEntry<TInput, TStateMap extends Record<string, unknown>> {
116
153
  fn: (ctx: EffectContext<TInput, TStateMap>) => (() => void) | void;
117
154
  }
155
+ interface OnMountEntry<TInput, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown>> {
156
+ fn: (ctx: OnMountContext<TInput, TStateMap, TDerivedMap>) => (() => void) | void;
157
+ }
118
158
  interface TransitionOptions {
119
- enter?: (el: Element) => Promise<void> | void;
120
- leave?: (el: Element) => Promise<void> | void;
159
+ enter?: (host: Element) => Promise<void> | void;
160
+ leave?: (host: Element) => Promise<void> | void;
121
161
  }
122
162
  interface MountOptions {
123
163
  root?: Element;
124
- hydrate?: boolean;
125
164
  lazy?: boolean;
126
165
  }
127
166
  interface MountResult {
128
167
  unmount: () => void;
129
168
  }
169
+ interface BuilderConfig<TInput, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown>, _TSlots extends SlotMap> {
170
+ schema: StandardSchemaV1 | null;
171
+ states: StateEntry<TInput>[];
172
+ deriveds: DerivedEntry<TInput, TStateMap>[];
173
+ ons: OnEntry<TInput, TStateMap, TDerivedMap>[];
174
+ effects: EffectEntry<TInput, TStateMap>[];
175
+ onMounts: OnMountEntry<TInput, TStateMap, TDerivedMap>[];
176
+ slots: Record<string, AnyIsland>;
177
+ transition: TransitionOptions | null;
178
+ binds: BindEntry<TStateMap>[];
179
+ }
130
180
  declare class IlhaBuilder<TInput extends Record<string, unknown>, TStateMap extends Record<string, unknown>, TDerivedMap extends Record<string, unknown> = Record<string, never>, TSlots extends SlotMap = Record<string, never>> {
131
- private readonly _schema;
132
- private readonly _states;
133
- private readonly _deriveds;
134
- private readonly _ons;
135
- private readonly _effects;
136
- private readonly _slots;
137
- private readonly _transition;
138
- private readonly _binds;
139
- constructor(_schema: StandardSchemaV1 | null, _states: StateEntry<TInput>[], _deriveds: DerivedEntry<TInput, TStateMap>[], _ons: OnEntry<TInput, TStateMap>[], _effects: EffectEntry<TInput, TStateMap>[], _slots: Record<string, AnyIsland>, _transition: TransitionOptions | null, _binds: BindEntry<TStateMap>[]);
181
+ readonly _cfg: BuilderConfig<TInput, TStateMap, TDerivedMap, TSlots>;
182
+ constructor(cfg: BuilderConfig<TInput, TStateMap, TDerivedMap, TSlots>);
140
183
  input<S extends StandardSchemaV1>(schema: S): IlhaBuilder<StandardSchemaV1.InferOutput<S> & Record<string, unknown>, Record<string, never>, Record<string, never>, Record<string, never>>;
141
- state<K extends string, V>(key: K, init: StateInit<TInput, V>): IlhaBuilder<TInput, TStateMap & Record<K, V>, TDerivedMap, TSlots>;
184
+ state<V = undefined, K extends string = string>(key: K, init?: StateInit<TInput, V> | undefined): IlhaBuilder<TInput, MergeState<TStateMap, K, V>, TDerivedMap, TSlots>;
142
185
  derived<K extends string, V>(key: K, fn: DerivedFn<TInput, TStateMap, V>): IlhaBuilder<TInput, TStateMap, TDerivedMap & Record<K, V>, TSlots>;
143
186
  bind(selector: string, stateKey: keyof TStateMap & string): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
144
- on(selectorOrCombined: string, callbackOrEventType: ((ctx: HandlerContext<TInput, TStateMap>) => void | Promise<void>) | string, handler?: (ctx: HandlerContext<TInput, TStateMap>) => void | Promise<void>): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
187
+ bind<T>(selector: string, externalSignal: ExternalSignal<T>): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
188
+ on<S extends OnSelectorString>(selectorOrCombined: S, handler: (ctx: S extends `${string}@${infer E}:${string}` ? HandlerContextFor<TInput, TStateMap, E, TDerivedMap> : S extends `${string}@${infer E}` ? HandlerContextFor<TInput, TStateMap, E, TDerivedMap> : HandlerContext<TInput, TStateMap, TDerivedMap>) => void | Promise<void>): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
189
+ on(selectorOrCombined: string, handler: (ctx: HandlerContext<TInput, TStateMap, TDerivedMap>) => void | Promise<void>): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
145
190
  effect(fn: (ctx: EffectContext<TInput, TStateMap>) => (() => void) | void): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
146
- slot<K extends string>(name: K, island: AnyIsland): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots & Record<K, AnyIsland>>;
191
+ onMount(fn: (ctx: OnMountContext<TInput, TStateMap, TDerivedMap>) => (() => void) | void): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
192
+ slot<K extends string, I extends AnyIsland>(name: K, island: I): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots & Record<K, I>>;
147
193
  transition(opts: TransitionOptions): IlhaBuilder<TInput, TStateMap, TDerivedMap, TSlots>;
148
- render(fn: (ctx: RenderContext<TInput, TStateMap, TDerivedMap, TSlots>) => string): Island<TInput, TStateMap>;
194
+ render(fn: (ctx: RenderContext<TInput, TStateMap, TDerivedMap, TSlots>) => string | RawHtml): Island<TInput, TStateMap>;
149
195
  }
150
196
  declare function ilhaFrom<TInput, TStateMap extends Record<string, unknown>>(selector: string | Element, island: Island<TInput, TStateMap>, props?: Partial<TInput>): (() => void) | null;
151
197
  type IslandRegistry = Record<string, AnyIsland>;
@@ -157,10 +203,11 @@ declare const ilha: IlhaBuilder<Record<string, unknown>, Record<string, never>,
157
203
  from: typeof ilhaFrom;
158
204
  context: typeof ilhaContext;
159
205
  };
206
+ declare function type<TInput, TOutput = TInput>(coerce?: (input: TInput) => TOutput): StandardSchemaV1<TInput, TOutput>;
160
207
  declare const html: typeof ilhaHtml;
161
208
  declare const raw: typeof ilhaRaw;
162
209
  declare const mount: typeof mountAll;
163
210
  declare const from: typeof ilhaFrom;
164
211
  declare const context: typeof ilhaContext;
165
212
  //#endregion
166
- export { DerivedValue, HandlerContext, Island, IslandDerived, IslandState, MountOptions, MountResult, SignalAccessor, SlotAccessor, context, ilha as default, from, html, mount, raw };
213
+ export { DerivedValue, HandlerContext, HandlerContextFor, HydratableOptions, Island, IslandDerived, IslandState, MountOptions, MountResult, OnMountContext, RawHtml, SignalAccessor, SlotAccessor, context, ilha as default, from, html, mount, raw, type };