react-cosmos-dom 6.0.0-alpha.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/LICENSE +21 -0
- package/dist/DomFixtureLoader.d.ts +12 -0
- package/dist/DomFixtureLoader.js +41 -0
- package/dist/ErrorCatch.d.ts +15 -0
- package/dist/ErrorCatch.js +52 -0
- package/dist/domRendererConnect.d.ts +1 -0
- package/dist/domRendererConnect.js +10 -0
- package/dist/domRendererId.d.ts +1 -0
- package/dist/domRendererId.js +14 -0
- package/dist/getDomContainer.d.ts +1 -0
- package/dist/getDomContainer.js +22 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/mountDomRenderer.d.ts +10 -0
- package/dist/mountDomRenderer.js +13 -0
- package/dist/selectedFixtureId.d.ts +2 -0
- package/dist/selectedFixtureId.js +25 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.js +1 -0
- package/dist/utils/isInsideCosmosPreviewIframe.d.ts +1 -0
- package/dist/utils/isInsideCosmosPreviewIframe.js +8 -0
- package/package.json +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-present Ovidiu Cherecheș
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ReactDecorators, ReactFixtureWrappers } from 'react-cosmos-core';
|
|
2
|
+
type Props = {
|
|
3
|
+
fixtures: ReactFixtureWrappers;
|
|
4
|
+
decorators: ReactDecorators;
|
|
5
|
+
playgroundUrl: string;
|
|
6
|
+
onErrorReset?: () => unknown;
|
|
7
|
+
};
|
|
8
|
+
export declare function DomFixtureLoader({ fixtures, decorators, playgroundUrl, onErrorReset, }: Props): JSX.Element;
|
|
9
|
+
export declare function renderDomMessage({ msg }: {
|
|
10
|
+
msg: string;
|
|
11
|
+
}): JSX.Element;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { useEffect, useMemo } from 'react';
|
|
2
|
+
import { FixtureLoader, } from 'react-cosmos-core';
|
|
3
|
+
import { createDomRendererConnect } from './domRendererConnect.js';
|
|
4
|
+
import { domRendererId } from './domRendererId.js';
|
|
5
|
+
import { ErrorCatch } from './ErrorCatch.js';
|
|
6
|
+
import { selectedFixtureId } from './selectedFixtureId.js';
|
|
7
|
+
export function DomFixtureLoader({ fixtures, decorators, playgroundUrl, onErrorReset, }) {
|
|
8
|
+
const domRendererConnect = useMemo(() => createDomRendererConnect(playgroundUrl), [playgroundUrl]);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
function handleGlobalError() {
|
|
11
|
+
domRendererConnect.postMessage({
|
|
12
|
+
type: 'rendererError',
|
|
13
|
+
payload: { rendererId: domRendererId },
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
// Unhandled errors from async code will not be caught by the error event, but
|
|
17
|
+
// the unhandledrejection event instead.
|
|
18
|
+
window.addEventListener('error', handleGlobalError);
|
|
19
|
+
window.addEventListener('unhandledrejection', handleGlobalError);
|
|
20
|
+
return () => {
|
|
21
|
+
window.removeEventListener('error', handleGlobalError);
|
|
22
|
+
window.removeEventListener('unhandledrejection', handleGlobalError);
|
|
23
|
+
};
|
|
24
|
+
}, [domRendererConnect]);
|
|
25
|
+
return (React.createElement(FixtureLoader, { rendererId: domRendererId, rendererConnect: domRendererConnect, fixtures: fixtures, selectedFixtureId: selectedFixtureId, systemDecorators: [ErrorCatch], userDecorators: decorators, renderMessage: renderDomMessage, onErrorReset: onErrorReset }));
|
|
26
|
+
}
|
|
27
|
+
const containerStyle = {
|
|
28
|
+
position: 'absolute',
|
|
29
|
+
top: 0,
|
|
30
|
+
bottom: 0,
|
|
31
|
+
left: 0,
|
|
32
|
+
right: 0,
|
|
33
|
+
display: 'flex',
|
|
34
|
+
justifyContent: 'center',
|
|
35
|
+
alignItems: 'center',
|
|
36
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, Ubuntu, "Helvetica Neue", Helvetica, sans-serif',
|
|
37
|
+
fontSize: 14,
|
|
38
|
+
};
|
|
39
|
+
export function renderDomMessage({ msg }) {
|
|
40
|
+
return React.createElement("div", { style: containerStyle }, msg);
|
|
41
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React, { Component } from 'react';
|
|
2
|
+
import { ReactDecoratorProps } from 'react-cosmos-core';
|
|
3
|
+
type State = {
|
|
4
|
+
error: null | string;
|
|
5
|
+
};
|
|
6
|
+
export declare class ErrorCatch extends Component<ReactDecoratorProps, State> {
|
|
7
|
+
state: State;
|
|
8
|
+
componentDidCatch(error: Error, info: {
|
|
9
|
+
componentStack: string;
|
|
10
|
+
}): void;
|
|
11
|
+
componentDidUpdate(prevProps: ReactDecoratorProps): void;
|
|
12
|
+
render(): string | number | boolean | React.ReactFragment | JSX.Element | null | undefined;
|
|
13
|
+
renderError(error: string): JSX.Element | null;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { isEqual } from 'lodash-es';
|
|
2
|
+
import React, { Component } from 'react';
|
|
3
|
+
import { areNodesEqual } from 'react-cosmos-core';
|
|
4
|
+
export class ErrorCatch extends Component {
|
|
5
|
+
state = {
|
|
6
|
+
error: null,
|
|
7
|
+
};
|
|
8
|
+
componentDidCatch(error, info) {
|
|
9
|
+
this.setState({
|
|
10
|
+
error: `${error.message}\n${info.componentStack}`,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
componentDidUpdate(prevProps) {
|
|
14
|
+
// A change in fixture (children) or fixture state signifies that the
|
|
15
|
+
// problem that caused the current error might've been solved. If the error
|
|
16
|
+
// persists, it will organically trigger the error state again in the next
|
|
17
|
+
// update
|
|
18
|
+
if (this.state.error &&
|
|
19
|
+
(fixtureChanged(this.props.children, prevProps.children) ||
|
|
20
|
+
fixtureStateChanged(this.props.fixtureState, prevProps.fixtureState))) {
|
|
21
|
+
this.setState({ error: null });
|
|
22
|
+
this.props.onErrorReset();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
render() {
|
|
26
|
+
return this.state.error
|
|
27
|
+
? this.renderError(this.state.error)
|
|
28
|
+
: this.props.children;
|
|
29
|
+
}
|
|
30
|
+
renderError(error) {
|
|
31
|
+
// Don't render error details here in dev mode because react-error-overlay
|
|
32
|
+
// takes care of it in a nicer way. We used to render both for a while but
|
|
33
|
+
// it proved annoying. react-error-overlay has a slight delay and seeing
|
|
34
|
+
// the same error reported twice feels clumsy.
|
|
35
|
+
if (__DEV__) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
// In static exports, however, where react-error-overlay is missing,
|
|
39
|
+
// rendering plain error details is superior to showing a blank screen.
|
|
40
|
+
return (React.createElement(React.Fragment, null,
|
|
41
|
+
React.createElement("p", null,
|
|
42
|
+
React.createElement("strong", null, "Ouch, something wrong!")),
|
|
43
|
+
React.createElement("pre", null, error),
|
|
44
|
+
React.createElement("p", null, "Check console for more info.")));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function fixtureChanged(f1, f2) {
|
|
48
|
+
return !areNodesEqual(f1, f2, true);
|
|
49
|
+
}
|
|
50
|
+
function fixtureStateChanged(fS1, fS2) {
|
|
51
|
+
return !isEqual(fS1, fS2);
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createDomRendererConnect(playgroundUrl: string): import("react-cosmos-core").RendererConnect<import("react-cosmos-core").RendererRequest, import("react-cosmos-core").RendererResponse>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createPostMessageConnect, createWebSocketsConnect, } from 'react-cosmos-core';
|
|
2
|
+
import { isInsideCosmosPreviewIframe } from './utils/isInsideCosmosPreviewIframe.js';
|
|
3
|
+
export function createDomRendererConnect(playgroundUrl) {
|
|
4
|
+
return isInsideCosmosPreviewIframe()
|
|
5
|
+
? createPostMessageConnect()
|
|
6
|
+
: createWebSocketsConnect(getWebSocketsUrl(playgroundUrl));
|
|
7
|
+
}
|
|
8
|
+
function getWebSocketsUrl(playgroundUrl) {
|
|
9
|
+
return playgroundUrl.replace(/^https?:/, 'ws:');
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const domRendererId: string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { uuid } from 'react-cosmos-core';
|
|
2
|
+
export const domRendererId = getRendererId();
|
|
3
|
+
// Renderer ID is remembered to avoid announcing a new renderer when reloading
|
|
4
|
+
// the renderer window. Note that each tab has creates a new session and thus
|
|
5
|
+
// a new rendererId.
|
|
6
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
|
|
7
|
+
function getRendererId() {
|
|
8
|
+
let id = sessionStorage.getItem('cosmosRendererId');
|
|
9
|
+
if (!id) {
|
|
10
|
+
id = uuid();
|
|
11
|
+
sessionStorage.setItem('cosmosRendererId', id);
|
|
12
|
+
}
|
|
13
|
+
return id;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getDomContainer(querySelector?: null | string): Element;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function getDomContainer(querySelector) {
|
|
2
|
+
if (!querySelector) {
|
|
3
|
+
return getFallbackDomContainer();
|
|
4
|
+
}
|
|
5
|
+
const existingContainer = document.querySelector(querySelector);
|
|
6
|
+
if (!existingContainer) {
|
|
7
|
+
console.warn(`Query selector "${querySelector}" doesn't match any existing DOM element. ` +
|
|
8
|
+
`Are you using a custom HTML template? ` +
|
|
9
|
+
`Add an element matching "${querySelector}" to your template or change the containerQuerySelector setting.`);
|
|
10
|
+
return getFallbackDomContainer();
|
|
11
|
+
}
|
|
12
|
+
return existingContainer;
|
|
13
|
+
}
|
|
14
|
+
function getFallbackDomContainer() {
|
|
15
|
+
return document.getElementById('root') || createDomContainer();
|
|
16
|
+
}
|
|
17
|
+
function createDomContainer() {
|
|
18
|
+
const container = document.createElement('div');
|
|
19
|
+
container.setAttribute('id', 'root');
|
|
20
|
+
document.body.appendChild(container);
|
|
21
|
+
return container;
|
|
22
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactDecorators, ReactFixtureWrappers } from 'react-cosmos-core';
|
|
2
|
+
import { DomRendererConfig } from './types.js';
|
|
3
|
+
type Args = {
|
|
4
|
+
rendererConfig: DomRendererConfig;
|
|
5
|
+
fixtures: ReactFixtureWrappers;
|
|
6
|
+
decorators: ReactDecorators;
|
|
7
|
+
onErrorReset?: () => unknown;
|
|
8
|
+
};
|
|
9
|
+
export declare function mountDomRenderer({ rendererConfig, fixtures, decorators, onErrorReset, }: Args): void;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import { DomFixtureLoader } from './DomFixtureLoader.js';
|
|
4
|
+
import { getDomContainer } from './getDomContainer.js';
|
|
5
|
+
let cachedRoot = null;
|
|
6
|
+
export function mountDomRenderer({ rendererConfig, fixtures, decorators, onErrorReset, }) {
|
|
7
|
+
const domContainer = getDomContainer(rendererConfig.containerQuerySelector);
|
|
8
|
+
if (!cachedRoot || cachedRoot.domContainer !== domContainer) {
|
|
9
|
+
const reactRoot = createRoot(domContainer);
|
|
10
|
+
cachedRoot = { domContainer, reactRoot };
|
|
11
|
+
}
|
|
12
|
+
cachedRoot.reactRoot.render(React.createElement(DomFixtureLoader, { fixtures: fixtures, decorators: decorators, playgroundUrl: rendererConfig.playgroundUrl, onErrorReset: onErrorReset }));
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { parseRendererUrlQuery } from 'react-cosmos-core';
|
|
2
|
+
import { isInsideCosmosPreviewIframe } from './utils/isInsideCosmosPreviewIframe.js';
|
|
3
|
+
export const selectedFixtureId = getSelectedFixtureId();
|
|
4
|
+
// The selected fixture ID is stored in session because components might change
|
|
5
|
+
// the URL as a side effect. This means that after the renderer stores the
|
|
6
|
+
// fixture ID initially, the renderer window can be refreshed and the same
|
|
7
|
+
// fixture will be selected even if the URL no longer contains the fixture ID.
|
|
8
|
+
function getSelectedFixtureId() {
|
|
9
|
+
const urlParams = parseRendererUrlQuery(location.search);
|
|
10
|
+
if (urlParams._fixtureId) {
|
|
11
|
+
setFixtureIdToSession(urlParams._fixtureId);
|
|
12
|
+
return urlParams._fixtureId;
|
|
13
|
+
}
|
|
14
|
+
return getFixtureIdFromSession();
|
|
15
|
+
}
|
|
16
|
+
function setFixtureIdToSession(fixtureId) {
|
|
17
|
+
sessionStorage.setItem('cosmosFixtureId', JSON.stringify(fixtureId));
|
|
18
|
+
}
|
|
19
|
+
function getFixtureIdFromSession() {
|
|
20
|
+
if (isInsideCosmosPreviewIframe()) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const stringifiedFixtureId = sessionStorage.getItem('cosmosFixtureId');
|
|
24
|
+
return stringifiedFixtureId ? JSON.parse(stringifiedFixtureId) : null;
|
|
25
|
+
}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isInsideCosmosPreviewIframe(): boolean;
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-cosmos-dom",
|
|
3
|
+
"version": "6.0.0-alpha.0",
|
|
4
|
+
"description": "React Cosmos DOM",
|
|
5
|
+
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-dom",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"lodash-es": "^4.17.21",
|
|
11
|
+
"react-cosmos-core": "^6.0.0-alpha.0"
|
|
12
|
+
},
|
|
13
|
+
"gitHead": "b021231584639f3028fe08deacd418f0ec4cc002"
|
|
14
|
+
}
|