Skip to content

Commit

Permalink
adds ability to override components from consuming app
Browse files Browse the repository at this point in the history
  • Loading branch information
cball committed Sep 1, 2020
1 parent 4e9840d commit facbe77
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
ContentValueType,
BlockMapType,
MapPageUrl,
MapImageUrl
MapImageUrl,
BlockValueTypeKeys,
CustomComponent
} from "./types";
import Asset from "./components/asset";
import Code from "./components/code";
Expand Down Expand Up @@ -62,6 +64,7 @@ interface Block {

fullPage?: boolean;
hideHeader?: boolean;
customComponents?: Record<BlockValueTypeKeys, CustomComponent>;
}

export const Block: React.FC<Block> = props => {
Expand All @@ -73,10 +76,17 @@ export const Block: React.FC<Block> = props => {
hideHeader,
blockMap,
mapPageUrl,
mapImageUrl
mapImageUrl,
customComponents
} = props;
const blockValue = block?.value;

// render a custom component first if passed.
if (customComponents && customComponents[blockValue?.type]) {
const CustomComponent = customComponents[blockValue?.type] as any;
return <CustomComponent blockValue={blockValue} />;
}

switch (blockValue?.type) {
case "page":
if (level === 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/components/asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const Asset: React.FC<{
>
<iframe
className="notion-image-inset"
src={type === "figma" ? value.properties.source[0][0] : display_source}
src={
type === "figma" ? value.properties.source[0][0] : display_source
}
/>
</div>
);
Expand Down
8 changes: 7 additions & 1 deletion src/renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React from "react";
import { BlockMapType, MapPageUrl, MapImageUrl } from "./types";
import {
BlockMapType,
MapPageUrl,
MapImageUrl,
CustomComponent
} from "./types";
import { Block } from "./block";
import { defaultMapImageUrl, defaultMapPageUrl } from "./utils";

Expand All @@ -12,6 +17,7 @@ export interface NotionRendererProps {

currentId?: string;
level?: number;
customComponents?: Record<string, CustomComponent>;
}

export const NotionRenderer: React.FC<NotionRendererProps> = ({
Expand Down
20 changes: 19 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*
*/

import { FC } from "react";

/**
* Base properties that all blocks share.
*/
Expand Down Expand Up @@ -256,6 +258,13 @@ interface TableCollectionType extends BaseValueType {
};
}

export interface TweetType extends BaseValueType {
type: "tweet";
properties: {
source: [string[]];
};
}

export type CollectionViewType = TableGalleryType | TableCollectionType;

/**
Expand All @@ -282,7 +291,10 @@ export type BlockValueType =
| CalloutValueType
| BookmarkValueType
| ToggleValueType
| CollectionValueType;
| CollectionValueType
| TweetType;

export type BlockValueTypeKeys = Pick<BlockValueType, "type">["type"];

export interface BlockType {
role: string;
Expand Down Expand Up @@ -329,3 +341,9 @@ export interface LoadPageChunkData {

export type MapPageUrl = (pageId: string) => string;
export type MapImageUrl = (image: string) => string;

export interface CustomComponentProps {
blockValue: BlockValueType;
}

export type CustomComponent = FC<CustomComponentProps>;

0 comments on commit facbe77

Please sign in to comment.