nesquick 0.0.20 → 0.0.22

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.
@@ -2,14 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NesquickComponent = void 0;
4
4
  const State_1 = require("./State");
5
- function functionizeProps(props) {
6
- for (const k in props) {
7
- if (typeof props[k] !== "function") {
8
- const v = props[k];
9
- props[k] = () => v;
10
- }
11
- }
12
- }
13
5
  const SVGNamespaces = new Map([
14
6
  ["xlink", "http://www.w3.org/1999/xlink"],
15
7
  ["xml", "http://www.w3.org/XML/1998/namespace"]
@@ -19,6 +11,7 @@ class NesquickComponent {
19
11
  this._render = _render;
20
12
  this.props = props;
21
13
  this._subscriptions = new State_1.Subscriptions();
14
+ this._styleSubscriptions = null;
22
15
  this._xmlns = null;
23
16
  this._children = [];
24
17
  this.props = props;
@@ -26,7 +19,6 @@ class NesquickComponent {
26
19
  render(document) {
27
20
  State_1.subscriptions.set(this._subscriptions);
28
21
  if (typeof this._render === "function") {
29
- functionizeProps(this.props);
30
22
  const element = this._render(this.props);
31
23
  if (this._xmlns) {
32
24
  element.setXmlns(this._xmlns);
@@ -98,7 +90,10 @@ class NesquickComponent {
98
90
  _renderPropsNs(attributes, element, props) {
99
91
  for (const k in props) {
100
92
  if (k !== "children" && k !== "xmlns" && k !== "ref") {
101
- if (typeof props[k] === "function") {
93
+ if (k === "style") {
94
+ this._renderStyle(element, props[k]);
95
+ }
96
+ else if (typeof props[k] === "function") {
102
97
  if (k.startsWith("on")) {
103
98
  element[k.toLowerCase()] = props[k];
104
99
  }
@@ -132,7 +127,10 @@ class NesquickComponent {
132
127
  _renderProps(element, props) {
133
128
  for (const k in props) {
134
129
  if (k !== "children" && k !== "xmlns" && k !== "ref") {
135
- if (typeof props[k] === "function") {
130
+ if (k === "style") {
131
+ this._renderStyle(element, props[k]);
132
+ }
133
+ else if (typeof props[k] === "function") {
136
134
  if (k.startsWith("on")) {
137
135
  element[k.toLowerCase()] = props[k];
138
136
  }
@@ -148,6 +146,65 @@ class NesquickComponent {
148
146
  }
149
147
  }
150
148
  }
149
+ _renderStyles(element, styles) {
150
+ for (const k in styles) {
151
+ if (typeof styles[k] === "function") {
152
+ (0, State_1.useRender)(styles[k], v => {
153
+ element.style[k] = String(v);
154
+ });
155
+ }
156
+ else {
157
+ element.style[k] = String(styles[k]);
158
+ }
159
+ }
160
+ }
161
+ _renderStyle(element, style) {
162
+ switch (typeof style) {
163
+ case "function": {
164
+ (0, State_1.useRender)(style, (style, isState) => {
165
+ if (this._styleSubscriptions != null) {
166
+ this._styleSubscriptions.dispose();
167
+ this._styleSubscriptions = null;
168
+ }
169
+ switch (typeof style) {
170
+ case "object": {
171
+ if (style) {
172
+ if (isState) {
173
+ element.removeAttribute("style");
174
+ this._styleSubscriptions = new State_1.Subscriptions();
175
+ State_1.subscriptions.set(this._styleSubscriptions);
176
+ this._renderStyles(element, style);
177
+ State_1.subscriptions.reset();
178
+ }
179
+ else {
180
+ this._renderStyles(element, style);
181
+ }
182
+ }
183
+ break;
184
+ }
185
+ default: {
186
+ element.setAttribute("style", String(style));
187
+ break;
188
+ }
189
+ }
190
+ });
191
+ break;
192
+ }
193
+ case "object": {
194
+ if (style) {
195
+ this._renderStyles(element, style);
196
+ }
197
+ else {
198
+ element.removeAttribute("style");
199
+ }
200
+ break;
201
+ }
202
+ default: {
203
+ element.setAttribute("style", String(style));
204
+ break;
205
+ }
206
+ }
207
+ }
151
208
  _renderChildren(document, parent, children) {
152
209
  if (children != null) {
153
210
  if (!Array.isArray(children)) {
@@ -279,6 +336,7 @@ class NesquickComponent {
279
336
  }
280
337
  dispose() {
281
338
  this._subscriptions.dispose();
339
+ this._styleSubscriptions?.dispose();
282
340
  for (const child of this._children) {
283
341
  if (child.component) {
284
342
  child.component.dispose();
package/lib/State.js CHANGED
@@ -144,5 +144,5 @@ function runSubscription(sub) {
144
144
  sub.states.delete(state);
145
145
  }
146
146
  }
147
- sub.reaction?.(res);
147
+ sub.reaction?.(res, sub.states.size > 0);
148
148
  }
@@ -26,6 +26,7 @@ export declare class NesquickComponent<P extends ComponentProps = {}> {
26
26
  private _render;
27
27
  protected props: P;
28
28
  private _subscriptions;
29
+ private _styleSubscriptions;
29
30
  private _xmlns;
30
31
  protected _children: NesquickChild[];
31
32
  constructor(_render: string | FunctionComponent<P>, props: P);
@@ -34,6 +35,8 @@ export declare class NesquickComponent<P extends ComponentProps = {}> {
34
35
  private _getAttributeNs;
35
36
  private _renderPropsNs;
36
37
  private _renderProps;
38
+ private _renderStyles;
39
+ private _renderStyle;
37
40
  protected _renderChildren(document: VeactDocument, parent: NesquickParent, children?: Children): void;
38
41
  protected _pushChild(): NesquickChild;
39
42
  protected _spliceChild(i: number): NesquickChild;
@@ -1,7 +1,7 @@
1
1
  export type State<T> = [get: () => T, set: (value: T) => void];
2
2
  export type Subscription<T> = {
3
3
  cb: () => T;
4
- reaction: ((data: T) => void) | null;
4
+ reaction: ((data: T, isState: boolean) => void) | null;
5
5
  iteration: number;
6
6
  states: Map<Set<Subscription<any>>, number>;
7
7
  next: Subscription<T> | null;
@@ -19,6 +19,6 @@ export declare namespace subscriptions {
19
19
  function reset(): void;
20
20
  }
21
21
  export declare function useState<T>(value: T): State<T>;
22
- export declare function useEffect<T>(cb: () => T, reaction?: ((data: T) => void) | null): void;
23
- export declare function useRender<T>(cb: () => T, reaction?: ((data: T) => void) | null): void;
22
+ export declare function useEffect<T>(cb: () => T, reaction?: ((data: T, isState: boolean) => void) | null): void;
23
+ export declare function useRender<T>(cb: () => T, reaction?: ((data: T, isState: boolean) => void) | null): void;
24
24
  export declare function useDispose(cb: () => void): void;
@@ -3,19 +3,24 @@ import { NesquickFragment } from "./NesquickFragment";
3
3
  export declare const Fragment: unique symbol;
4
4
  export declare function jsxs<P extends ComponentProps>(type: string | FunctionComponent<P> | typeof Fragment, props: P, key?: string | number | null): NesquickFragment | NesquickComponent<P>;
5
5
  export declare const jsx: typeof jsxs;
6
+ type HasUndefined<T, K extends keyof T> = {
7
+ [L in K]-?: T[K] | undefined;
8
+ } extends {
9
+ [L in K]?: T[K];
10
+ } ? undefined extends T[K] ? true : false : false;
6
11
  declare const WrappedFunctionType: unique symbol;
7
12
  type WrappedFunction<T> = (() => T) & {
8
13
  readonly [WrappedFunctionType]?: T;
9
14
  };
10
- type UserProp<T> = T extends (...args: infer A) => infer R ? (((...args: A) => R) | T) : WrappedFunction<T>;
11
- type ComponentProp<T> = T extends {
12
- readonly [WrappedFunctionType]?: infer R;
13
- } ? (T | R) : T extends (...args: any[]) => any ? T : (T | (() => T));
15
+ type UserProp<T> = T extends (...args: any[]) => any ? T : WrappedFunction<T>;
14
16
  type UserProps<T> = {
15
- [K in keyof T]: UserProp<T[K]>;
17
+ readonly [K in keyof T]: HasUndefined<T, K> extends true ? UserProp<T[K] | undefined> : UserProp<Exclude<T[K], undefined>>;
16
18
  };
19
+ type JSXProp<T> = T extends {
20
+ readonly [WrappedFunctionType]?: infer R;
21
+ } ? R : T;
17
22
  type JSXProps<T> = keyof T extends never ? {} : {
18
- [K in keyof T]: ComponentProp<T[K]>;
23
+ [K in keyof T]: JSXProp<T[K]>;
19
24
  };
20
25
  export type Generic<T> = T extends (...args: any) => infer R ? R : T;
21
26
  export { UserProps as Props };
@@ -32,23 +37,22 @@ export declare namespace JSX {
32
37
  };
33
38
  export interface Props<T extends EventTarget = HTMLElement> extends JSXHTMLEvent<T>, JSXSVGEvent<T> {
34
39
  [k: string]: any;
35
- style?: StyleProps;
36
- xmlns?: string;
37
- ref?: (el: T) => void;
40
+ style?: Style;
41
+ xmlns?: string | null;
42
+ ref?: ((el: T) => void) | null;
38
43
  }
44
+ export type Style = StyleProps | string;
39
45
  export type StyleProps = {
40
46
  [K in keyof CSSStyleDeclaration]?: CSSStyleDeclaration[K] extends Function ? never : CSSStyleDeclaration[K] | (() => CSSStyleDeclaration[K]);
41
47
  };
42
48
  export type HTMLProps<T extends HTMLElement = HTMLElement> = Props<T>;
43
49
  export type SVGProps<T extends SVGElement = SVGElement> = Props<T>;
44
- export type JSXElements = {
50
+ export type IntrinsicElements = {
45
51
  [K in keyof HTMLElementTagNameMap]: HTMLProps<HTMLElementTagNameMap[K]>;
46
52
  } & {
47
53
  [K in keyof SVGElementTagNameMap]: SVGProps<SVGElementTagNameMap[K]>;
48
54
  };
49
55
  export type Element = NesquickComponent<any>;
50
- export interface IntrinsicElements extends JSXElements {
51
- }
52
56
  export type ElementType = keyof IntrinsicElements | Component<any> | typeof NesquickComponent<any>;
53
57
  const NotEmptyObject: unique symbol;
54
58
  export type IntrinsicAttributes = {
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types",
6
6
  "main": "./lib",
7
7
  "typesVersions": {
8
8
  "*": {
9
9
  "jsx-runtime": ["./lib/types/jsx-runtime.d.ts"],
10
- "jsx-dev-runtime": ["./lib/types/jsx-dev-runtime.d.ts"]
10
+ "jsx-dev-runtime": ["./lib/types/jsx-dev-runtime.d.ts"],
11
+ "no-transformer/jsx-runtime": ["./lib/types/no-transformer/jsx-runtime.d.ts"],
12
+ "no-transformer/jsx-dev-runtime": ["./lib/types/no-transformer/jsx-dev-runtime.d.ts"]
11
13
  }
12
14
  },
13
15
  "bin": {
@@ -17,6 +19,7 @@
17
19
  "sideEffects": false,
18
20
  "files": [
19
21
  "lib/",
22
+ "no-transformer/",
20
23
  "jsx-runtime.js",
21
24
  "jsx-dev-runtime.js"
22
25
  ],