---
title: "Image Cropper · Ark UI — StyleX Blocks"
description: "Crop selection over an image with drag-to-move, eight resize handles, an optional locked aspect ratio, arrow-key nudging, and a rule-of-thirds grid while dragging."
url: "https://stylexblocks.com/components/ark-ui/image-cropper/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Image Cropper

Crop selection over an image with drag-to-move, eight resize handles, an optional locked aspect ratio, arrow-key nudging, and a rule-of-thirds grid while dragging.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/image-cropper.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/image-cropper.md)
- Category: Form
- HTML page: https://stylexblocks.com/components/ark-ui/image-cropper/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add image-cropper
```

Equivalent: `npx shadcn add @stylexblocks/image-cropper`. Registry item JSON: https://stylexblocks.com/r/ark-ui/image-cropper.json

## Usage

```tsx
import { useState } from "react";
import { ImageCropper } from "#components/ui/ark-ui/image-cropper.tsx";

export function Example() {
  const [crop, setCrop] = useState({ x: 0, y: 0, width: 0, height: 0 });
  return (
    // Selection renders the 8 handles + rule-of-thirds grid by default.
    // Drag to move, drag handles to resize; arrow keys nudge (Shift = 10px,
    // Alt + arrows resize), +/- zoom. Crop is in viewport pixels.
    <ImageCropper.Root aspectRatio={16 / 9} onCropChange={(d) => setCrop(d.crop)}>
      <ImageCropper.Viewport>
        <ImageCropper.Image src="/photo.jpg" />
        <ImageCropper.Selection />
      </ImageCropper.Viewport>
    </ImageCropper.Root>
  );
}

// custom handles: <ImageCropper.Selection>
//   {ImageCropper.handles.map((p) => <ImageCropper.Handle key={p} position={p} />)}
//   <ImageCropper.Grid axis="horizontal" /><ImageCropper.Grid axis="vertical" />
// </ImageCropper.Selection>
// export: <ImageCropper.Context>{(api) => api.getCroppedImage()}</ImageCropper.Context>
```

## Examples

### Fixed aspect ratio

`aspectRatio={1}` locks the selection to a square; corner and edge handles keep the ratio and stay inside the image.

```tsx
<ImageCropper.Root aspectRatio={1}>
  <ImageCropper.Viewport>
    <ImageCropper.Image src="/photo.jpg" />
    <ImageCropper.Selection />
  </ImageCropper.Viewport>
</ImageCropper.Root>
```

### Circle crop

`cropShape="circle"` rounds the selection for avatar crops; the crop rect is still the bounding box.

```tsx
<ImageCropper.Root cropShape="circle" aspectRatio={1}>
  <ImageCropper.Viewport>
    <ImageCropper.Image src="/avatar.jpg" />
    <ImageCropper.Selection />
  </ImageCropper.Viewport>
</ImageCropper.Root>
```

## Source

`components/ui/ark-ui/image-cropper.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { ImageCropper as ArkImageCropper } from "@ark-ui/react/image-cropper";
import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";
import { defineSlotRecipe } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { colors } from '@stylexblocks/ui/tokens/colors.stylex.ts'
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'

const imageCropperRoot = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    gap: spacing._3,
    width: '100%',
    position: 'relative',
  },
})

// Same surface as the file-upload dropzone: page background, panel radius,
// 1px border. The image fills it; the crop selection lives on top.
const imageCropperViewport = stylex.create({
  base: {
    position: 'relative',
    overflow: 'hidden',
    touchAction: 'none',
    userSelect: 'none',
    width: '100%',
    backgroundColor: semanticColors.bgMuted,
    borderRadius: semanticRadii.panel,
    borderWidth: '1px',
    borderStyle: 'solid',
    borderColor: semanticColors.border,
  },
})

const imageCropperImage = stylex.create({
  base: {
    display: 'block',
    width: '100%',
    height: 'auto',
    maxWidth: '100%',
    pointerEvents: 'none',
    userSelect: 'none',
  },
})

// The selection dims everything outside it with a huge spread shadow and
// draws a light frame that reads on any photo, in both color schemes.
const imageCropperSelection = stylex.create({
  base: {
    position: 'absolute',
    boxSizing: 'border-box',
    cursor: {
      default: 'move',
      ':is([data-disabled])': 'default',
    },
    touchAction: 'none',
    borderWidth: '1px',
    borderStyle: 'solid',
    borderColor: colors.whiteAlpha700,
    borderRadius: {
      default: 0,
      ':is([data-shape="circle"])': radii.full,
    },
    boxShadow: `0 0 0 9999px ${colors.blackAlpha600}`,
    outlineWidth: {
      default: null,
      ':focus-visible': '2px',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '0px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
  },
})

// Edge handles are invisible hit strips (the engine sizes them along the
// edge); corner handles are visible square knobs.
const imageCropperHandle = stylex.create({
  base: {
    position: 'absolute',
    boxSizing: 'border-box',
    zIndex: 1,
    touchAction: 'none',
    width: {
      default: null,
      ':is([data-position="e"], [data-position="w"])': spacing._3,
      ':is([data-position="ne"], [data-position="se"], [data-position="sw"], [data-position="nw"])': spacing._3,
    },
    height: {
      default: null,
      ':is([data-position="n"], [data-position="s"])': spacing._3,
      ':is([data-position="ne"], [data-position="se"], [data-position="sw"], [data-position="nw"])': spacing._3,
    },
    backgroundColor: {
      default: 'transparent',
      ':is([data-position="ne"], [data-position="se"], [data-position="sw"], [data-position="nw"])': colors.white,
    },
    borderWidth: {
      default: 0,
      ':is([data-position="ne"], [data-position="se"], [data-position="sw"], [data-position="nw"])': '1px',
    },
    borderStyle: 'solid',
    borderColor: colorPalette.solid,
    borderRadius: radii.xs,
    display: {
      default: null,
      ':is([data-disabled])': 'none',
    },
  },
})

// Rule-of-thirds lines: one element per axis spanning the middle third,
// bordered on both sides. Hidden until the selection is dragged or panned.
const imageCropperGrid = stylex.create({
  base: {
    position: 'absolute',
    pointerEvents: 'none',
    borderStyle: 'solid',
    borderColor: colors.whiteAlpha600,
    borderBlockWidth: {
      default: 0,
      ':is([data-axis="horizontal"])': '1px',
    },
    borderInlineWidth: {
      default: 0,
      ':is([data-axis="vertical"])': '1px',
    },
    opacity: {
      default: 0,
      ':is([data-dragging], [data-panning])': 1,
    },
    transitionProperty: 'opacity',
    transitionDuration: durations.fast,
  },
})

const imageCropperRecipe = defineSlotRecipe({
  slots: {
    root: { styles: imageCropperRoot },
    viewport: { styles: imageCropperViewport },
    image: { styles: imageCropperImage },
    selection: { styles: imageCropperSelection },
    handle: { styles: imageCropperHandle },
    grid: { styles: imageCropperGrid },
  },
  defaultVariants: {},
});

const { withProvider, withContext } = createSlotRecipeContext(imageCropperRecipe);

const Handle = withContext(ArkImageCropper.Handle, "handle", "ImageCropperHandle");
const Grid = withContext(ArkImageCropper.Grid, "grid", "ImageCropperGrid");
const StyledSelection = withContext(
  ArkImageCropper.Selection,
  "selection",
  "ImageCropperSelection",
);

/**
 * Selection — the crop rectangle. Without children it renders the eight
 * resize handles plus the rule-of-thirds grid (shown while dragging), so
 * the common case is a bare `<ImageCropper.Selection />`.
 */
function Selection(props: React.ComponentProps<typeof StyledSelection>) {
  const { children, ...rest } = props;
  return (
    <StyledSelection {...rest}>
      {children ?? (
        <>
          {ArkImageCropper.handles.map((position) => (
            <Handle key={position} position={position} />
          ))}
          <Grid axis="horizontal" />
          <Grid axis="vertical" />
        </>
      )}
    </StyledSelection>
  );
}
Selection.displayName = "ImageCropperSelection";

/**
 * ImageCropper — Ark UI's image-cropper machine with the StyleX slot recipe:
 * a bordered viewport (the file-upload surface), a dimmed-outside crop
 * selection with corner knobs and edge hit strips, and a rule-of-thirds
 * grid that fades in while dragging. Drag to move, drag handles to resize,
 * arrow keys nudge (Shift for bigger steps, Alt to resize), `aspectRatio`
 * locks the shape, and `onCropChange` reports the crop rect in viewport px.
 */
export const ImageCropper = {
  Root: withProvider(ArkImageCropper.Root, "root", "ImageCropperRoot"),
  Viewport: withContext(ArkImageCropper.Viewport, "viewport", "ImageCropperViewport"),
  Image: withContext(ArkImageCropper.Image, "image", "ImageCropperImage"),
  Selection,
  Handle,
  Grid,
  Context: ArkImageCropper.Context,
  handles: ArkImageCropper.handles,
};

export type ImageCropperRootProps = React.ComponentProps<typeof ImageCropper.Root>;
export type { ImageCropperCropChangeDetails, ImageCropperRect } from "@ark-ui/react/image-cropper";
```
