| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import React, { useRef, memo } from 'react'
- import { useSelector } from 'state'
- import styled from 'styles'
- import { getShapeUtils } from 'lib/shape-utils'
- import { getPage } from 'utils/utils'
- import { ShapeStyles } from 'types'
- import useShapeEvents from 'hooks/useShapeEvents'
-
- function Shape({ id, isSelecting }: { id: string; isSelecting: boolean }) {
- const isHovered = useSelector((state) => state.data.hoveredId === id)
-
- const isSelected = useSelector((state) => state.values.selectedIds.has(id))
-
- const shape = useSelector(({ data }) => getPage(data).shapes[id])
-
- const rGroup = useRef<SVGGElement>(null)
-
- const events = useShapeEvents(id, rGroup)
-
- // This is a problem with deleted shapes. The hooks in this component
- // may sometimes run before the hook in the Page component, which means
- // a deleted shape will still be pulled here before the page component
- // detects the change and pulls this component.
- if (!shape) return null
-
- const transform = `
- rotate(${shape.rotation * (180 / Math.PI)},
- ${getShapeUtils(shape).getCenter(shape)})
- translate(${shape.point})`
-
- return (
- <StyledGroup
- ref={rGroup}
- isHovered={isHovered}
- isSelected={isSelected}
- transform={transform}
- {...events}
- >
- {isSelecting && <HoverIndicator as="use" href={'#' + id} />}
- <StyledShape id={id} style={shape.style} />
- </StyledGroup>
- )
- }
-
- const StyledShape = memo(
- ({ id, style }: { id: string; style: ShapeStyles }) => {
- return <MainShape as="use" href={'#' + id} {...style} />
- }
- )
-
- const MainShape = styled('use', {
- zStrokeWidth: 1,
- })
-
- const HoverIndicator = styled('path', {
- fill: 'none',
- stroke: 'transparent',
- pointerEvents: 'all',
- strokeLinecap: 'round',
- strokeLinejoin: 'round',
- transform: 'all .2s',
- })
-
- const StyledGroup = styled('g', {
- [`& ${HoverIndicator}`]: {
- opacity: '0',
- },
- variants: {
- isSelected: {
- true: {},
- false: {},
- },
- isHovered: {
- true: {},
- false: {},
- },
- },
- compoundVariants: [
- {
- isSelected: true,
- isHovered: true,
- css: {
- [`& ${HoverIndicator}`]: {
- opacity: '1',
- stroke: '$hint',
- zStrokeWidth: [8, 4],
- },
- },
- },
- {
- isSelected: true,
- isHovered: false,
- css: {
- [`& ${HoverIndicator}`]: {
- opacity: '1',
- stroke: '$hint',
- zStrokeWidth: [6, 3],
- },
- },
- },
- {
- isSelected: false,
- isHovered: true,
- css: {
- [`& ${HoverIndicator}`]: {
- opacity: '1',
- stroke: '$hint',
- zStrokeWidth: [8, 4],
- },
- },
- },
- ],
- })
-
- export { HoverIndicator }
-
- export default memo(Shape)
|