Skip to content

Enable embedding of Dash in React application #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions react/src/lib/components/WebvizEmbedDash/WebvizEmbedDash.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2021- Equinor ASA
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from "react";
import PropTypes from "prop-types";


const propTypes = {
/**
* The ID used to identify this component in Dash callbacks
*/
id: PropTypes.string.isRequired,
eventName: PropTypes.string.isRequired,
value: PropTypes.string,
/**
* Dash-assigned callback that gets fired when the input changes
*/
setProps: PropTypes.func,
};

/**
* WebvizEmbedDash is a component for enabling publish-subscription state sharing
* between a React application where the Dash application is embedded.
*
* It will listen on dispatched events with name eventName, and store the event detail
* in a prop called "value", which can be accessed as normally by other Dash components.
*/
export const WebvizEmbedDash = ({eventName, setProps}) => {

const onEvent = (event) => {
setProps({value: JSON.stringify(event.detail)})
};

React.useEffect(() => {
window.addEventListener(eventName, onEvent);

return () => {
window.removeEventListener(eventName, onEvent)
}
}, []);

return <></>;
};

WebvizEmbedDash.propTypes = propTypes;
8 changes: 8 additions & 0 deletions react/src/lib/components/WebvizEmbedDash/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) 2021- Equinor ASA
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export { WebvizEmbedDash } from "./WebvizEmbedDash";
2 changes: 2 additions & 0 deletions react/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ScrollArea } from "./components/ScrollArea";
import { Dialog } from "./components/Dialog";
import { WebvizContentManager } from "./components/WebvizContentManager";
import { WebvizDialog } from "./components/WebvizDialog";
import { WebvizEmbedDash } from "./components/WebvizEmbedDash";
import { WebvizPluginsWrapper } from "./components/WebvizPluginsWrapper";
import { WebvizPluginWrapper } from "./components/WebvizPluginWrapper";
import { WebvizSettingsDrawer } from "./components/WebvizSettingsDrawer";
Expand All @@ -41,6 +42,7 @@ import "./components/Layout";
export {
WebvizContentManager,
WebvizDialog,
WebvizEmbedDash,
WebvizPluginWrapper,
WebvizPluginsWrapper,
WebvizSettingsDrawer,
Expand Down