| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {ReactProps} from './utils'; |
| 9 | |
| 10 | import * as stylex from '@stylexjs/stylex'; |
| 11 | import {spacing} from './theme/tokens.stylex'; |
| 12 | |
| 13 | type ContainerProps = ReactProps<HTMLDivElement> & {xstyle?: stylex.StyleXStyles}; |
| 14 | |
| 15 | const styles = stylex.create({ |
| 16 | center: { |
| 17 | display: 'flex', |
| 18 | width: '100%', |
| 19 | height: '100%', |
| 20 | alignItems: 'center', |
| 21 | justifyContent: 'center', |
| 22 | }, |
| 23 | column: { |
| 24 | flexDirection: 'column', |
| 25 | alignItems: 'flex-start', |
| 26 | }, |
| 27 | row: { |
| 28 | flexDirection: 'row', |
| 29 | alignItems: 'center', |
| 30 | }, |
| 31 | flex: { |
| 32 | display: 'flex', |
| 33 | gap: spacing.pad, |
| 34 | }, |
| 35 | spacer: { |
| 36 | flexGrow: 1, |
| 37 | }, |
| 38 | alignStart: { |
| 39 | alignItems: 'flex-start', |
| 40 | }, |
| 41 | alignCenter: { |
| 42 | alignItems: 'center', |
| 43 | }, |
| 44 | alignCustom: alignItems => ({ |
| 45 | alignItems, |
| 46 | }), |
| 47 | }); |
| 48 | |
| 49 | export type ColumnAlignmentProps = |
| 50 | | {alignStart: true; alignCenter?: undefined | false; alignItems?: undefined} |
| 51 | | {alignStart?: undefined | false; alignCenter?: true; alignItems?: undefined} |
| 52 | | { |
| 53 | alignStart?: undefined | false; |
| 54 | alignCenter?: undefined | false; |
| 55 | alignItems: 'stretch' | 'normal' | 'end'; |
| 56 | }; |
| 57 | |
| 58 | /** Vertical flex layout */ |
| 59 | export function Column(props: ContainerProps & ColumnAlignmentProps) { |
| 60 | const {xstyle, alignStart, alignCenter, alignItems, className, ...rest} = props; |
| 61 | |
| 62 | const {className: stylexClassName, ...otherStylex} = stylex.props( |
| 63 | styles.flex, |
| 64 | styles.column, |
| 65 | xstyle, |
| 66 | alignStart && styles.alignStart, |
| 67 | alignCenter && styles.alignCenter, |
| 68 | alignItems && styles.alignCustom(alignItems), |
| 69 | ); |
| 70 | return ( |
| 71 | <div |
| 72 | {...rest} |
| 73 | className={stylexClassName + (className ? ' ' + className : '')} |
| 74 | {...otherStylex} |
| 75 | /> |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** Horizontal flex layout */ |
| 80 | export function Row(props: ContainerProps) { |
| 81 | const {xstyle, ...rest} = props; |
| 82 | return <div {...rest} {...stylex.props(styles.flex, styles.row, xstyle)} />; |
| 83 | } |
| 84 | |
| 85 | /** Visually empty flex item with `flex-grow: 1` to insert as much space as possible between siblings. */ |
| 86 | export function FlexSpacer() { |
| 87 | return <div {...stylex.props(styles.spacer)} />; |
| 88 | } |
| 89 | |