| 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 type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 11 | import {Button, buttonStyles} from './Button'; |
| b69ab31 | | | 12 | import {Icon} from './Icon'; |
| b69ab31 | | | 13 | import {colors, spacing} from './theme/tokens.stylex'; |
| b69ab31 | | | 14 | import {Tooltip, type TooltipProps} from './Tooltip'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | export const styles = stylex.create({ |
| b69ab31 | | | 17 | container: { |
| b69ab31 | | | 18 | display: 'flex', |
| b69ab31 | | | 19 | alignItems: 'stretch', |
| b69ab31 | | | 20 | position: 'relative', |
| b69ab31 | | | 21 | '::before': { |
| b69ab31 | | | 22 | content: '', |
| b69ab31 | | | 23 | position: 'absolute', |
| b69ab31 | | | 24 | width: '100%', |
| b69ab31 | | | 25 | height: '100%', |
| b69ab31 | | | 26 | top: 0, |
| b69ab31 | | | 27 | left: 0, |
| b69ab31 | | | 28 | pointerEvents: 'none', |
| b69ab31 | | | 29 | }, |
| b69ab31 | | | 30 | }, |
| b69ab31 | | | 31 | button: { |
| b69ab31 | | | 32 | borderBottomRightRadius: 0, |
| b69ab31 | | | 33 | borderTopRightRadius: 0, |
| b69ab31 | | | 34 | }, |
| b69ab31 | | | 35 | chevron: { |
| b69ab31 | | | 36 | color: 'var(--button-secondary-foreground)', |
| b69ab31 | | | 37 | position: 'absolute', |
| b69ab31 | | | 38 | top: 'calc(50% - 0.5em)', |
| b69ab31 | | | 39 | right: 'calc(var(--halfpad) - 1px)', |
| b69ab31 | | | 40 | pointerEvents: 'none', |
| b69ab31 | | | 41 | }, |
| b69ab31 | | | 42 | select: { |
| b69ab31 | | | 43 | backgroundColor: { |
| b69ab31 | | | 44 | default: 'var(--button-secondary-background)', |
| b69ab31 | | | 45 | ':hover': 'var(--button-secondary-hover-background)', |
| b69ab31 | | | 46 | }, |
| b69ab31 | | | 47 | color: 'var(--button-secondary-foreground)', |
| b69ab31 | | | 48 | opacity: { |
| b69ab31 | | | 49 | default: 1, |
| b69ab31 | | | 50 | ':disabled': 0.5, |
| b69ab31 | | | 51 | }, |
| b69ab31 | | | 52 | cursor: { |
| b69ab31 | | | 53 | default: 'pointer', |
| b69ab31 | | | 54 | ':disabled': 'not-allowed', |
| b69ab31 | | | 55 | }, |
| b69ab31 | | | 56 | width: '24px', |
| b69ab31 | | | 57 | borderRadius: '0px 2px 2px 0px', |
| b69ab31 | | | 58 | outline: { |
| b69ab31 | | | 59 | default: 'none', |
| b69ab31 | | | 60 | ':focus': '1px solid var(--focus-border)', |
| b69ab31 | | | 61 | }, |
| b69ab31 | | | 62 | outlineOffset: '2px', |
| b69ab31 | | | 63 | verticalAlign: 'bottom', |
| b69ab31 | | | 64 | appearance: 'none', |
| b69ab31 | | | 65 | lineHeight: '0', |
| b69ab31 | | | 66 | border: '1px solid var(--button-border)', |
| b69ab31 | | | 67 | borderLeft: '1px solid var(--button-secondary-foreground)', |
| b69ab31 | | | 68 | }, |
| b69ab31 | | | 69 | builtinButtonBorder: { |
| b69ab31 | | | 70 | borderLeft: 'unset', |
| b69ab31 | | | 71 | }, |
| b69ab31 | | | 72 | iconButton: { |
| b69ab31 | | | 73 | borderTopRightRadius: 0, |
| b69ab31 | | | 74 | borderBottomRightRadius: 0, |
| b69ab31 | | | 75 | paddingRight: spacing.half, |
| b69ab31 | | | 76 | }, |
| b69ab31 | | | 77 | iconSelect: { |
| b69ab31 | | | 78 | borderTopLeftRadius: 0, |
| b69ab31 | | | 79 | borderBottomLeftRadius: 0, |
| b69ab31 | | | 80 | borderLeftColor: colors.hoverDarken, |
| b69ab31 | | | 81 | }, |
| b69ab31 | | | 82 | iconChevron: { |
| b69ab31 | | | 83 | color: 'var(--button-icon-foreground)', |
| b69ab31 | | | 84 | }, |
| b69ab31 | | | 85 | chevronDisabled: { |
| b69ab31 | | | 86 | opacity: 0.5, |
| b69ab31 | | | 87 | }, |
| b69ab31 | | | 88 | }); |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | export function ButtonDropdown<T extends {label: ReactNode; id: string}>({ |
| b69ab31 | | | 91 | options, |
| b69ab31 | | | 92 | kind, |
| b69ab31 | | | 93 | onClick, |
| b69ab31 | | | 94 | selected, |
| b69ab31 | | | 95 | onChangeSelected, |
| b69ab31 | | | 96 | buttonDisabled, |
| b69ab31 | | | 97 | pickerDisabled, |
| b69ab31 | | | 98 | icon, |
| b69ab31 | | | 99 | customSelectComponent, |
| b69ab31 | | | 100 | primaryTooltip, |
| b69ab31 | | | 101 | ...rest |
| b69ab31 | | | 102 | }: { |
| b69ab31 | | | 103 | options: ReadonlyArray<T>; |
| b69ab31 | | | 104 | kind?: 'primary' | 'icon' | undefined; |
| b69ab31 | | | 105 | onClick: (selected: T, event: React.MouseEvent<HTMLButtonElement>) => unknown; |
| b69ab31 | | | 106 | selected: T; |
| b69ab31 | | | 107 | onChangeSelected: (newSelected: T) => unknown; |
| b69ab31 | | | 108 | buttonDisabled?: boolean; |
| b69ab31 | | | 109 | pickerDisabled?: boolean; |
| b69ab31 | | | 110 | /** Icon to place in the button */ |
| b69ab31 | | | 111 | icon?: React.ReactNode; |
| b69ab31 | | | 112 | customSelectComponent?: React.ReactNode; |
| b69ab31 | | | 113 | primaryTooltip?: TooltipProps; |
| b69ab31 | | | 114 | 'data-testId'?: string; |
| b69ab31 | | | 115 | }) { |
| b69ab31 | | | 116 | const selectedOption = options.find(opt => opt.id === selected.id) ?? options[0]; |
| b69ab31 | | | 117 | // const themeName = useAtomValue(themeNameState); // TODO |
| b69ab31 | | | 118 | // // Slightly hacky: in these themes, the border is too strong. Use the button border instead. |
| b69ab31 | | | 119 | // const useBuiltinBorder = ['Default Light Modern', 'Default Dark Modern'].includes( |
| b69ab31 | | | 120 | // themeName as string, |
| b69ab31 | | | 121 | // ); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | const buttonComponent = ( |
| b69ab31 | | | 124 | <Button |
| b69ab31 | | | 125 | kind={kind} |
| b69ab31 | | | 126 | onClick={buttonDisabled ? undefined : e => onClick(selected, e)} |
| b69ab31 | | | 127 | disabled={buttonDisabled} |
| b69ab31 | | | 128 | xstyle={[styles.button, kind === 'icon' && styles.iconButton]} |
| b69ab31 | | | 129 | {...rest}> |
| b69ab31 | | | 130 | {icon ?? null} {selected.label} |
| b69ab31 | | | 131 | </Button> |
| b69ab31 | | | 132 | ); |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | return ( |
| b69ab31 | | | 135 | <div {...stylex.props(styles.container)}> |
| b69ab31 | | | 136 | {primaryTooltip ? <Tooltip {...primaryTooltip}>{buttonComponent}</Tooltip> : buttonComponent} |
| b69ab31 | | | 137 | {customSelectComponent ?? ( |
| b69ab31 | | | 138 | <select |
| b69ab31 | | | 139 | {...stylex.props( |
| b69ab31 | | | 140 | styles.select, |
| b69ab31 | | | 141 | kind === 'icon' && buttonStyles.icon, |
| b69ab31 | | | 142 | kind === 'icon' && styles.iconSelect, |
| b69ab31 | | | 143 | // useBuiltinBorder && styles.builtinButtonBorder, |
| b69ab31 | | | 144 | )} |
| b69ab31 | | | 145 | disabled={pickerDisabled} |
| b69ab31 | | | 146 | value={selectedOption.id} |
| b69ab31 | | | 147 | onClick={e => e.stopPropagation()} |
| b69ab31 | | | 148 | onChange={event => { |
| b69ab31 | | | 149 | const matching = options.find(opt => opt.id === (event.target.value as T['id'])); |
| b69ab31 | | | 150 | if (matching != null) { |
| b69ab31 | | | 151 | onChangeSelected(matching); |
| b69ab31 | | | 152 | } |
| b69ab31 | | | 153 | }}> |
| b69ab31 | | | 154 | {options.map(option => ( |
| b69ab31 | | | 155 | <option key={option.id} value={option.id}> |
| b69ab31 | | | 156 | {option.label} |
| b69ab31 | | | 157 | </option> |
| b69ab31 | | | 158 | ))} |
| b69ab31 | | | 159 | </select> |
| b69ab31 | | | 160 | )} |
| b69ab31 | | | 161 | <Icon |
| b69ab31 | | | 162 | icon="chevron-down" |
| b69ab31 | | | 163 | {...stylex.props( |
| b69ab31 | | | 164 | styles.chevron, |
| b69ab31 | | | 165 | kind === 'icon' && styles.iconChevron, |
| b69ab31 | | | 166 | pickerDisabled && styles.chevronDisabled, |
| b69ab31 | | | 167 | )} |
| b69ab31 | | | 168 | /> |
| b69ab31 | | | 169 | </div> |
| b69ab31 | | | 170 | ); |
| b69ab31 | | | 171 | } |