import * as VisuallyHidden from "@radix-ui/react-visually-hidden"; import { QuestionMarkIcon } from "outline-icons"; import { transparentize } from "polished"; import * as React from "react"; import styled from "styled-components"; import { s } from "@shared/styles"; import Text from "~/components/Text"; import useMobile from "~/hooks/useMobile"; import Separator from "./ContextMenu/Separator"; import Flex from "./Flex"; import { LabelText } from "./Input"; import NudeButton from "./NudeButton"; import Scrollable from "./Scrollable"; import Tooltip from "./Tooltip"; import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger, } from "./primitives/Drawer"; import { InputSelectRoot, InputSelectContent, InputSelectItem, InputSelectSeparator, InputSelectTrigger, type TriggerButtonProps, } from "./primitives/InputSelect"; import { SelectItemIndicator, SelectItem as SelectItemWrapper, SelectButton, } from "./primitives/components/InputSelect"; type Separator = { /* Denotes a horizontal divider line to be rendered in the menu, */ type: "separator"; }; export type Item = { /* Denotes a selectable option in the menu. */ type: "item"; /* Representative text shown in the menu for this option. */ label: string; /* Actual value of this option. */ value: string; /* Additional info shown alongside the label. */ description?: string; /* An icon shown alongside the label. */ icon?: React.ReactElement; }; export type Option = Item | Separator; type Props = { /* Options to display in the select menu. */ options: Option[]; /* Current chosen value. */ value?: string | null; /* Callback when an option is selected. */ onChange: (value: string) => void; /* Label for the select menu. */ label: string; /* When true, label is hidden in an accessible manner. */ hideLabel?: boolean; /* When true, menu is disabled. */ disabled?: boolean; /* When true, width of the menu trigger is restricted. Otherwise, takes up the full width of parent. */ short?: boolean; /** Display a tooltip with the descriptive help text about the select menu. */ help?: string; } & TriggerButtonProps; export const InputSelect = React.forwardRef( (props, ref) => { const { options, value, onChange, label, hideLabel, short, help, ...triggerProps } = props; const [localValue, setLocalValue] = React.useState(value); const [open, setOpen] = React.useState(false); const contentRef = React.useRef>(null); const isMobile = useMobile(); const placeholder = `Select a ${label.toLowerCase()}`; const optionsHaveIcon = options.some( (opt) => opt.type === "item" && !!opt.icon ); const renderOption = React.useCallback( (option: Option, idx: number) => { if (option.type === "separator") { return ; } return ( ); }, [optionsHaveIcon] ); const onValueChange = React.useCallback( async (val: string) => { setLocalValue(val); onChange(val); }, [onChange, setLocalValue] ); const enablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "auto"; } }, []); const disablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "none"; } }, []); React.useEffect(() => { setLocalValue(value); }, [value]); if (isMobile) { return ( ); } return ( ); } ); InputSelect.displayName = "InputSelect"; type MobileSelectProps = Props & { placeholder: string; optionsHaveIcon: boolean; }; const MobileSelect = React.forwardRef( (props, ref) => { const { options, value, onChange, label, hideLabel, disabled, short, placeholder, optionsHaveIcon, ...triggerProps } = props; const [open, setOpen] = React.useState(false); const contentRef = React.useRef>(null); const selectedOption = React.useMemo( () => value ? options.find((opt) => opt.type === "item" && opt.value === value) : undefined, [value, options] ); const handleSelect = React.useCallback( async (val: string) => { setOpen(false); onChange(val); }, [onChange] ); const renderOption = React.useCallback( (option: Option) => { if (option.type === "separator") { return ; } const isSelected = option === selectedOption; return ( handleSelect(option.value)} data-state={isSelected ? "checked" : "unchecked"} > ); }, [handleSelect, selectedOption, optionsHaveIcon] ); const enablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "auto"; } }, []); const disablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "none"; } }, []); return ( ); } ); MobileSelect.displayName = "InputSelect"; function Label({ text, hidden, help, }: { text: string; hidden: boolean; help?: string; }) { const content = ( {text} {help ? ( ) : null} ); return hidden ? ( {content} ) : ( content ); } function Option({ option, optionsHaveIcon, }: { option: Item; optionsHaveIcon: boolean; }) { const icon = optionsHaveIcon ? ( option.icon ? ( {option.icon} ) : ( ) ) : null; return ( {icon} {option.label} {option.description && ( <>   – {option.description} )} ); } const Wrapper = styled.label<{ short?: boolean }>` display: block; max-width: ${(props) => (props.short ? "350px" : "100%")}; `; const OptionContainer = styled(Flex)` min-height: 24px; `; const Description = styled(Text)` @media (hover: hover) { &:hover, &:focus { color: ${(props) => transparentize(0.5, props.theme.accentText)}; } } `; const IconWrapper = styled.span` display: flex; justify-content: center; align-items: center; width: 24px; height: 24px; margin-left: -4px; margin-right: 4px; overflow: hidden; flex-shrink: 0; `; const IconSpacer = styled.div` width: 24px; height: 24px; flex-shrink: 0; `; const StyledScrollable = styled(Scrollable)` max-height: 75vh; `; const TooltipButton = styled(NudeButton)` color: ${s("textSecondary")}; &:hover, &[aria-expanded="true"] { background: none !important; } `;