import * as InputSelectPrimitive from "@radix-ui/react-select"; import * as React from "react"; import styled from "styled-components"; import Text from "@shared/components/Text"; import { depths, s, borderRadius } from "@shared/styles"; import type { Props as ButtonProps } from "~/components/Button"; import { fadeAndSlideDown, fadeAndSlideUp } from "~/styles/animations"; import { SelectItemIndicator, SelectItem as SelectItemWrapper, SelectButton, } from "./components/InputSelect"; /** Root InputSelect component - all the other components are rendered inside it. */ const InputSelectRoot = InputSelectPrimitive.Root; /** InputSelect's trigger. */ export type TriggerButtonProps = { /** When true, "nude" variant of Button is rendered. */ nude?: boolean; /** Optional css class names to pass to the trigger. */ className?: string; } & Pick, "borderOnHover">; type InputSelectTriggerProps = { placeholder: string; /** When provided, overrides the selected value rendered inside the trigger. */ displayValue?: React.ReactNode; ref?: React.Ref>; } & TriggerButtonProps & React.ComponentPropsWithoutRef; function InputSelectTrigger(props: InputSelectTriggerProps) { const { ref, placeholder, children, nude, displayValue, ...buttonProps } = props; return ( {displayValue !== undefined ? ( <>{displayValue} ) : ( )} ); } InputSelectTrigger.displayName = InputSelectPrimitive.Trigger.displayName; /** InputSelect's content - renders the options in a scrollable element. */ type ContentProps = Omit< React.ComponentPropsWithoutRef, "position" > & { ref?: React.Ref>; }; function InputSelectContent(props: ContentProps) { const { ref, children, ...rest } = props; return ( {children} ); } InputSelectContent.displayName = InputSelectPrimitive.Content.displayName; /** Individual InputSelect option rendered in the menu. */ function InputSelectItem( props: React.ComponentPropsWithoutRef & { ref?: React.Ref>; } ) { const { ref, children, ...rest } = props; return ( {children} ); } InputSelectItem.displayName = InputSelectPrimitive.Item.displayName; /** Horizontal separator rendered between the options. */ function InputSelectSeparator({ ref, ...props }: React.ComponentPropsWithoutRef & { ref?: React.Ref>; }) { return ( ); } InputSelectSeparator.displayName = InputSelectPrimitive.Separator.displayName; const Separator = styled.hr` margin: 6px 0; `; /** Non-selectable heading rendered to group options in the menu. */ function InputSelectHeading({ ref, children, }: { children?: React.ReactNode; ref?: React.Ref; }) { return ( {children} ); } InputSelectHeading.displayName = "InputSelectHeading"; const Heading = styled(Text).attrs({ type: "tertiary", size: "xsmall", weight: "bold", })` display: block; padding-block: 8px 4px; padding-inline: 8px; text-transform: uppercase; letter-spacing: 0.04em; `; /** Styled components. */ const StyledContent = styled(InputSelectPrimitive.Content)` z-index: ${depths.menu}; min-width: var(--radix-select-trigger-width); max-width: 400px; min-height: 44px; max-height: 350px; padding: 4px; ${borderRadius(8)} background: ${s("menuBackground")}; box-shadow: ${s("menuShadow")}; transform-origin: 50% 0; &[data-side="bottom"] { animation: ${fadeAndSlideDown} 200ms ease; } &[data-side="top"] { animation: ${fadeAndSlideUp} 200ms ease; } @media print { display: none; } `; export { InputSelectRoot, InputSelectTrigger, InputSelectContent, InputSelectItem, InputSelectSeparator, InputSelectHeading, };