Skip to content

Open source CapCut alternative.

TypeScript

fix(site): keep a dialog form's footer pinned and its rings whole

David Lee19fa1add783
diff --git a/site/src/components/ui/dialog.test.tsx b/site/src/components/ui/dialog.test.tsx
new file mode 100644
index 00000000..74c68e0a
--- /dev/null
+++ b/site/src/components/ui/dialog.test.tsx
@@ -0,0 +1,43 @@
+import { expect, test } from "bun:test"
+import * as React from "react"
+import {
+ DialogBody,
+ DialogFooter,
+ DialogHeader,
+ withScrollingMiddle,
+} from "./dialog"
+
+const CHROME = [DialogHeader, DialogFooter]
+
+type Shaped = React.ReactElement<{ className?: string; children?: React.ReactNode }>
+
+test("a form that holds the footer keeps it pinned and scrolls only its fields", () => {
+ const out = React.Children.toArray(
+ withScrollingMiddle(
+ [
+ <DialogHeader key="h" />,
+ <form key="f">
+ <input />
+ <DialogFooter className="mt-4" />
+ </form>,
+ ],
+ CHROME
+ )
+ ) as Shaped[]
+
+ // The form stays the popup's child, so the footer's edge-to-edge margins sit
+ // against the popup and nothing overflows the scrolling part.
+ const form = out[1]
+ expect(form.type).toBe("form")
+ const inner = React.Children.toArray(form.props.children) as Shaped[]
+ expect(inner.map((node) => node.type)).toEqual([DialogBody, DialogFooter])
+})
+
+test("the scrolling part leaves room for a focused field's ring", () => {
+ const out = React.Children.toArray(
+ withScrollingMiddle([<DialogHeader key="h" />, <input key="i" />], CHROME)
+ ) as Shaped[]
+ const body = out[1]
+ expect(body.type).toBe(DialogBody)
+ expect(body.props.className).not.toContain("px-0")
+})
diff --git a/site/src/components/ui/dialog.tsx b/site/src/components/ui/dialog.tsx
index 9c8d16a4..45c2b0a7 100644
--- a/site/src/components/ui/dialog.tsx
+++ b/site/src/components/ui/dialog.tsx
@@ -73,17 +73,41 @@ const modalPopupClasses =
// 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.
+// so the dialog lays out exactly as it did with the room it has, plus a few
+// pixels on every side so a focused field's ring stays inside it.
function withScrollingMiddle(
children: React.ReactNode,
chromeTypes: React.ElementType[]
-) {
+): React.ReactNode {
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))
+
+ // A form that wraps its own footer becomes the column itself, so the footer
+ // stays pinned and its edge-to-edge margins meet the popup's padding. Inside
+ // the scrolling part they would overflow it and show both scrollbars.
+ const formIndex = nodes.findIndex(
+ (node) =>
+ React.isValidElement<{ className?: string; children?: React.ReactNode }>(node) &&
+ node.type === "form" &&
+ React.Children.toArray(node.props.children).some(chrome)
+ )
+ if (formIndex !== -1) {
+ const form = nodes[formIndex] as React.ReactElement<{
+ className?: string
+ children?: React.ReactNode
+ }>
+ nodes[formIndex] = React.cloneElement(
+ form,
+ { className: cn("flex min-h-0 flex-col", form.props.className) },
+ withScrollingMiddle(form.props.children, chromeTypes)
+ )
+ return nodes
+ }
+
let start = 0
while (start < nodes.length && chrome(nodes[start])) start += 1
let end = nodes.length
@@ -94,7 +118,7 @@ function withScrollingMiddle(
...nodes.slice(0, start),
<DialogBody
key="dialog-body"
- className="mx-0 my-0 flex flex-col gap-[inherit] px-0 py-0"
+ className="-m-1 flex flex-col gap-[inherit] p-1"
>
{middle}
</DialogBody>,