Files
react/components/grid/grid-container.tsx
Florian Levis 384a4a050f chore(deps): update styled-jsx to ^3.3.1 (#520)
* chore(deps): update styled-jsx to ^3.3.1

update styled-jsx to ^3.3.1 to allow compatiblity with react@^17

* fix(modules): fix 695-issue to compatible with React 17

* docs: fix module error caused by styled-jsx update

Co-authored-by: unix <unix.bio@gmail.com>
2021-08-13 17:06:26 +08:00

57 lines
1.5 KiB
TypeScript

import React, { useMemo } from 'react'
import useTheme from '../use-theme'
import GridBasicItem, { GridBasicItemComponentProps } from './basic-item'
import { Wrap } from './grid-types'
import { css } from 'styled-jsx/css'
interface Props {
gap: number
wrap: Wrap
className: string
}
const defaultProps = {
gap: 0,
wrap: 'wrap' as Wrap,
className: '',
}
export type GridContainerProps = Props & typeof defaultProps & GridBasicItemComponentProps
const GridContainer: React.FC<React.PropsWithChildren<GridContainerProps>> = ({
gap,
wrap,
children,
className,
...props
}) => {
const theme = useTheme()
const gapUnit = useMemo(() => {
return `calc(${gap} * ${theme.layout.gapQuarter})`
}, [gap, theme.layout.gapQuarter])
const { className: resolveClassName, styles } = css.resolve`
--gaid-gap-unit: ${gapUnit};
display: flex;
flex-wrap: ${wrap};
box-sizing: border-box;
margin: calc(-1 * var(--gaid-gap-unit));
width: calc(100% + var(--gaid-gap-unit) * 2);
`
return (
<GridBasicItem className={`${resolveClassName} ${className}`} {...props}>
{children}
{styles}
</GridBasicItem>
)
}
type MemoGridContainerComponent<P = {}> = React.NamedExoticComponent<P>
type ComponentProps = Partial<typeof defaultProps> &
Omit<Props, keyof typeof defaultProps> &
GridBasicItemComponentProps
GridContainer.defaultProps = defaultProps
export default React.memo(GridContainer) as MemoGridContainerComponent<ComponentProps>