Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

8.6 KB
Raw Blame
import * as Primitive from "@radix-ui/react-select";
import {
  type ReactNode,
  type Ref,
  type ComponentProps,
  type JSX,
  useMemo,
  forwardRef,
  useState,
  useEffect,
  useRef,
} from "react";
import { styled, theme } from "../stitches.config";
import {
  menuCss,
  menuItemCss,
  menuItemIndicatorCss,
  labelCss,
  separatorCss,
  MenuCheckedIcon,
} from "./menu";
import { SelectButton } from "./select-button";
import { Box } from "./box";
import { ScrollArea } from "./scroll-area";
import { cssVar, declareCssVar } from "../css-var";

const selectTriggerWidth = declareCssVar("--radix-select-trigger-width");

export const SelectContent = styled(Primitive.Content, menuCss, {
  width: cssVar(selectTriggerWidth),
  '& [data-select-description$="separator"]': {
    display: "none",
  },
  "&[data-side=top]": {
    '& [data-select-description="top-separator"]': {
      display: "block",
    },
    '& [data-select-description="content"]': {
      order: 0,
    },
  },
  "&[data-side=bottom]": {
    '& [data-select-description="bottom-separator"]': {
      display: "block",
    },
    '& [data-select-description="content"]': {
      order: 2,
    },
  },
});

export const SelectViewport = Primitive.Viewport;

export const SelectLabel = styled(Primitive.Label, labelCss);

export const SelectSeparator = styled(Primitive.Separator, separatorCss);

export const SelectGroup = Primitive.Group;

const StyledItem = styled(Primitive.Item, menuItemCss);

const StyledIndicator = styled(Primitive.ItemIndicator, menuItemIndicatorCss);

const scrollButtonStyles = {
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  height: 25,
  color: cssVar("--foreground-primary"),
  cursor: "default",
};

export const SelectScrollUpButton = styled(
  Primitive.ScrollUpButton,
  scrollButtonStyles
);

export const SelectScrollDownButton = styled(
  Primitive.ScrollDownButton,
  scrollButtonStyles
);

const SelectItemBase = (
  { children, icon = <MenuCheckedIcon />, ...props }: SelectItemProps,
  forwardedRef: Ref<HTMLDivElement>
) => {
  return (
    <StyledItem {...props} withIndicator ref={forwardedRef}>
      <StyledIndicator>{icon}</StyledIndicator>
      <Primitive.ItemText>{children}</Primitive.ItemText>
    </StyledItem>
  );
};

type SelectItemProps = ComponentProps<typeof StyledItem> & {
  children: ReactNode;
  icon?: ReactNode;
};
export const SelectItem = forwardRef(SelectItemBase);

export type SelectOption = string;

const Description = styled("div", menuItemCss);

// Note this only works in combination with position: popper on Content component, because only popper exposes data-side attribute
export const SelectItemDescription = ({
  children,
  descriptions,
}: {
  children: ReactNode;
  descriptions: ReactNode[];
}) => {
  return (
    <>
      <SelectSeparator
        data-select-description="bottom-separator"
        css={{ order: 2 }}
      />

      <Description
        data-select-description="content"
        css={{
          display: "grid",
        }}
        hint
      >
        {descriptions.map((descr, index) => (
          <Box
            css={{
              gridColumn: "1",
              gridRow: "1",
              visibility: "hidden",
            }}
            key={index}
          >
            {descr}
          </Box>
        ))}
        <Box
          css={{
            gridColumn: "1",
            gridRow: "1",
          }}
        >
          {children}
        </Box>
      </Description>

      <SelectSeparator
        data-select-description="top-separator"
        css={{ order: 0 }}
      />
    </>
  );
};

type TriggerPassThroughProps = Omit<
  ComponentProps<typeof Primitive.Trigger>,
  "onChange" | "value" | "defaultValue" | "asChild" | "prefix" | "color"
> &
  Omit<ComponentProps<typeof SelectButton>, "onChange" | "value">;

export type SelectProps<Option = SelectOption> = {
  options: readonly Option[];
  defaultValue?: Option;
  value?: Option;
  onChange?: (option: Option) => void;
  onItemHighlight?: (value?: Option) => void;
  open?: boolean;
  onOpenChange?: (open: boolean) => void;
  placeholder?: string;
  children?: ReactNode;
  getDescription?: (option: Option) => ReactNode | undefined;
  getItemProps?: (
    option: Option
  ) => Omit<ComponentProps<typeof SelectItem>, "children" | "value">;
} & (Option extends string
  ? {
      getLabel?: (option: Option) => ReactNode | undefined;
      getValue?: (option: Option) => string | undefined;
    }
  : {
      getLabel: (option: Option) => ReactNode | undefined;
      getValue: (option: Option) => string | undefined;
    }) &
  TriggerPassThroughProps;

const defaultGetValue = (option: unknown) => {
  if (typeof option === "string") {
    return option;
  }
  throw new Error(
    `Cannot automatically convert ${typeof option} to string. Provide a getValue/getLabel/getDescription`
  );
};

const SelectBase = <Option,>(
  {
    options,
    value,
    defaultValue,
    placeholder = "Select an option",
    onChange,
    onOpenChange,
    onItemHighlight,
    open,
    getLabel = defaultGetValue,
    getValue = defaultGetValue,
    getDescription,
    getItemProps,
    name,
    children,
    prefix,
    color,
    ...props
  }: SelectProps<Option>,
  forwardedRef: Ref<HTMLButtonElement>
) => {
  const valueToOption = useMemo(() => {
    const map = new Map<string, Option>();
    for (const option of options) {
      map.set(getValue(option) ?? "", option);
    }
    return map;
  }, [options, getValue]);

  const [highlightedItem, setHighlightedItem] = useState<Option>();

  const itemForDescription = highlightedItem ?? value ?? defaultValue;
  const description = itemForDescription
    ? getDescription?.(itemForDescription)
    : undefined;

  const descriptions = options.map((option) => getDescription?.(option));

  // Allow reset select fix https://github.com/radix-ui/primitives/issues/2706
  const [selectResetKeyFix, setSelectResetKeyFix] = useState(0);
  const prevValue = useRef(value);

  useEffect(() => {
    if (prevValue.current !== undefined && value === undefined) {
      setSelectResetKeyFix((prev) => prev + 1);
    }

    prevValue.current = value;
  }, [value]);

  return (
    <Primitive.Root
      key={selectResetKeyFix}
      name={name}
      // null because of https://github.com/radix-ui/primitives/issues/2706
      value={value === undefined ? undefined : getValue(value)}
      defaultValue={
        defaultValue === undefined ? undefined : getValue(defaultValue)
      }
      onValueChange={(value) => {
        const option = valueToOption.get(value);
        if (option !== undefined) {
          onChange?.(option);
        }
      }}
      open={open}
      onOpenChange={onOpenChange}
    >
      <Primitive.Trigger ref={forwardedRef} {...props} asChild>
        <SelectButton prefix={prefix} color={color}>
          <Primitive.Value placeholder={placeholder} />
        </SelectButton>
      </Primitive.Trigger>
      <Primitive.Portal>
        <SelectContent position="popper">
          <Box
            css={{
              display: "flex",
              flexDirection: "column",
              maxHeight: theme.spacing[34],
              order: 1,
            }}
          >
            <ScrollArea>
              <SelectViewport>
                {children ||
                  options.map((option, index) => {
                    const value = getValue(option) ?? "";
                    const { textValue, ...rest } = getItemProps?.(option) ?? {};
                    return (
                      <SelectItem
                        key={value ?? index}
                        value={value}
                        textValue={textValue ?? value}
                        onFocus={() => {
                          onItemHighlight?.(option);
                          setHighlightedItem(option);
                        }}
                        onBlur={() => {
                          onItemHighlight?.(undefined);
                          setHighlightedItem(undefined);
                        }}
                        text="sentence"
                        {...rest}
                      >
                        {getLabel(option)}
                      </SelectItem>
                    );
                  })}
              </SelectViewport>
            </ScrollArea>
          </Box>
          {description && (
            <SelectItemDescription descriptions={descriptions}>
              {description}
            </SelectItemDescription>
          )}
        </SelectContent>
      </Primitive.Portal>
    </Primitive.Root>
  );
};

export const Select = forwardRef(SelectBase) as <Option>(
  props: SelectProps<Option> & { ref?: Ref<HTMLButtonElement> }
) => JSX.Element | null;