tldraw/components/canvas/defs.tsx

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
import React from 'react'
2021-05-28 16:25:43 +00:00
import { useSelector } from 'state'
2021-06-29 12:00:59 +00:00
import tld from 'utils/tld'
2021-06-05 06:36:39 +00:00
import { DotCircle, Handle } from './misc'
2021-07-03 16:30:06 +00:00
import useShape from 'hooks/useShape'
import useShapesToRender from 'hooks/useShapesToRender'
2021-06-29 14:54:46 +00:00
import styled from 'styles'
2021-05-28 13:08:51 +00:00
2021-06-21 21:35:28 +00:00
export default function Defs(): JSX.Element {
const shapeIdsToRender = useShapesToRender()
2021-05-28 13:08:51 +00:00
return (
<defs>
2021-06-05 06:36:39 +00:00
<DotCircle id="dot" r={4} />
<Handle id="handle" r={4} />
<ExpandDef />
2021-06-29 14:54:46 +00:00
<ShadowDef />
{shapeIdsToRender.map((id) => (
<Def key={id} id={id} />
))}
2021-05-28 13:08:51 +00:00
</defs>
)
}
function Def({ id }: { id: string }) {
2021-07-03 16:30:06 +00:00
const shape = useShape(id)
2021-05-28 16:25:43 +00:00
if (!shape) return null
2021-07-03 16:30:06 +00:00
return getShapeUtils(shape).render(shape, { isEditing: false })
}
function ExpandDef() {
2021-06-29 12:00:59 +00:00
const zoom = useSelector((s) => tld.getCurrentCamera(s.data).zoom)
return (
<filter id="expand">
<feMorphology operator="dilate" radius={2 / zoom} />
</filter>
)
}
2021-06-29 14:54:46 +00:00
function ShadowDef() {
return (
<filter id="hover">
<StyledShadow dx="0" dy="0" stdDeviation="1.2" floodOpacity="1" />
</filter>
)
}
const StyledShadow = styled('feDropShadow', {
floodColor: '$selected',
})