addons/components/Flex.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {ReactProps} from './utils';
b69ab319
b69ab3110import * as stylex from '@stylexjs/stylex';
b69ab3111import {spacing} from './theme/tokens.stylex';
b69ab3112
b69ab3113type ContainerProps = ReactProps<HTMLDivElement> & {xstyle?: stylex.StyleXStyles};
b69ab3114
b69ab3115const styles = stylex.create({
b69ab3116 center: {
b69ab3117 display: 'flex',
b69ab3118 width: '100%',
b69ab3119 height: '100%',
b69ab3120 alignItems: 'center',
b69ab3121 justifyContent: 'center',
b69ab3122 },
b69ab3123 column: {
b69ab3124 flexDirection: 'column',
b69ab3125 alignItems: 'flex-start',
b69ab3126 },
b69ab3127 row: {
b69ab3128 flexDirection: 'row',
b69ab3129 alignItems: 'center',
b69ab3130 },
b69ab3131 flex: {
b69ab3132 display: 'flex',
b69ab3133 gap: spacing.pad,
b69ab3134 },
b69ab3135 spacer: {
b69ab3136 flexGrow: 1,
b69ab3137 },
b69ab3138 alignStart: {
b69ab3139 alignItems: 'flex-start',
b69ab3140 },
b69ab3141 alignCenter: {
b69ab3142 alignItems: 'center',
b69ab3143 },
b69ab3144 alignCustom: alignItems => ({
b69ab3145 alignItems,
b69ab3146 }),
b69ab3147});
b69ab3148
b69ab3149export type ColumnAlignmentProps =
b69ab3150 | {alignStart: true; alignCenter?: undefined | false; alignItems?: undefined}
b69ab3151 | {alignStart?: undefined | false; alignCenter?: true; alignItems?: undefined}
b69ab3152 | {
b69ab3153 alignStart?: undefined | false;
b69ab3154 alignCenter?: undefined | false;
b69ab3155 alignItems: 'stretch' | 'normal' | 'end';
b69ab3156 };
b69ab3157
b69ab3158/** Vertical flex layout */
b69ab3159export function Column(props: ContainerProps & ColumnAlignmentProps) {
b69ab3160 const {xstyle, alignStart, alignCenter, alignItems, className, ...rest} = props;
b69ab3161
b69ab3162 const {className: stylexClassName, ...otherStylex} = stylex.props(
b69ab3163 styles.flex,
b69ab3164 styles.column,
b69ab3165 xstyle,
b69ab3166 alignStart && styles.alignStart,
b69ab3167 alignCenter && styles.alignCenter,
b69ab3168 alignItems && styles.alignCustom(alignItems),
b69ab3169 );
b69ab3170 return (
b69ab3171 <div
b69ab3172 {...rest}
b69ab3173 className={stylexClassName + (className ? ' ' + className : '')}
b69ab3174 {...otherStylex}
b69ab3175 />
b69ab3176 );
b69ab3177}
b69ab3178
b69ab3179/** Horizontal flex layout */
b69ab3180export function Row(props: ContainerProps) {
b69ab3181 const {xstyle, ...rest} = props;
b69ab3182 return <div {...rest} {...stylex.props(styles.flex, styles.row, xstyle)} />;
b69ab3183}
b69ab3184
b69ab3185/** Visually empty flex item with `flex-grow: 1` to insert as much space as possible between siblings. */
b69ab3186export function FlexSpacer() {
b69ab3187 return <div {...stylex.props(styles.spacer)} />;
b69ab3188}