tldraw/components/canvas/page.tsx

34 lines
922 B
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { useSelector } from 'state'
2021-05-28 14:37:23 +00:00
import Shape from './shape'
import HoveredShape from './hovered-shape'
import usePageShapes from 'hooks/usePageShapes'
2021-05-09 21:22:25 +00:00
/*
On each state change, compare node ids of all shapes
on the current page. Kind of expensive but only happens
here; and still cheaper than any other pattern I've found.
*/
2021-06-21 21:35:28 +00:00
export default function Page(): JSX.Element {
const showHovers = useSelector((s) =>
s.isInAny('selecting', 'text', 'editingShape')
)
2021-05-28 14:37:23 +00:00
const visiblePageShapeIds = usePageShapes()
const hoveredShapeId = useSelector((s) => {
return visiblePageShapeIds.find((id) => id === s.data.hoveredId)
})
2021-05-09 21:22:25 +00:00
return (
<g pointerEvents={showHovers ? 'all' : 'none'}>
{showHovers && hoveredShapeId && (
<HoveredShape key={hoveredShapeId} id={hoveredShapeId} />
)}
{visiblePageShapeIds.map((id) => (
<Shape key={id} id={id} />
2021-05-09 21:22:25 +00:00
))}
2021-05-30 13:20:25 +00:00
</g>
2021-05-09 21:22:25 +00:00
)
}