Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

add autoFocus to modals

daniel31x138f1093012721
diff --git a/apps/web/components/ModalContent/InviteModal.tsx b/apps/web/components/ModalContent/InviteModal.tsx
index 05ed3551..637d3423 100644
--- a/apps/web/components/ModalContent/InviteModal.tsx
+++ b/apps/web/components/ModalContent/InviteModal.tsx
@@ -1,7 +1,7 @@
import toast from "react-hot-toast";
import Modal from "../Modal";
import TextInput from "../TextInput";
-import { FormEvent, useState } from "react";
+import { FormEvent, useLayoutEffect, useRef, useState } from "react";
import { useTranslation, Trans } from "next-i18next";
import { useAddUser } from "@linkwarden/router/users";
import Link from "next/link";
@@ -67,6 +67,12 @@ export default function InviteModal({ onClose }: Props) {
}
}
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ useLayoutEffect(() => {
+ inputRef.current?.focus();
+ }, []);
+
return (
<Modal toggleModal={onClose}>
<p className="text-xl font-thin">{t("invite_user")}</p>
@@ -78,6 +84,7 @@ export default function InviteModal({ onClose }: Props) {
{emailEnabled ? (
<div>
<TextInput
+ ref={inputRef}
placeholder={t("placeholder_email")}
className="bg-base-200"
onChange={(e) => setForm({ ...form, email: e.target.value })}
@@ -93,6 +100,7 @@ export default function InviteModal({ onClose }: Props) {
)}
</p>
<TextInput
+ ref={inputRef}
placeholder={t("placeholder_john")}
className="bg-base-200"
onChange={(e) => setForm({ ...form, username: e.target.value })}
diff --git a/apps/web/components/ModalContent/NewCollectionModal.tsx b/apps/web/components/ModalContent/NewCollectionModal.tsx
index e2941875..a4b2cf1d 100644
--- a/apps/web/components/ModalContent/NewCollectionModal.tsx
+++ b/apps/web/components/ModalContent/NewCollectionModal.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
import TextInput from "@/components/TextInput";
import { Collection } from "@linkwarden/prisma/client";
import Modal from "../Modal";
@@ -60,6 +60,12 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
});
};
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ useLayoutEffect(() => {
+ inputRef.current?.focus();
+ }, []);
+
return (
<Modal toggleModal={onClose}>
{parent?.id ? (
@@ -103,6 +109,7 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
<div className="w-full">
<p className="mb-2">{t("name")}</p>
<TextInput
+ ref={inputRef}
className="bg-base-200"
value={collection.name}
placeholder={t("collection_name_placeholder")}
diff --git a/apps/web/components/ModalContent/NewLinkModal.tsx b/apps/web/components/ModalContent/NewLinkModal.tsx
index 62c50826..6310614d 100644
--- a/apps/web/components/ModalContent/NewLinkModal.tsx
+++ b/apps/web/components/ModalContent/NewLinkModal.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
import TagSelection from "@/components/InputSelect/TagSelection";
import TextInput from "@/components/TextInput";
@@ -31,6 +31,7 @@ export default function NewLinkModal({ onClose }: Props) {
},
} as PostLinkSchemaType;
+ const inputRef = useRef<HTMLInputElement>(null);
const [link, setLink] = useState<PostLinkSchemaType>(initial);
const addLink = useAddLink();
const [submitLoader, setSubmitLoader] = useState(false);
@@ -74,6 +75,10 @@ export default function NewLinkModal({ onClose }: Props) {
});
}, []);
+ useLayoutEffect(() => {
+ inputRef.current?.focus();
+ }, []);
+
const submit = async () => {
if (!submitLoader) {
setSubmitLoader(true);
@@ -103,6 +108,7 @@ export default function NewLinkModal({ onClose }: Props) {
<div className="sm:col-span-3 col-span-5">
<p className="mb-2">{t("link")}</p>
<TextInput
+ ref={inputRef}
value={link.url || ""}
onChange={(e) => setLink({ ...link, url: e.target.value })}
placeholder={t("link_url_placeholder")}
diff --git a/apps/web/components/ModalContent/NewRssSubscriptionModal.tsx b/apps/web/components/ModalContent/NewRssSubscriptionModal.tsx
index c0267e98..4ab674e5 100644
--- a/apps/web/components/ModalContent/NewRssSubscriptionModal.tsx
+++ b/apps/web/components/ModalContent/NewRssSubscriptionModal.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useLayoutEffect, useRef, useState } from "react";
import Modal from "../Modal";
import { useTranslation } from "next-i18next";
import { useAddRssSubscription } from "@linkwarden/router/rss";
@@ -54,6 +54,12 @@ export default function NewRssSubscriptionModal({ onClose }: Props) {
});
};
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ useLayoutEffect(() => {
+ inputRef.current?.focus();
+ }, []);
+
return (
<Modal toggleModal={onClose}>
<>
@@ -61,6 +67,18 @@ export default function NewRssSubscriptionModal({ onClose }: Props) {
<Separator className="my-3" />
+ <div className="w-full mb-3">
+ <label>{t("link")}</label>
+ <TextInput
+ ref={inputRef}
+ type="text"
+ placeholder="https://example.com/rss"
+ className="bg-base-200 mt-2"
+ value={form.url}
+ onChange={(e) => setForm({ ...form, url: e.target.value })}
+ />
+ </div>
+
<div className="flex sm:flex-row flex-col gap-3 items-center">
<div className="w-full">
<label>{t("name")}</label>
@@ -87,16 +105,7 @@ export default function NewRssSubscriptionModal({ onClose }: Props) {
/>
</div>
</div>
- <div className="w-full mt-3">
- <label>{t("link")}</label>
- <TextInput
- type="text"
- placeholder="https://example.com/rss"
- className="bg-base-200 mt-2"
- value={form.url}
- onChange={(e) => setForm({ ...form, url: e.target.value })}
- />
- </div>
+
<div className="flex justify-end items-center mt-5">
<Button variant="accent" onClick={submit}>
{t("create_rss_subscription")}
diff --git a/apps/web/components/ModalContent/NewTokenModal.tsx b/apps/web/components/ModalContent/NewTokenModal.tsx
index 3e0ff69a..80c30404 100644
--- a/apps/web/components/ModalContent/NewTokenModal.tsx
+++ b/apps/web/components/ModalContent/NewTokenModal.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useLayoutEffect, useRef, useState } from "react";
import TextInput from "@/components/TextInput";
import { TokenExpiry } from "@linkwarden/types";
import toast from "react-hot-toast";
@@ -69,6 +69,12 @@ export default function NewTokenModal({ onClose }: Props) {
}
};
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ useLayoutEffect(() => {
+ inputRef.current?.focus();
+ }, []);
+
return (
<Modal toggleModal={onClose}>
{newToken ? (
@@ -95,6 +101,7 @@ export default function NewTokenModal({ onClose }: Props) {
<p className="mb-2">{t("name")}</p>
<TextInput
+ ref={inputRef}
value={token.name}
onChange={(e) => setToken({ ...token, name: e.target.value })}
placeholder={t("token_name_placeholder")}
diff --git a/apps/web/components/TextInput.tsx b/apps/web/components/TextInput.tsx
index 2003823a..1bddf84a 100644
--- a/apps/web/components/TextInput.tsx
+++ b/apps/web/components/TextInput.tsx
@@ -1,4 +1,8 @@
-import { ChangeEventHandler, KeyboardEventHandler } from "react";
+import React, {
+ forwardRef,
+ ChangeEventHandler,
+ KeyboardEventHandler,
+} from "react";
type Props = {
autoFocus?: boolean;
@@ -6,36 +10,50 @@ type Props = {
type?: string;
placeholder?: string;
onChange: ChangeEventHandler<HTMLInputElement>;
- onKeyDown?: KeyboardEventHandler<HTMLInputElement> | undefined;
+ onKeyDown?: KeyboardEventHandler<HTMLInputElement>;
className?: string;
spellCheck?: boolean;
"data-testid"?: string;
};
-export default function TextInput({
- autoFocus,
- value,
- type,
- placeholder,
- onChange,
- onKeyDown,
- className,
- spellCheck,
- "data-testid": dataTestId,
-}: Props) {
- return (
- <input
- data-testid={dataTestId}
- spellCheck={spellCheck}
- autoFocus={autoFocus}
- type={type ? type : "text"}
- placeholder={placeholder}
- value={value}
- onChange={onChange}
- onKeyDown={onKeyDown}
- className={`w-full rounded-md p-2 border-neutral-content border-solid border outline-none focus:border-primary duration-100 ${
- className || ""
- }`}
- />
- );
-}
+const TextInput = forwardRef<HTMLInputElement, Props>(
+ (
+ {
+ autoFocus,
+ value,
+ type,
+ placeholder,
+ onChange,
+ onKeyDown,
+ className,
+ spellCheck,
+ "data-testid": dataTestId,
+ },
+ ref
+ ) => {
+ return (
+ <input
+ ref={ref}
+ data-testid={dataTestId}
+ spellCheck={spellCheck}
+ autoFocus={autoFocus}
+ type={type ?? "text"}
+ placeholder={placeholder}
+ value={value}
+ onChange={onChange}
+ onKeyDown={onKeyDown}
+ className={`
+ w-full rounded-md p-2
+ border-neutral-content border-solid border
+ outline-none focus:border-primary duration-100
+ ${className ?? ""}
+ `}
+ />
+ );
+ }
+);
+
+// Give it a display name for easier debugging
+TextInput.displayName = "TextInput";
+
+export default TextInput;