sliftutils 0.42.0 → 0.43.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/.cursorrules CHANGED
@@ -79,6 +79,12 @@ Coding Styles
79
79
 
80
80
  Do not try catch for no reason. If you can't actually handle the exception, just let it throw.
81
81
 
82
+ For input events, always use event.currentTarget.
83
+
84
+ Use ref={elem => } callbacks. NEVER use .createRef.
85
+
86
+ NEVER render images with a fixed width+height. This will cause them to be stretched or cut off. This is terrible. Only set the width or height.
87
+
82
88
 
83
89
 
84
90
  General Styling
package/index.d.ts CHANGED
@@ -452,6 +452,7 @@ declare module "sliftutils/render-utils/modal" {
452
452
  }): {
453
453
  close: () => void;
454
454
  };
455
+ export declare function closeAllModals(): void;
455
456
 
456
457
  }
457
458
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sliftutils",
3
- "version": "0.42.0",
3
+ "version": "0.43.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -4,3 +4,4 @@ export declare function showModal(config: {
4
4
  }): {
5
5
  close: () => void;
6
6
  };
7
+ export declare function closeAllModals(): void;
@@ -1,18 +1,51 @@
1
1
  import preact from "preact";
2
+ import { observable } from "mobx";
3
+ import * as mobx from "mobx";
4
+ import { observer } from "./observer";
5
+ import { lazy } from "socket-function/src/caching";
6
+
7
+ // Global observable map to store all active modals
8
+ const activeModals = new mobx.ObservableMap<string, preact.ComponentChildren>();
9
+
10
+ @observer
11
+ class ModalRoot extends preact.Component {
12
+ render() {
13
+ const modals: Array<[string, preact.ComponentChildren]> = Array.from(activeModals.entries());
14
+ return <>
15
+ {modals.map(([id, contents]) => (
16
+ <div key={id}>
17
+ {contents}
18
+ </div>
19
+ ))}
20
+ </>;
21
+ }
22
+ }
23
+
24
+ const ensureRootMounted = lazy(() => {
25
+ const root = document.createElement("div");
26
+ document.body.appendChild(root);
27
+ preact.render(<ModalRoot />, root);
28
+ });
29
+
30
+ let modalIdCounter = 0;
2
31
 
3
32
  export function showModal(config: {
4
33
  contents: preact.ComponentChildren;
5
34
  }): {
6
35
  close: () => void;
7
36
  } {
8
- let root = document.createElement("div");
9
- document.body.appendChild(root);
10
- preact.render(config.contents, root);
37
+ ensureRootMounted();
38
+
39
+ const id = `modal-${modalIdCounter++}`;
40
+ activeModals.set(id, config.contents);
11
41
 
12
42
  return {
13
43
  close() {
14
- preact.render(undefined, root);
15
- document.body.removeChild(root);
44
+ activeModals.delete(id);
16
45
  }
17
46
  };
47
+ }
48
+
49
+ export function closeAllModals() {
50
+ activeModals.clear();
18
51
  }
package/web/Page.tsx CHANGED
@@ -31,6 +31,12 @@ export class Page extends preact.Component {
31
31
  el.click();
32
32
  }
33
33
  };
34
+ componentDidMount() {
35
+ document.addEventListener("keydown", this.onKeyDown);
36
+ }
37
+ componentWillUnmount() {
38
+ document.removeEventListener("keydown", this.onKeyDown);
39
+ }
34
40
  render() {
35
41
  let pages = [
36
42
  {