Skip to content

Commit facbe77

Browse files
committed
adds ability to override components from consuming app
1 parent 4e9840d commit facbe77

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

src/block.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
ContentValueType,
66
BlockMapType,
77
MapPageUrl,
8-
MapImageUrl
8+
MapImageUrl,
9+
BlockValueTypeKeys,
10+
CustomComponent
911
} from "./types";
1012
import Asset from "./components/asset";
1113
import Code from "./components/code";
@@ -62,6 +64,7 @@ interface Block {
6264

6365
fullPage?: boolean;
6466
hideHeader?: boolean;
67+
customComponents?: Record<BlockValueTypeKeys, CustomComponent>;
6568
}
6669

6770
export const Block: React.FC<Block> = props => {
@@ -73,10 +76,17 @@ export const Block: React.FC<Block> = props => {
7376
hideHeader,
7477
blockMap,
7578
mapPageUrl,
76-
mapImageUrl
79+
mapImageUrl,
80+
customComponents
7781
} = props;
7882
const blockValue = block?.value;
7983

84+
// render a custom component first if passed.
85+
if (customComponents && customComponents[blockValue?.type]) {
86+
const CustomComponent = customComponents[blockValue?.type] as any;
87+
return <CustomComponent blockValue={blockValue} />;
88+
}
89+
8090
switch (blockValue?.type) {
8191
case "page":
8292
if (level === 0) {

src/components/asset.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ const Asset: React.FC<{
3434
>
3535
<iframe
3636
className="notion-image-inset"
37-
src={type === "figma" ? value.properties.source[0][0] : display_source}
37+
src={
38+
type === "figma" ? value.properties.source[0][0] : display_source
39+
}
3840
/>
3941
</div>
4042
);

src/renderer.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import React from "react";
2-
import { BlockMapType, MapPageUrl, MapImageUrl } from "./types";
2+
import {
3+
BlockMapType,
4+
MapPageUrl,
5+
MapImageUrl,
6+
CustomComponent
7+
} from "./types";
38
import { Block } from "./block";
49
import { defaultMapImageUrl, defaultMapPageUrl } from "./utils";
510

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

1318
currentId?: string;
1419
level?: number;
20+
customComponents?: Record<string, CustomComponent>;
1521
}
1622

1723
export const NotionRenderer: React.FC<NotionRendererProps> = ({

src/types.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*
99
*/
1010

11+
import { FC } from "react";
12+
1113
/**
1214
* Base properties that all blocks share.
1315
*/
@@ -256,6 +258,13 @@ interface TableCollectionType extends BaseValueType {
256258
};
257259
}
258260

261+
export interface TweetType extends BaseValueType {
262+
type: "tweet";
263+
properties: {
264+
source: [string[]];
265+
};
266+
}
267+
259268
export type CollectionViewType = TableGalleryType | TableCollectionType;
260269

261270
/**
@@ -282,7 +291,10 @@ export type BlockValueType =
282291
| CalloutValueType
283292
| BookmarkValueType
284293
| ToggleValueType
285-
| CollectionValueType;
294+
| CollectionValueType
295+
| TweetType;
296+
297+
export type BlockValueTypeKeys = Pick<BlockValueType, "type">["type"];
286298

287299
export interface BlockType {
288300
role: string;
@@ -329,3 +341,9 @@ export interface LoadPageChunkData {
329341

330342
export type MapPageUrl = (pageId: string) => string;
331343
export type MapImageUrl = (image: string) => string;
344+
345+
export interface CustomComponentProps {
346+
blockValue: BlockValueType;
347+
}
348+
349+
export type CustomComponent = FC<CustomComponentProps>;

0 commit comments

Comments
 (0)