diff --git a/packages/ariakit/src/panel/PanelFileInput.tsx b/packages/ariakit/src/panel/PanelFileInput.tsx index 12aef33147..29bc9560c5 100644 --- a/packages/ariakit/src/panel/PanelFileInput.tsx +++ b/packages/ariakit/src/panel/PanelFileInput.tsx @@ -4,22 +4,33 @@ import { } from "@ariakit/react"; import { assertEmpty } from "@blocknote/core"; -import { ComponentProps } from "@blocknote/react"; +import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react"; import { forwardRef, type ChangeEvent } from "react"; export const PanelFileInput = forwardRef< HTMLInputElement, ComponentProps["FilePanel"]["FileInput"] >((props, ref) => { - const { className, accept, value, placeholder, onChange, ...rest } = props; + const { + autoFocus, + className, + accept, + value, + placeholder, + onChange, + ...rest + } = props; assertEmpty(rest); + const inputRef = useAutoFocus(autoFocus); + const mergedRef = useMergeRefs([ref, inputRef]); + return ( ((props, ref) => { - const { className, value, placeholder, onKeyDown, onChange, ...rest } = props; + const { + autoFocus, + className, + value, + placeholder, + onKeyDown, + onChange, + ...rest + } = props; assertEmpty(rest); + const inputRef = useAutoFocus(autoFocus); + const mergedRef = useMergeRefs([ref, inputRef]); + return ( ); diff --git a/packages/mantine/src/panel/PanelFileInput.tsx b/packages/mantine/src/panel/PanelFileInput.tsx index ed2efe8eb1..a1e5ae9ba2 100644 --- a/packages/mantine/src/panel/PanelFileInput.tsx +++ b/packages/mantine/src/panel/PanelFileInput.tsx @@ -1,22 +1,33 @@ import { FileInput as MantineFileInput } from "@mantine/core"; import { assertEmpty } from "@blocknote/core"; -import { ComponentProps } from "@blocknote/react"; +import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react"; import { forwardRef } from "react"; export const PanelFileInput = forwardRef< HTMLButtonElement, ComponentProps["FilePanel"]["FileInput"] >((props, ref) => { - const { className, accept, value, placeholder, onChange, ...rest } = props; + const { + autoFocus, + className, + accept, + value, + placeholder, + onChange, + ...rest + } = props; assertEmpty(rest); + const inputRef = useAutoFocus(autoFocus); + const mergedRef = useMergeRefs([ref, inputRef]); + return ( ((props, ref) => { - const { className, value, placeholder, onKeyDown, onChange, ...rest } = props; + const { + autoFocus, + className, + value, + placeholder, + onKeyDown, + onChange, + ...rest + } = props; assertEmpty(rest); + const inputRef = useAutoFocus(autoFocus); + const mergedRef = useMergeRefs([ref, inputRef]); + return ( ) { + await focusUntil(page.getByRole("textbox")); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + await userEvent.keyboard("/image"); + await expect + .element(page.getByRole("option", { name: /Image/ })) + .toBeVisible(); + await pressKey("Enter"); + const panel = page.getByRole("tabpanel"); + await expect.element(panel).toBeVisible(); + return panel; +} + +describe("keyboard", () => { + it("file panel: Embed URL input receives focus on opening", async ({ + setupEditor, + pressKey, + focusUntil, + }) => { + await setupEditor({ initialContent: imageDocument }); + const panel = await openImagePanel({ pressKey, focusUntil }); + await expect.element(panel.getByPlaceholder("Enter URL")).toHaveFocus(); + }); + + it("file panel: Upload button receives focus on opening", async ({ + setupEditor, + pressKey, + focusUntil, + }) => { + await setupEditor({ + initialContent: imageDocument, + uploadFile: async () => "/image.svg", + }); + const panel = await openImagePanel({ pressKey, focusUntil }); + await expect + .element(panel.getByRole("button", { name: "Upload image", exact: true })) + .toHaveFocus(); + }); +}); diff --git a/packages/react/src/editor/ComponentsContext.tsx b/packages/react/src/editor/ComponentsContext.tsx index 73ee014e71..13a6692a3e 100644 --- a/packages/react/src/editor/ComponentsContext.tsx +++ b/packages/react/src/editor/ComponentsContext.tsx @@ -114,6 +114,7 @@ export type ComponentProps = { | { children?: undefined; label: string } ); FileInput: { + autoFocus?: boolean; className?: string; accept: string; value: File | null; @@ -125,6 +126,7 @@ export type ComponentProps = { children?: ReactNode; }; TextInput: { + autoFocus?: boolean; className?: string; value: string; placeholder: string; diff --git a/packages/shadcn/src/components/ui/input.tsx b/packages/shadcn/src/components/ui/input.tsx index e5712d8412..d07f8e17c5 100644 --- a/packages/shadcn/src/components/ui/input.tsx +++ b/packages/shadcn/src/components/ui/input.tsx @@ -5,9 +5,14 @@ import { Input as InputPrimitive } from "@base-ui/react/input"; // CLI's "@/lib/utils" alias (not resolvable when consumed from source). import { cn } from "../../lib/utils"; -function Input({ className, type, ...props }: React.ComponentProps<"input">) { +// Forward refs explicitly to support React 18 as well as React 19. +const Input = React.forwardRef< + HTMLInputElement, + React.ComponentPropsWithoutRef<"input"> +>(function Input({ className, type, ...props }, ref) { return ( ) { {...props} /> ); -} +}); export { Input }; diff --git a/packages/shadcn/src/panel/PanelFileInput.tsx b/packages/shadcn/src/panel/PanelFileInput.tsx index 3c4f2cbc75..120a0502a0 100644 --- a/packages/shadcn/src/panel/PanelFileInput.tsx +++ b/packages/shadcn/src/panel/PanelFileInput.tsx @@ -1,5 +1,5 @@ import { assertEmpty } from "@blocknote/core"; -import { ComponentProps } from "@blocknote/react"; +import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react"; import { forwardRef } from "react"; import { useShadCNComponentsContext } from "../ShadCNComponentsContext.js"; @@ -8,17 +8,28 @@ export const PanelFileInput = forwardRef< HTMLInputElement, ComponentProps["FilePanel"]["FileInput"] >((props, ref) => { - const { className, accept, value, placeholder, onChange, ...rest } = props; + const { + autoFocus, + className, + accept, + value, + placeholder, + onChange, + ...rest + } = props; assertEmpty(rest); + const inputRef = useAutoFocus(autoFocus); + const mergedRef = useMergeRefs([ref, inputRef]); + const ShadCNComponents = useShadCNComponentsContext()!; return ( onChange?.(e.target.files![0])} diff --git a/packages/shadcn/src/panel/PanelTextInput.tsx b/packages/shadcn/src/panel/PanelTextInput.tsx index 8165271d96..98980dfbea 100644 --- a/packages/shadcn/src/panel/PanelTextInput.tsx +++ b/packages/shadcn/src/panel/PanelTextInput.tsx @@ -1,5 +1,5 @@ import { assertEmpty } from "@blocknote/core"; -import { ComponentProps } from "@blocknote/react"; +import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react"; import { forwardRef } from "react"; import { cn } from "../lib/utils.js"; @@ -9,17 +9,28 @@ export const PanelTextInput = forwardRef< HTMLInputElement, ComponentProps["FilePanel"]["TextInput"] >((props, ref) => { - const { className, value, placeholder, onKeyDown, onChange, ...rest } = props; + const { + autoFocus, + className, + value, + placeholder, + onKeyDown, + onChange, + ...rest + } = props; assertEmpty(rest); + const inputRef = useAutoFocus(autoFocus); + const mergedRef = useMergeRefs([ref, inputRef]); + const ShadCNComponents = useShadCNComponentsContext()!; return (