ho-gridb0.0.7

스타일

기본 스타일은 엑셀과 유사하게 구성되어 있다. 색상 · 글꼴 · 크기는 CSS 변수로, 행 · 셀 단위 강조는 클래스로 변경한다. 그리드 내부 CSS 를 덮어쓸 필요는 거의 없다.

styling.js
import { createGrid } from 'ho-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-created추가 행 표시#1c7ed6
--hg-error유효하지 않은 값 테두리#d93025
--hg-found찾은 셀 배경연한 노란색
--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-status상태 표시줄
.hg-lookup코드 도움 🔍 버튼
.hg-foot하단 고정 행 영역
.hg-loading서버 데이터 수신 중 (.hg)
.hg-lines-row · .hg-lines-nonelines 옵션 (.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;
}