| 1 |
import { css } from '@patternfly/react-styles'; |
| 2 |
import styles from '@patternfly/react-styles/css/layouts/Flex/flex'; |
| 3 |
import * as flexToken from '@patternfly/react-tokens/dist/esm/l_flex_item_Order'; |
| 4 |
|
| 5 |
import { formatBreakpointMods, setBreakpointCssVars } from '../../helpers/util'; |
| 6 |
|
| 7 |
export interface FlexItemProps extends React.HTMLProps<HTMLDivElement> { |
| 8 |
/** content rendered inside the Flex layout */ |
| 9 |
children?: React.ReactNode; |
| 10 |
/** additional classes added to the Flex layout */ |
| 11 |
className?: string; |
| 12 |
/** Spacers at various breakpoints */ |
| 13 |
spacer?: { |
| 14 |
default?: |
| 15 |
| 'spacerNone' |
| 16 |
| 'spacerXs' |
| 17 |
| 'spacerSm' |
| 18 |
| 'spacerMd' |
| 19 |
| 'spacerLg' |
| 20 |
| 'spacerXl' |
| 21 |
| 'spacer2xl' |
| 22 |
| 'spacer3xl' |
| 23 |
| 'spacer4xl'; |
| 24 |
sm?: |
| 25 |
| 'spacerNone' |
| 26 |
| 'spacerXs' |
| 27 |
| 'spacerSm' |
| 28 |
| 'spacerMd' |
| 29 |
| 'spacerLg' |
| 30 |
| 'spacerXl' |
| 31 |
| 'spacer2xl' |
| 32 |
| 'spacer3xl' |
| 33 |
| 'spacer4xl'; |
| 34 |
md?: |
| 35 |
| 'spacerNone' |
| 36 |
| 'spacerXs' |
| 37 |
| 'spacerSm' |
| 38 |
| 'spacerMd' |
| 39 |
| 'spacerLg' |
| 40 |
| 'spacerXl' |
| 41 |
| 'spacer2xl' |
| 42 |
| 'spacer3xl' |
| 43 |
| 'spacer4xl'; |
| 44 |
lg?: |
| 45 |
| 'spacerNone' |
| 46 |
| 'spacerXs' |
| 47 |
| 'spacerSm' |
| 48 |
| 'spacerMd' |
| 49 |
| 'spacerLg' |
| 50 |
| 'spacerXl' |
| 51 |
| 'spacer2xl' |
| 52 |
| 'spacer3xl' |
| 53 |
| 'spacer4xl'; |
| 54 |
xl?: |
| 55 |
| 'spacerNone' |
| 56 |
| 'spacerXs' |
| 57 |
| 'spacerSm' |
| 58 |
| 'spacerMd' |
| 59 |
| 'spacerLg' |
| 60 |
| 'spacerXl' |
| 61 |
| 'spacer2xl' |
| 62 |
| 'spacer3xl' |
| 63 |
| 'spacer4xl'; |
| 64 |
'2xl'?: |
| 65 |
| 'spacerNone' |
| 66 |
| 'spacerXs' |
| 67 |
| 'spacerSm' |
| 68 |
| 'spacerMd' |
| 69 |
| 'spacerLg' |
| 70 |
| 'spacerXl' |
| 71 |
| 'spacer2xl' |
| 72 |
| 'spacer3xl' |
| 73 |
| 'spacer4xl'; |
| 74 |
}; |
| 75 |
/** Whether to add flex: grow at various breakpoints */ |
| 76 |
grow?: { |
| 77 |
default?: 'grow'; |
| 78 |
sm?: 'grow'; |
| 79 |
md?: 'grow'; |
| 80 |
lg?: 'grow'; |
| 81 |
xl?: 'grow'; |
| 82 |
'2xl'?: 'grow'; |
| 83 |
}; |
| 84 |
/** Whether to add flex: shrink at various breakpoints */ |
| 85 |
shrink?: { |
| 86 |
default?: 'shrink'; |
| 87 |
sm?: 'shrink'; |
| 88 |
md?: 'shrink'; |
| 89 |
lg?: 'shrink'; |
| 90 |
xl?: 'shrink'; |
| 91 |
'2xl'?: 'shrink'; |
| 92 |
}; |
| 93 |
/** Value to add for flex property at various breakpoints */ |
| 94 |
flex?: { |
| 95 |
default?: 'flexDefault' | 'flexNone' | 'flex_1' | 'flex_2' | 'flex_3' | 'flex_4'; |
| 96 |
sm?: 'flexDefault' | 'flexNone' | 'flex_1' | 'flex_2' | 'flex_3' | 'flex_4'; |
| 97 |
md?: 'flexDefault' | 'flexNone' | 'flex_1' | 'flex_2' | 'flex_3' | 'flex_4'; |
| 98 |
lg?: 'flexDefault' | 'flexNone' | 'flex_1' | 'flex_2' | 'flex_3' | 'flex_4'; |
| 99 |
xl?: 'flexDefault' | 'flexNone' | 'flex_1' | 'flex_2' | 'flex_3' | 'flex_4'; |
| 100 |
'2xl'?: 'flexDefault' | 'flexNone' | 'flex_1' | 'flex_2' | 'flex_3' | 'flex_4'; |
| 101 |
}; |
| 102 |
/** Value to add for align-self property at various breakpoints */ |
| 103 |
alignSelf?: { |
| 104 |
default?: 'alignSelfFlexStart' | 'alignSelfFlexEnd' | 'alignSelfCenter' | 'alignSelfStretch' | 'alignSelfBaseline'; |
| 105 |
sm?: 'alignSelfFlexStart' | 'alignSelfFlexEnd' | 'alignSelfCenter' | 'alignSelfStretch' | 'alignSelfBaseline'; |
| 106 |
md?: 'alignSelfFlexStart' | 'alignSelfFlexEnd' | 'alignSelfCenter' | 'alignSelfStretch' | 'alignSelfBaseline'; |
| 107 |
lg?: 'alignSelfFlexStart' | 'alignSelfFlexEnd' | 'alignSelfCenter' | 'alignSelfStretch' | 'alignSelfBaseline'; |
| 108 |
xl?: 'alignSelfFlexStart' | 'alignSelfFlexEnd' | 'alignSelfCenter' | 'alignSelfStretch' | 'alignSelfBaseline'; |
| 109 |
'2xl'?: 'alignSelfFlexStart' | 'alignSelfFlexEnd' | 'alignSelfCenter' | 'alignSelfStretch' | 'alignSelfBaseline'; |
| 110 |
}; |
| 111 |
/** Value to use for margin: auto at various breakpoints */ |
| 112 |
align?: { |
| 113 |
default?: 'alignLeft' | 'alignRight'; |
| 114 |
sm?: 'alignLeft' | 'alignRight'; |
| 115 |
md?: 'alignLeft' | 'alignRight'; |
| 116 |
lg?: 'alignLeft' | 'alignRight'; |
| 117 |
xl?: 'alignLeft' | 'alignRight'; |
| 118 |
'2xl'?: 'alignLeft' | 'alignRight'; |
| 119 |
}; |
| 120 |
/** Whether to set width: 100% at various breakpoints */ |
| 121 |
fullWidth?: { |
| 122 |
default?: 'fullWidth'; |
| 123 |
sm?: 'fullWidth'; |
| 124 |
md?: 'fullWidth'; |
| 125 |
lg?: 'fullWidth'; |
| 126 |
xl?: 'fullWidth'; |
| 127 |
'2xl'?: 'fullWidth'; |
| 128 |
}; |
| 129 |
/** Modifies the flex layout element order property */ |
| 130 |
order?: { |
| 131 |
default?: string; |
| 132 |
md?: string; |
| 133 |
lg?: string; |
| 134 |
xl?: string; |
| 135 |
'2xl'?: string; |
| 136 |
}; |
| 137 |
/** Sets the base component to render. defaults to div */ |
| 138 |
component?: React.ElementType<any> | React.ComponentType<any>; |
| 139 |
} |
| 140 |
|
| 141 |
export const FlexItem: React.FunctionComponent<FlexItemProps> = ({ |
| 142 |
children = null, |
| 143 |
className = '', |
| 144 |
component = 'div', |
| 145 |
spacer, |
| 146 |
grow, |
| 147 |
shrink, |
| 148 |
flex, |
| 149 |
alignSelf, |
| 150 |
align, |
| 151 |
fullWidth, |
| 152 |
order, |
| 153 |
style, |
| 154 |
...props |
| 155 |
}: FlexItemProps) => { |
| 156 |
const Component: any = component; |
| 157 |
|
| 158 |
return ( |
| 159 |
<Component |
| 160 |
{...props} |
| 161 |
className={css( |
| 162 |
formatBreakpointMods(spacer, styles), |
| 163 |
formatBreakpointMods(grow, styles), |
| 164 |
formatBreakpointMods(shrink, styles), |
| 165 |
formatBreakpointMods(flex, styles), |
| 166 |
formatBreakpointMods(alignSelf, styles), |
| 167 |
formatBreakpointMods(align, styles), |
| 168 |
formatBreakpointMods(fullWidth, styles), |
| 169 |
className |
| 170 |
)} |
| 171 |
style={ |
| 172 |
style || order ? { ...style, ...setBreakpointCssVars(order, flexToken.l_flex_item_Order.name) } : undefined |
| 173 |
} |
| 174 |
> |
| 175 |
{children} |
| 176 |
</Component> |
| 177 |
); |
| 178 |
}; |
| 179 |
FlexItem.displayName = 'FlexItem'; |