위젯 영역은 관리자가 콘텐츠 블록을 배치할 수 있는 템플릿의 이름이 지정된 영역입니다. 사이드바, 푸터 열, 프로모션 배너 또는 편집자가 코드를 건드리지 않고 제어해야 하는 섹션에 사용하세요.
위젯 영역 조회
이름으로 위젯 영역을 가져오려면 getWidgetArea()를 사용합니다.
---
import { getWidgetArea } from "emdash";
const sidebar = await getWidgetArea("sidebar");
---
{sidebar && sidebar.widgets.length > 0 && (
<aside class="sidebar">
{sidebar.widgets.map(widget => (
<div class="widget">
{widget.title && <h3>{widget.title}</h3>}
<!-- 위젯 콘텐츠 렌더링 -->
</div>
))}
</aside>
)}
위젯 영역이 없으면 null이 반환됩니다.
위젯 영역 구조
위젯 영역에는 메타데이터와 위젯 배열이 포함됩니다.
interface WidgetArea {
id: string;
name: string; // 고유 식별자 ("sidebar", "footer-1")
label: string; // 표시 이름 ("메인 사이드바")
description?: string;
widgets: Widget[];
}
interface Widget {
id: string;
type: "content" | "menu" | "component";
title?: string;
// 유형별 필드
content?: PortableTextBlock[]; // 콘텐츠 위젯
menuName?: string; // 메뉴 위젯
componentId?: string; // 컴포넌트 위젯
componentProps?: Record<string, unknown>;
}
위젯 유형
EmDash는 세 가지 위젯 유형을 지원합니다.
콘텐츠 위젯
Portable Text로 저장된 서식 있는 텍스트. PortableText 컴포넌트로 렌더링합니다.
---
import { PortableText } from "emdash/ui";
---
{widget.type === "content" && widget.content && (
<div class="widget-content">
<PortableText value={widget.content} />
</div>
)}
메뉴 위젯
위젯 영역 안에 탐색 메뉴를 표시합니다.
---
import { getMenu } from "emdash";
const menu = widget.menuName ? await getMenu(widget.menuName) : null;
---
{widget.type === "menu" && menu && (
<nav class="widget-nav">
<ul>
{menu.items.map(item => (
<li><a href={item.url}>{item.label}</a></li>
))}
</ul>
</nav>
)}
컴포넌트 위젯
등록된 컴포넌트를 구성 가능한 props로 렌더링합니다. EmDash에는 다음 핵심 컴포넌트가 포함됩니다.
| 컴포넌트 ID | 설명 | Props |
|---|---|---|
core:recent-posts | 최근 글 목록 | count, showThumbnails, showDate |
core:categories | 카테고리 목록 | showCount, hierarchical |
core:tags | 태그 클라우드 | showCount, limit |
core:search | 검색 폼 | placeholder |
core:archives | 월별/연별 아카이브 | type, limit |
위젯 렌더링
재사용 가능한 위젯 렌더러 컴포넌트를 만듭니다.
---
import { PortableText } from "emdash/ui";
import { getMenu } from "emdash";
import type { Widget } from "emdash";
import RecentPosts from "./widgets/RecentPosts.astro";
import Categories from "./widgets/Categories.astro";
import TagCloud from "./widgets/TagCloud.astro";
import SearchForm from "./widgets/SearchForm.astro";
import Archives from "./widgets/Archives.astro";
interface Props {
widget: Widget;
}
const { widget } = Astro.props;
const componentMap: Record<string, any> = {
"core:recent-posts": RecentPosts,
"core:categories": Categories,
"core:tags": TagCloud,
"core:search": SearchForm,
"core:archives": Archives,
};
const menu = widget.type === "menu" && widget.menuName
? await getMenu(widget.menuName)
: null;
---
<div class="widget">
{widget.title && <h3 class="widget-title">{widget.title}</h3>}
{widget.type === "content" && widget.content && (
<div class="widget-content">
<PortableText value={widget.content} />
</div>
)}
{widget.type === "menu" && menu && (
<nav class="widget-menu">
<ul>
{menu.items.map(item => (
<li><a href={item.url}>{item.label}</a></li>
))}
</ul>
</nav>
)}
{widget.type === "component" && widget.componentId && componentMap[widget.componentId] && (
<Fragment>
{(() => {
const Component = componentMap[widget.componentId!];
return <Component {...widget.componentProps} />;
})()}
</Fragment>
)}
</div>
위젯 컴포넌트 예시
최근 글 위젯
---
import { getEmDashCollection } from "emdash";
interface Props {
count?: number;
showThumbnails?: boolean;
showDate?: boolean;
}
const { count = 5, showThumbnails = false, showDate = true } = Astro.props;
const { entries: posts } = await getEmDashCollection("posts", {
limit: count,
orderBy: { publishedAt: "desc" },
});
---
<ul class="recent-posts">
{posts.map(post => (
<li>
{showThumbnails && post.data.featured_image && (
<img src={post.data.featured_image} alt="" class="thumbnail" />
)}
<a href={`/posts/${post.slug}`}>{post.data.title}</a>
{showDate && post.data.publishedAt && (
<time datetime={post.data.publishedAt.toISOString()}>
{post.data.publishedAt.toLocaleDateString()}
</time>
)}
</li>
))}
</ul>
검색 위젯
---
interface Props {
placeholder?: string;
}
const { placeholder = "검색…" } = Astro.props;
---
<form action="/search" method="get" class="search-form">
<input
type="search"
name="q"
placeholder={placeholder}
aria-label="검색"
/>
<button type="submit">검색</button>
</form>
레이아웃에서 위젯 영역 사용
사이드바 위젯 영역이 있는 블로그 레이아웃 예시입니다.
---
import { getWidgetArea } from "emdash";
import WidgetRenderer from "../components/WidgetRenderer.astro";
const sidebar = await getWidgetArea("sidebar");
---
<div class="layout">
<main class="content">
<slot />
</main>
{sidebar && sidebar.widgets.length > 0 && (
<aside class="sidebar">
{sidebar.widgets.map(widget => (
<WidgetRenderer widget={widget} />
))}
</aside>
)}
</div>
<style>
.layout {
display: grid;
grid-template-columns: 1fr 300px;
gap: 2rem;
}
@media (max-width: 768px) {
.layout {
grid-template-columns: 1fr;
}
}
</style>
모든 위젯 영역 나열
getWidgetAreas()로 위젯이 채워진 모든 영역을 가져옵니다.
import { getWidgetAreas } from "emdash";
const areas = await getWidgetAreas();
// 위젯이 포함된 모든 영역 반환
위젯 영역 만들기
관리 화면 /_emdash/admin/widgets에서 만들거나 관리 API를 사용합니다.
POST /_emdash/api/widget-areas
Content-Type: application/json
{
"name": "footer-1",
"label": "푸터 열 1",
"description": "푸터의 첫 번째 열"
}
콘텐츠 위젯 추가:
POST /_emdash/api/widget-areas/footer-1/widgets
Content-Type: application/json
{
"type": "content",
"title": "소개",
"content": [
{
"_type": "block",
"style": "normal",
"children": [{ "_type": "span", "text": "사이트에 오신 것을 환영합니다." }]
}
]
}
컴포넌트 위젯 추가:
POST /_emdash/api/widget-areas/sidebar/widgets
Content-Type: application/json
{
"type": "component",
"title": "최근 글",
"componentId": "core:recent-posts",
"componentProps": { "count": 5, "showDate": true }
}
API 참조
getWidgetArea(name)
이름으로 위젯 영역과 모든 위젯을 가져옵니다.
매개변수:
name— 위젯 영역의 고유 식별자(문자열)
반환: Promise<WidgetArea | null>
getWidgetAreas()
모든 위젯 영역과 위젯을 나열합니다.
반환: Promise<WidgetArea[]>
getWidgetComponents()
관리 UI에서 사용할 수 있는 위젯 컴포넌트 정의를 나열합니다.
반환: WidgetComponentDef[]