|
1 | 1 | /* eslint-disable react/display-name */ |
2 | 2 | import * as React from "react"; |
3 | 3 | import { GrowingEntry } from "../internal/growing-entry/growing-entry.js"; |
4 | | -import { drawTextCell, prepTextCell } from "../internal/data-grid/render/data-grid-lib.js"; |
| 4 | +import { |
| 5 | + drawTextCell, |
| 6 | + measureTextCached, |
| 7 | + prepTextCell, |
| 8 | + roundedRect, |
| 9 | +} from "../internal/data-grid/render/data-grid-lib.js"; |
5 | 10 | import { GridCellKind, type TextCell } from "../internal/data-grid/data-grid-types.js"; |
6 | 11 | import type { InternalCellRenderer } from "./cell-types.js"; |
| 12 | +import { withAlpha } from "../internal/data-grid/color-parser.js"; |
7 | 13 |
|
8 | 14 | export const textCellRenderer: InternalCellRenderer<TextCell> = { |
9 | 15 | getAccessibilityString: c => c.data?.toString() ?? "", |
10 | 16 | kind: GridCellKind.Text, |
11 | | - needsHover: false, |
| 17 | + needsHover: textCell => textCell.hoverEffect === true, |
12 | 18 | needsHoverPosition: false, |
13 | 19 | drawPrep: prepTextCell, |
14 | 20 | useLabel: true, |
15 | | - draw: a => (drawTextCell(a, a.cell.displayData, a.cell.contentAlign, a.cell.allowWrapping, a.hyperWrapping), true), |
| 21 | + draw: a => { |
| 22 | + const { cell, hoverAmount, hyperWrapping, ctx, rect, theme, overrideCursor } = a; |
| 23 | + const { displayData, contentAlign, hoverEffect, allowWrapping } = cell; |
| 24 | + if (hoverEffect === true && hoverAmount > 0) { |
| 25 | + ctx.textBaseline = "alphabetic"; |
| 26 | + const padX = theme.cellHorizontalPadding; |
| 27 | + const padY = theme.cellVerticalPadding; |
| 28 | + const m = measureTextCached(displayData, ctx, theme.baseFontFull, "alphabetic"); |
| 29 | + const maxH = rect.height - padY; |
| 30 | + const h = Math.min(maxH, m.actualBoundingBoxAscent * 2.5); |
| 31 | + ctx.beginPath(); |
| 32 | + roundedRect( |
| 33 | + ctx, |
| 34 | + rect.x + padX / 2, |
| 35 | + rect.y + (rect.height - h) / 2 + 1, |
| 36 | + m.width + padX * 3, |
| 37 | + h - 1, |
| 38 | + theme.roundingRadius ?? 4 |
| 39 | + ); |
| 40 | + ctx.globalAlpha = hoverAmount; |
| 41 | + ctx.fillStyle = withAlpha(theme.textDark, 0.1); |
| 42 | + ctx.fill(); |
| 43 | + |
| 44 | + // restore |
| 45 | + ctx.globalAlpha = 1; |
| 46 | + ctx.fillStyle = theme.textDark; |
| 47 | + ctx.textBaseline = "middle"; |
| 48 | + |
| 49 | + overrideCursor?.("text"); |
| 50 | + } |
| 51 | + drawTextCell(a, displayData, contentAlign, allowWrapping, hyperWrapping); |
| 52 | + }, |
16 | 53 | measure: (ctx, cell, t) => { |
17 | 54 | const lines = cell.displayData.split("\n", cell.allowWrapping === true ? undefined : 1); |
18 | 55 | let maxLineWidth = 0; |
|
0 commit comments