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

# Cutout

A surface with shapes cut out of it, exported from `@fabiocv/ui/cutout`. A part set into a surface does not sit on top of it: the surface is cut around the part, with a thin gap between the two, corners that stay concentric, and a rounded fillet wherever a cut meets an edge. `ImageCard`, `Menu` and its submenus are drawn with it.

```tsx
import { useSpring } from 'framer-motion';

import { Cutout, useCutout } from '@fabiocv/ui/cutout';
import { physics, SLIDE } from '@fabiocv/ui/spring';

function Hole() {
  const x = useSpring(0, physics(SLIDE));

  useCutout(
    () => ({
      left: x.get(),
      top: 0,
      right: x.get() + 40,
      bottom: 40,
      radius: 8,
    }),
    { values: [x] },
  );

  return null;
}

<Cutout.Root radius={8} gap={2}>
  <Cutout.Surface
    style={{ height: 240, background: 'var(--color-surface-subtle)' }}
  />
  <Hole />
</Cutout.Root>;
```

## Words

- **Surface**: what holes are cut from, its box and its corner radius.
- **Shape**: a part's rounded rectangle, in the surface's pixels.
- **Gap**: the space between a shape and the edge of its cut, `2px` unless set.
- **Hole**: the shape grown by the gap. Its corners grow by the gap too, so a `6px` corner with a `2px` gap cuts an `8px` one. A square corner stays square.
- **Fillet**: the round where what is left of the surface turns a corner between a hole and an edge. It is the surface's radius unless set.

## Parts

- `Cutout.Root` is the box shapes are placed in. `radius` starts at `6` and `gap` at `2`. With `href` it is a link, which goes through [the app's router](https://ui.fabio.cv/router/index.md) to one of its own pages.
- `Cutout.Surface` is the material. Its content is clipped to the outline.
- `useCutout(shape, options)` cuts a shape while its component is mounted. `shape` is called on every paint with the measured sizes, and returns the shape or `null` for none. It never reads layout.
  - `values` are the motion values the shape reads. A change to one repaints in the same frame.
  - `measure` is a ref to an element whose size the shape follows.
- `Cutout.Backdrop` goes inside the part that `measure` points to. It takes a copy of the surface's content as `children` and shows it lined up with the surface, frosted by `blur`. The frost blurs the surface only, never the page past it.
- `CutoutColors.Fill` is a fill for a cut surface, light grey in light and Surface subtle in dark.

## Placing a part on an edge

`placeInsertion` gives the box of a part that sits flush on a side, from its measured size. `placementStyle` puts the part there in CSS. `slideShape` uncovers the hole from an edge, from `0` to `1`, and `slideClip` and `slideOffset` do the same for the part's own box and content, so the two move as one.

```tsx
import { motion } from 'framer-motion';
import type { MotionStyle } from 'framer-motion';

const placement = { side: 'bottom', align: 'start', alignOffset: 0 } as const;

useCutout(
  ({ surface, element }) =>
    element &&
    slideShape({
      rect: placeInsertion(placement, element, surface),
      origin: 'left',
      amount: reveal.get(),
      radius: 6,
      gap: 2,
      fillet: 6,
    }),
  { values: [reveal], measure: ref },
);

<motion.div ref={ref} style={placementStyle(placement) as MotionStyle} />;
```

`revealShape` and `insertionHole` grow a hole from a point instead.

## How it behaves

- A hole that reaches past an edge of the surface becomes a notch. A hole clear of every edge stays a hole.
- A fillet grows from nothing as its hole crosses the edge, and is full one gap past it, so the outline never jumps as a hole moves out through an edge.
- Between two holes the fillet grows from nothing as they first touch. A strip of surface thinner than two fillets rounds off whole, rather than leaving a sliver.
- A hole leaving through an edge travels its own size and both gaps. Short of that, a sliver the width of the gaps would be left, and vanish in one frame.
- Past full size a shape holds, so a spring's overshoot never widens the gap. Give a cut that grows a damping ratio of `1`. One that moves takes its part's spring.
- Read the same motion values in the shape and in the part's style. The cut is painted in the step that writes those styles, so both land in one frame.
- The cut is a `clip-path`, so it clips the pointer too. The gap belongs to neither the surface nor the part. Where the pointer crosses from one to the other, let the part take it in the gap.

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

```ts
/**
 * The cutout language: shapes cut out of a surface, such as the insertions
 * set into it. Each shape sits in a hole, the shape grown by the gap. Where a
 * hole meets an edge of the surface, the remaining material is rounded by a
 * fillet.
 */
export declare const CUTOUT_RADIUS = 6;

export declare const CUTOUT_GAP = 2;

/** At or below this amount an insertion is hidden and cuts nothing. */
export declare const CUTOUT_CLOSED = 0.004;

export type Rect = {
    left: number;
    top: number;
    right: number;
    bottom: number;
    width: number;
    height: number;
};

export type Box = Pick<Rect, 'left' | 'top' | 'right' | 'bottom'>;

export type Size = Pick<Rect, 'width' | 'height'>;

export type CutoutSide = 'top' | 'right' | 'bottom' | 'left';

export type CutoutCorner = 'topLeft' | 'topRight' | 'bottomRight' | 'bottomLeft';

export type CutoutAlign = 'start' | 'center' | 'end';

export type PerSide = Record<CutoutSide, number>;

export type PerCorner = Record<CutoutCorner, number>;

export type CutoutHole = Box & {
    /** The hole's own corners, concave where they show. */
    radius: number | PerCorner;
    /** Rounding of the surface where the hole meets each of its edges. */
    fillet: number | PerSide;
};

/** A rounded rect that cuts the surface, in the surface's pixels. */
export type CutoutShape = Box & {
    /** The shape's corners. Its hole's grow by the gap, but by no more than their own radius. */
    radius: number | PerCorner;
    /** Space between the shape and the edge of its hole. */
    gap?: number;
    /** Rounding where the hole meets each edge of the surface. */
    fillet?: number | PerSide;
};

export type CutoutOrigin = 'center' | 'top' | 'right' | 'bottom' | 'left' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';

/** Where an insertion grows from, as fractions of its own box. */
export declare const ORIGIN_POINT: Record<CutoutOrigin, readonly [x: number, y: number]>;

export declare function perSide(value: number | PerSide): PerSide;

export declare function perCorner(value: number | PerCorner): PerCorner;

export type InsertionPlacement = {
    side: CutoutSide;
    align: CutoutAlign;
    /** Distance from the aligned end of the side. Ignored when centered. */
    alignOffset: number;
};

/** An insertion's full-size box, from its placement and its measured size. */
export declare function placeInsertion({ side, align, alignOffset }: InsertionPlacement, size: Size, container: Size): Rect;

/**
 * The hole a shape cuts: the shape grown by the gap. Each corner grows by the
 * gap too, but by no more than its own radius, so a square corner stays square
 * and a small one stays small. From a radius of one gap on, the corners are
 * concentric. Each fillet grows from zero as the hole crosses its edge of the
 * container and is full once the hole is a gap past it, so a hole breaking out
 * through an edge keeps a continuous outline. The shape's own gap and fillet
 * win over the defaults.
 */
export declare function shapeHole(shape: CutoutShape, container: Box, defaults: {
    gap: number;
    fillet: number | PerSide;
}): CutoutHole;

export type RevealShapeInput = {
    /** The shape at full size, in the container's pixels. */
    rect: Box;
    origin: CutoutOrigin;
    /** 0 is closed, 1 is full size. */
    amount: number;
    /** Corner radius at full size. */
    radius: number;
    /** Gap at full size. */
    gap: number;
    /** Fillets at full size, per edge of the container. */
    fillet: number | PerSide;
};

/** A shape part way through growing from its origin. Past full size it holds, so a spring's overshoot never widens the gap. */
export declare function revealShape({ rect, origin, amount, radius, gap, fillet, }: RevealShapeInput): CutoutShape | null;

export type SlideShapeInput = RevealShapeInput;

/**
 * A shape part way through sliding in from its origin. Corners, gap and
 * fillets keep their size. It travels its own size and both gaps, so the hole
 * leaves completely. Past full size it holds, so a spring's overshoot never
 * widens the gap.
 */
export declare function slideShape({ rect, origin, amount, radius, gap, fillet, }: SlideShapeInput): CutoutShape | null;

export type InsertionHoleInput = {
    /** The insertion at full size, in the container's pixels. */
    rect: Box;
    container: Box;
    origin: CutoutOrigin;
    /** 0 is closed, 1 is full size. */
    amount: number;
    gap: number;
    /** Corner radius of the hole at full size. */
    radius: number;
    /** Fillets at full size, per edge of the container. */
    fillet: number | PerSide;
};

/**
 * The hole around an insertion scaled about its origin. Fillets on an edge
 * grow from zero as the hole crosses it, keeping the outline continuous.
 */
export declare function insertionHole({ rect, container, origin, amount, gap, radius, fillet, }: InsertionHoleInput): CutoutHole | null;

export type CutoutBackdropProps = Pick<InsertionPlacement, 'side'> & Partial<Pick<InsertionPlacement, 'align' | 'alignOffset'>> & {
    /** Frosts what shows through, in pixels of blur. */
    blur?: number;
    className?: string;
    /** The surface's content again, lined up with the surface. */
    children?: ReactNode;
};

/** Lets a part show the surface behind it through a copy of the surface's content. The part sits flush on `side` and is the element its cut measures. */
export declare function Backdrop({ side, align, alignOffset, blur, className, children, }: CutoutBackdropProps): import("react").JSX.Element;

export type CutoutRootProps = ComponentProps<'div'> & {
    /** Corner radius of the surface. Also the default fillet of every cut. */
    radius?: number;
    /** Default space between a shape and the edge of its cut. */
    gap?: number;
    /**
     * Makes the box a link to this address. To one of the app's own pages it goes
     * through its router.
     */
    href?: string;
};

/** Positioning box for a surface and its shapes; shapes are given in its pixels. */
export declare function Root({ radius, gap, href, className, style, onClick, ...props }: CutoutRootProps): import("react").JSX.Element;

export type CutoutSurfaceProps = ComponentPropsWithoutRef<'div'>;

/** The material shapes are cut out of. Clipped to the cutout outline. */
export declare function Surface({ className, ...props }: CutoutSurfaceProps): import("react").JSX.Element;

/** The fill of the cutout language, as a `--cutout-*` custom property. */
export declare const CutoutColors: {
    Fill: `var(--${string})`;
};

/** Sizes a shape can follow, measured without reading layout. */
export type CutoutSizes = {
    surface: Size;
    /** The element the shape measures, or null when it measures none. */
    element: Size | null;
};

export type UseCutoutOptions = {
    /** Motion values the shape reads. A change repaints in the frame it happens. */
    values?: readonly MotionValue[];
    /** An element whose size the shape follows. The shape waits for its first size. */
    measure?: RefObject<Element | null>;
};

/** Cuts the surface around a shape while mounted. The shape is read on every paint; drive it from the same motion values as the element it cuts for. */
export declare function useCutout(shape: (sizes: CutoutSizes) => CutoutShape | null, { values, measure }?: UseCutoutOptions): void;

/** Absolute position flush on a side of the root, where `placeInsertion` puts it. */
export declare function placementStyle({ side, align, alignOffset, }: InsertionPlacement): CSSProperties;

export type CutoutPathInput = {
    container: Box;
    /** Corner radius of the surface. */
    radius: number;
    holes: readonly CutoutHole[];
};

/**
 * Outline of a surface with holes cut into it, as a CSS `path()` in the
 * container's pixels. Loops keep the surface on their right: the outside runs
 * clockwise and interior holes counter-clockwise, as the nonzero rule needs.
 */
export declare function cutoutPath(input: CutoutPathInput): string;

export type InsertionClipInput = {
    origin: CutoutOrigin;
    /** 0 is closed, 1 is full size. */
    amount: number;
    /** Corner radius of the insertion at full size. */
    radius: number;
};

/**
 * The insertion's own clip, as a CSS `inset()` on its box. The box grows from
 * the origin in step with its hole, so the content is revealed in place
 * instead of scaled. `none` at full size.
 */
export declare function insertionClip({ origin, amount, radius }: InsertionClipInput): string;

export type SlideClipInput = InsertionClipInput & {
    /** Space between the part and the edge of its hole. */
    gap: number;
};

/**
 * The clip of a part sliding in from its origin, as a CSS `inset()` on its
 * box: uncovered from the origin's edge a gap behind `slideShape`'s hole, its
 * corners at full size. `none` at full size.
 */
export declare function slideClip({ origin, amount, radius, gap }: SlideClipInput): string;

export type SlideOffsetInput = Pick<SlideClipInput, 'origin' | 'amount' | 'gap'>;

/**
 * How far the content of a sliding part trails its box, as CSS translations:
 * it rides in on the edge `slideClip` uncovers it from.
 */
export declare function slideOffset({ origin, amount, gap }: SlideOffsetInput): {
    x: string;
    y: string;
};

/**
 * Parts for the cutout language: a surface with shapes cut out of it. Animate the UI as usual and read the same motion values in the shape.
 */
export declare const Cutout: {
    Root: typeof Root;
    Surface: typeof Surface;
    Backdrop: typeof Backdrop;
};
```

## Example

```tsx
'use client';

import { motion, useSpring } from 'framer-motion';

import { Cutout, useCutout } from '@fabiocv/ui/cutout';
import { physics, SLIDE } from '@fabiocv/ui/spring';

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

const SIZE = 40;

/** Cuts the surface wherever it is, by reading the values that move it. */
function Cursor() {
  const x = useSpring(120, physics(SLIDE));
  const y = useSpring(0, physics(SLIDE));

  useCutout(
    () => ({
      left: x.get(),
      top: y.get(),
      right: x.get() + SIZE,
      bottom: y.get() + SIZE,
      radius: 8,
    }),
    { values: [x, y] },
  );

  return (
    <div
      className={styles.cursorArea}
      onPointerMove={(event) => {
        const box = event.currentTarget.getBoundingClientRect();
        x.set(event.clientX - box.left - SIZE / 2);
        y.set(event.clientY - box.top - SIZE / 2);
      }}
    >
      <motion.div className={styles.cursor} style={{ x, y }} />
    </div>
  );
}

export function CutoutDemo() {
  return (
    <Cutout.Root className={styles.cutout}>
      <Cutout.Surface className={styles.surface} />
      <Cursor />
    </Cutout.Root>
  );
}
```

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