tldraw/components/style-panel/align-distribute.tsx

162 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-05-26 19:20:52 +00:00
import {
AlignBottomIcon,
AlignCenterHorizontallyIcon,
AlignCenterVerticallyIcon,
AlignLeftIcon,
AlignRightIcon,
AlignTopIcon,
SpaceEvenlyHorizontallyIcon,
SpaceEvenlyVerticallyIcon,
StretchHorizontallyIcon,
StretchVerticallyIcon,
2021-05-28 20:30:27 +00:00
} from '@radix-ui/react-icons'
import { breakpoints, IconButton } from 'components/shared'
import { memo } from 'react'
2021-05-28 20:30:27 +00:00
import state from 'state'
import styled from 'styles'
import { AlignType, DistributeType, StretchType } from 'types'
2021-05-26 19:20:52 +00:00
function alignTop() {
2021-05-28 20:30:27 +00:00
state.send('ALIGNED', { type: AlignType.Top })
2021-05-26 19:20:52 +00:00
}
function alignCenterVertical() {
2021-05-28 20:30:27 +00:00
state.send('ALIGNED', { type: AlignType.CenterVertical })
2021-05-26 19:20:52 +00:00
}
function alignBottom() {
2021-05-28 20:30:27 +00:00
state.send('ALIGNED', { type: AlignType.Bottom })
2021-05-26 19:20:52 +00:00
}
function stretchVertically() {
2021-05-28 20:30:27 +00:00
state.send('STRETCHED', { type: StretchType.Vertical })
2021-05-26 19:20:52 +00:00
}
function distributeVertically() {
2021-05-28 20:30:27 +00:00
state.send('DISTRIBUTED', { type: DistributeType.Vertical })
2021-05-26 19:20:52 +00:00
}
function alignLeft() {
2021-05-28 20:30:27 +00:00
state.send('ALIGNED', { type: AlignType.Left })
2021-05-26 19:20:52 +00:00
}
function alignCenterHorizontal() {
2021-05-28 20:30:27 +00:00
state.send('ALIGNED', { type: AlignType.CenterHorizontal })
2021-05-26 19:20:52 +00:00
}
function alignRight() {
2021-05-28 20:30:27 +00:00
state.send('ALIGNED', { type: AlignType.Right })
2021-05-26 19:20:52 +00:00
}
function stretchHorizontally() {
2021-05-28 20:30:27 +00:00
state.send('STRETCHED', { type: StretchType.Horizontal })
2021-05-26 19:20:52 +00:00
}
function distributeHorizontally() {
2021-05-28 20:30:27 +00:00
state.send('DISTRIBUTED', { type: DistributeType.Horizontal })
2021-05-26 19:20:52 +00:00
}
function AlignDistribute({
2021-05-26 21:47:46 +00:00
hasTwoOrMore,
hasThreeOrMore,
}: {
hasTwoOrMore: boolean
hasThreeOrMore: boolean
2021-06-21 21:35:28 +00:00
}): JSX.Element {
2021-05-26 19:20:52 +00:00
return (
<Container>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={alignLeft}
>
2021-05-26 19:20:52 +00:00
<AlignLeftIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={alignCenterHorizontal}
>
2021-05-26 19:20:52 +00:00
<AlignCenterHorizontallyIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={alignRight}
>
2021-05-26 19:20:52 +00:00
<AlignRightIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={stretchHorizontally}
>
2021-05-26 19:20:52 +00:00
<StretchHorizontallyIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasThreeOrMore}
onClick={distributeHorizontally}
>
2021-05-26 19:20:52 +00:00
<SpaceEvenlyHorizontallyIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={alignTop}
>
2021-05-26 21:47:46 +00:00
<AlignTopIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={alignCenterVertical}
>
2021-05-26 21:47:46 +00:00
<AlignCenterVerticallyIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={alignBottom}
>
2021-05-26 21:47:46 +00:00
<AlignBottomIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasTwoOrMore}
onClick={stretchVertically}
>
2021-05-26 21:47:46 +00:00
<StretchVerticallyIcon />
</IconButton>
<IconButton
bp={breakpoints}
size="small"
disabled={!hasThreeOrMore}
onClick={distributeVertically}
>
2021-05-26 21:47:46 +00:00
<SpaceEvenlyVerticallyIcon />
</IconButton>
2021-05-26 19:20:52 +00:00
</Container>
)
}
export default memo(AlignDistribute)
2021-05-28 20:30:27 +00:00
const Container = styled('div', {
display: 'grid',
2021-05-26 19:20:52 +00:00
padding: 4,
2021-05-28 20:30:27 +00:00
gridTemplateColumns: 'repeat(5, auto)',
2021-05-26 19:20:52 +00:00
[`& ${IconButton} > svg`]: {
2021-05-28 20:30:27 +00:00
stroke: 'transparent',
2021-05-26 19:20:52 +00:00
},
})