Skip to content

Commit 8c07fc0

Browse files
committed
Export renderer
1 parent 9d270c1 commit 8c07fc0

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

packages/editor/api-report.api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { HistoryEntry } from '@tldraw/store';
2121
import { IndexKey } from '@tldraw/utils';
2222
import { JsonObject } from '@tldraw/utils';
2323
import { JSX } from 'react/jsx-runtime';
24+
import { JSXElementConstructor } from 'react';
2425
import { LegacyMigrations } from '@tldraw/store';
2526
import { MigrationSequence } from '@tldraw/store';
2627
import { NamedExoticComponent } from 'react';
@@ -657,6 +658,9 @@ export const DefaultShapeIndicator: NamedExoticComponent<TLShapeIndicatorProps>;
657658
// @public (undocumented)
658659
export const DefaultShapeIndicators: NamedExoticComponent<TLShapeIndicatorsProps>;
659660

661+
// @public (undocumented)
662+
export function DefaultShapeRenderer({ renderShape }: TLShapeRendererProps): ReactElement<unknown, JSXElementConstructor<any> | string>[];
663+
660664
// @public (undocumented)
661665
export const DefaultShapeWrapper: ForwardRefExoticComponent<TLShapeWrapperProps & RefAttributes<HTMLDivElement>>;
662666

@@ -3620,6 +3624,8 @@ export interface TLEditorComponents {
36203624
// (undocumented)
36213625
ShapeIndicators?: ComponentType | null;
36223626
// (undocumented)
3627+
ShapeRenderer?: ComponentType<TLShapeRendererProps> | null;
3628+
// (undocumented)
36233629
ShapeWrapper?: ComponentType<TLShapeWrapperProps & RefAttributes<HTMLDivElement>> | null;
36243630
// (undocumented)
36253631
SnapIndicator?: ComponentType<TLSnapIndicatorProps> | null;
@@ -4344,6 +4350,12 @@ export interface TLShapeIndicatorsProps {
43444350
showAll?: boolean;
43454351
}
43464352

4353+
// @public (undocumented)
4354+
export interface TLShapeRendererProps {
4355+
// (undocumented)
4356+
renderShape(shape: TLRenderingShape): ReactElement;
4357+
}
4358+
43474359
// @public
43484360
export interface TLShapeUtilCanBeLaidOutOpts {
43494361
shapes?: TLShape[];

packages/editor/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export { DefaultBackground } from './lib/components/default-components/DefaultBa
1717
export { DefaultBrush, type TLBrushProps } from './lib/components/default-components/DefaultBrush'
1818
export {
1919
DefaultCanvas,
20+
DefaultShapeRenderer,
2021
type TLCanvasComponentProps,
22+
type TLShapeRendererProps,
2123
} from './lib/components/default-components/DefaultCanvas'
2224
export {
2325
DefaultCollaboratorHint,

packages/editor/src/lib/components/default-components/DefaultCanvas.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { useQuickReactor, useValue } from '@tldraw/state-react'
33
import { TLHandle, TLShapeId } from '@tldraw/tlschema'
44
import { dedupe, modulate, objectMapValues } from '@tldraw/utils'
55
import classNames from 'classnames'
6-
import { Fragment, JSX, useEffect, useRef, useState } from 'react'
6+
import { Fragment, JSX, ReactElement, useEffect, useRef, useState } from 'react'
7+
import type { TLRenderingShape } from '../../editor/Editor'
78
import { tlenv } from '../../globals/environment'
89
import { useCanvasEvents } from '../../hooks/useCanvasEvents'
910
import { useCoarsePointer } from '../../hooks/useCoarsePointer'
@@ -450,21 +451,30 @@ function CullingController() {
450451
}
451452

452453
function ShapesToDisplay() {
453-
const editor = useEditor()
454-
455-
const renderingShapes = useValue('rendering shapes', () => editor.getRenderingShapes(), [editor])
454+
const { ShapeRenderer } = useEditorComponents()
455+
const Renderer = ShapeRenderer ?? DefaultShapeRenderer
456456

457457
return (
458458
<ShapeCullingProvider>
459-
{renderingShapes.map((result) => (
460-
<Shape key={result.id + '_shape'} {...result} />
461-
))}
459+
<Renderer renderShape={(shape) => <Shape key={shape.id + '_shape'} {...shape} />} />
462460
<CullingController />
463461
{tlenv.isSafari && <ReflowIfNeeded />}
464462
</ShapeCullingProvider>
465463
)
466464
}
467465

466+
/** @public */
467+
export interface TLShapeRendererProps {
468+
renderShape(shape: TLRenderingShape): ReactElement
469+
}
470+
471+
/** @public @react */
472+
export function DefaultShapeRenderer({ renderShape }: TLShapeRendererProps) {
473+
const editor = useEditor()
474+
const renderingShapes = useValue('rendering shapes', () => editor.getRenderingShapes(), [editor])
475+
return renderingShapes.map((shape) => renderShape(shape))
476+
}
477+
468478
function HintedShapeIndicator() {
469479
const editor = useEditor()
470480
const { ShapeIndicator } = useEditorComponents()

packages/editor/src/lib/hooks/useEditorComponents.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { DefaultBackground } from '../components/default-components/DefaultBackg
33
import { DefaultBrush, TLBrushProps } from '../components/default-components/DefaultBrush'
44
import {
55
DefaultCanvas,
6+
DefaultShapeRenderer,
67
TLCanvasComponentProps,
8+
TLShapeRendererProps,
79
} from '../components/default-components/DefaultCanvas'
810
import {
911
DefaultCollaboratorHint,
@@ -72,6 +74,7 @@ export interface TLEditorComponents {
7274
SelectionForeground?: ComponentType<TLSelectionForegroundProps> | null
7375
ShapeIndicator?: ComponentType<TLShapeIndicatorProps> | null
7476
ShapeIndicators?: ComponentType | null
77+
ShapeRenderer?: ComponentType<TLShapeRendererProps> | null
7578
ShapeWrapper?: ComponentType<TLShapeWrapperProps & RefAttributes<HTMLDivElement>> | null
7679
SnapIndicator?: ComponentType<TLSnapIndicatorProps> | null
7780
Spinner?: ComponentType<React.SVGProps<SVGSVGElement>> | null
@@ -119,6 +122,7 @@ export function EditorComponentsProvider({
119122
SelectionForeground: DefaultSelectionForeground,
120123
ShapeIndicator: DefaultShapeIndicator,
121124
ShapeIndicators: DefaultShapeIndicators,
125+
ShapeRenderer: DefaultShapeRenderer,
122126
ShapeWrapper: DefaultShapeWrapper,
123127
SnapIndicator: DefaultSnapIndicator,
124128
Spinner: DefaultSpinner,

0 commit comments

Comments
 (0)