260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
@@ -1,106 +1,39 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { cookieStore } from "@/store/cookie/cookie";
|
||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||
import {FONT_SIZE} from "@/const/commonConst";
|
||||
import {
|
||||
createFontSizeHook,
|
||||
} from "@kri/common/hooks";
|
||||
import {
|
||||
FONT_SIZE,
|
||||
} from "@/const/commonConst";
|
||||
import {
|
||||
cookieStore,
|
||||
} from "@/store/cookie/client-cookie";
|
||||
import {
|
||||
CookieKeys,
|
||||
} from "@/store/cookie/cookie-keys";
|
||||
|
||||
const MIN_FONT_SIZE = 12;
|
||||
const MAX_FONT_SIZE = 24;
|
||||
const DEFAULT_DAYS = 365;
|
||||
|
||||
const clampFontSize = (size: number) => {
|
||||
return Math.min(MAX_FONT_SIZE, Math.max(MIN_FONT_SIZE, size));
|
||||
}
|
||||
|
||||
/**
|
||||
* 쿠키에서 폰트 사이즈 읽기
|
||||
*/
|
||||
const readFontSizeFromCookie = (defaultSize: number): number => {
|
||||
const raw = cookieStore.get(CookieKeys.FontSize);
|
||||
if (!raw) return defaultSize;
|
||||
|
||||
const parsed = parseInt(raw, 10);
|
||||
if (Number.isNaN(parsed)) return defaultSize;
|
||||
|
||||
return clampFontSize(parsed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 쿠키에 폰트 사이즈 저장
|
||||
*/
|
||||
const writeFontSizeToCookie = (size: number) => {
|
||||
const clamped = clampFontSize(size);
|
||||
cookieStore.set(CookieKeys.FontSize, String(clamped), {
|
||||
days: DEFAULT_DAYS,
|
||||
path: "/",
|
||||
sameSite: "Lax",
|
||||
});
|
||||
}
|
||||
|
||||
type UseFontSizeOptions = {
|
||||
/**
|
||||
* 클라이언트 마운트 시 쿠키에서 초기값을 다시 읽어올지 여부
|
||||
* - true: (기본값) 쿠키에서 읽어옴
|
||||
* - false: 전달받은 defaultSize를 그대로 사용 (SSR 값 유지, FOUC 방지)
|
||||
*/
|
||||
hydrateFromCookie?: boolean;
|
||||
}
|
||||
|
||||
export const useFontSize = (
|
||||
defaultSize: number = FONT_SIZE.DEFAULT,
|
||||
options: UseFontSizeOptions = {},
|
||||
) => {
|
||||
const { hydrateFromCookie = true } = options;
|
||||
|
||||
const [fontSize, setFontSize] = useState<number>(() => {
|
||||
// SSR로 전달된 초기값을 신뢰해야 하는 경우 쿠키 재조회 생략
|
||||
if (!hydrateFromCookie) return clampFontSize(defaultSize);
|
||||
return readFontSizeFromCookie(defaultSize);
|
||||
const fontSizeHook =
|
||||
createFontSizeHook({
|
||||
defaultSize: FONT_SIZE.DEFAULT,
|
||||
minSize: FONT_SIZE.MIN,
|
||||
maxSize: FONT_SIZE.MAX,
|
||||
cookieStore,
|
||||
cookieKey: CookieKeys.FontSize,
|
||||
cookieOptions: {
|
||||
days: 365,
|
||||
path: "/",
|
||||
sameSite: "Lax",
|
||||
},
|
||||
cssVariable: "--content-font-size",
|
||||
eventName: "fontSizeChange",
|
||||
});
|
||||
|
||||
// CSS 변수 동기화
|
||||
useEffect(() => {
|
||||
if (typeof document === "undefined") return;
|
||||
document.documentElement.style.setProperty(
|
||||
"--content-font-size",
|
||||
`${fontSize}px`,
|
||||
);
|
||||
}, [fontSize]);
|
||||
export const useFontSize =
|
||||
fontSizeHook.useFontSize;
|
||||
|
||||
// 다른 컴포넌트/페이지에서 변경한 폰트 크기와 상태 동기화
|
||||
// 요청사항 반영: 브로드캐스트 수신 시 event.detail 대신 "쿠키"를 다시 읽어 반영한다.
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
export const useFontSizeValue =
|
||||
fontSizeHook.useFontSizeValue;
|
||||
|
||||
const handler = (_e: Event) => {
|
||||
// 쿠키에 저장된 최신 값을 읽어 상태를 동기화한다.
|
||||
// 기본값은 현재 상태값을 폴백으로 사용하여 불필요한 변경을 방지한다.
|
||||
const next = readFontSizeFromCookie(fontSize);
|
||||
setFontSize(next);
|
||||
};
|
||||
|
||||
window.addEventListener("fontSizeChange", handler as EventListener);
|
||||
return () => window.removeEventListener("fontSizeChange", handler as EventListener);
|
||||
}, []);
|
||||
|
||||
const updateFontSize = useCallback((next: number) => {
|
||||
const clamped = clampFontSize(next);
|
||||
setFontSize(clamped);
|
||||
writeFontSizeToCookie(clamped);
|
||||
// 전역 이벤트로 다른 훅 인스턴스들에 변경 사항 전파
|
||||
if (typeof window !== "undefined") {
|
||||
window.dispatchEvent(new CustomEvent("fontSizeChange", { detail: clamped }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
return [fontSize, updateFontSize] as const;
|
||||
}
|
||||
|
||||
export const useFontSizeValue = (
|
||||
options: UseFontSizeOptions = {},
|
||||
) => {
|
||||
const defaultSize = FONT_SIZE.DEFAULT;
|
||||
const [fontSize] = useFontSize(defaultSize, options);
|
||||
return fontSize;
|
||||
}
|
||||
export const clampFontSize =
|
||||
fontSizeHook.clampFontSize;
|
||||
|
||||
Reference in New Issue
Block a user