All components
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.
measuring…
1234567891011121314151617181920212223import { 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>Fixed aspect ratio
`aspectRatio={1}` locks the selection to a square; corner and edge handles keep the ratio and stay inside the image.
measuring…
123456<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.
measuring…
123456<ImageCropper.Root cropShape="circle" aspectRatio={1}> <ImageCropper.Viewport> <ImageCropper.Image src="/avatar.jpg" /> <ImageCropper.Selection /> </ImageCropper.Viewport></ImageCropper.Root>Install
1npx stylexblocks add image-cropperSource
components/ui/ark-ui/image-cropper.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216import * 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";