|
| 1 | +import { ShapeSizeRestrictions, ShapeType } from '@/core/model'; |
| 2 | +import { forwardRef } from 'react'; |
| 3 | +import { ShapeProps } from '../shape.model'; |
| 4 | +import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions'; |
| 5 | +import { Circle, Group, Rect, Image } from 'react-konva'; |
| 6 | +import { useShapeProps } from '../../shapes/use-shape-props.hook'; |
| 7 | +import { BASIC_SHAPE } from '../front-components/shape.const'; |
| 8 | +import { useGroupShapeProps } from '../mock-components.utils'; |
| 9 | +import sunIconUrl from '/icons/sun.svg'; |
| 10 | +import moonIconUrl from '/icons/moonalt.svg'; |
| 11 | + |
| 12 | +const iconSize = 20; |
| 13 | + |
| 14 | +const toggleLightDarkShapeRestrictions: ShapeSizeRestrictions = { |
| 15 | + minWidth: 50, |
| 16 | + minHeight: 25, |
| 17 | + maxWidth: 50, |
| 18 | + maxHeight: 25, |
| 19 | + defaultWidth: 50, |
| 20 | + defaultHeight: 25, |
| 21 | +}; |
| 22 | + |
| 23 | +const shapeType: ShapeType = 'toggleLightDark'; |
| 24 | + |
| 25 | +export const getToggleLightDarkShapeSizeRestrictions = |
| 26 | + (): ShapeSizeRestrictions => toggleLightDarkShapeRestrictions; |
| 27 | + |
| 28 | +export const ToggleLightDark = forwardRef<any, ShapeProps>((props, ref) => { |
| 29 | + const { x, y, width, height, id, onSelected, otherProps, ...shapeProps } = |
| 30 | + props; |
| 31 | + const restrictedSize = fitSizeToShapeSizeRestrictions( |
| 32 | + toggleLightDarkShapeRestrictions, |
| 33 | + width, |
| 34 | + height |
| 35 | + ); |
| 36 | + const { width: restrictedWidth, height: restrictedHeight } = restrictedSize; |
| 37 | + |
| 38 | + const { isOn } = useShapeProps(otherProps, BASIC_SHAPE); |
| 39 | + |
| 40 | + const circleX = isOn |
| 41 | + ? restrictedHeight / 2 |
| 42 | + : restrictedWidth - restrictedHeight / 2; |
| 43 | + |
| 44 | + const commonGroupProps = useGroupShapeProps( |
| 45 | + props, |
| 46 | + restrictedSize, |
| 47 | + shapeType, |
| 48 | + ref |
| 49 | + ); |
| 50 | + |
| 51 | + const toggleIcon = new window.Image(); |
| 52 | + toggleIcon.src = isOn ? sunIconUrl : moonIconUrl; |
| 53 | + |
| 54 | + return ( |
| 55 | + <Group {...commonGroupProps} {...shapeProps}> |
| 56 | + <Rect |
| 57 | + x={0} |
| 58 | + y={0} |
| 59 | + width={restrictedWidth} |
| 60 | + height={restrictedHeight} |
| 61 | + cornerRadius={50} |
| 62 | + stroke="black" |
| 63 | + strokeWidth={2} |
| 64 | + fill={isOn ? 'white' : 'gray'} |
| 65 | + /> |
| 66 | + <Circle |
| 67 | + x={circleX} |
| 68 | + y={restrictedHeight / 2} |
| 69 | + radius={restrictedHeight / 2} |
| 70 | + stroke="black" |
| 71 | + strokeWidth={2} |
| 72 | + fill={isOn ? 'white' : 'gray'} |
| 73 | + /> |
| 74 | + <Image |
| 75 | + image={toggleIcon} |
| 76 | + x={circleX - iconSize / 2} |
| 77 | + y={restrictedHeight / 2 - iconSize / 2} |
| 78 | + width={iconSize} |
| 79 | + height={iconSize} |
| 80 | + /> |
| 81 | + </Group> |
| 82 | + ); |
| 83 | +}); |
0 commit comments