|
| 1 | +/** |
| 2 | + * This file is a copy-paste from https://github.com/rebassjs/grid/blob/master/src/index.js. |
| 3 | + * See https://github.com/opencollective/opencollective/issues/2929 for more info. |
| 4 | + */ |
| 5 | + |
| 6 | +import propTypes from '@styled-system/prop-types'; |
| 7 | +import styled from 'styled-components'; |
| 8 | +import type { ColorProps, FlexboxProps, GridProps, LayoutProps, SpaceProps, TypographyProps } from 'styled-system'; |
| 9 | +import { border, color, compose, flexbox, grid, layout, space, typography } from 'styled-system'; |
| 10 | + |
| 11 | +export const boxProps = compose(space, color, layout, typography, flexbox, grid, border); |
| 12 | + |
| 13 | +type BoxProps = SpaceProps & |
| 14 | + ColorProps & |
| 15 | + FlexboxProps & |
| 16 | + GridProps & |
| 17 | + LayoutProps & |
| 18 | + TypographyProps & { |
| 19 | + gap?: string | number; |
| 20 | + css?: string | object; |
| 21 | + }; |
| 22 | + |
| 23 | +const FILTERED_PROPS = new Set(['display', 'width', 'height']); |
| 24 | + |
| 25 | +export const Box = styled.div.withConfig({ |
| 26 | + shouldForwardProp: (prop, validate) => validate(prop) && !FILTERED_PROPS.has(prop), |
| 27 | +})<BoxProps>( |
| 28 | + { |
| 29 | + boxSizing: 'border-box', |
| 30 | + }, |
| 31 | + boxProps, |
| 32 | +); |
| 33 | + |
| 34 | +Box.displayName = 'Box'; |
| 35 | + |
| 36 | +Box.propTypes = { |
| 37 | + ...propTypes.space, |
| 38 | + ...propTypes.color, |
| 39 | + ...propTypes.layout, |
| 40 | + ...propTypes.typography, |
| 41 | + ...propTypes.flexbox, |
| 42 | +}; |
| 43 | + |
| 44 | +export type FlexProps = BoxProps; |
| 45 | + |
| 46 | +export const Flex = styled(Box)<FlexProps>( |
| 47 | + (props) => ({ |
| 48 | + display: 'flex', |
| 49 | + gap: props.gap, |
| 50 | + }), |
| 51 | + compose(space, layout, flexbox), |
| 52 | +); |
| 53 | + |
| 54 | +Flex.displayName = 'Flex'; |
| 55 | + |
| 56 | +export const Grid = styled.div<BoxProps>( |
| 57 | + { |
| 58 | + boxSizing: 'border-box', |
| 59 | + display: 'grid', |
| 60 | + '> div': { 'min-width': 0 }, |
| 61 | + }, |
| 62 | + compose(space, grid, layout, flexbox), |
| 63 | +); |
0 commit comments