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