Files
admin/packages/hooks/src/usePageMeta/usePageMeta.ts
T
2026-06-25 15:53:49 +03:00

46 lines
927 B
TypeScript

'use client';
import { useEffect } from 'react';
export interface UsePageMetaParams {
title?: string;
description?: string;
}
export function usePageMeta({ title, description }: UsePageMetaParams) {
useEffect(() => {
if (!title) return;
const previousTitle = document.title;
document.title = title;
return () => {
document.title = previousTitle;
};
}, [title]);
useEffect(() => {
if (!description) return;
let tag = document.querySelector<HTMLMetaElement>('meta[name="description"]');
const created = !tag;
if (!tag) {
tag = document.createElement('meta');
tag.name = 'description';
document.head.appendChild(tag);
}
const previousContent = tag.content;
tag.content = description;
return () => {
if (created) {
tag.remove();
} else {
tag.content = previousContent;
}
};
}, [description]);
}