Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion docs/guides/renderer-kit-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ not have to reinvent change emphasis, legends, summary bars, or styling rules:
`icon` picks from the SDK vocabulary (`breaking`, `upgraded`, `enforced`),
`ids` makes the fact clickable as a filter, `added`/`removed`/`modified`
show comparison deltas in change colors (use `summaryChange(was, now)` for a
net measure delta like `39,882 lines of code +2,056`), and `description`
net measure delta like `39,882 lines of code +2,056`, and
`countFact(model.entities, {id, noun, key})` for a count of distinct keys on
After, with the keys the comparison added and removed), and `description`
sets the tooltip (for example, the classification rules). The shell appends
facts after kind counts and enforces protocol limits—if a fact references
too many items, it keeps its count and drops its filter.
Expand Down Expand Up @@ -311,6 +313,32 @@ accept token scale steps rather than raw lengths or colors—this is how
- `Text` renders typography using `size` (`xs`, `sm`, `md`, `lg`, `xl`),
`weight`, and `tone` (`default`, `muted`, `subtle`).

## Categories

For items colored by a category (a language, a tool kind, a team),
`useCategories(rows, key, {title, colorMode})` colors each category from the
kit's categorical palette, ranked over both revisions, so a category keeps its
color and its place in the legend from Before to After and at every progress.
Pass the comparison rows (`model.entities`) and a stable `key(item)` that
returns a category key, a `Category` (`languageCategory(name)` keeps a
language's own color) or `undefined`. Items rank by count over both revisions,
or by `weight(item)`; `distinct` and `pinned` are `categoryPalette`'s options.

It returns `color(key)` (CSS paint), `legend` (report it with
`useLegend(props.reportLegend, legend)`), `swatch(key)` (a dot in the category's
color, the `Swatch` component) and the `palette` itself. Everything painted with
a category's color carries `data-projections-category`, as `Swatch` does, so the
checks read it as a categorical color rather than a change. `categoriesOf` is
the same without React.

```tsx
const kindOf = (tool: Entity) => tool.data?.kind as string | undefined
const kinds = useCategories(props.model.entities, kindOf, {title: 'Tool kinds', colorMode: props.colorMode})
useLegend(props.reportLegend, kinds.legend)
// …
{kinds.swatch(kindOf(tool) ?? 'other')}
```

## Data tables

When your entities share comparable properties, reach for `DataTable` instead of
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/renderer-kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ Build categories with `fileCategory(path)` (Linguist language grouped by parent,
falling back to `.ext` or "No extension"), `languageCategory(name)` (for
tool-reported language names), or `plainCategory(key)`. Each entry provides
`paint` for CSS and `swatch` for the host legend (`{color}` for languages,
`{token}` otherwise), and `paletteLegend(palette, title)` formats the legend for
the host. Your renderer receives `colorMode` in `RendererProps`.
`{token}` otherwise), `paletteLegend` makes the host legend, and
[`useCategories`](renderer-kit-components.md#categories) does both for rows.

## Styles

Expand Down
132 changes: 132 additions & 0 deletions docs/reference/renderer-kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,62 @@ The narrowest and widest graph card, in card units (the widest grows by an icon'
`graphCardWidth`), so a graph of short names packs densely and its layers stay aligned;
text longer than the widest card holds is cut short with an ellipsis, and the card's tooltip has it whole.

### `Categories`

```ts
interface Categories
```

What `categoriesOf` returns.

| Member | Signature | Description |
|---|---|---|
| `color` | `"color": { (key: string): string }` | A category key's color, as CSS paint; the neutral "other" color for a key the items don't have. |
| `legend` | `"legend": RendererLegend` | The host's legend: the categories ranked over both revisions. Report it with `useLegend`. |
| `palette` | `"palette": Palette` | The palette, for anything beyond the color and the legend (`Palette.entry`, `Palette.ranked`). |

### `categoriesOf`

```ts
categoriesOf<T>(rows: Iterable<CategorizedRow<T>, any, any>, key: { (item: T): Category | string | undefined }, options: CategoriesOptions<T>): Categories
```

The categories of a set of comparison rows, colored from the kit's categorical palette (`categoryPalette`) and
ranked over both revisions, so a category keeps its color, and its place in the legend, from Before to After and
at every progress. `key` gives an item's category: a key, a `Category` (`languageCategory(name)` for a language's
color) or `undefined` for none. Pass stable functions (defined outside the component, or memoized), so the palette
is computed once per document.

### `CategoriesOptions`

```ts
interface CategoriesOptions<T>
```

How `categoriesOf` colors and keys its categories.

| Member | Signature | Description |
|---|---|---|
| `colorMode` | `"colorMode"?: "dark" \| "light"` | The host theme's color mode (`RendererProps.colorMode`), for language colors. |
| `distinct` | `"distinct"?: false \| true` | Choose each color to stand as far as possible from those already used (`PaletteOptions.distinct`). |
| `limit` | `"limit"?: number` | How many categories the legend names before "N other types"; 14 by default. |
| `pinned` | `"pinned"?: ReadonlyMap<string, "border-width" \| "border-width-thick" \| "chart-1" \| "chart-10" \| "chart-11" \| "chart-12" \| "chart-13" \| "chart-14" \| "chart-15" \| "chart-16" \| "chart-2" \| "chart-3" \| "chart-4" \| "chart-5" \| "chart-6" \| "chart-7" \| "chart-8" \| "chart-9" \| "color-accent" \| "color-accent-emphasis" \| "color-accent-muted" \| "color-added" \| "color-added-border" \| "color-added-emphasis" \| "color-added-highlight" \| "color-added-muted" \| "color-bg" \| "color-bg-inset" \| "color-bg-overlay" \| "color-bg-subtle" \| "color-border" \| "color-border-emphasis" \| "color-border-muted" \| "color-divider" \| "color-fg" \| "color-fg-muted" \| "color-fg-on-emphasis" \| "color-fg-subtle" \| "color-focus" \| "color-highlight" \| "color-modified" \| "color-modified-border" \| "color-modified-emphasis" \| "color-modified-highlight" \| "color-modified-muted" \| "color-moved" \| "color-moved-border" \| "color-moved-emphasis" \| "color-moved-highlight" \| "color-moved-muted" \| "color-removed" \| "color-removed-border" \| "color-removed-emphasis" \| "color-removed-highlight" \| "color-removed-muted" \| "color-surface-canvas" \| "color-surface-header" \| "color-surface-inset" \| "color-surface-raised" \| "control-height" \| "control-height-lg" \| "control-height-sm" \| "focus-ring-offset" \| "focus-ring-width" \| "font-family" \| "font-family-mono" \| "font-size" \| "font-size-lg" \| "font-size-sm" \| "font-size-xl" \| "font-size-xs" \| "font-weight-bold" \| "font-weight-medium" \| "font-weight-normal" \| "font-weight-semibold" \| "icon-added" \| "icon-additive" \| "icon-agent" \| "icon-application" \| "icon-blocked" \| "icon-breaking" \| "icon-bytecode" \| "icon-changed" \| "icon-collapse" \| "icon-comparator" \| "icon-compatible" \| "icon-credential" \| "icon-downgraded" \| "icon-enforced" \| "icon-error" \| "icon-exclude" \| "icon-expand" \| "icon-extractor" \| "icon-file" \| "icon-generated" \| "icon-hidden" \| "icon-host" \| "icon-import" \| "icon-include" \| "icon-info" \| "icon-modified" \| "icon-module" \| "icon-moved" \| "icon-namespace" \| "icon-network" \| "icon-other" \| "icon-policy" \| "icon-process" \| "icon-read" \| "icon-redeploy" \| "icon-removed" \| "icon-renamed" \| "icon-renderer" \| "icon-replace" \| "icon-rescoped" \| "icon-settings" \| "icon-unenforced" \| "icon-unknown" \| "icon-upgraded" \| "icon-varied" \| "icon-visible" \| "icon-warning" \| "icon-workload" \| "label-font-size" \| "label-font-weight" \| "label-height" \| "label-padding-x" \| "label-radius" \| "line-height" \| "line-height-relaxed" \| "line-height-tight" \| "radius" \| "radius-full" \| "radius-lg" \| "radius-sm" \| "shadow-lg" \| "shadow-md" \| "shadow-sm" \| "space-1" \| "space-2" \| "space-3" \| "space-4" \| "space-5" \| "space-6" \| "space-7" \| "space-8">` | Categories whose color is set, such as a definition's kind colors (`PaletteOptions.pinned`). |
| `title` | `"title": string` | The legend's title, such as `Languages` or `Tools`. |
| `weight` | `"weight"?: { (item: T): number }` | An item's weight, for ranking; 1 by default, so categories rank by how many items they have in both revisions. |

### `CategorizedRow`

```ts
interface CategorizedRow<T>
```

A comparison row as `categoriesOf` reads it: either revision's item.

| Member | Signature | Description |
|---|---|---|
| `after` | `"after"?: T \| undefined` | The item in After, if it has one. |
| `before` | `"before"?: T \| undefined` | The item in Before, if it has one. |

### `Category`

```ts
Expand Down Expand Up @@ -758,6 +814,49 @@ Which relationships `followContext` follows, by kind: to an item's sources (`inc
| `incoming` | `"incoming"?: { (kind: string): false \| true }` | Follow a relationship of this kind from its target back to its source (a dependency to the package that needs it). |
| `outgoing` | `"outgoing"?: { (kind: string): false \| true }` | Follow a relationship of this kind from its source to its target (a probe to the hook it attaches to). |

### `CountedRow`

```ts
interface CountedRow<T>
```

A comparison row as `countFact` reads it: its ID and either revision's item.

| Member | Signature | Description |
|---|---|---|
| `after` | `"after"?: T \| undefined` | The item in After, if it has one. |
| `before` | `"before"?: T \| undefined` | The item in Before, if it has one. |
| `id` | `"id": string` | The row's ID, which a figure filters by. |

### `countFact`

```ts
countFact<T extends { "id": string }>(rows: Iterable<CountedRow<T>, any, any>, options: CountFactOptions<T>): SummaryFactItem
```

A count for the host's summary bar (`SummaryFactItem`), the same at every progress: the distinct keys on After,
with what the comparison added (keys After has and Before hasn't) and removed (the other way round). Its `ids` are
the After items counted (their own IDs, which the host's filter matches, not the rows' presentation IDs), so
activating the figure filters to them. Pass `model.entities` (or any rows with
`before` and `after`); one extraction has no additions or removals.

### `CountFactOptions`

```ts
interface CountFactOptions<T>
```

What `countFact` counts and how the summary bar words it.

| Member | Signature | Description |
|---|---|---|
| `description` | `"description"?: string` | One sentence of context, the figure's tooltip. |
| `icon` | `"icon"?: string` | A theme icon's name, without its `icon-` prefix. |
| `id` | `"id": string` | The fact's stable ID, distinct among the summary's facts. |
| `key` | `"key"?: { (item: T, row: CountedRow<T>): string \| undefined }` | The key an item is counted by, so several items with one key count once (tools by name, say); `undefined` leaves the item out. By default each row counts once, by its ID, in each revision it has. |
| `noun` | `"noun": SummaryNoun` | The figure's words. |
| `tone` | `"tone"?: "added" \| "modified" \| "neutral" \| "removed"` | The figure's color while it counts anything; neutral by default. |

### `countText`

```ts
Expand Down Expand Up @@ -3720,6 +3819,21 @@ CardGrid(__0: { "children": Iterable<ReactNode, any, any> | Promise<Iterable<Rea

Cards in a responsive grid: as many columns as fit, one on a phone.

### `CategoriesView`

```ts
interface CategoriesView extends Categories
```

What `useCategories` returns: `categoriesOf`'s palette, colors and legend, and a swatch per category.

| Member | Signature | Description |
|---|---|---|
| `color` | `"color": { (key: string): string }` | A category key's color, as CSS paint; the neutral "other" color for a key the items don't have. |
| `legend` | `"legend": RendererLegend` | The host's legend: the categories ranked over both revisions. Report it with `useLegend`. |
| `palette` | `"palette": Palette` | The palette, for anything beyond the color and the legend (`Palette.entry`, `Palette.ranked`). |
| `swatch` | `"swatch": { (key: string): ReactElement<unknown, string \| { (props: any): Iterable<Iterable \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined, any, any> \| Promise<Iterable<Iterable<ReactNode, any, any> \| Promise \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined, any, any> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| Promise<Iterable<ReactNode, any, any> \| Promise<Iterable<ReactNode, any, any> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined> \| ReactElement<unknown, recursive> \| ReactPortal \| bigint \| false \| null \| number \| string \| true \| undefined } \| { new (props: any, context: any): Component<any, any, any> }> }` | A category's color as a dot (`Swatch`). |

### `ChangeFooter`

```ts
Expand Down Expand Up @@ -4467,6 +4581,15 @@ the old value struck through in the removed hue, an arrow, then the new
value in the added hue. Pass identifier attributes (the SDK's
`identifier`) for code values such as versions.

### `Swatch`

```ts
Swatch(__0: { "category": string; "color": string; "size"?: "md" | "sm" }): Element
```

A category's color as a small dot, marked `data-projections-category` so the renderer checks read it as a
categorical color. `color` is the category's paint (`Categories.color(key)`).

### `TablePropsSource`

```ts
Expand Down Expand Up @@ -4505,6 +4628,15 @@ useBorderWidths(ref: RefObject<Element | null>): BorderWidths

The border widths in effect on a rendered element, read when it mounts (before its first paint); the defaults until then.

### `useCategories`

```ts
useCategories<T>(rows: Array<CategorizedRow<T>>, key: { (item: T): Category | string | undefined }, __2: CategoriesOptions<T>): CategoriesView
```

`categoriesOf` (the categories of comparison rows, ranked over both revisions), computed once per document and
option set, with a swatch per category. Report the legend with `useLegend(props.reportLegend, categories.legend)`.

### `useContentAspect`

```ts
Expand Down
65 changes: 65 additions & 0 deletions packages/renderers/kit/src/categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Categorical colors for a renderer's items, ranked over both revisions, with the host legend.
import type {ColorMode, RendererLegend, ThemeTokenName} from '@githubnext/projections-renderer-sdk/protocol'
import {categoryPalette, paletteLegend, plainCategory, type Category, type Palette} from './palette.js'

/** A comparison row as `categoriesOf` reads it: either revision's item. */
export interface CategorizedRow<T> {
/** The item in Before, if it has one. */
before?: T | undefined
/** The item in After, if it has one. */
after?: T | undefined
}

/** How `categoriesOf` colors and keys its categories. */
export interface CategoriesOptions<T> {
/** The host theme's color mode (`RendererProps.colorMode`), for language colors. */
colorMode?: ColorMode
/** The legend's title, such as `Languages` or `Tools`. */
title: string
/** How many categories the legend names before "N other types"; 14 by default. */
limit?: number
/** An item's weight, for ranking; 1 by default, so categories rank by how many items they have in both revisions. */
weight?: (item: T) => number
/** Choose each color to stand as far as possible from those already used (`PaletteOptions.distinct`). */
distinct?: boolean
/** Categories whose color is set, such as a definition's kind colors (`PaletteOptions.pinned`). */
pinned?: ReadonlyMap<string, ThemeTokenName>
}

/** What `categoriesOf` returns. */
export interface Categories {
/** The palette, for anything beyond the color and the legend (`Palette.entry`, `Palette.ranked`). */
palette: Palette
/** A category key's color, as CSS paint; the neutral "other" color for a key the items don't have. */
color: (key: string) => string
/** The host's legend: the categories ranked over both revisions. Report it with `useLegend`. */
legend: RendererLegend
}

/**
* The categories of a set of comparison rows, colored from the kit's categorical palette (`categoryPalette`) and
* ranked over both revisions, so a category keeps its color, and its place in the legend, from Before to After and
* at every progress. `key` gives an item's category: a key, a `Category` (`languageCategory(name)` for a language's
* color) or `undefined` for none. Pass stable functions (defined outside the component, or memoized), so the palette
* is computed once per document.
*/
export function categoriesOf<T>(
rows: Iterable<CategorizedRow<T>>,
key: (item: T) => string | Category | undefined,
options: CategoriesOptions<T>,
): Categories {
const weighed: {category: Category; weight: number}[] = []
for (const row of rows) {
for (const item of [row.before, row.after]) {
if (item === undefined) continue
const found = key(item)
if (found === undefined) continue
weighed.push({category: typeof found === 'string' ? plainCategory(found) : found, weight: options.weight?.(item) ?? 1})
}
}
const palette = categoryPalette(weighed, options.colorMode, {
...(options.distinct === undefined ? {} : {distinct: options.distinct}),
...(options.pinned === undefined ? {} : {pinned: options.pinned}),
})
return {palette, color: palette.paintOf, legend: paletteLegend(palette, options.title, options.limit)}
}
Loading