import { BaseBoxShapeTool, DefaultKeyboardShortcutsDialog, DefaultKeyboardShortcutsDialogContent, DefaultToolbar, DefaultToolbarContent, Rectangle2d, ShapeUtil, T, TLBaseShape, TLComponents, TLOnResizeHandler, TLUiOverrides, Tldraw, TldrawUiMenuItem, resizeBox, useIsToolSelected, useTools, } from 'tldraw' import { getRotatedBoxShadow } from 'tldraw/src/lib/shapes/shared/rotated-box-shadow' import 'tldraw/tldraw.css' export type SurfaceShape = TLBaseShape< 'surface', { w: number h: number } > export class SurfaceShapeUtil extends ShapeUtil { static override type = 'surface' as const static override props = { w: T.number, h: T.number, } getDefaultProps(): SurfaceShape['props'] { return { w: 300, h: 300, } } getGeometry(shape: SurfaceShape) { return new Rectangle2d({ width: shape.props.w, height: shape.props.h, isFilled: true, }) } component(shape: SurfaceShape) { const pageRotation = this.editor.getShapePageTransform(shape)!.rotation() return ( <>
) } // [7] indicator(shape: SurfaceShape) { return } // [8] override onResize: TLOnResizeHandler = (shape, info) => { return resizeBox(shape, info) } } export class SurfaceShapeTool extends BaseBoxShapeTool { static override id = 'surface' static override initial = 'idle' override shapeType = 'surface' } export const overrides: TLUiOverrides = { tools(editor, tools) { // Create a tool item in the ui's context. tools.surface = { id: 'surface', icon: 'color', label: 'Surface', kbd: 'c', onSelect: () => { editor.setCurrentTool('surface') }, } return tools }, } export const components: TLComponents = { Toolbar: (props) => { const tools = useTools() const isSurfaceSelected = useIsToolSelected(tools['surface']) return ( ) }, KeyboardShortcutsDialog: (props) => { const tools = useTools() return ( ) }, } const shapeUtils = [SurfaceShapeUtil] const tools = [SurfaceShapeTool] export default function SurfaceExample() { return (
) }