mono-jsx-dom 0.1.8 → 0.1.10

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/README.md CHANGED
@@ -44,22 +44,25 @@ async function App(this: FC<{ word: string }>) {
44
44
  document.body.mount(<App />);
45
45
  ```
46
46
 
47
- You can also define [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) with the `defineComponent` function:
47
+ You can also define a component as a [custom element](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) with the `register` function:
48
48
 
49
49
  ```tsx
50
- import { defineComponent } from "mono-jsx-dom";
50
+ // app.tsx
51
+
52
+ import { register } from "mono-jsx-dom";
51
53
 
52
54
  function App(this: FC<{ word: string }>) {
53
55
  return <div>Hello, {this.word}!</div>;
54
56
  }
55
57
 
56
- defineComponent("my-app", App);
58
+ register("my-app", App, { mode: "open", style: "div { color: black; }" });
57
59
  ```
58
60
 
59
- Then you can use the `<my-app>` element in your HTML.
61
+ Then you can use the `<my-app>` element in your HTML:
60
62
 
61
63
  ```html
62
64
  <my-app word="world"></my-app>
65
+ <script type="module" src="app.tsx"></script>
63
66
  ```
64
67
 
65
68
  >[!TIP]
package/index.mjs CHANGED
@@ -1,14 +1,29 @@
1
1
  // index.ts
2
- import { atom, jsx, store } from "./jsx-runtime.mjs";
3
- function defineComponent(name, Component) {
2
+ import { atom, jsx, render, store } from "./jsx-runtime.mjs";
3
+ function register(name, Component, shadow) {
4
4
  customElements.define(
5
5
  name,
6
6
  class extends HTMLElement {
7
7
  #ac;
8
8
  connectedCallback() {
9
- this.#ac ??= new AbortController();
10
9
  const props = Object.fromEntries(this.getAttributeNames().map((name2) => [name2, this.getAttribute(name2)]));
11
- this.mount(jsx(Component, props), this.#ac.signal);
10
+ this.#ac ??= new AbortController();
11
+ if (shadow) {
12
+ const { mode = "open", style } = typeof shadow === "boolean" ? {} : shadow;
13
+ const shadowRoot = this.attachShadow({ mode });
14
+ if (style) {
15
+ let styleSheet = new CSSStyleSheet();
16
+ if (typeof style === "string") {
17
+ styleSheet.replaceSync(style);
18
+ } else {
19
+ styleSheet = style;
20
+ }
21
+ shadowRoot.adoptedStyleSheets = [styleSheet];
22
+ }
23
+ render(null, jsx(Component, props), shadowRoot, this.#ac.signal);
24
+ } else {
25
+ this.mount(jsx(Component, props), this.#ac.signal);
26
+ }
12
27
  }
13
28
  disconnectedCallback() {
14
29
  this.#ac?.abort();
@@ -18,6 +33,6 @@ function defineComponent(name, Component) {
18
33
  }
19
34
  export {
20
35
  atom,
21
- defineComponent,
36
+ register,
22
37
  store
23
38
  };
package/jsx-runtime.mjs CHANGED
@@ -417,7 +417,7 @@ var render = (scope, child, root, abortSignal) => {
417
417
  }
418
418
  // `<slot>` element
419
419
  case "slot": {
420
- const slots = scope[$slots];
420
+ const slots = scope?.[$slots];
421
421
  if (slots) {
422
422
  renderChildren(scope, slots, root, abortSignal);
423
423
  }
@@ -786,5 +786,6 @@ export {
786
786
  jsx,
787
787
  jsx as jsxDEV,
788
788
  jsx as jsxs,
789
+ render,
789
790
  store
790
791
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mono-jsx-dom",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "A JSX runtime for browsers.",
5
5
  "keywords": [
6
6
  "jsx",
package/types/index.d.ts CHANGED
@@ -13,4 +13,8 @@ export const store: <T extends Record<string, unknown>>(initValue: T) => T;
13
13
  /**
14
14
  * Defines a custom element with the given name and component.
15
15
  */
16
- export const defineComponent: <P = {}>(name: string, Component: ComponentType<P>) => void;
16
+ export const register: (
17
+ name: string,
18
+ Component: ComponentType<any>,
19
+ shadow?: boolean | { mode?: "open" | "closed"; style?: string },
20
+ ) => void;