This repository was archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathstory-stack.tsx
88 lines (75 loc) · 2.12 KB
/
story-stack.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as React from 'react'
import styled from '../styled'
import { spacing } from '../tokens'
import { sumOfElements, numberOfValues } from './custom-validations'
const alignItems = {
top: 'flex-start',
center: 'center',
bottom: 'flex-end'
}
const justifyContent = {
fill: 'space-between',
'space-between': 'space-between',
left: 'flex-start',
right: 'flex-end'
}
const StyledStack = styled.div`
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: ${(props) => alignItems[props.alignVertical]};
justify-content: ${(props) => justifyContent[props.align]};
line-height: inherit;
> * {
flex: ${(props) => (props.align === 'fill' ? 1 : 'none')};
margin-right: ${(props) => (props.align === 'fill' ? spacing.xsmall : 0)};
}
> *:last-child {
margin-right: 0;
}
`
const StackedItem = styled.div`
flex-basis: ${(props) => props.width}%;
line-height: inherit;
`
/*
wrap children in col
flex-basis makes flex redundant, have one
accept widths on parent = Stack
*/
const Stack = (props: IStackProps) => {
let children
if (props.align === 'fill' || props.align === 'space-between') {
children = React.Children.map(props.children, (child, index) => {
let width: string | number = 0
if (props.widths) { width = `${props.widths[index]}` || 0 }
return <StackedItem width={width}>{child}</StackedItem>
})
} else {
children = props.children
}
return (
<StyledStack {...props} align={props.align}>
{children}
</StyledStack>
)
}
export interface IStackProps {
align?: 'fill' | 'left' | 'right' | 'space-between'
alignVertical?: 'top' | 'center' | 'bottom'
widths?: number[]
children?: React.ReactNode
style?: Object
}
Stack.propTypes = {
/* internal props only used for validation */
/* sum of width values should be 100% */
_sum: (props) => sumOfElements(props.widths, 100),
/* the number of widths should match number of children */
_numberOfValues: (props) => numberOfValues(props.widths, React.Children.count(props.children))
}
Stack.defaultProps = {
align: 'fill',
alignVertical: 'center'
}
export default Stack