| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 9 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 10 | import {notEmpty} from 'shared/utils'; |
| b69ab31 | | | 11 | import {spacing} from '../../components/theme/tokens.stylex'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import './ComponentUtils.css'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | const styles = stylex.create({ |
| b69ab31 | | | 16 | center: { |
| b69ab31 | | | 17 | display: 'flex', |
| b69ab31 | | | 18 | width: '100%', |
| b69ab31 | | | 19 | height: '100%', |
| b69ab31 | | | 20 | alignItems: 'center', |
| b69ab31 | | | 21 | justifyContent: 'center', |
| b69ab31 | | | 22 | }, |
| b69ab31 | | | 23 | flex: { |
| b69ab31 | | | 24 | display: 'flex', |
| b69ab31 | | | 25 | alignItems: 'center', |
| b69ab31 | | | 26 | gap: spacing.pad, |
| b69ab31 | | | 27 | }, |
| b69ab31 | | | 28 | spacer: { |
| b69ab31 | | | 29 | flexGrow: 1, |
| b69ab31 | | | 30 | }, |
| b69ab31 | | | 31 | alignStart: { |
| b69ab31 | | | 32 | alignItems: 'flex-start', |
| b69ab31 | | | 33 | }, |
| b69ab31 | | | 34 | }); |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | export type ReactProps<T extends HTMLElement> = React.DetailedHTMLProps<React.HTMLAttributes<T>, T>; |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | export function LargeSpinner() { |
| b69ab31 | | | 39 | return ( |
| b69ab31 | | | 40 | <div data-testid="loading-spinner"> |
| b69ab31 | | | 41 | <Icon icon="loading" size="L" /> |
| b69ab31 | | | 42 | </div> |
| b69ab31 | | | 43 | ); |
| b69ab31 | | | 44 | } |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | export function Center(props: ContainerProps) { |
| b69ab31 | | | 47 | const {className, xstyle, ...rest} = props; |
| b69ab31 | | | 48 | return ( |
| b69ab31 | | | 49 | <div |
| b69ab31 | | | 50 | {...stylexPropsWithClassName([styles.center, xstyle].filter(notEmpty), className)} |
| b69ab31 | | | 51 | {...rest} |
| b69ab31 | | | 52 | /> |
| b69ab31 | | | 53 | ); |
| b69ab31 | | | 54 | } |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | /** Flexbox container with horizontal children. */ |
| b69ab31 | | | 57 | export function Row(props: ContainerProps) { |
| b69ab31 | | | 58 | return FlexBox(props, 'row'); |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | /** Flexbox container with vertical children. */ |
| b69ab31 | | | 62 | export function Column(props: ContainerProps) { |
| b69ab31 | | | 63 | return FlexBox(props, 'column'); |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | /** Container that scrolls horizontally. */ |
| b69ab31 | | | 67 | export function ScrollX(props: ScrollProps) { |
| b69ab31 | | | 68 | return Scroll({...props, direction: 'x'}); |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | /** Container that scrolls vertically. */ |
| b69ab31 | | | 72 | export function ScrollY(props: ScrollProps) { |
| b69ab31 | | | 73 | return Scroll({...props, direction: 'y'}); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | /** Visually empty flex item with `flex-grow: 1` to insert as much space as possible between siblings. */ |
| b69ab31 | | | 77 | export function FlexSpacer() { |
| b69ab31 | | | 78 | return <div {...stylex.props(styles.spacer)} />; |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | type ContainerProps = ReactProps<HTMLDivElement> & {xstyle?: stylex.StyleXStyles} & { |
| b69ab31 | | | 82 | /** If true, use alignItems: flex-start instead of centering */ |
| b69ab31 | | | 83 | alignStart?: boolean; |
| b69ab31 | | | 84 | }; |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | /** See `<Row>` and `<Column>`. */ |
| b69ab31 | | | 87 | function FlexBox(props: ContainerProps, flexDirection: 'row' | 'column') { |
| b69ab31 | | | 88 | const {className, style, alignStart, xstyle, ...rest} = props; |
| b69ab31 | | | 89 | return ( |
| b69ab31 | | | 90 | <div |
| b69ab31 | | | 91 | {...stylexPropsWithClassName( |
| b69ab31 | | | 92 | [styles.flex, alignStart && styles.alignStart, xstyle].filter(notEmpty), |
| b69ab31 | | | 93 | className, |
| b69ab31 | | | 94 | )} |
| b69ab31 | | | 95 | {...rest} |
| b69ab31 | | | 96 | style={{flexDirection, ...style}} |
| b69ab31 | | | 97 | /> |
| b69ab31 | | | 98 | ); |
| b69ab31 | | | 99 | } |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | type ScrollProps = ContainerProps & { |
| b69ab31 | | | 102 | /** Scroll direction. */ |
| b69ab31 | | | 103 | direction?: 'x' | 'y'; |
| b69ab31 | | | 104 | /** maxHeight or maxWidth depending on scroll direction. */ |
| b69ab31 | | | 105 | maxSize?: string | number; |
| b69ab31 | | | 106 | /** height or width depending on scroll direction. */ |
| b69ab31 | | | 107 | size?: string | number; |
| b69ab31 | | | 108 | /** Whether to hide the scroll bar. */ |
| b69ab31 | | | 109 | hideBar?: boolean; |
| b69ab31 | | | 110 | /** On-scroll event handler. */ |
| b69ab31 | | | 111 | onScroll?: React.UIEventHandler; |
| b69ab31 | | | 112 | }; |
| b69ab31 | | | 113 | |
| b69ab31 | | | 114 | /** See <ScrollX> and <ScrollY> */ |
| b69ab31 | | | 115 | function Scroll(props: ScrollProps) { |
| b69ab31 | | | 116 | let className = props.className ?? ''; |
| b69ab31 | | | 117 | const direction = props.direction ?? 'x'; |
| b69ab31 | | | 118 | const hideBar = props.hideBar ?? false; |
| b69ab31 | | | 119 | const style: React.CSSProperties = {}; |
| b69ab31 | | | 120 | if (direction === 'x') { |
| b69ab31 | | | 121 | style.overflowX = 'auto'; |
| b69ab31 | | | 122 | style.maxWidth = props.maxSize ?? '100%'; |
| b69ab31 | | | 123 | if (props.size != null) { |
| b69ab31 | | | 124 | style.width = props.size; |
| b69ab31 | | | 125 | } |
| b69ab31 | | | 126 | } else { |
| b69ab31 | | | 127 | style.overflowY = 'auto'; |
| b69ab31 | | | 128 | style.maxHeight = props.maxSize ?? '100%'; |
| b69ab31 | | | 129 | if (props.size != null) { |
| b69ab31 | | | 130 | style.height = props.size; |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | if (hideBar) { |
| b69ab31 | | | 134 | style.scrollbarWidth = 'none'; |
| b69ab31 | | | 135 | className += ' hide-scrollbar'; |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | const mergedProps = {...props, className, style: {...style, ...props.style}}; |
| b69ab31 | | | 139 | delete mergedProps.children; |
| b69ab31 | | | 140 | delete mergedProps.maxSize; |
| b69ab31 | | | 141 | delete mergedProps.hideBar; |
| b69ab31 | | | 142 | delete mergedProps.direction; |
| b69ab31 | | | 143 | |
| b69ab31 | | | 144 | // The outer <div> seems to avoid issues where |
| b69ab31 | | | 145 | // the other direction of scrollbar gets used. |
| b69ab31 | | | 146 | // See https://pxl.cl/3bvWh for the difference. |
| b69ab31 | | | 147 | // I don't fully understand how this works exactly. |
| b69ab31 | | | 148 | // See also https://stackoverflow.com/a/6433475. |
| b69ab31 | | | 149 | return ( |
| b69ab31 | | | 150 | <div style={{overflow: 'visible'}}> |
| b69ab31 | | | 151 | <div {...mergedProps}>{props.children}</div> |
| b69ab31 | | | 152 | </div> |
| b69ab31 | | | 153 | ); |
| b69ab31 | | | 154 | } |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | /** |
| b69ab31 | | | 157 | * Like stylex.props(), but also adds in extra classNames. |
| b69ab31 | | | 158 | * Useful since `{...stylex.props()}` sets className, |
| b69ab31 | | | 159 | * and either overwrites or is overwritten by other `className="..."` props. |
| b69ab31 | | | 160 | */ |
| b69ab31 | | | 161 | export function stylexPropsWithClassName( |
| b69ab31 | | | 162 | style: stylex.StyleXStyles, |
| b69ab31 | | | 163 | ...names: Array<string | undefined> |
| b69ab31 | | | 164 | ) { |
| b69ab31 | | | 165 | const {className, ...rest} = stylex.props(style); |
| b69ab31 | | | 166 | return {...rest, className: className + ' ' + names.filter(notEmpty).join(' ')}; |
| b69ab31 | | | 167 | } |