Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: make editor title font-size relative to title length

Abdullah Atta11a3092bacc9
diff --git a/apps/web/src/components/editor/title-box.tsx b/apps/web/src/components/editor/title-box.tsx
index 7deb9cf1f..9fe6678a9 100644
--- a/apps/web/src/components/editor/title-box.tsx
+++ b/apps/web/src/components/editor/title-box.tsx
@@ -17,10 +17,12 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React, { useEffect, useRef } from "react";
+import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { Input } from "@theme-ui/components";
import { useStore, store } from "../../stores/editor-store";
import { debounceWithId } from "../../utils/debounce";
+import useMobile from "../../hooks/use-mobile";
+import useTablet from "../../hooks/use-tablet";
type TitleBoxProps = {
readonly: boolean;
@@ -31,10 +33,28 @@ function TitleBox(props: TitleBoxProps) {
const inputRef = useRef<HTMLInputElement>(null);
const title = useStore((store) => store.session.title);
const id = useStore((store) => store.session.id);
+ const isMobile = useMobile();
+ const isTablet = useTablet();
+
+ const MAX_FONT_SIZE = useMemo(() => {
+ return isMobile || isTablet ? 1.625 : 2.625;
+ }, [isMobile, isTablet]);
+
+ const updateFontSize = useCallback(() => {
+ if (!inputRef.current) return;
+ const fontSize = textLengthToFontSize(
+ inputRef.current.value.length,
+ MAX_FONT_SIZE
+ );
+ inputRef.current.style.fontSize = `${fontSize}em`;
+ }, [MAX_FONT_SIZE]);
useEffect(() => {
- if (inputRef.current) inputRef.current.value = title;
- }, [id]);
+ if (inputRef.current) {
+ inputRef.current.value = title;
+ updateFontSize();
+ }
+ }, [id, updateFontSize]);
return (
<Input
@@ -54,6 +74,7 @@ function TitleBox(props: TitleBoxProps) {
onChange={(e) => {
const { sessionId, id } = store.get().session;
debouncedOnTitleChange(sessionId, id, e.target.value);
+ updateFontSize();
}}
/>
);
@@ -69,3 +90,10 @@ function onTitleChange(noteId: string, title: string) {
}
const debouncedOnTitleChange = debounceWithId(onTitleChange, 100);
+
+function textLengthToFontSize(length: number, max: number) {
+ const stepLength = 35;
+ const decreaseStep = 0.5;
+ const steps = length / stepLength;
+ return Math.max(1.2, Math.min(max, max - steps * decreaseStep));
+}