見た目の変更
既定の見た目は Excel に近く整えてあります。色・フォント・サイズは 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, {
locale: 'ja',
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)か、その上のどの要素にでも指定します。アプリ全体で 1 セット、画面ごとに別々、グリッド 1 つだけ — すべて同じ方法です。
:root {
--hg-font: "Noto Sans JP", 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'(既定) | 横 · 縦の線。Excel と同じ |
'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; }セル 1 つには列の 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;
}