Skip to content

Commit 677f4bb

Browse files
timolinstobiaslins
andcommitted
Improve typing & pass children to custom components
Co-Authored-By: Tobias Lins <[email protected]>
1 parent facbe77 commit 677f4bb

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

Diff for: src/block.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
BlockMapType,
77
MapPageUrl,
88
MapImageUrl,
9-
BlockValueTypeKeys,
10-
CustomComponent
9+
CustomComponents,
10+
BlockValueProp
1111
} from "./types";
1212
import Asset from "./components/asset";
1313
import Code from "./components/code";
@@ -64,7 +64,7 @@ interface Block {
6464

6565
fullPage?: boolean;
6666
hideHeader?: boolean;
67-
customComponents?: Record<BlockValueTypeKeys, CustomComponent>;
67+
customComponents?: CustomComponents;
6868
}
6969

7070
export const Block: React.FC<Block> = props => {
@@ -83,10 +83,15 @@ export const Block: React.FC<Block> = props => {
8383

8484
// render a custom component first if passed.
8585
if (customComponents && customComponents[blockValue?.type]) {
86-
const CustomComponent = customComponents[blockValue?.type] as any;
87-
return <CustomComponent blockValue={blockValue} />;
86+
const CustomComponent = customComponents[blockValue?.type]!;
87+
return (
88+
<CustomComponent
89+
blockValue={blockValue as BlockValueProp<typeof blockValue.type>}
90+
>
91+
{children}
92+
</CustomComponent>
93+
);
8894
}
89-
9095
switch (blockValue?.type) {
9196
case "page":
9297
if (level === 0) {

Diff for: src/renderer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
BlockMapType,
44
MapPageUrl,
55
MapImageUrl,
6-
CustomComponent
6+
CustomComponents
77
} from "./types";
88
import { Block } from "./block";
99
import { defaultMapImageUrl, defaultMapPageUrl } from "./utils";
@@ -17,7 +17,7 @@ export interface NotionRendererProps {
1717

1818
currentId?: string;
1919
level?: number;
20-
customComponents?: Record<string, CustomComponent>;
20+
customComponents?: CustomComponents;
2121
}
2222

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

Diff for: src/types.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export type BlockValueType =
294294
| CollectionValueType
295295
| TweetType;
296296

297-
export type BlockValueTypeKeys = Pick<BlockValueType, "type">["type"];
297+
export type BlockValueTypeKeys = BlockValueType["type"];
298298

299299
export interface BlockType {
300300
role: string;
@@ -342,8 +342,17 @@ export interface LoadPageChunkData {
342342
export type MapPageUrl = (pageId: string) => string;
343343
export type MapImageUrl = (image: string) => string;
344344

345-
export interface CustomComponentProps {
346-
blockValue: BlockValueType;
345+
export type BlockValueProp<T> = Extract<BlockValueType, { type: T }>;
346+
347+
export interface CustomComponentProps<T extends BlockValueTypeKeys> {
348+
blockValue: T extends BlockValueType
349+
? Extract<BlockValueType, { type: T }>
350+
: BaseValueType;
347351
}
348352

349-
export type CustomComponent = FC<CustomComponentProps>;
353+
export type CustomComponent<T extends BlockValueTypeKeys> = FC<
354+
CustomComponentProps<T>
355+
>;
356+
export type CustomComponents = {
357+
[K in BlockValueTypeKeys]?: CustomComponent<K>;
358+
};

0 commit comments

Comments
 (0)