> A guide to [@fabiocv/ui](https://ui.fabio.cv/). Every guide is listed at https://ui.fabio.cv/llms.txt.

# Highlight

One background for the hovered item of a list, which slides and resizes from item to item, exported from `@fabiocv/ui/highlight`. `Button`, `Tag` and `Menu` have it already.

```tsx
import { Highlight, highlightFrame, useHighlight } from '@fabiocv/ui/highlight';

function List({ items }: { items: string[] }) {
  const { frame, enter, leave, style } = useHighlight();

  return (
    <div ref={frame} className={highlightFrame}>
      <Highlight style={style} />
      {items.map((item) => (
        <div key={item} onPointerEnter={enter} onPointerLeave={leave}>
          {item}
        </div>
      ))}
    </div>
  );
}
```

- `frame` goes on what holds the items, with `highlightFrame` as its class, and `Highlight` goes first inside it. `frame` can be the one item itself.
- It is Surface subtle with `8px` corners, painted under the items, which have no ground of their own.
- On a surface that is Surface subtle itself, or inside corners other than `8px`,
  give `Highlight` a class with Surface sunken or the corners. Write it as
  `.list > .highlight`, so it outranks the highlight's own wherever it loads.
- It comes in from where the pointer was a frame ago and leaves to where it will be a frame on, up to `8px`, so a quick pointer carries it and a slow one barely moves it.
- It fades in on `TRACK`, so it shows as soon as an item is hovered, and out on `APPEAR`. It grows from a scale of `0.9` and back on `APPEAR`.
- It leaves from the item the pointer left, and not from where the slide had got to, which trails a quick pointer and would send it back the way it came.
- It slides on `SLIDE`. Pass another spring for a list that should follow quicker, as a menu does with `TRACK`: `useHighlight(TRACK)`.
- `followSize` keeps it as wide as a `frame` that changes width under the pointer, such as a button growing round its icon, or a list in a pane that is dragged.
- `frame` has `data-active` while the pointer is over an item, and the item hovered last has `data-current`, for styling the rest. They change in the same frame as the springs.
- Touch shows no highlight. With reduced motion it only fades.

## Types of `@fabiocv/ui/highlight`

```ts
export declare const frame: string;

type Point = {
    x: number;
    y: number;
};

type Box = Point & {
    width: number;
    height: number;
};

type HighlightStyle = Record<keyof Box | 'opacity' | 'scale', MotionValue<number>>;

/**
 * One background for the hovered item of a list, placed within `frame`, which
 * slides and resizes from item to item. It fades in from where the pointer
 * was a moment ago, and fades out to where it is heading, so a quick pointer
 * carries it up to `DRIFT` and a slow one barely moves it. It fades in on
 * `TRACK` and out on `APPEAR`, and grows from `SHRUNK` and back on `APPEAR`.
 * With reduced motion it only fades. `frame` can be the one item itself.
 *
 * Sets `data-active` on `frame` while the pointer is over an item, and
 * `data-current` on the item hovered last. They are set here rather than
 * rendered, so they change in the same frame as the springs.
 *
 * `slide` moves it between items. Pass a constant, such as `TRACK` for a
 * quicker one.
 *
 * `followSize` keeps it as wide as `frame` while `frame` grows and shrinks,
 * for a single item that opens around an icon on hover. It is measured as the
 * pointer enters, so it would otherwise keep the size it had then.
 */
export declare function useHighlight<T extends HTMLElement = HTMLDivElement>(slide?: Spring, { followSize }?: {
    followSize?: boolean;
}): {
    frame: import("react").RefObject<T | null>;
    enter: (event: PointerEvent<HTMLElement>) => void;
    leave: (event: PointerEvent<HTMLElement>) => void;
    style: HighlightStyle;
};

/** The background `useHighlight` moves. Put it first inside `frame`. */
export declare function Highlight({ style, className, }: {
    style: HighlightStyle;
    className?: string;
}): import("react").JSX.Element;

export { frame as highlightFrame };
```

## Example

```tsx
'use client';

import { Highlight, highlightFrame, useHighlight } from '@fabiocv/ui/highlight';

import * as styles from './demos.css';

export function HighlightDemo() {
  const { frame, enter, leave, style } = useHighlight();

  return (
    <div ref={frame} className={highlightFrame}>
      <Highlight style={style} />
      {['Work', 'Writing', 'Lab'].map((label) => (
        <div
          key={label}
          className={styles.row}
          onPointerEnter={enter}
          onPointerLeave={leave}
        >
          {label}
        </div>
      ))}
    </div>
  );
}
```

See it at https://ui.fabio.cv/highlight/.
