Skip to content

Open source CapCut alternative.

TypeScript

fix(site): modals fit the window at any size

David Le21d87f808bd1
diff --git a/site/src/app/su/outreach/ComposeDialog.tsx b/site/src/app/su/outreach/ComposeDialog.tsx
index b5e372da..de659b49 100644
--- a/site/src/app/su/outreach/ComposeDialog.tsx
+++ b/site/src/app/su/outreach/ComposeDialog.tsx
@@ -390,7 +390,7 @@ export function OutreachComposeDialog({
}}
open={person !== null}
>
- <DialogContent className="grid h-[85vh] grid-rows-[auto_minmax(0,1fr)_auto] sm:max-w-5xl">
+ <DialogContent className="grid h-[85dvh] grid-rows-[auto_minmax(0,1fr)_auto] sm:max-w-5xl">
<DialogHeader>
<DialogTitle>Email {target?.name ?? opening?.name}</DialogTitle>
<DialogDescription>{target?.email ?? opening?.email}</DialogDescription>
diff --git a/site/src/components/ui/alert-dialog.tsx b/site/src/components/ui/alert-dialog.tsx
index 0ee2c5f4..98b3df8f 100644
--- a/site/src/components/ui/alert-dialog.tsx
+++ b/site/src/components/ui/alert-dialog.tsx
@@ -5,6 +5,11 @@ import { AlertDialog as AlertDialogPrimitive } from "@base-ui/react/alert-dialog
import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
+import {
+ ModalPositioner,
+ modalPopupClasses,
+ withScrollingMiddle,
+} from "@/components/ui/dialog"
function AlertDialog({ ...props }: AlertDialogPrimitive.Root.Props) {
return <AlertDialogPrimitive.Root data-slot="alert-dialog" {...props} />
@@ -40,6 +45,7 @@ function AlertDialogOverlay({
function AlertDialogContent({
className,
+ children,
size = "default",
...props
}: AlertDialogPrimitive.Popup.Props & {
@@ -48,15 +54,20 @@ function AlertDialogContent({
return (
<AlertDialogPortal>
<AlertDialogOverlay />
- <AlertDialogPrimitive.Popup
- data-slot="alert-dialog-content"
- data-size={size}
- className={cn(
- "group/alert-dialog-content fixed top-1/2 left-1/2 z-50 grid w-full -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none data-[size=default]:max-w-xs data-[size=sm]:max-w-xs data-[size=default]:sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
- className
- )}
- {...props}
- />
+ <ModalPositioner>
+ <AlertDialogPrimitive.Popup
+ data-slot="alert-dialog-content"
+ data-size={size}
+ className={cn(
+ modalPopupClasses,
+ "group/alert-dialog-content flex flex-col gap-4 p-4 data-[size=default]:max-w-xs data-[size=sm]:max-w-xs data-[size=default]:sm:max-w-sm",
+ className
+ )}
+ {...props}
+ >
+ {withScrollingMiddle(children, [AlertDialogHeader, AlertDialogFooter])}
+ </AlertDialogPrimitive.Popup>
+ </ModalPositioner>
</AlertDialogPortal>
)
}
diff --git a/site/src/components/ui/dialog.tsx b/site/src/components/ui/dialog.tsx
index 712e8231..9c8d16a4 100644
--- a/site/src/components/ui/dialog.tsx
+++ b/site/src/components/ui/dialog.tsx
@@ -39,6 +39,69 @@ function DialogOverlay({
)
}
+// Where every modal hangs: a full-viewport column holding the popup, with a
+// collapsible spacer above it. The spacer holds the dialog in the top third
+// while the viewport has room and gives its height back when it runs short, so
+// the whole dialog stays on screen in a frame as short as the ChatGPT card or a
+// phone in landscape. A dialog never sets its own top.
+function ModalPositioner({
+ className,
+ children,
+ ...props
+}: React.ComponentProps<"div">) {
+ return (
+ <div
+ data-slot="modal-positioner"
+ className={cn(
+ "pointer-events-none fixed inset-0 z-50 flex flex-col items-center p-4",
+ className
+ )}
+ {...props}
+ >
+ <div aria-hidden className="w-0 shrink-[999] basis-[18dvh]" />
+ {children}
+ </div>
+ )
+}
+
+// The popup fills at most the space the positioner leaves it, and its scrolling
+// part takes up the slack. A dialog that declares no scrolling part at all
+// scrolls whole.
+const modalPopupClasses =
+ "pointer-events-auto relative max-h-full min-h-0 w-full overflow-y-auto overscroll-contain rounded-xl bg-popover text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none has-data-[slot=dialog-body]:overflow-y-visible data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95"
+
+// A dialog that never named its scrolling part still gets one: the header and
+// footer it declared stay where they are, and everything between them scrolls
+// in the space that is left. The wrapper inherits the popup's column and gap,
+// so the dialog lays out exactly as it did with the room it has.
+function withScrollingMiddle(
+ children: React.ReactNode,
+ chromeTypes: React.ElementType[]
+) {
+ const nodes = React.Children.toArray(children)
+ const isType = (node: React.ReactNode, type: React.ElementType) =>
+ React.isValidElement(node) && node.type === type
+ if (nodes.some((node) => isType(node, DialogBody))) return children
+ const chrome = (node: React.ReactNode) =>
+ chromeTypes.some((type) => isType(node, type))
+ let start = 0
+ while (start < nodes.length && chrome(nodes[start])) start += 1
+ let end = nodes.length
+ while (end > start && chrome(nodes[end - 1])) end -= 1
+ const middle = nodes.slice(start, end)
+ if (middle.length === 0 || middle.length === nodes.length) return children
+ return [
+ ...nodes.slice(0, start),
+ <DialogBody
+ key="dialog-body"
+ className="mx-0 my-0 flex flex-col gap-[inherit] px-0 py-0"
+ >
+ {middle}
+ </DialogBody>,
+ ...nodes.slice(end),
+ ]
+}
+
function DialogContent({
className,
children,
@@ -52,32 +115,34 @@ function DialogContent({
return (
<DialogPortal>
<DialogOverlay className={overlayClassName} />
- <DialogPrimitive.Popup
- data-slot="dialog-content"
- className={cn(
- "fixed top-1/2 left-1/2 z-50 flex max-h-[calc(100vh-2rem)] w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
- className
- )}
- {...props}
- >
- {children}
- {showCloseButton && (
- <DialogPrimitive.Close
- data-slot="dialog-close"
- render={
- <Button
- variant="ghost"
- className="absolute top-2 right-2"
- size="icon-sm"
- />
- }
- >
- <XIcon
- />
- <span className="sr-only">Close</span>
- </DialogPrimitive.Close>
- )}
- </DialogPrimitive.Popup>
+ <ModalPositioner>
+ <DialogPrimitive.Popup
+ data-slot="dialog-content"
+ className={cn(
+ modalPopupClasses,
+ "flex flex-col gap-4 p-4 text-sm sm:max-w-sm",
+ className
+ )}
+ {...props}
+ >
+ {withScrollingMiddle(children, [DialogHeader, DialogFooter])}
+ {showCloseButton && (
+ <DialogPrimitive.Close
+ data-slot="dialog-close"
+ render={
+ <Button
+ variant="ghost"
+ className="absolute top-2 right-2"
+ size="icon-sm"
+ />
+ }
+ >
+ <XIcon />
+ <span className="sr-only">Close</span>
+ </DialogPrimitive.Close>
+ )}
+ </DialogPrimitive.Popup>
+ </ModalPositioner>
</DialogPortal>
)
}
@@ -162,6 +227,9 @@ function DialogDescription({
export {
Dialog,
+ ModalPositioner,
+ modalPopupClasses,
+ withScrollingMiddle,
DialogClose,
DialogContent,
DialogDescription,
diff --git a/site/src/components/ui/sheet.tsx b/site/src/components/ui/sheet.tsx
index 78c0a764..d16a0b6e 100644
--- a/site/src/components/ui/sheet.tsx
+++ b/site/src/components/ui/sheet.tsx
@@ -53,7 +53,7 @@ function SheetContent({
data-slot="sheet-content"
data-side={side}
className={cn(
- "fixed z-50 flex flex-col gap-4 bg-popover bg-clip-padding text-sm text-popover-foreground shadow-lg transition duration-200 ease-in-out data-ending-style:opacity-0 data-starting-style:opacity-0 data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:border-t data-[side=bottom]:data-ending-style:translate-y-[2.5rem] data-[side=bottom]:data-starting-style:translate-y-[2.5rem] data-[side=left]:inset-y-0 data-[side=left]:left-0 data-[side=left]:h-full data-[side=left]:w-3/4 data-[side=left]:border-r data-[side=left]:data-ending-style:translate-x-[-2.5rem] data-[side=left]:data-starting-style:translate-x-[-2.5rem] data-[side=right]:inset-y-0 data-[side=right]:right-0 data-[side=right]:h-full data-[side=right]:w-3/4 data-[side=right]:border-l data-[side=right]:data-ending-style:translate-x-[2.5rem] data-[side=right]:data-starting-style:translate-x-[2.5rem] data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:border-b data-[side=top]:data-ending-style:translate-y-[-2.5rem] data-[side=top]:data-starting-style:translate-y-[-2.5rem] data-[side=left]:sm:max-w-sm data-[side=right]:sm:max-w-sm",
+ "fixed z-50 flex flex-col gap-4 bg-popover bg-clip-padding text-sm text-popover-foreground shadow-lg transition duration-200 ease-in-out data-ending-style:opacity-0 data-starting-style:opacity-0 data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:max-h-[85dvh] data-[side=bottom]:overflow-y-auto data-[side=bottom]:border-t data-[side=bottom]:data-ending-style:translate-y-[2.5rem] data-[side=bottom]:data-starting-style:translate-y-[2.5rem] data-[side=left]:inset-y-0 data-[side=left]:left-0 data-[side=left]:h-full data-[side=left]:w-3/4 data-[side=left]:border-r data-[side=left]:data-ending-style:translate-x-[-2.5rem] data-[side=left]:data-starting-style:translate-x-[-2.5rem] data-[side=right]:inset-y-0 data-[side=right]:right-0 data-[side=right]:h-full data-[side=right]:w-3/4 data-[side=right]:border-l data-[side=right]:data-ending-style:translate-x-[2.5rem] data-[side=right]:data-starting-style:translate-x-[2.5rem] data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:max-h-[85dvh] data-[side=top]:overflow-y-auto data-[side=top]:border-b data-[side=top]:data-ending-style:translate-y-[-2.5rem] data-[side=top]:data-starting-style:translate-y-[-2.5rem] data-[side=left]:sm:max-w-sm data-[side=right]:sm:max-w-sm",
className
)}
{...props}
diff --git a/site/src/cut/components/ExportDialog.tsx b/site/src/cut/components/ExportDialog.tsx
index 1acc32be..ae42dba4 100644
--- a/site/src/cut/components/ExportDialog.tsx
+++ b/site/src/cut/components/ExportDialog.tsx
@@ -26,6 +26,7 @@ import {
EXPORT_QUICK_PRESETS,
choiceSettings,
estimateExportBytes,
+ fixedRate,
formatSizeEstimate,
quickPresetOf,
resolutionOptions,
@@ -46,10 +47,10 @@ import { subtitleFiles } from "@/cut/lib/subtitleFile";
import { formatTime } from "@/cut/lib/time";
import { cn } from "@/lib/utils";
-/** A file format the menu offers: a container carrying a codec. MP4 cannot
- * carry ProRes, so that pairing is left out. */
+/** A file format the menu offers: a container carrying a codec. A codec that
+ * demands a container of its own is only offered with it. */
const FORMATS = EXPORT_CONTAINERS.flatMap((c) =>
- EXPORT_CODECS.filter((k) => !(c.id === "mp4" && k.id === "prores")).map((k) => ({
+ EXPORT_CODECS.filter((k) => !k.container || k.container === c.id).map((k) => ({
id: `${c.id}-${k.id}`,
container: c.id as ExportContainer,
codec: k.id as ExportCodec,
@@ -201,7 +202,9 @@ export function ExportDialog() {
};
const sizeEstimate = formatSizeEstimate(estimateExportBytes(settings, span));
- const prores = settings.codec === "prores";
+ // ProRes encodes at one rate per profile, so the quality scale and the
+ // typed bitrate have nothing to act on.
+ const prores = fixedRate(settings.codec);
const resolution = resolveResolution(resolutions, choice.resolution);
const rungIndex = Math.max(0, rungs.findIndex((r) => r.id === resolution.id));
const qualityIndex = Math.max(0, QUALITY_RUNGS.findIndex((q) => q.id === choice.quality));
@@ -212,7 +215,7 @@ export function ExportDialog() {
return (
<Dialog open onOpenChange={(o) => !o && setExportOpen(false)}>
- <DialogContent className="top-[18%] translate-y-0 gap-0 p-0 sm:max-w-lg">
+ <DialogContent className="gap-0 p-0 sm:max-w-lg">
<DialogHeader className="px-6 pt-6 pb-4">
<DialogTitle className="text-xl">Export</DialogTitle>
</DialogHeader>
@@ -318,11 +321,11 @@ export function ExportDialog() {
onValueChange={(v) => {
const f = FORMATS.find((x) => x.id === v);
if (!f) return;
- if (f.codec === "prores") setMbpsText("");
+ if (fixedRate(f.codec)) setMbpsText("");
set({
container: f.container,
codec: f.codec,
- ...(f.codec === "prores" ? { bitrateMbps: undefined } : {}),
+ ...(fixedRate(f.codec) ? { bitrateMbps: undefined } : {}),
// MP4 cannot carry PCM either; picking it turns the audio to AAC.
...(f.container === "mp4" && choice.audioCodec === "pcm" ? { audioCodec: "aac" } : {}),
});
diff --git a/site/src/cut/components/ExportsDock.tsx b/site/src/cut/components/ExportsDock.tsx
index 7aac4377..966496c2 100644
--- a/site/src/cut/components/ExportsDock.tsx
+++ b/site/src/cut/components/ExportsDock.tsx
@@ -69,7 +69,7 @@ export function ExportsDock() {
const multi = items.length > 1;
return (
- <div className="fixed right-4 bottom-4 z-50 w-72 overflow-hidden rounded-xl border border-border bg-card shadow-lg">
+ <div className="fixed right-4 bottom-4 z-50 max-h-[calc(100dvh-2rem)] w-72 max-w-[calc(100vw-2rem)] overflow-y-auto overscroll-contain rounded-xl border border-border bg-card shadow-lg">
{multi && (
<div className="flex items-center gap-2 px-3 py-2">
<span className="text-xs font-semibold">Exports</span>
diff --git a/site/src/cut/components/Lightbox.tsx b/site/src/cut/components/Lightbox.tsx
index 757b1067..a60fb58e 100644
--- a/site/src/cut/components/Lightbox.tsx
+++ b/site/src/cut/components/Lightbox.tsx
@@ -116,7 +116,7 @@ export function Lightbox() {
onClick={() => useLightbox.getState().close()}
>
<div
- className="relative flex max-h-[92vh] flex-col gap-3"
+ className="relative flex max-h-full flex-col gap-3"
style={{ width }}
onClick={(e) => e.stopPropagation()}
>
diff --git a/site/src/cut/components/OfferDialog.tsx b/site/src/cut/components/OfferDialog.tsx
index 0acec7e8..cdf06bd1 100644
--- a/site/src/cut/components/OfferDialog.tsx
+++ b/site/src/cut/components/OfferDialog.tsx
@@ -126,7 +126,7 @@ export function OfferDialog({
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent
showCloseButton={false}
- className="top-[18%] translate-y-0 gap-0 overflow-hidden rounded-lg bg-background p-0 sm:max-w-sm"
+ className="gap-0 overflow-hidden rounded-lg bg-background p-0 sm:max-w-sm"
>
<div className="relative">
{isOfferBanner(banner) ? (
diff --git a/site/src/cut/components/StorageUpgradeDialog.tsx b/site/src/cut/components/StorageUpgradeDialog.tsx
index a7e8ecc1..fa60f390 100644
--- a/site/src/cut/components/StorageUpgradeDialog.tsx
+++ b/site/src/cut/components/StorageUpgradeDialog.tsx
@@ -85,7 +85,7 @@ function OpenDialog({ detail, onClose }: { detail: StorageQuotaDetail; onClose:
if (isPro) {
return (
<Dialog open onOpenChange={(open) => !open && onClose()}>
- <DialogContent className="top-[18%] translate-y-0 sm:max-w-sm">
+ <DialogContent className="sm:max-w-sm">
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
</DialogHeader>