Created
February 23, 2021 15:38
-
-
Save jacques-blom/1dde14006b055336e006a51313fe2746 to your computer and use it in GitHub Desktop.
Atom Families 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {InputGroup, InputRightElement, NumberInput, NumberInputField, Text, VStack} from '@chakra-ui/react' | |
export const EditProperties = () => { | |
return ( | |
<Card> | |
<Section heading="Position"> | |
<Property label="Top" value={1} onChange={(top) => {}} /> | |
<Property label="Left" value={1} onChange={(left) => {}} /> | |
</Section> | |
</Card> | |
) | |
} | |
const Section: React.FC<{heading: string}> = ({heading, children}) => { | |
return ( | |
<VStack spacing={2} align="flex-start"> | |
<Text fontWeight="500">{heading}</Text> | |
{children} | |
</VStack> | |
) | |
} | |
const Property = ({label, value, onChange}: {label: string; value: number; onChange: (value: number) => void}) => { | |
return ( | |
<div> | |
<Text fontSize="14px" fontWeight="500" mb="2px"> | |
{label} | |
</Text> | |
<InputGroup size="sm" variant="filled"> | |
<NumberInput value={value} onChange={(_, value) => onChange(value)}> | |
<NumberInputField borderRadius="md" /> | |
<InputRightElement pointerEvents="none" children="px" lineHeight="1" fontSize="12px" /> | |
</NumberInput> | |
</InputGroup> | |
</div> | |
) | |
} | |
const Card: React.FC = ({children}) => ( | |
<VStack | |
position="absolute" | |
top="20px" | |
right="20px" | |
backgroundColor="white" | |
padding={2} | |
boxShadow="md" | |
borderRadius="md" | |
spacing={3} | |
align="flex-start" | |
onClick={(e) => e.stopPropagation()} | |
> | |
{children} | |
</VStack> | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Resizable, ResizeHandle} from 'react-resizable' | |
import {Handle} from './Handle' | |
import {ElementStyle} from './Rectangle/Rectangle' | |
const handlePlacements: ResizeHandle[] = ['n', 's', 'e', 'w', 'ne', 'nw', 'se', 'sw'] | |
type ResizeProps = { | |
selected: boolean | |
onResize: (style: ElementStyle) => void | |
} & ElementStyle | |
export const Resize: React.FC<ResizeProps> = ({selected, children, position, size, onResize}) => { | |
return ( | |
<Resizable | |
width={size.width} | |
height={size.height} | |
onResize={(_, {size: newSize, handle}) => { | |
let topDiff = 0 | |
if (handle.includes('n')) { | |
topDiff = size.height - newSize.height | |
} | |
let leftDiff = 0 | |
if (handle.includes('w')) { | |
leftDiff = size.width - newSize.width | |
} | |
onResize({ | |
size: { | |
width: Math.round(newSize.width), | |
height: Math.round(newSize.height), | |
}, | |
position: { | |
top: position.top + topDiff, | |
left: position.left + leftDiff, | |
}, | |
}) | |
}} | |
resizeHandles={handlePlacements} | |
handle={(placement) => ( | |
<div> | |
<Handle placement={placement} visible={selected} /> | |
</div> | |
)} | |
> | |
<div>{children}</div> | |
</Resizable> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment