본문으로 건너뛰기Skip to content
예제 첫화면Examples home

행·칸 선택 색 바꾸기Change the row and cell selection colors

선택한 행, 선택한 칸 블록, 처음 누른 칸의 색은 그리드의 CSS 변수라서 몇 줄로 바꿀 수 있어요. 글자색을 섞는 방식(color-mix)으로 정하면 밝은 테마와 어두운 테마 모두에서 잘 읽혀요.The colors of selected rows, the selected cell block and the anchor cell are CSS variables of the grid, so a few lines restyle them. Mixing with the theme color (color-mix) keeps them readable in light and dark themes.

활용Intermediate
직접 써 보기Live preview행·칸 선택 색 바꾸기Change the row and cell selection colors

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="선택 색" id="colors">
      <option value="default" selected>처음 색 (기본)</option>
      <option value="green">초록</option>
      <option value="violet">보라</option>
    </select>
    <button type="button" id="pick">행·칸 함께 고르기</button>
    <button type="button" id="clear">선택 지우기</button>
  </div>
  <div id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('colors').addEventListener('change', (event) => actions.colors(api, event.target.value));
document.getElementById('pick').addEventListener('click', () => actions.pick(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = ['김민준', '이서연', '박지호', '최유나', '정도윤', '한소희'];
const countries = ['일본', '한국', '중국', '아일랜드', '이탈리아', '영국'];
// 행 선택(체크박스)과 칸 선택(끌기)을 함께 켰어요. 두 선택은 서로 다른 색으로 보여요.
export const options = {
  columns: [
    { id: 'name', title: '이름', width: 110 },
    { id: 'country', title: '국가', width: 110 },
    { id: 'product', title: '상품', width: 130 },
    { id: 'price', title: '가격(만원)', kind: 'number', width: 110, align: 'end' },
  ],
  rows: Array.from({ length: 10 }, (_, i) => ({
    id: i + 1,
    name: names[i % 6],
    country: countries[(i * 5) % 6],
    product: `상품 ${i + 1}`,
    price: 80 + i * 37,
  })),
  rowKey: 'id',
  selection: { mode: 'multiple', checkbox: true },
  cellSelection: 'multiple',
};
// 선택 색은 그리드의 CSS 변수예요. --mxg-bg-selected는 선택한 행, --mxg-bg-cell-selected는 선택한 칸,
// --mxg-bg-cell-anchor는 처음 누른 칸, --mxg-cell-sel-line은 칸 블록의 테두리 색이에요.
// 테마 규칙보다 우선하도록 선택자를 조금 더 구체적으로 쓰고(:root:root .mxg.mxg), 섞는 색(color-mix)으로 정해서, 밝은 테마와 어두운 테마 어디서나 읽기 편해요.
const PALETTES = {
  green: `:root:root .mxg.mxg {
  --mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
  --mxg-fg-selected: var(--mxg-fg);
  --mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
  --mxg-bg-cell-anchor: var(--mxg-bg);
  --mxg-cell-sel-line: #3fa534;
  --mxg-focus-ring: #3fa534;
}`,
  violet: `:root:root .mxg.mxg {
  --mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
  --mxg-fg-selected: var(--mxg-fg);
  --mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
  --mxg-bg-cell-anchor: var(--mxg-bg);
  --mxg-cell-sel-line: #c026d3;
  --mxg-focus-ring: #c026d3;
}`,
  default: '',
};
let style;
export function setup(_api) {
  style = document.createElement('style');
  document.head.append(style);
  return () => {
    style?.remove();
    style = undefined;
  };
}
export const actions = {
  colors: (_api, value) => {
    if (style)
      style.textContent = PALETTES[value ?? 'default'] ?? '';
  },
  // 행 두 개와 칸 블록 하나를 한꺼번에 골라 두 색을 나란히 보여 줘요.
  pick: (api) => {
    api.setSelectedKeys([2, 3]);
    api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
  },
  clear: (api) => {
    api.clearSelection();
    api.clearCellSelection();
  },
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="Selection colors" id="colors">
      <option value="default" selected>Theme default</option>
      <option value="green">Green</option>
      <option value="violet">Violet</option>
    </select>
    <button type="button" id="pick">Pick rows and cells</button>
    <button type="button" id="clear">Clear selection</button>
  </div>
  <div id="grid" style="height: 380px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api);
document.getElementById('colors').addEventListener('change', (event) => actions.colors(api, event.target.value));
document.getElementById('pick').addEventListener('click', () => actions.pick(api));
document.getElementById('clear').addEventListener('click', () => actions.clear(api));
example.js
const names = [
  'Liam Carter',
  'Emma Stone',
  'Noah Parker',
  'Olivia Reed',
  'Ethan Brooks',
  'Sophia Hayes',
];
const countries = ['Japan', 'Korea', 'China', 'Ireland', 'Italy', 'UK'];
// Row selection (checkbox) and cell selection (drag) are both on. The two selections show in different colors.
export const options = {
  columns: [
    { id: 'name', title: 'Name', width: 130 },
    { id: 'country', title: 'Country', width: 110 },
    { id: 'product', title: 'Product', width: 130 },
    { id: 'price', title: 'Price ($)', kind: 'number', width: 110, align: 'end' },
  ],
  rows: Array.from({ length: 10 }, (_, i) => ({
    id: i + 1,
    name: names[i % 6],
    country: countries[(i * 5) % 6],
    product: `Product ${i + 1}`,
    price: 80 + i * 37,
  })),
  rowKey: 'id',
  selection: { mode: 'multiple', checkbox: true },
  cellSelection: 'multiple',
};
// Selection colors are CSS variables of the grid. --mxg-bg-selected is the selected row, --mxg-bg-cell-selected is the selected cell,
// --mxg-bg-cell-anchor is the cell pressed first, and --mxg-cell-sel-line is the border color of the cell block.
// The selector is made a bit more specific (:root:root .mxg.mxg) so it wins over the theme rules, and the colors are mixed (color-mix), so they read well on both light and dark themes.
const PALETTES = {
  green: `:root:root .mxg.mxg {
  --mxg-bg-selected: color-mix(in srgb, #3fa534 28%, var(--mxg-bg));
  --mxg-fg-selected: var(--mxg-fg);
  --mxg-bg-cell-selected: color-mix(in srgb, #3fa534 14%, var(--mxg-bg));
  --mxg-bg-cell-anchor: var(--mxg-bg);
  --mxg-cell-sel-line: #3fa534;
  --mxg-focus-ring: #3fa534;
}`,
  violet: `:root:root .mxg.mxg {
  --mxg-bg-selected: color-mix(in srgb, #a855f7 26%, var(--mxg-bg));
  --mxg-fg-selected: var(--mxg-fg);
  --mxg-bg-cell-selected: color-mix(in srgb, #a855f7 14%, var(--mxg-bg));
  --mxg-bg-cell-anchor: var(--mxg-bg);
  --mxg-cell-sel-line: #c026d3;
  --mxg-focus-ring: #c026d3;
}`,
  default: '',
};
let style;
export function setup(_api) {
  style = document.createElement('style');
  document.head.append(style);
  return () => {
    style?.remove();
    style = undefined;
  };
}
export const actions = {
  colors: (_api, value) => {
    if (style)
      style.textContent = PALETTES[value ?? 'default'] ?? '';
  },
  // Select two rows and one cell block at once to show the two colors side by side.
  pick: (api) => {
    api.setSelectedKeys([2, 3]);
    api.selectCells({ rowKey: 5, columnId: 'country' }, { rowKey: 8, columnId: 'price' });
  },
  clear: (api) => {
    api.clearSelection();
    api.clearCellSelection();
  },
};