11import React from "react"
22import styled from "styled-components"
3- import { ResponsiveValue , system } from "styled-system"
3+ import { ResponsiveValue , system , TLengthStyledSystem } from "styled-system"
44import { Flex } from "../"
55import { BoxProps } from "./Box"
6- import theme , { Theme } from "../theme"
6+ import { Theme } from "../theme"
77
8- type SpaceValue = keyof typeof theme . space
8+ type SpaceValue = number
99type DirectionValue = "vertical" | "horizontal"
1010
1111type StackBaseProps = Omit < BoxProps , "wrap" > & {
@@ -29,6 +29,9 @@ type StackProps = StackBaseProps & {
2929 as ?: React . ElementType
3030}
3131
32+ const toPx = ( value : TLengthStyledSystem ) : string =>
33+ typeof value === "number" ? `${ value } px` : value
34+
3235const StackBase : React . FC < StackBaseProps > = styled (
3336 ( { gap, direction, wrap, ...props } : StackProps ) => < Flex { ...props } />
3437) < StackBaseProps > (
@@ -39,30 +42,41 @@ const StackBase: React.FC<StackBaseProps> = styled(
3942 // By using double && we increase specificity of this CSS selector
4043 property : "&&" as any ,
4144 scale : "space" ,
42- // And here instead of the value for the property we return an object
4345 // @ts -ignore solves the undefined is not assignable to string[]
4446 transform : ( value : SpaceValue , scale : Theme [ "space" ] ) => {
45- const gap = scale ? scale [ value ] : 0
47+ const gapValue = toPx ( scale [ value ] )
48+ // And here instead of the value for the property we return an object
4649 return {
50+ "--gap" : gapValue ,
4751 position : "relative" ,
48- width : `calc(100% + ${ gap } )` ,
49- marginTop : `calc(-1 * ${ gap } )` ,
50- marginLeft : `calc(-1 * ${ gap } )` ,
52+ width : `calc(100% + var(-- gap) )` ,
53+ marginTop : `calc(-1 * var(-- gap) )` ,
54+ marginLeft : `var(--root-ml )` ,
5155 "> *" : {
52- marginTop : gap ,
53- marginLeft : gap
56+ marginTop : `var(-- gap)` ,
57+ marginLeft : `var(--child-ml)`
5458 }
5559 }
5660 }
5761 } ,
58- direction : {
59- property : "flexDirection" ,
60- transform : ( value : DirectionValue ) => ( value === "vertical" ? "column" : "row" )
61- } ,
6262 wrap : {
6363 property : "flexWrap" ,
6464 transform : ( value : boolean ) => ( value ? "wrap" : "nowrap" )
6565 }
66+ } ) ,
67+ // Because we are using `property: "&&" in both cases, putting them into one `system` call
68+ // will produce conflicts and break for the case when both `gap` and `direction` are arrays.
69+ system ( {
70+ direction : {
71+ property : "&&" as any ,
72+ transform : ( value : DirectionValue ) => {
73+ return {
74+ "--root-ml" : value === "vertical" ? 0 : "calc(-1 * var(--gap))" ,
75+ "--child-ml" : value === "vertical" ? 0 : "var(--gap)" ,
76+ flexDirection : value === "vertical" ? "column" : "row"
77+ }
78+ }
79+ }
6680 } )
6781)
6882
0 commit comments