-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFlexBox.tsx
More file actions
72 lines (65 loc) · 1.91 KB
/
FlexBox.tsx
File metadata and controls
72 lines (65 loc) · 1.91 KB
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
// Libraries
import React, {forwardRef} from 'react'
import classnames from 'classnames'
// Types
import {
FlexDirection,
JustifyContent,
AlignItems,
ComponentSize,
StandardFunctionProps,
} from '../../Types'
// Styles
import './FlexBox.scss'
export interface FlexBoxProps extends StandardFunctionProps {
/** Vertical or horizontal flex alignment */
direction?: FlexDirection
/** Inserted margin between children */
margin?: ComponentSize
/** Can be FlexStart, FlexEnd, Center, SpaceBetween, or SpaceAround */
justifyContent?: JustifyContent
/** Can be FlexStart, FlexEnd, Center, or Stretch */
alignItems?: AlignItems
/** stretches component spacer to fit parent width */
stretchToFitWidth?: boolean
/** stretches component spacer to fit parent height */
stretchToFitHeight?: boolean
/** HTML element type, must pass in a valid type */
element?: string
}
export type FlexBoxRef = HTMLElement
export const FlexBoxRoot = forwardRef<FlexBoxRef, FlexBoxProps>(
(
{
id,
style,
testID = 'flex-box',
margin,
element = 'div',
children,
direction = FlexDirection.Row,
className,
alignItems = AlignItems.Center,
justifyContent = JustifyContent.FlexStart,
stretchToFitWidth = false,
stretchToFitHeight = false,
},
ref
) => {
const flexBoxClass = classnames('cf-flex-box', {
[`cf-flex-box__margin-${margin}`]: margin,
[`cf-flex-box__${direction}`]: direction,
[`cf-flex-box__justify-${justifyContent}`]: justifyContent,
[`cf-flex-box__align-${alignItems}`]: alignItems,
'cf-flex-box__stretch-w': stretchToFitWidth,
'cf-flex-box__stretch-h': stretchToFitHeight,
[`${className}`]: className,
})
return React.createElement(
element,
{id: id, ref, style, className: flexBoxClass, 'data-testid': testID},
children
)
}
)
FlexBoxRoot.displayName = 'FlexBox'