Skip to content

Commit 5e5c874

Browse files
feat: File panel input/upload button autofocus (BLO-1220) (#3220)
* Made file panel input/upload button autofocus * Skipped visual snapshots due to them being incorrect * Updated test structure * Reverted focusing first element in custom file panels * Small fix
1 parent 5a5648d commit 5e5c874

11 files changed

Lines changed: 144 additions & 20 deletions

File tree

‎packages/ariakit/src/panel/PanelFileInput.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,33 @@ import {
44
} from "@ariakit/react";
55

66
import { assertEmpty } from "@blocknote/core";
7-
import { ComponentProps } from "@blocknote/react";
7+
import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react";
88
import { forwardRef, type ChangeEvent } from "react";
99

1010
export const PanelFileInput = forwardRef<
1111
HTMLInputElement,
1212
ComponentProps["FilePanel"]["FileInput"]
1313
>((props, ref) => {
14-
const { className, accept, value, placeholder, onChange, ...rest } = props;
14+
const {
15+
autoFocus,
16+
className,
17+
accept,
18+
value,
19+
placeholder,
20+
onChange,
21+
...rest
22+
} = props;
1523

1624
assertEmpty(rest);
1725

26+
const inputRef = useAutoFocus<HTMLInputElement>(autoFocus);
27+
const mergedRef = useMergeRefs([ref, inputRef]);
28+
1829
return (
1930
<AriakitFormProvider>
2031
<AriakitFormInput
2132
className={className}
22-
ref={ref}
33+
ref={mergedRef}
2334
name={"panel-input"}
2435
type={"file"}
2536
accept={accept}

‎packages/ariakit/src/panel/PanelTextInput.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ import {
44
} from "@ariakit/react";
55

66
import { assertEmpty, mergeCSSClasses } from "@blocknote/core";
7-
import { ComponentProps } from "@blocknote/react";
7+
import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react";
88
import { forwardRef } from "react";
99

1010
export const PanelTextInput = forwardRef<
1111
HTMLInputElement,
1212
ComponentProps["FilePanel"]["TextInput"]
1313
>((props, ref) => {
14-
const { className, value, placeholder, onKeyDown, onChange, ...rest } = props;
14+
const {
15+
autoFocus,
16+
className,
17+
value,
18+
placeholder,
19+
onKeyDown,
20+
onChange,
21+
...rest
22+
} = props;
1523

1624
assertEmpty(rest);
1725

26+
const inputRef = useAutoFocus<HTMLInputElement>(autoFocus);
27+
const mergedRef = useMergeRefs([ref, inputRef]);
28+
1829
return (
1930
<AriakitFormProvider>
2031
<AriakitFormInput
@@ -25,7 +36,7 @@ export const PanelTextInput = forwardRef<
2536
onChange={onChange}
2637
onKeyDown={onKeyDown}
2738
data-test={"embed-input"}
28-
ref={ref}
39+
ref={mergedRef}
2940
/>
3041
</AriakitFormProvider>
3142
);

‎packages/mantine/src/panel/PanelFileInput.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
import { FileInput as MantineFileInput } from "@mantine/core";
22

33
import { assertEmpty } from "@blocknote/core";
4-
import { ComponentProps } from "@blocknote/react";
4+
import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react";
55
import { forwardRef } from "react";
66

77
export const PanelFileInput = forwardRef<
88
HTMLButtonElement,
99
ComponentProps["FilePanel"]["FileInput"]
1010
>((props, ref) => {
11-
const { className, accept, value, placeholder, onChange, ...rest } = props;
11+
const {
12+
autoFocus,
13+
className,
14+
accept,
15+
value,
16+
placeholder,
17+
onChange,
18+
...rest
19+
} = props;
1220

1321
assertEmpty(rest);
1422

23+
const inputRef = useAutoFocus<HTMLButtonElement>(autoFocus);
24+
const mergedRef = useMergeRefs([ref, inputRef]);
25+
1526
return (
1627
<MantineFileInput
1728
size={"xs"}
1829
className={className}
19-
ref={ref}
30+
ref={mergedRef}
2031
accept={accept}
2132
value={value}
2233
placeholder={placeholder}

‎packages/mantine/src/panel/PanelTextInput.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
import { TextInput as MantineTextInput } from "@mantine/core";
22

33
import { assertEmpty } from "@blocknote/core";
4-
import { ComponentProps } from "@blocknote/react";
4+
import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react";
55
import { forwardRef } from "react";
66

77
export const PanelTextInput = forwardRef<
88
HTMLInputElement,
99
ComponentProps["FilePanel"]["TextInput"]
1010
>((props, ref) => {
11-
const { className, value, placeholder, onKeyDown, onChange, ...rest } = props;
11+
const {
12+
autoFocus,
13+
className,
14+
value,
15+
placeholder,
16+
onKeyDown,
17+
onChange,
18+
...rest
19+
} = props;
1220

1321
assertEmpty(rest);
1422

23+
const inputRef = useAutoFocus<HTMLInputElement>(autoFocus);
24+
const mergedRef = useMergeRefs([ref, inputRef]);
25+
1526
return (
1627
<MantineTextInput
1728
size={"xs"}
1829
data-test={"embed-input"}
1930
className={className}
20-
ref={ref}
31+
ref={mergedRef}
2132
value={value}
2233
placeholder={placeholder}
2334
onKeyDown={onKeyDown}

‎packages/react/src/components/FilePanel/DefaultTabs/EmbedTab.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const EmbedTab = <
7575
}
7676
>
7777
<Components.FilePanel.TextInput
78+
autoFocus
7879
className={"bn-text-input"}
7980
placeholder={dict.file_panel.embed.url_placeholder}
8081
value={currentURL}

‎packages/react/src/components/FilePanel/DefaultTabs/UploadTab.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export const UploadTab = <
9191
return (
9292
<Components.FilePanel.TabPanel className={"bn-tab-panel"}>
9393
<Components.FilePanel.FileInput
94+
autoFocus
9495
className="bn-file-input"
9596
data-test="upload-input"
9697
accept={accept}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { PartialBlock } from "@blocknote/core";
2+
import { describe, expect } from "vite-plus/test";
3+
import { page, userEvent } from "vite-plus/test/browser";
4+
import type { BrowserContext } from "../../../../../tests/src/fixtures/editor.js";
5+
6+
const imageDocument: PartialBlock[] = [{ type: "paragraph" }];
7+
8+
async function openImagePanel({
9+
pressKey,
10+
focusUntil,
11+
}: Pick<BrowserContext, "pressKey" | "focusUntil">) {
12+
await focusUntil(page.getByRole("textbox"));
13+
await expect.element(page.getByRole("textbox")).toHaveFocus();
14+
await userEvent.keyboard("/image");
15+
await expect
16+
.element(page.getByRole("option", { name: /Image/ }))
17+
.toBeVisible();
18+
await pressKey("Enter");
19+
const panel = page.getByRole("tabpanel");
20+
await expect.element(panel).toBeVisible();
21+
return panel;
22+
}
23+
24+
describe("keyboard", () => {
25+
it("file panel: Embed URL input receives focus on opening", async ({
26+
setupEditor,
27+
pressKey,
28+
focusUntil,
29+
}) => {
30+
await setupEditor({ initialContent: imageDocument });
31+
const panel = await openImagePanel({ pressKey, focusUntil });
32+
await expect.element(panel.getByPlaceholder("Enter URL")).toHaveFocus();
33+
});
34+
35+
it("file panel: Upload button receives focus on opening", async ({
36+
setupEditor,
37+
pressKey,
38+
focusUntil,
39+
}) => {
40+
await setupEditor({
41+
initialContent: imageDocument,
42+
uploadFile: async () => "/image.svg",
43+
});
44+
const panel = await openImagePanel({ pressKey, focusUntil });
45+
await expect
46+
.element(panel.getByRole("button", { name: "Upload image", exact: true }))
47+
.toHaveFocus();
48+
});
49+
});

‎packages/react/src/editor/ComponentsContext.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export type ComponentProps = {
114114
| { children?: undefined; label: string }
115115
);
116116
FileInput: {
117+
autoFocus?: boolean;
117118
className?: string;
118119
accept: string;
119120
value: File | null;
@@ -125,6 +126,7 @@ export type ComponentProps = {
125126
children?: ReactNode;
126127
};
127128
TextInput: {
129+
autoFocus?: boolean;
128130
className?: string;
129131
value: string;
130132
placeholder: string;

‎packages/shadcn/src/components/ui/input.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import { Input as InputPrimitive } from "@base-ui/react/input";
55
// CLI's "@/lib/utils" alias (not resolvable when consumed from source).
66
import { cn } from "../../lib/utils";
77

8-
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
8+
// Forward refs explicitly to support React 18 as well as React 19.
9+
const Input = React.forwardRef<
10+
HTMLInputElement,
11+
React.ComponentPropsWithoutRef<"input">
12+
>(function Input({ className, type, ...props }, ref) {
913
return (
1014
<InputPrimitive
15+
ref={ref}
1116
type={type}
1217
data-slot="input"
1318
className={cn(
@@ -17,6 +22,6 @@ function Input({ className, type, ...props }: React.ComponentProps<"input">) {
1722
{...props}
1823
/>
1924
);
20-
}
25+
});
2126

2227
export { Input };

‎packages/shadcn/src/panel/PanelFileInput.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assertEmpty } from "@blocknote/core";
2-
import { ComponentProps } from "@blocknote/react";
2+
import { ComponentProps, useAutoFocus, useMergeRefs } from "@blocknote/react";
33
import { forwardRef } from "react";
44

55
import { useShadCNComponentsContext } from "../ShadCNComponentsContext.js";
@@ -8,17 +8,28 @@ export const PanelFileInput = forwardRef<
88
HTMLInputElement,
99
ComponentProps["FilePanel"]["FileInput"]
1010
>((props, ref) => {
11-
const { className, accept, value, placeholder, onChange, ...rest } = props;
11+
const {
12+
autoFocus,
13+
className,
14+
accept,
15+
value,
16+
placeholder,
17+
onChange,
18+
...rest
19+
} = props;
1220

1321
assertEmpty(rest);
1422

23+
const inputRef = useAutoFocus<HTMLInputElement>(autoFocus);
24+
const mergedRef = useMergeRefs([ref, inputRef]);
25+
1526
const ShadCNComponents = useShadCNComponentsContext()!;
1627

1728
return (
1829
<ShadCNComponents.Input.Input
1930
type={"file"}
2031
className={className}
21-
ref={ref}
32+
ref={mergedRef}
2233
accept={accept}
2334
value={value ? value.name : undefined}
2435
onChange={async (e) => onChange?.(e.target.files![0])}

0 commit comments

Comments
 (0)