|
| 1 | +import type { ReactNode } from "react"; |
| 2 | + |
| 3 | +import constants from "@lib-components/defaults"; |
| 4 | + |
| 5 | +import { mergeClasses } from "@lib-theme"; |
| 6 | +import type { TThemeSpacing, TThemeShorthandSpacing } from "@lib-theme"; |
| 7 | + |
| 8 | +import { |
| 9 | + useGap, |
| 10 | + useFlexBox, |
| 11 | + useMargin, |
| 12 | + usePadding, |
| 13 | + useShorthandDimension, |
| 14 | +} from "@lib-components/layout/Flex/hooks"; |
| 15 | + |
| 16 | +import type { |
| 17 | + TFlexDirection, |
| 18 | + TFlexOption, |
| 19 | + TFlexWrap, |
| 20 | + TFlexShorthandDimensions, |
| 21 | +} from "@lib-components/layout/Flex/types"; |
| 22 | + |
| 23 | +type TProps = { |
| 24 | + children: ReactNode; |
| 25 | + direction?: TFlexDirection; |
| 26 | + justifyContent?: TFlexOption; |
| 27 | + alignItems?: TFlexOption; |
| 28 | + wrap?: TFlexWrap; |
| 29 | + className?: string; |
| 30 | + gap?: TThemeSpacing; |
| 31 | + margin?: TThemeShorthandSpacing; |
| 32 | + padding?: TThemeShorthandSpacing; |
| 33 | + testId?: string; |
| 34 | + shWidth?: TFlexShorthandDimensions; |
| 35 | + shHeight?: TFlexShorthandDimensions; |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * @description |
| 40 | + * - fluent does not provide a `Flex` component for consistent layout (it was removed in the latest version) |
| 41 | + * - to have a straight forward implementation of `Flex` component, we have created this layout component |
| 42 | + * - having this allows to use fewer makeStyles call and repeting flex configurations in the code |
| 43 | + * - its especially usefull when certain layout styles have to be applied conditionally |
| 44 | + * - for this the entire conditional logic is abstracted inside this component, providing very much styled-component like ergonomics |
| 45 | + * |
| 46 | + * |
| 47 | + * @props |
| 48 | + * - `direction`: flex-direction property |
| 49 | + * - `justifyContent`: justify-content property |
| 50 | + * - `alignItems`: align-items property |
| 51 | + * - `wrap`: flex-wrap property |
| 52 | + * - `className`: to add additional classes to the component, will override all specified styles from props |
| 53 | + * - `gap`: gap between children, with fixed predefined values from the design system, not discriminating between horizontal and vertical gap (because there are literally the same values) |
| 54 | + * - `margin`: margin property, using the same values like gap, expects the shorthand notation |
| 55 | + * - `padding`: same like margin, but for padding |
| 56 | + * |
| 57 | + * ``` |
| 58 | + * // the shorthand is not a simple string, but rather defined as an array that can be of size 1 up to 4 |
| 59 | + * // each element provides additional restraint from the design system tokens |
| 60 | + * // following examples will only use padding, but the same applies to margin |
| 61 | + * // the * between tokens.spacing and the further specifier can be interpreted as either horizontal or vertical (both the same values, reasoning is explained in gap comment above) |
| 62 | + * |
| 63 | + * <Flex padding={["S"]} /> // like saying padding: tokens.spacing*S; |
| 64 | + * <Flex padding={["S", "M"]} /> // like saying padding: `${tokens.spacing*S} ${tokens.spacing*M}`; |
| 65 | + * <Flex padding={["S", "M", "L"]} /> // like saying padding: `${tokens.spacing*S} ${tokens.spacing*M} ${tokens.spacing*L}`; |
| 66 | + * <Flex padding={["S", "M", "L", "XL"]} /> // like saying padding: `${tokens.spacing*S} ${tokens.spacing*M} ${tokens.spacing*L} ${tokens.spacing*XL}`; |
| 67 | + * |
| 68 | + * ``` |
| 69 | + * - `testId`: passed down the data-testid attribute |
| 70 | + * - `shWidth`: shorthand for width property |
| 71 | + * - `shHeight`: shorthand for height property |
| 72 | + * |
| 73 | + * @default |
| 74 | + * direction = "row", justifyContent = "start", alignItems = "start", wrap = "nowrap", gap = "None", margin = ["None"], padding = ["None"], className = "NoClass" |
| 75 | + */ |
| 76 | +export default function Flex({ |
| 77 | + direction = "row", |
| 78 | + justifyContent = "start", |
| 79 | + alignItems = "start", |
| 80 | + wrap = "nowrap", |
| 81 | + gap = "None", |
| 82 | + margin = ["None"], |
| 83 | + padding = ["None"], |
| 84 | + shHeight = "auto", |
| 85 | + shWidth = "auto", |
| 86 | + className = constants.propsValues.undefinedClassString, |
| 87 | + testId = constants.propsValues.undefinedDataTestId, |
| 88 | + children, |
| 89 | +}: TProps) { |
| 90 | + const flexBoxClass = useFlexBox(justifyContent, alignItems, direction, wrap); |
| 91 | + const gapClass = useGap(gap); |
| 92 | + const marginClass = useMargin(margin); |
| 93 | + const paddingClass = usePadding(padding); |
| 94 | + const dimensionClass = useShorthandDimension(shWidth, shHeight); |
| 95 | + |
| 96 | + return ( |
| 97 | + <div |
| 98 | + data-testid={testId} |
| 99 | + className={mergeClasses( |
| 100 | + flexBoxClass, |
| 101 | + gapClass, |
| 102 | + marginClass, |
| 103 | + paddingClass, |
| 104 | + dimensionClass, |
| 105 | + className, |
| 106 | + )} |
| 107 | + > |
| 108 | + {children} |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
0 commit comments