@rxdrag/website-lib 0.0.2

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 ADDED
@@ -0,0 +1,2 @@
1
+ @rxdrag/website-lib
2
+ 含 astro 组件,不能在 React 中使用
@@ -0,0 +1,170 @@
1
+ import { createElement, startTransition } from 'react';
2
+ import require$$0 from 'react-dom';
3
+
4
+ var hydrateRoot;
5
+ var createRoot;
6
+
7
+ var m = require$$0;
8
+ if (process.env.NODE_ENV === 'production') {
9
+ createRoot = m.createRoot;
10
+ hydrateRoot = m.hydrateRoot;
11
+ } else {
12
+ var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
13
+ createRoot = function(c, o) {
14
+ i.usingClientEntryPoint = true;
15
+ try {
16
+ return m.createRoot(c, o);
17
+ } finally {
18
+ i.usingClientEntryPoint = false;
19
+ }
20
+ };
21
+ hydrateRoot = function(c, h, o) {
22
+ i.usingClientEntryPoint = true;
23
+ try {
24
+ return m.hydrateRoot(c, h, o);
25
+ } finally {
26
+ i.usingClientEntryPoint = false;
27
+ }
28
+ };
29
+ }
30
+
31
+ /**
32
+ * Astro passes `children` as a string of HTML, so we need
33
+ * a wrapper `div` to render that content as VNodes.
34
+ *
35
+ * As a bonus, we can signal to React that this subtree is
36
+ * entirely static and will never change via `shouldComponentUpdate`.
37
+ */
38
+ const StaticHtml = ({ value, name, hydrate = true }) => {
39
+ if (!value) return null;
40
+ const tagName = hydrate ? 'astro-slot' : 'astro-static-slot';
41
+ return createElement(tagName, {
42
+ name,
43
+ suppressHydrationWarning: true,
44
+ dangerouslySetInnerHTML: { __html: value },
45
+ });
46
+ };
47
+
48
+ /**
49
+ * This tells React to opt-out of re-rendering this subtree,
50
+ * In addition to being a performance optimization,
51
+ * this also allows other frameworks to attach to `children`.
52
+ *
53
+ * See https://preactjs.com/guide/v8/external-dom-mutations
54
+ */
55
+ StaticHtml.shouldComponentUpdate = () => false;
56
+
57
+ function isAlreadyHydrated(element) {
58
+ for (const key in element) {
59
+ if (key.startsWith('__reactContainer')) {
60
+ return key;
61
+ }
62
+ }
63
+ }
64
+
65
+ function createReactElementFromDOMElement(element) {
66
+ let attrs = {};
67
+ for (const attr of element.attributes) {
68
+ attrs[attr.name] = attr.value;
69
+ }
70
+ // If the element has no children, we can create a simple React element
71
+ if (element.firstChild === null) {
72
+ return createElement(element.localName, attrs);
73
+ }
74
+
75
+ return createElement(
76
+ element.localName,
77
+ attrs,
78
+ Array.from(element.childNodes)
79
+ .map((c) => {
80
+ if (c.nodeType === Node.TEXT_NODE) {
81
+ return c.data;
82
+ } else if (c.nodeType === Node.ELEMENT_NODE) {
83
+ return createReactElementFromDOMElement(c);
84
+ } else {
85
+ return undefined;
86
+ }
87
+ })
88
+ .filter((a) => !!a),
89
+ );
90
+ }
91
+
92
+ function getChildren(childString, experimentalReactChildren) {
93
+ if (experimentalReactChildren && childString) {
94
+ let children = [];
95
+ let template = document.createElement('template');
96
+ template.innerHTML = childString;
97
+ for (let child of template.content.children) {
98
+ children.push(createReactElementFromDOMElement(child));
99
+ }
100
+ return children;
101
+ } else if (childString) {
102
+ return createElement(StaticHtml, { value: childString });
103
+ } else {
104
+ return undefined;
105
+ }
106
+ }
107
+
108
+ // Keep a map of roots so we can reuse them on re-renders
109
+ let rootMap = new WeakMap();
110
+ const getOrCreateRoot = (element, creator) => {
111
+ let root = rootMap.get(element);
112
+ if (!root) {
113
+ root = creator();
114
+ rootMap.set(element, root);
115
+ }
116
+ return root;
117
+ };
118
+
119
+ const client = (element) =>
120
+ (Component, props, { default: children, ...slotted }, { client }) => {
121
+ if (!element.hasAttribute('ssr')) return;
122
+
123
+ const actionKey = element.getAttribute('data-action-key');
124
+ const actionName = element.getAttribute('data-action-name');
125
+ const stringifiedActionResult = element.getAttribute('data-action-result');
126
+
127
+ const formState =
128
+ actionKey && actionName && stringifiedActionResult
129
+ ? [JSON.parse(stringifiedActionResult), actionKey, actionName]
130
+ : undefined;
131
+
132
+ const renderOptions = {
133
+ identifierPrefix: element.getAttribute('prefix'),
134
+ formState,
135
+ };
136
+ for (const [key, value] of Object.entries(slotted)) {
137
+ props[key] = createElement(StaticHtml, { value, name: key });
138
+ }
139
+
140
+ const componentEl = createElement(
141
+ Component,
142
+ props,
143
+ getChildren(children, element.hasAttribute('data-react-children')),
144
+ );
145
+ const rootKey = isAlreadyHydrated(element);
146
+ // HACK: delete internal react marker for nested components to suppress aggressive warnings
147
+ if (rootKey) {
148
+ delete element[rootKey];
149
+ }
150
+ if (client === 'only') {
151
+ return startTransition(() => {
152
+ const root = getOrCreateRoot(element, () => {
153
+ const r = createRoot(element);
154
+ element.addEventListener('astro:unmount', () => r.unmount(), { once: true });
155
+ return r;
156
+ });
157
+ root.render(componentEl);
158
+ });
159
+ }
160
+ startTransition(() => {
161
+ const root = getOrCreateRoot(element, () => {
162
+ const r = hydrateRoot(element, componentEl, renderOptions);
163
+ element.addEventListener('astro:unmount', () => r.unmount(), { once: true });
164
+ return r;
165
+ });
166
+ root.render(componentEl);
167
+ });
168
+ };
169
+
170
+ export { client as default };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@rxdrag/website-lib",
3
+ "version": "0.0.2",
4
+ "module": "dist/index.mjs",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "devDependencies": {
12
+ "@astrojs/react": "^3.0.0",
13
+ "@astrojs/vercel": "^8.1.2",
14
+ "@babel/types": "^7.26.9",
15
+ "@types/react": "^18.2.22",
16
+ "@types/react-dom": "^18.2.7",
17
+ "astro": "^4.0.0",
18
+ "eslint": "^7.32.0",
19
+ "typescript": "^5",
20
+ "@rxdrag/entify-hooks": "0.2.38",
21
+ "@rxdrag/eslint-config-custom": "0.2.10",
22
+ "@rxdrag/slate-preview": "1.2.53",
23
+ "@rxdrag/tsconfig": "0.2.0"
24
+ },
25
+ "dependencies": {
26
+ "add": "^2.0.6",
27
+ "clsx": "^2.1.0",
28
+ "motion": "^12.4.7",
29
+ "react": "^18.2.0",
30
+ "react-dom": "^18.2.0",
31
+ "@rxdrag/rxcms-models": "0.3.44",
32
+ "@rxdrag/website-lib-core": "0.0.2"
33
+ },
34
+ "scripts": {
35
+ "lint": "eslint . --ext .ts,tsx",
36
+ "clean": "rimraf -rf .turbo && rimraf -rf node_modules && rimraf -rf dist",
37
+ "build": "astro build"
38
+ },
39
+ "typings": "dist/index.d.ts"
40
+ }