Introduce PluginRenderer to render plugins

Summary: PluginContainer will now wrap Sandy plugins in PluginRenderer. PluginRenderer will also be used by plugin unit tests in the future

Reviewed By: jknoxville

Differential Revision: D22159359

fbshipit-source-id: 69f9c8f4bec9392022c1d7a14957f5aca0339d97
This commit is contained in:
Michel Weststrate
2020-07-01 08:58:40 -07:00
committed by Facebook GitHub Bot
parent ba01fa5bc9
commit f2c39aed55
7 changed files with 216 additions and 66 deletions

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {createContext} from 'react';
export type SandyPluginContext = {
deactivate(): void;
};
// TODO: to be filled in later with testing and such
const stubPluginContext: SandyPluginContext = {
deactivate() {},
};
export const SandyPluginContext = createContext<SandyPluginContext>(
stubPluginContext,
);

View File

@@ -0,0 +1,38 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import React, {memo, useEffect, createElement} from 'react';
import {SandyPluginContext} from './PluginContext';
import {SandyPluginInstance} from './Plugin';
type Props = {
plugin: SandyPluginInstance;
};
/**
* Component to render a Sandy plugin container
*/
export const SandyPluginRenderer = memo(
({plugin}: Props) => {
useEffect(() => {
plugin.deactivate();
}, [plugin]);
return (
<SandyPluginContext.Provider value={plugin}>
{createElement(plugin.definition.module.Component)}
</SandyPluginContext.Provider>
);
},
() => {
// One of the goals of the ModernPluginContainer is that we want to prevent it from rendering
// for any outside change. Whatever happens outside of us, we don't care. If it is relevant for use, we take care about it from the insde
return true;
},
);