nesquick 0.0.19 → 0.0.21

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.
@@ -19,6 +19,7 @@ class NesquickComponent {
19
19
  this._render = _render;
20
20
  this.props = props;
21
21
  this._subscriptions = new State_1.Subscriptions();
22
+ this._styleSubscriptions = null;
22
23
  this._xmlns = null;
23
24
  this._children = [];
24
25
  this.props = props;
@@ -98,7 +99,10 @@ class NesquickComponent {
98
99
  _renderPropsNs(attributes, element, props) {
99
100
  for (const k in props) {
100
101
  if (k !== "children" && k !== "xmlns" && k !== "ref") {
101
- if (typeof props[k] === "function") {
102
+ if (k === "style") {
103
+ this._renderStyle(element, props[k]);
104
+ }
105
+ else if (typeof props[k] === "function") {
102
106
  if (k.startsWith("on")) {
103
107
  element[k.toLowerCase()] = props[k];
104
108
  }
@@ -131,8 +135,11 @@ class NesquickComponent {
131
135
  ;
132
136
  _renderProps(element, props) {
133
137
  for (const k in props) {
134
- if (k !== "children" && k !== "xmlns") {
135
- if (typeof props[k] === "function") {
138
+ if (k !== "children" && k !== "xmlns" && k !== "ref") {
139
+ if (k === "style") {
140
+ this._renderStyle(element, props[k]);
141
+ }
142
+ else if (typeof props[k] === "function") {
136
143
  if (k.startsWith("on")) {
137
144
  element[k.toLowerCase()] = props[k];
138
145
  }
@@ -148,6 +155,65 @@ class NesquickComponent {
148
155
  }
149
156
  }
150
157
  }
158
+ _renderStyles(element, styles) {
159
+ for (const k in styles) {
160
+ if (typeof styles[k] === "function") {
161
+ (0, State_1.useRender)(styles[k], v => {
162
+ element.style[k] = String(v);
163
+ });
164
+ }
165
+ else {
166
+ element.style[k] = String(styles[k]);
167
+ }
168
+ }
169
+ }
170
+ _renderStyle(element, style) {
171
+ switch (typeof style) {
172
+ case "function": {
173
+ (0, State_1.useRender)(style, (style, isState) => {
174
+ if (this._styleSubscriptions != null) {
175
+ this._styleSubscriptions.dispose();
176
+ this._styleSubscriptions = null;
177
+ }
178
+ switch (typeof style) {
179
+ case "object": {
180
+ if (style) {
181
+ if (isState) {
182
+ element.removeAttribute("style");
183
+ this._styleSubscriptions = new State_1.Subscriptions();
184
+ State_1.subscriptions.set(this._styleSubscriptions);
185
+ this._renderStyles(element, style);
186
+ State_1.subscriptions.reset();
187
+ }
188
+ else {
189
+ this._renderStyles(element, style);
190
+ }
191
+ }
192
+ break;
193
+ }
194
+ default: {
195
+ element.setAttribute("style", String(style));
196
+ break;
197
+ }
198
+ }
199
+ });
200
+ break;
201
+ }
202
+ case "object": {
203
+ if (style) {
204
+ this._renderStyles(element, style);
205
+ }
206
+ else {
207
+ element.removeAttribute("style");
208
+ }
209
+ break;
210
+ }
211
+ default: {
212
+ element.setAttribute("style", String(style));
213
+ break;
214
+ }
215
+ }
216
+ }
151
217
  _renderChildren(document, parent, children) {
152
218
  if (children != null) {
153
219
  if (!Array.isArray(children)) {
@@ -279,6 +345,7 @@ class NesquickComponent {
279
345
  }
280
346
  dispose() {
281
347
  this._subscriptions.dispose();
348
+ this._styleSubscriptions?.dispose();
282
349
  for (const child of this._children) {
283
350
  if (child.component) {
284
351
  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;
@@ -32,9 +32,9 @@ export declare namespace JSX {
32
32
  };
33
33
  export interface Props<T extends EventTarget = HTMLElement> extends JSXHTMLEvent<T>, JSXSVGEvent<T> {
34
34
  [k: string]: any;
35
- style?: StyleProps;
36
- xmlns?: string;
37
- ref?: (el: T) => void;
35
+ style?: StyleProps | string;
36
+ xmlns?: string | null;
37
+ ref?: ((el: T) => void) | null;
38
38
  }
39
39
  export type StyleProps = {
40
40
  [K in keyof CSSStyleDeclaration]?: CSSStyleDeclaration[K] extends Function ? never : CSSStyleDeclaration[K] | (() => CSSStyleDeclaration[K]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types",
6
6
  "main": "./lib",