Skip to content

Commit

Permalink
feat: 🎸 add renderWrap() prop
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Aug 17, 2019
1 parent 9cfbbd0 commit f984b4f
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/ReactEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ const defaultBlocks: Blocks = {

export type ReactEmbedRouterResult = undefined | [undefined | React.ComponentType<BlockProps>, EmbedBlockId];
export type ReactEmbedRouter = (blocks: Blocks, url: ParsedUrl) => ReactEmbedRouterResult;
export type ReactEmbedRenderer = (block: React.ComponentType<BlockProps>, id: EmbedBlockId, props: ReactEmbedProps, state: ReactEmbedState) => React.ReactElement<any> | null;
export type ReactEmbedVoidRenderer = (props: ReactEmbedProps, state: ReactEmbedState, error?: Error) => React.ReactElement<any> | null;
export type ReactEmbedRenderer = (
block: React.ComponentType<BlockProps>,
id: EmbedBlockId,
props: ReactEmbedProps,
state: ReactEmbedState,
) => React.ReactElement<any> | null;
export type ReactEmbedVoidRenderer = (
props: ReactEmbedProps,
state: ReactEmbedState,
error?: Error,
) => React.ReactElement<any> | null;

const renderNull = () => null;
const renderWrap = (children) => children;

export interface ReactEmbedProps {
url: string;
Expand All @@ -55,6 +65,7 @@ export interface ReactEmbedProps {
* If called on on error, error will available in `error` argument.
*/
renderVoid?: ReactEmbedVoidRenderer;
renderWrap?: (children: React.ReactElement<any> | null) => React.ReactElement<any> | null;
}

export interface ReactEmbedState {
Expand All @@ -68,9 +79,10 @@ export class ReactEmbed extends React.PureComponent<ReactEmbedProps, ReactEmbedS
router: defaultRouter,
render: defaultRender,
renderVoid: renderNull,
renderWrap,
};

static getDerivedStateFromProps (props) {
static getDerivedStateFromProps(props) {
if (!IS_BROWSER) return null;

if (typeof props.url === 'string') {
Expand All @@ -94,22 +106,22 @@ export class ReactEmbed extends React.PureComponent<ReactEmbedProps, ReactEmbedS
}
}

static getDerivedStateFromError (error) {
static getDerivedStateFromError(error) {
return {error};
}

state: ReactEmbedState = {};

url: undefined | ParsedUrl;

render () {
render() {
if (!IS_BROWSER) return null;

const {props, state} = this;

if (state.error) return props.renderVoid!(props, state, state.error);

let result: ReactEmbedRouterResult
let result: ReactEmbedRouterResult;
try {
result = props.router!(props.blocks!, state.url!);
} catch (error) {
Expand All @@ -122,9 +134,7 @@ export class ReactEmbed extends React.PureComponent<ReactEmbedProps, ReactEmbedS
if (!result || !result[0]) return props.renderVoid!(props, state);

const [Block, id] = result as any;
return (
props.render!(Block, id, props, state)
);
return props.renderWrap!(props.render!(Block, id, props, state));
}
}

Expand Down

0 comments on commit f984b4f

Please sign in to comment.