모양 바꾸기
기본 모양은 엑셀과 비슷하게 잡혀 있다. 색 · 글꼴 · 크기는 CSS 변수로, 행 · 칸별 강조는 클래스로 바꾼다. 그리드의 내부 CSS 를 덮어쓸 일은 거의 없다.
styling.js
import { createGrid } from 'horee-grid'
const rows = Array.from({ length: 100 }, (_, i) => ({ id: i + 1, item: `품목 ${i + 1}`, stock: (i * 37) % 120, safe: 40 }))
const grid = createGrid(host, {
rowKey: 'id',
rowHeight: 32,
columns: [
{ field: 'item', title: '품목', flex: 1 },
{ field: 'stock', title: '재고', type: 'number', width: 100 },
{ field: 'safe', title: '안전재고', type: 'number', width: 100 }
],
rows,
rowClass: r => (r.stock < r.safe ? 'short' : undefined)
})
const css = document.createElement('style')
css.textContent = `
.hg.theme-ocean { --hg-accent: #0b63ce; --hg-sel: #dbe9ff; --hg-head-bg: #eef3fa; --hg-line: #d5deea; }
.hg.theme-ink { --hg-bg: #111418; --hg-fg: #e5e8eb; --hg-line: #2a2f36; --hg-head-bg: #191d22; --hg-head-fg: #aab2bb;
--hg-sel: #273244; --hg-hover: #1a2029; --hg-accent: #f0b429; }
.hg-row.short .hg-cell { color: #d1242f; }`
document.head.append(css)
toolbar.innerHTML = `
<button data-theme="">기본</button><button data-theme="theme-ocean">ocean</button><button data-theme="theme-ink">ink</button>
<button data-lines="all">선 all</button><button data-lines="row">선 row</button><button data-lines="none">선 none</button>`
toolbar.onclick = e => {
const el = host.querySelector('.hg')
const { theme, lines } = e.target.dataset
if (theme !== undefined) {
el.classList.remove('theme-ocean', 'theme-ink')
if (theme) el.classList.add(theme)
}
if (lines) {
// lines 옵션이 붙이는 클래스와 같다 (만들 때 lines: 'row' 로 주는 것이 보통)
el.classList.remove('hg-lines-row', 'hg-lines-none')
if (lines !== 'all') el.classList.add('hg-lines-' + lines)
}
}CSS 변수
그리드 요소(.hg)나 그 위 어느 요소에든 준다. 앱 전체에 한 벌, 화면마다 다르게, 그리드 하나만 — 모두 같은 방법이다.
:root {
--hg-font: "Pretendard", sans-serif;
--hg-font-size: 13px;
--hg-accent: #0b63ce;
}
.dark .hg {
--hg-bg: #111418;
--hg-fg: #e5e8eb;
--hg-line: #2a2f36;
--hg-head-bg: #191d22;
}| 변수 | 쓰이는 곳 | 기본 |
|---|---|---|
--hg-font | 글꼴 | 부모를 따름 |
--hg-font-size | 글자 크기 | 13px |
--hg-bg | 칸 배경 | #fff |
--hg-fg | 글자 | #1f2328 |
--hg-line | 칸 선 · 테두리 | #d4d4d4 |
--hg-head-bg | 머리글 · 행 번호 배경 | #f3f3f3 |
--hg-head-fg | 머리글 글자 | #333 |
--hg-foot-bg | 아래 고정 행(합계) 배경 | --hg-head-bg |
--hg-sel | 고른 행 배경 | #e2f0d9 |
--hg-hover | 마우스가 올라간 행 | #f5f9ff |
--hg-accent | 활성 칸 · 정렬 표시 · 체크 · 끌기 표시 | #107c41 |
--hg-dirty | 바뀐 칸 표시 | #e8590c |
--hg-error | 틀린 값 테두리 | #d93025 |
--hg-row-h | 행 높이 (옵션 rowHeight 가 정한다) | 27px |
행 높이는 옵션으로
--hg-row-h 는 그리드가 옵션 rowHeight 로 직접 넣는다. 스크롤 계산에 쓰이므로 CSS 로 바꾸지 말고 rowHeight 를 준다. 머리글 줄 높이는 headerHeight(기본 행 높이 + 2).
칸 선
createGrid(host, { columns, rows, lines: 'row' })lines | 모양 |
|---|---|
'all' (기본) | 가로 · 세로 선. 엑셀과 같다 |
'row' | 가로 선만. 조회 목록에 어울린다 |
'none' | 선 없음 |
행 · 칸 강조
createGrid(host, {
columns,
rows,
rowClass: row => (row.stock < row.safe ? 'short' : undefined)
}).hg-row.short .hg-cell { color: #d1242f; }
.hg-row.short .hg-cell:first-child { font-weight: 600; }칸 하나는 컬럼의 cellClass:
{ field: 'diff', title: '차이', type: 'number', cellClass: ({ value }) => (value ? 'warn' : undefined) }배경색은 칸에
.hg-row 에 배경을 주면 보이지 않는다. 칸(.hg-cell)이 자기 배경을 갖기 때문이다. .hg-row.이름 .hg-cell 로 준다.
그리드가 붙이는 클래스
화면 CSS 에서 기대어도 되는 클래스다. 이 밖의 내부 클래스 이름은 판이 바뀌면 달라질 수 있다.
| 클래스 | 붙는 곳 |
|---|---|
.hg | 그리드 바깥 요소 |
.hg-row · .hg-cell | 행 · 칸 |
.hg-cell[data-c="컬럼 id"] | 특정 컬럼의 칸 |
.hg-sel | 고른 행 (.hg-row) |
.hg-cur | 지금 보는 행 (setCurrent) |
.hg-active | 활성 칸 |
.hg-dirty | 바뀐 칸 |
.hg-foot | 아래 고정 행 영역 |
.hg-loading | 서버 자료를 받는 중 (.hg) |
.hg-lines-row · .hg-lines-none | 옵션 lines (.hg) |
어두운 화면
변수만 바꾸면 된다. 이 문서의 예제도 오른쪽 위 ☾ 단추를 누르면 같은 방법으로 바뀐다.
[data-theme="dark"] .hg {
--hg-bg: #14181c;
--hg-fg: #e6e9ec;
--hg-line: #2a3037;
--hg-head-bg: #1b2026;
--hg-head-fg: #c3cad2;
--hg-sel: #17352a;
--hg-hover: #1a222a;
--hg-accent: #3ccf7f;
}