Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

5.0 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

"use client";

import * as PopoverPrimitive from "@radix-ui/react-popover";
import { Slot } from "@radix-ui/react-slot";
import type { ComponentPropsWithoutRef, ElementRef } from "react";
import { forwardRef } from "react";
import { ActionPresentationBoundary } from "./ActionPresentation";
import {
  BottomSheet,
  BottomSheetClose,
  BottomSheetTrigger
} from "./BottomSheet";
import { PopupSheetContent } from "./PopupSheet";
import { cn } from "./utils/cn";
import { useViewport } from "./Viewport";

/*
 * Phones show a popover as a real bottom sheet (a Radix Dialog): each part
 * switches to its Dialog counterpart, so call sites stay unchanged.
 */
const Popover = ({
  open,
  defaultOpen,
  onOpenChange,
  modal,
  children
}: ComponentPropsWithoutRef<typeof PopoverPrimitive.Root>) => {
  const { isPhone } = useViewport();
  return isPhone ? (
    <BottomSheet
      open={open}
      defaultOpen={defaultOpen}
      onOpenChange={onOpenChange}
    >
      {children}
    </BottomSheet>
  ) : (
    <PopoverPrimitive.Root
      open={open}
      defaultOpen={defaultOpen}
      onOpenChange={onOpenChange}
      modal={modal}
    >
      {children}
    </PopoverPrimitive.Root>
  );
};

const PopoverTrigger = forwardRef<
  ElementRef<typeof PopoverPrimitive.Trigger>,
  ComponentPropsWithoutRef<typeof PopoverPrimitive.Trigger>
>((props, ref) => {
  const { isPhone } = useViewport();
  return isPhone ? (
    <BottomSheetTrigger ref={ref} {...props} />
  ) : (
    <PopoverPrimitive.Trigger ref={ref} {...props} />
  );
});
PopoverTrigger.displayName = PopoverPrimitive.Trigger.displayName;

/** Phones: a sheet has no anchor, so this renders its element plainly. */
const PopoverAnchor = forwardRef<
  ElementRef<typeof PopoverPrimitive.Anchor>,
  ComponentPropsWithoutRef<typeof PopoverPrimitive.Anchor>
>(({ asChild, ...props }, ref) => {
  const { isPhone } = useViewport();
  if (!isPhone) {
    return <PopoverPrimitive.Anchor ref={ref} asChild={asChild} {...props} />;
  }
  const Comp = asChild ? Slot : "div";
  return <Comp ref={ref} {...props} />;
});
PopoverAnchor.displayName = PopoverPrimitive.Anchor.displayName;

const PopoverContent = forwardRef<
  ElementRef<typeof PopoverPrimitive.Content>,
  ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
>(
  (
    { className, align = "center", sideOffset = 4, children, ...props },
    ref
  ) => {
    const { isPhone } = useViewport();
    if (isPhone) {
      return (
        <PopupSheetContent
          ref={ref}
          label={props["aria-label"]}
          className={className}
          {...props}
        >
          {children}
        </PopupSheetContent>
      );
    }
    return (
      <PopoverPrimitive.Portal>
        <PopoverPrimitive.Content
          ref={ref}
          align={align}
          sideOffset={sideOffset}
          className={cn(
            "z-50 origin-(--radix-popover-content-transform-origin) rounded-md border border-border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
            className
          )}
          {...props}
        >
          <ActionPresentationBoundary>{children}</ActionPresentationBoundary>
        </PopoverPrimitive.Content>
      </PopoverPrimitive.Portal>
    );
  }
);
PopoverContent.displayName = PopoverPrimitive.Content.displayName;

const PopoverHeader = forwardRef<
  ElementRef<"div">,
  ComponentPropsWithoutRef<"div">
>(({ className, ...props }, ref) => (
  <div
    ref={ref}
    className={cn(
      "-mx-4 -mt-4 mb-4 px-4 py-2 border-b border-border text-sm font-medium text-foreground",
      className
    )}
    {...props}
  />
));
PopoverHeader.displayName = "PopoverHeader";

const PopoverFooter = forwardRef<
  ElementRef<"div">,
  ComponentPropsWithoutRef<"div">
>(({ className, ...props }, ref) => (
  <div
    ref={ref}
    className={cn(
      "-mx-4 -mb-4 mt-4 px-4 py-2 border-t border-border text-sm font-medium text-foreground",
      className
    )}
    {...props}
  />
));
PopoverFooter.displayName = "PopoverFooter";

const PopoverClose = forwardRef<
  ElementRef<typeof PopoverPrimitive.Close>,
  ComponentPropsWithoutRef<typeof PopoverPrimitive.Close>
>((props, ref) => {
  const { isPhone } = useViewport();
  return isPhone ? (
    <BottomSheetClose ref={ref} {...props} />
  ) : (
    <PopoverPrimitive.Close ref={ref} {...props} />
  );
});
PopoverClose.displayName = PopoverPrimitive.Close.displayName;

export {
  Popover,
  PopoverAnchor,
  PopoverClose,
  PopoverContent,
  PopoverFooter,
  PopoverHeader,
  PopoverTrigger
};