tldraw/components/tools-panel/zoom.tsx

83 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-05-29 13:59:11 +00:00
import { ZoomInIcon, ZoomOutIcon } from '@radix-ui/react-icons'
import { IconButton } from 'components/shared'
import state, { useSelector } from 'state'
import styled from 'styles'
2021-06-03 12:06:39 +00:00
import { getCurrentCamera } from 'utils/utils'
2021-06-02 21:17:38 +00:00
import Tooltip from '../tooltip'
2021-05-29 13:59:11 +00:00
const zoomIn = () => state.send('ZOOMED_IN')
const zoomOut = () => state.send('ZOOMED_OUT')
const zoomToFit = () => state.send('ZOOMED_TO_FIT')
const zoomToActual = () => state.send('ZOOMED_TO_ACTUAL')
export default function Zoom() {
return (
<Container size={{ '@sm': 'small' }}>
2021-06-02 21:17:38 +00:00
<Tooltip label="Zoom Out">
<IconButton onClick={zoomOut}>
<ZoomOutIcon />
</IconButton>
</Tooltip>
<Tooltip label="Zoom In">
<IconButton onClick={zoomIn}>
<ZoomInIcon />
</IconButton>
</Tooltip>
<Tooltip label="Reset Zoom">
<ZoomCounter />
</Tooltip>
2021-05-29 13:59:11 +00:00
</Container>
)
}
function ZoomCounter() {
2021-06-03 12:06:39 +00:00
const zoom = useSelector((s) => getCurrentCamera(s.data).zoom)
2021-05-29 13:59:11 +00:00
return (
<ZoomButton onClick={zoomToActual} onDoubleClick={zoomToFit}>
2021-06-03 12:06:39 +00:00
{Math.round(zoom * 100)}%
2021-05-29 13:59:11 +00:00
</ZoomButton>
)
}
2021-06-02 16:11:42 +00:00
const ZoomButton = styled(IconButton, {
fontSize: '$0',
padding: 8,
})
2021-05-29 13:59:11 +00:00
const Container = styled('div', {
position: 'absolute',
left: 12,
bottom: 64,
2021-05-29 13:59:11 +00:00
backgroundColor: '$panel',
borderRadius: '4px',
overflow: 'hidden',
alignSelf: 'flex-end',
pointerEvents: 'all',
userSelect: 'none',
zIndex: 200,
2021-05-30 13:49:33 +00:00
border: '1px solid $panel',
boxShadow: '0px 2px 4px rgba(0,0,0,.12)',
2021-05-29 13:59:11 +00:00
display: 'flex',
padding: 4,
2021-06-02 16:11:42 +00:00
flexDirection: 'column',
alignItems: 'center',
2021-05-29 13:59:11 +00:00
'& svg': {
strokeWidth: 0,
},
variants: {
size: {
small: {
bottom: 12,
2021-06-02 16:11:42 +00:00
flexDirection: 'row',
alignItems: 'center',
[`& ${ZoomButton}`]: {
width: 44,
},
},
},
},
2021-05-29 13:59:11 +00:00
})