mono-jsx-dom 0.1.9 → 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 +7 -4
- package/index.mjs +15 -5
- package/jsx-runtime.mjs +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +5 -1
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
|
|
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
|
-
|
|
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
|
-
|
|
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,15 +1,25 @@
|
|
|
1
1
|
// index.ts
|
|
2
2
|
import { atom, jsx, render, store } from "./jsx-runtime.mjs";
|
|
3
|
-
function
|
|
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
|
-
|
|
12
|
-
|
|
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
|
+
}
|
|
13
23
|
render(null, jsx(Component, props), shadowRoot, this.#ac.signal);
|
|
14
24
|
} else {
|
|
15
25
|
this.mount(jsx(Component, props), this.#ac.signal);
|
|
@@ -23,6 +33,6 @@ function defineComponent(name, Component, attachShadow) {
|
|
|
23
33
|
}
|
|
24
34
|
export {
|
|
25
35
|
atom,
|
|
26
|
-
|
|
36
|
+
register,
|
|
27
37
|
store
|
|
28
38
|
};
|
package/jsx-runtime.mjs
CHANGED
package/package.json
CHANGED
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
|
|
16
|
+
export const register: (
|
|
17
|
+
name: string,
|
|
18
|
+
Component: ComponentType<any>,
|
|
19
|
+
shadow?: boolean | { mode?: "open" | "closed"; style?: string },
|
|
20
|
+
) => void;
|