mi-element 0.1.0 → 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.
package/types/escape.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export function escHtml(string: string): string
2
- export function escAttr(string: string): string
3
- export function esc(strings: string[], ...vars: any[]): string
1
+ export function escHtml(string: string): string;
2
+ export function escAttr(string: string): string;
3
+ export function esc(strings: string[], ...vars: any[]): string;
package/types/index.d.ts CHANGED
@@ -1,14 +1,15 @@
1
- export type Context = import('./context.js').Context
2
- export type HostController = import('./element.js').HostController
3
- export type Callback = import('./signal.js').Callback
4
- export type Action = import('./store.js').Action
5
- export {
6
- ContextConsumer,
7
- ContextProvider,
8
- ContextRequestEvent
9
- } from './context.js'
10
- export { MiElement, convertType, define } from './element.js'
11
- export { esc, escAttr, escHtml } from './escape.js'
12
- export { refsById, refsBySelector } from './refs.js'
13
- export { Signal, createSignal, isSignalLike } from './signal.js'
14
- export { Store, subscribeToStore } from './store.js'
1
+ export { Signal };
2
+ export { Store } from "./store.js";
3
+ export type Context = import("./context.js").Context;
4
+ export type HostController = import("./element.js").HostController;
5
+ /**
6
+ * <T>
7
+ */
8
+ export type SignalOptions<T> = import("./signal.js").SignalOptions<T>;
9
+ export type Action = import("./store.js").Action;
10
+ import Signal from './signal.js';
11
+ export { ContextConsumer, ContextProvider, ContextRequestEvent } from "./context.js";
12
+ export { MiElement, convertType, define } from "./element.js";
13
+ export { esc, escAttr, escHtml } from "./escape.js";
14
+ export { refsById, refsBySelector } from "./refs.js";
15
+ export { classMap, styleMap } from "./styling.js";
package/types/refs.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Helper function to find `id` attributes in `container`s node tree
2
+ * Helper function to find `id` attributes in `container`s node tree.
3
+ * id names are camelCased, e.g. 'list-container' becomes 'listContainer'
3
4
  * @param {Element} container root element
4
5
  * @returns {Record<string, Node>|{}} record of found references
5
6
  * @example
@@ -7,7 +8,7 @@
7
8
  * references = refs(el)
8
9
  * //> references = { p: <p>, named: <span> }
9
10
  */
10
- export function refsById(container: Element): Record<string, Node> | {}
11
+ export function refsById(container: Element): Record<string, Node> | {};
11
12
  /**
12
13
  * Helper function to gather references by a map of selectors
13
14
  * @param {Element} container root element
@@ -18,7 +19,4 @@ export function refsById(container: Element): Record<string, Node> | {}
18
19
  * references = refs(el, { p: 'p', named: 'p > span' })
19
20
  * //> references = { p: <p>, named: <span> }
20
21
  */
21
- export function refsBySelector(
22
- container: Element,
23
- selectors: Record<string, string>
24
- ): Record<string, Node> | {}
22
+ export function refsBySelector(container: Element, selectors: Record<string, string>): Record<string, Node> | {};
package/types/signal.d.ts CHANGED
@@ -1,36 +1,54 @@
1
1
  /**
2
- * @typedef {(value: any) => void} Callback
2
+ * @param {() => void} cb
3
3
  */
4
- export class Signal {
5
- /**
6
- * creates a new signal with an initial value
7
- * @param {any} [initialValue]
8
- */
9
- constructor(initialValue?: any)
10
- _subscribers: Set<any>
11
- _value: any
12
- /**
13
- * set new value on signal;
14
- * if value has changed all subscribers are called
15
- * @param {any} newValue
16
- */
17
- set value(newValue: any)
18
- /**
19
- * return current value
20
- * @returns {any}
21
- */
22
- get value(): any
23
- /**
24
- * notify all subscribers on current value
25
- */
26
- notify(): void
27
- /**
28
- * subscribe to signal to receive value updates
29
- * @param {Callback} callback
30
- * @returns {() => void} unsubscribe function
31
- */
32
- subscribe(callback: Callback): () => void
4
+ export function effect(cb: () => void): () => void;
5
+ /**
6
+ * @template T
7
+ * @typedef {{equals: (value?: T|null, nextValue?: T|null) => boolean}} SignalOptions
8
+ */
9
+ /**
10
+ * tries to follow proposal (a bit)
11
+ * @see https://github.com/tc39/proposal-signals
12
+ * @template T
13
+ */
14
+ export class State<T> {
15
+ /**
16
+ * @param {T|null} [value]
17
+ * @param {SignalOptions<T>} [options]
18
+ */
19
+ constructor(value?: T | null | undefined, options?: SignalOptions<T> | undefined);
20
+ subscribers: Set<any>;
21
+ value: T | null | undefined;
22
+ equals: (value?: T | null | undefined, nextValue?: T | null | undefined) => boolean;
23
+ /**
24
+ * @returns {T|null|undefined}
25
+ */
26
+ get(): T | null | undefined;
27
+ /**
28
+ * @param {T|null|undefined} nextValue
29
+ */
30
+ set(nextValue: T | null | undefined): void;
31
+ }
32
+ export function createSignal<T>(value: T): State<T>;
33
+ export class Computed {
34
+ /**
35
+ * @param {() => void} cb
36
+ */
37
+ constructor(cb: () => void);
38
+ state: State<any>;
39
+ /**
40
+ * @template T
41
+ * @returns {T}
42
+ */
43
+ get<T>(): T;
44
+ }
45
+ declare namespace _default {
46
+ export { State };
47
+ export { createSignal };
48
+ export { effect };
49
+ export { Computed };
33
50
  }
34
- export function createSignal(initialValue?: any): Signal
35
- export function isSignalLike(possibleSignal: any): boolean
36
- export type Callback = (value: any) => void
51
+ export default _default;
52
+ export type SignalOptions<T> = {
53
+ equals: (value?: T | null, nextValue?: T | null) => boolean;
54
+ };
package/types/store.d.ts CHANGED
@@ -4,43 +4,61 @@
4
4
  /**
5
5
  * @typedef {(state: any, data?: any) => any} Action
6
6
  */
7
+ /**
8
+ * @template T
9
+ * @typedef {import('./signal.js').SignalOptions<T>} SignalOptions<T>
10
+ */
7
11
  /**
8
12
  * Store implementing [Flux](https://www.npmjs.com/package/flux) pattern based
9
13
  * on Signals
14
+ * @template T
10
15
  */
11
- export class Store extends Signal {
12
- /**
13
- * @param {Record<string, Action>} actions
14
- * @param {any} [initialValue]
15
- * @example
16
- * ```js
17
- * const actions = { increment: (by = 1) => (current) => current + by }
18
- * const initialValue = 1
19
- * const store = new Store(actions, initialValue)
20
- * // subscribe with a callback function
21
- * const unsubscribe = store.subscribe((value) => console.log(`count is ${value}`))
22
- * // change the store
23
- * store.increment(2) // increment by 2
24
- * //> count is 3
25
- * unsubscribe()
26
- * ```
27
- *
28
- * if `initialValue` is an object, the object's reference must be changed
29
- * using the spread operator, in order to notify on state changes, e.g.
30
- * ```js
31
- * const initialValue = { count: 0, other: 'foo' }
32
- * const actions = {
33
- * increment: (by = 1) => (state) => ({...state, count: state.count + by})
34
- * }
35
- * ```
36
- */
37
- constructor(actions: Record<string, Action>, initialValue?: any)
16
+ export class Store<T> extends State<any> {
17
+ /**
18
+ * @param {Record<string, Action>} actions
19
+ * @param {T|null} [initialValue]
20
+ * @param {SignalOptions<T>} [options]
21
+ * @example
22
+ * ```js
23
+ * import { Signal, Store } from 'mi-element'
24
+ * const actions = { increment: (by = 1) => (current) => current + by }
25
+ * const initialValue = 1
26
+ * const store = new Store(actions, initialValue)
27
+ * // subscribe with a callback function
28
+ * const unsubscribe = Signal.effect(() => console.log(`count is ${store.get()}`))
29
+ * // change the store
30
+ * store.increment(2) // increment by 2
31
+ * //> count is 3
32
+ * unsubscribe()
33
+ * ```
34
+ *
35
+ * if `initialValue` is an object, the object's reference must be changed
36
+ * using the spread operator, in order to notify on state changes, e.g.
37
+ * ```js
38
+ * const initialValue = { count: 0, other: 'foo' }
39
+ * const actions = {
40
+ * increment: (by = 1) => (state) => ({...state, count: state.count + by})
41
+ * }
42
+ * ```
43
+ * or you change the signals options equality function
44
+ * ```js
45
+ * const actions = {
46
+ * increment: (by = 1) => (state) => {
47
+ * state.count += by
48
+ * return state
49
+ * }
50
+ * }
51
+ * const initialValue = { count: 0, other: 'foo' }
52
+ * const options = { equals: (value, nextValue) => true }
53
+ * const store = new Store(actions, initialValue, options)
54
+ * ```
55
+ */
56
+ constructor(actions: Record<string, Action>, initialValue?: T | null | undefined, options?: SignalOptions<T> | undefined);
38
57
  }
39
- export function subscribeToStore(
40
- element: MiElement,
41
- store: Store,
42
- prop: string | string[]
43
- ): void
44
- export type MiElement = import('./element.js').MiElement
45
- export type Action = (state: any, data?: any) => any
46
- import { Signal } from './signal.js'
58
+ export type MiElement = import("./element.js").MiElement;
59
+ export type Action = (state: any, data?: any) => any;
60
+ /**
61
+ * <T>
62
+ */
63
+ export type SignalOptions<T> = import("./signal.js").SignalOptions<T>;
64
+ import { State } from './signal.js';
@@ -0,0 +1,8 @@
1
+ export function classMap(map: {
2
+ [name: string]: string | boolean | number;
3
+ }): string;
4
+ export function styleMap(map: {
5
+ [name: string]: string | number | undefined | null;
6
+ }, options?: {
7
+ unit?: string | undefined;
8
+ } | undefined): string;