507 lines
17 KiB
TypeScript
507 lines
17 KiB
TypeScript
/* =============================================================================
|
|
* ui_template_elements.ts
|
|
* 공통 컴포넌트 및 그룹 템플릿 정의
|
|
*
|
|
* 원칙:
|
|
* - 모든 컴포넌트는 임의 스타일링 금지, 여기 규격을 최우선 상속/참조 (frontend.md §2).
|
|
* - 색상/간격은 CSS 클래스 + theme.css 변수로만 처리, JS 내 인라인 색상 금지.
|
|
* - 팩토리 함수는 HTMLElement를 반환하는 vanilla TS 패턴.
|
|
* - 스타일 규칙은 injectBaseStyles()로 1회 주입 (design.md 컴포넌트 명세 기반).
|
|
* ========================================================================== */
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 0. 내부 유틸
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
/** 요소 생성 + 속성/클래스/자식 일괄 설정 헬퍼 */
|
|
function el<K extends keyof HTMLElementTagNameMap>(
|
|
tag: K,
|
|
options: {
|
|
className?: string;
|
|
text?: string;
|
|
attrs?: Record<string, string>;
|
|
children?: (HTMLElement | string)[];
|
|
} = {},
|
|
): HTMLElementTagNameMap[K] {
|
|
const node = document.createElement(tag);
|
|
if (options.className) node.className = options.className;
|
|
if (options.text !== undefined) node.textContent = options.text;
|
|
if (options.attrs) {
|
|
for (const [k, v] of Object.entries(options.attrs)) {
|
|
node.setAttribute(k, v);
|
|
}
|
|
}
|
|
if (options.children) {
|
|
for (const child of options.children) {
|
|
node.append(child);
|
|
}
|
|
}
|
|
return node;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 1. 버튼 (Button) — design.md: Filled Brand / Ghost Outlined / Pill Nav
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
export type ButtonVariant = "filled" | "ghost" | "pill" | "danger";
|
|
|
|
export interface ButtonOptions {
|
|
/** 표시 텍스트 (i18n 결과 문자열을 전달) */
|
|
label: string;
|
|
variant?: ButtonVariant;
|
|
/** 이벤트 핸들러 (frontend.md §4: on[페이지]_[기능]_[액션] 명명) */
|
|
onClick?: (ev: MouseEvent) => void;
|
|
disabled?: boolean;
|
|
type?: "button" | "submit" | "reset";
|
|
/** 앞쪽 아이콘 요소 (선택) */
|
|
iconStart?: HTMLElement;
|
|
}
|
|
|
|
export function createButton(opts: ButtonOptions): HTMLButtonElement {
|
|
const variant = opts.variant ?? "filled";
|
|
const btn = el("button", {
|
|
className: `ui-btn ui-btn--${variant}`,
|
|
attrs: { type: opts.type ?? "button" },
|
|
});
|
|
if (opts.iconStart) {
|
|
opts.iconStart.classList.add("ui-btn__icon");
|
|
btn.append(opts.iconStart);
|
|
}
|
|
btn.append(el("span", { className: "ui-btn__label", text: opts.label }));
|
|
if (opts.disabled) btn.disabled = true;
|
|
if (opts.onClick) btn.addEventListener("click", opts.onClick);
|
|
return btn;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 2. 입력 필드 (Input Field) — design.md: 1px mist border, 8px radius, focus ring
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
export interface InputFieldOptions {
|
|
/** 라벨 텍스트 (i18n 결과) */
|
|
label?: string;
|
|
/** placeholder (i18n 결과) */
|
|
placeholder?: string;
|
|
type?: "text" | "password" | "email" | "number" | "search" | "tel";
|
|
value?: string;
|
|
required?: boolean;
|
|
/** number 타입 범위 (1차 유효성 검사용, frontend.md §4) */
|
|
min?: number;
|
|
max?: number;
|
|
onInput?: (value: string) => void;
|
|
}
|
|
|
|
export interface InputFieldHandle {
|
|
/** 필드 컨테이너 (라벨 + 입력 + 에러 슬롯) */
|
|
root: HTMLDivElement;
|
|
input: HTMLInputElement;
|
|
/** 에러 메시지 표시/해제. msg가 없으면 에러 해제. */
|
|
setError: (msg?: string) => void;
|
|
}
|
|
|
|
export function createInputField(opts: InputFieldOptions): InputFieldHandle {
|
|
const root = el("div", { className: "ui-field" });
|
|
|
|
if (opts.label) {
|
|
root.append(el("label", { className: "ui-field__label", text: opts.label }));
|
|
}
|
|
|
|
const input = el("input", {
|
|
className: "ui-input",
|
|
attrs: {
|
|
type: opts.type ?? "text",
|
|
...(opts.placeholder ? { placeholder: opts.placeholder } : {}),
|
|
...(opts.required ? { required: "true" } : {}),
|
|
...(opts.min !== undefined ? { min: String(opts.min) } : {}),
|
|
...(opts.max !== undefined ? { max: String(opts.max) } : {}),
|
|
},
|
|
});
|
|
if (opts.value !== undefined) input.value = opts.value;
|
|
if (opts.onInput) {
|
|
input.addEventListener("input", () => opts.onInput!(input.value));
|
|
}
|
|
|
|
// 에러 메시지 슬롯 (입력창 하단 표기, frontend.md §4)
|
|
const errorSlot = el("span", { className: "ui-field__error", attrs: { role: "alert" } });
|
|
|
|
root.append(input, errorSlot);
|
|
|
|
const setError = (msg?: string): void => {
|
|
if (msg) {
|
|
input.classList.add("ui-input--error");
|
|
errorSlot.textContent = msg;
|
|
errorSlot.classList.add("is-visible");
|
|
} else {
|
|
input.classList.remove("ui-input--error");
|
|
errorSlot.textContent = "";
|
|
errorSlot.classList.remove("is-visible");
|
|
}
|
|
};
|
|
|
|
return { root, input, setError };
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 3. 카드 / 패널 (Card) — design.md: white surface, 8px radius, shadow
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
export interface CardOptions {
|
|
title?: string;
|
|
/** 본문 자식 요소들 */
|
|
body?: (HTMLElement | string)[];
|
|
/** 그림자 강조 여부 (shadow-lg 사용) */
|
|
raised?: boolean;
|
|
}
|
|
|
|
export function createCard(opts: CardOptions): HTMLDivElement {
|
|
const card = el("div", {
|
|
className: `ui-card${opts.raised ? " ui-card--raised" : ""}`,
|
|
});
|
|
if (opts.title) {
|
|
card.append(el("h3", { className: "ui-card__title", text: opts.title }));
|
|
}
|
|
if (opts.body) {
|
|
card.append(el("div", { className: "ui-card__body", children: opts.body }));
|
|
}
|
|
return card;
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 4. 태그 / 배지 (Tag / Pill) — design.md: 1440px radius pill
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
export type TagVariant = "accent" | "neutral" | "success" | "warning" | "danger";
|
|
|
|
export function createTag(label: string, variant: TagVariant = "neutral"): HTMLSpanElement {
|
|
return el("span", { className: `ui-tag ui-tag--${variant}`, text: label });
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 5. 로딩 오버레이 (Loading Overlay) — frontend.md §4: API 호출 시 필수
|
|
* 싱글턴 오버레이. show/hide로 참조 카운트 관리.
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
let overlayEl: HTMLDivElement | null = null;
|
|
let overlayCount = 0;
|
|
|
|
function ensureOverlay(): HTMLDivElement {
|
|
if (overlayEl) return overlayEl;
|
|
const spinner = el("div", { className: "ui-spinner", attrs: { "aria-hidden": "true" } });
|
|
overlayEl = el("div", {
|
|
className: "ui-overlay",
|
|
attrs: { role: "status", "aria-live": "polite" },
|
|
children: [spinner],
|
|
});
|
|
document.body.append(overlayEl);
|
|
return overlayEl;
|
|
}
|
|
|
|
/** 로딩 스피너 오버레이 표시 (중첩 호출 안전) */
|
|
export function showLoadingOverlay(): void {
|
|
overlayCount += 1;
|
|
ensureOverlay().classList.add("is-active");
|
|
}
|
|
|
|
/** 로딩 스피너 오버레이 해제 (모든 참조 종료 시 실제 숨김) */
|
|
export function hideLoadingOverlay(): void {
|
|
overlayCount = Math.max(0, overlayCount - 1);
|
|
if (overlayCount === 0 && overlayEl) {
|
|
overlayEl.classList.remove("is-active");
|
|
}
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 6. 토스트 알림 (Toast) — 성공/에러 배너
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
export type ToastKind = "success" | "error" | "info" | "warning";
|
|
|
|
let toastContainer: HTMLDivElement | null = null;
|
|
|
|
function ensureToastContainer(): HTMLDivElement {
|
|
if (toastContainer) return toastContainer;
|
|
toastContainer = el("div", { className: "ui-toast-container" });
|
|
document.body.append(toastContainer);
|
|
return toastContainer;
|
|
}
|
|
|
|
/** 토스트 표시. durationMs 후 자동 소멸. */
|
|
export function showToast(message: string, kind: ToastKind = "info", durationMs = 3000): void {
|
|
const toast = el("div", { className: `ui-toast ui-toast--${kind}`, text: message });
|
|
ensureToastContainer().append(toast);
|
|
// 진입 애니메이션 트리거
|
|
requestAnimationFrame(() => toast.classList.add("is-visible"));
|
|
window.setTimeout(() => {
|
|
toast.classList.remove("is-visible");
|
|
window.setTimeout(() => toast.remove(), 200);
|
|
}, durationMs);
|
|
}
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
* 7. 워크플로우 3단 레이아웃 셸 (frontend.md §2)
|
|
* 상단 헤더 + 좌측 고정 패널 + 우측 뷰어 영역.
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
export interface WorkflowShellOptions {
|
|
/** 상단 페이지 타이틀 (i18n 결과) */
|
|
title: string;
|
|
/** 상단 진행 단계 라벨 배열 (i18n 결과) */
|
|
steps?: string[];
|
|
/** 현재 활성 단계 인덱스 */
|
|
activeStep?: number;
|
|
}
|
|
|
|
export interface WorkflowShellHandle {
|
|
root: HTMLDivElement;
|
|
/** 좌측 입력 폼/설정 영역 (너비 고정) */
|
|
leftPanel: HTMLDivElement;
|
|
/** 우측 WebCAD 뷰어 / 결과 그리드 영역 */
|
|
rightArea: HTMLDivElement;
|
|
}
|
|
|
|
export function createWorkflowShell(opts: WorkflowShellOptions): WorkflowShellHandle {
|
|
// 상단 헤더
|
|
const titleEl = el("h2", { className: "ui-wf__title", text: opts.title });
|
|
const headerChildren: HTMLElement[] = [titleEl];
|
|
|
|
if (opts.steps && opts.steps.length > 0) {
|
|
const stepsEl = el("div", { className: "ui-wf__steps" });
|
|
opts.steps.forEach((label, idx) => {
|
|
const isActive = idx === (opts.activeStep ?? 0);
|
|
stepsEl.append(
|
|
el("span", {
|
|
className: `ui-wf__step${isActive ? " is-active" : ""}`,
|
|
text: label,
|
|
}),
|
|
);
|
|
});
|
|
headerChildren.push(stepsEl);
|
|
}
|
|
|
|
const header = el("header", { className: "ui-wf__header", children: headerChildren });
|
|
const leftPanel = el("div", { className: "ui-wf__left" });
|
|
const rightArea = el("div", { className: "ui-wf__right" });
|
|
const bodyRow = el("div", { className: "ui-wf__body", children: [leftPanel, rightArea] });
|
|
|
|
const root = el("div", { className: "ui-wf", children: [header, bodyRow] });
|
|
return { root, leftPanel, rightArea };
|
|
}
|
|
|
|
/* =============================================================================
|
|
* 8. 기본 컴포넌트 스타일 주입 (injectBaseStyles)
|
|
* theme.css 변수만 참조. 앱 진입 시 1회 호출.
|
|
* ========================================================================== */
|
|
|
|
const BASE_STYLE_ID = "ui-template-elements-style";
|
|
|
|
const BASE_CSS = `
|
|
/* --- Button --- */
|
|
.ui-btn {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: var(--spacing-8);
|
|
font-family: var(--font-body);
|
|
font-size: var(--text-body);
|
|
font-weight: var(--font-weight-medium);
|
|
line-height: 1;
|
|
border: 1px solid transparent;
|
|
border-radius: var(--radius-buttons);
|
|
padding: var(--spacing-16) var(--spacing-24);
|
|
cursor: pointer;
|
|
transition: background-color var(--transition-fast),
|
|
border-color var(--transition-fast), color var(--transition-fast);
|
|
}
|
|
.ui-btn:disabled { opacity: 0.5; cursor: not-allowed; }
|
|
.ui-btn__icon { display: inline-flex; width: 16px; height: 16px; }
|
|
|
|
.ui-btn--filled {
|
|
background-color: var(--color-primary);
|
|
color: var(--color-primary-text);
|
|
box-shadow: var(--shadow-sm);
|
|
}
|
|
.ui-btn--filled:hover:not(:disabled) { background-color: var(--color-royal-amethyst); }
|
|
|
|
.ui-btn--ghost {
|
|
background-color: transparent;
|
|
border-color: var(--color-primary);
|
|
color: var(--color-primary);
|
|
}
|
|
.ui-btn--ghost:hover:not(:disabled) { background-color: var(--color-mist-violet); }
|
|
|
|
.ui-btn--pill {
|
|
background-color: transparent;
|
|
color: var(--color-plum-velvet);
|
|
border-radius: var(--radius-pills);
|
|
padding: var(--spacing-8) var(--spacing-16);
|
|
font-size: var(--text-body-sm);
|
|
}
|
|
.ui-btn--pill:hover:not(:disabled),
|
|
.ui-btn--pill.is-active { background-color: var(--color-mist-violet); }
|
|
|
|
.ui-btn--danger {
|
|
background-color: var(--color-danger);
|
|
color: var(--color-canvas);
|
|
}
|
|
|
|
/* --- Input Field --- */
|
|
.ui-field { display: flex; flex-direction: column; gap: var(--spacing-4); }
|
|
.ui-field__label {
|
|
font-size: var(--text-caption);
|
|
font-weight: var(--font-weight-medium);
|
|
color: var(--color-text-secondary);
|
|
}
|
|
.ui-input {
|
|
font-family: var(--font-body);
|
|
font-size: var(--text-body-sm);
|
|
color: var(--color-text-body);
|
|
background-color: var(--color-canvas);
|
|
border: 1px solid var(--color-border);
|
|
border-radius: var(--radius-inputs);
|
|
padding: 10px 14px;
|
|
transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
|
|
}
|
|
.ui-input::placeholder { color: var(--color-text-muted); }
|
|
.ui-input:focus {
|
|
outline: none;
|
|
border-color: var(--color-focus-ring);
|
|
box-shadow: 0 0 0 1px var(--color-focus-ring);
|
|
}
|
|
.ui-input--error { border-color: var(--color-danger); }
|
|
.ui-field__error {
|
|
font-size: var(--text-caption);
|
|
color: var(--color-danger);
|
|
min-height: 0;
|
|
visibility: hidden;
|
|
}
|
|
.ui-field__error.is-visible { visibility: visible; }
|
|
|
|
/* --- Card --- */
|
|
.ui-card {
|
|
background-color: var(--color-surface-raised);
|
|
border: 1px solid var(--color-border);
|
|
border-radius: var(--radius-cards);
|
|
padding: var(--card-padding);
|
|
box-shadow: var(--shadow-sm);
|
|
}
|
|
.ui-card--raised { box-shadow: var(--shadow-lg); border-color: transparent; }
|
|
.ui-card__title {
|
|
font-family: var(--font-display);
|
|
color: var(--color-plum-velvet);
|
|
margin-bottom: var(--spacing-16);
|
|
}
|
|
.ui-card__body { display: flex; flex-direction: column; gap: var(--spacing-16); }
|
|
|
|
/* --- Tag / Pill --- */
|
|
.ui-tag {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
border-radius: var(--radius-pills);
|
|
padding: var(--spacing-4) var(--spacing-16);
|
|
font-size: var(--text-caption);
|
|
font-weight: var(--font-weight-medium);
|
|
line-height: 1.3;
|
|
}
|
|
.ui-tag--accent { background-color: var(--color-mist-violet); color: var(--color-royal-amethyst); }
|
|
.ui-tag--neutral { background-color: var(--color-paper); color: var(--color-slate); }
|
|
.ui-tag--success { background-color: var(--color-mist-violet); color: var(--color-success); }
|
|
.ui-tag--warning { background-color: var(--color-paper); color: var(--color-warning); }
|
|
.ui-tag--danger { background-color: var(--color-paper); color: var(--color-danger); }
|
|
|
|
/* --- Loading Overlay + Spinner --- */
|
|
.ui-overlay {
|
|
position: fixed;
|
|
inset: 0;
|
|
display: none;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background-color: rgba(38, 17, 74, 0.24);
|
|
z-index: var(--z-overlay);
|
|
}
|
|
.ui-overlay.is-active { display: flex; }
|
|
.ui-spinner {
|
|
width: 40px;
|
|
height: 40px;
|
|
border: 3px solid var(--color-mist-violet);
|
|
border-top-color: var(--color-royal-amethyst);
|
|
border-radius: var(--radius-pills);
|
|
animation: ui-spin 0.8s linear infinite;
|
|
}
|
|
@keyframes ui-spin { to { transform: rotate(360deg); } }
|
|
|
|
/* --- Toast --- */
|
|
.ui-toast-container {
|
|
position: fixed;
|
|
top: var(--spacing-24);
|
|
right: var(--spacing-24);
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: var(--spacing-8);
|
|
z-index: var(--z-toast);
|
|
}
|
|
.ui-toast {
|
|
padding: var(--spacing-16) var(--spacing-24);
|
|
border-radius: var(--radius-cards);
|
|
font-size: var(--text-body-sm);
|
|
color: var(--color-canvas);
|
|
box-shadow: var(--shadow-lg);
|
|
opacity: 0;
|
|
transform: translateX(16px);
|
|
transition: opacity var(--transition-base), transform var(--transition-base);
|
|
}
|
|
.ui-toast.is-visible { opacity: 1; transform: translateX(0); }
|
|
.ui-toast--success { background-color: var(--color-success); }
|
|
.ui-toast--error { background-color: var(--color-danger); }
|
|
.ui-toast--warning { background-color: var(--color-warning); }
|
|
.ui-toast--info { background-color: var(--color-royal-amethyst); }
|
|
|
|
/* --- Workflow Shell (3단 레이아웃) --- */
|
|
.ui-wf { display: flex; flex-direction: column; height: 100%; }
|
|
.ui-wf__header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
height: var(--wf-header-height);
|
|
padding: 0 var(--spacing-24);
|
|
border-bottom: 1px solid var(--color-border);
|
|
background-color: var(--color-surface-raised);
|
|
}
|
|
.ui-wf__title { font-size: var(--text-subheading); }
|
|
.ui-wf__steps { display: flex; gap: var(--spacing-8); }
|
|
.ui-wf__step {
|
|
padding: var(--spacing-4) var(--spacing-16);
|
|
border-radius: var(--radius-pills);
|
|
font-size: var(--text-caption);
|
|
font-weight: var(--font-weight-medium);
|
|
color: var(--color-slate);
|
|
background-color: var(--color-paper);
|
|
}
|
|
.ui-wf__step.is-active {
|
|
color: var(--color-royal-amethyst);
|
|
background-color: var(--color-mist-violet);
|
|
}
|
|
.ui-wf__body { display: flex; flex: 1; min-height: 0; }
|
|
.ui-wf__left {
|
|
width: var(--wf-left-panel-width);
|
|
flex-shrink: 0;
|
|
padding: var(--spacing-24);
|
|
border-right: 1px solid var(--color-border);
|
|
overflow-y: auto;
|
|
background-color: var(--color-surface);
|
|
}
|
|
.ui-wf__right {
|
|
flex: 1;
|
|
min-width: 0;
|
|
overflow: auto;
|
|
background-color: var(--color-bg);
|
|
}
|
|
`;
|
|
|
|
/** 공통 컴포넌트 기본 스타일을 <head>에 1회 주입. 앱 진입 시 호출. */
|
|
export function injectBaseStyles(): void {
|
|
if (document.getElementById(BASE_STYLE_ID)) return;
|
|
const style = el("style", { attrs: { id: BASE_STYLE_ID } });
|
|
style.textContent = BASE_CSS;
|
|
document.head.append(style);
|
|
}
|