@ui-sdk/react 0.0.0-stage → 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +10 -2
- package/dist/accordion.d.ts +331 -0
- package/dist/accordion.js +25 -0
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +8 -0
- package/dist/alert-dialog.d.ts +56 -0
- package/dist/alert-dialog.js +45 -0
- package/dist/async-options.d.ts +22 -0
- package/dist/async-options.js +56 -0
- package/dist/autocomplete.d.ts +102 -0
- package/dist/autocomplete.js +195 -0
- package/dist/avatar.d.ts +301 -0
- package/dist/avatar.js +17 -0
- package/dist/button.d.ts +21 -0
- package/dist/button.js +45 -0
- package/dist/calendar-context.d.ts +10 -0
- package/dist/calendar-context.js +9 -0
- package/dist/calendar.d.ts +80 -0
- package/dist/calendar.js +257 -0
- package/dist/carousel.d.ts +52 -0
- package/dist/carousel.js +151 -0
- package/dist/checkbox.d.ts +24 -0
- package/dist/checkbox.js +98 -0
- package/dist/children.d.ts +38 -0
- package/dist/children.js +63 -0
- package/dist/collapsible.d.ts +302 -0
- package/dist/collapsible.js +19 -0
- package/dist/color-picker.d.ts +72 -0
- package/dist/color-picker.js +133 -0
- package/dist/color.d.ts +2 -0
- package/dist/color.js +22 -0
- package/dist/combobox.d.ts +152 -0
- package/dist/combobox.js +338 -0
- package/dist/command.d.ts +88 -0
- package/dist/command.js +233 -0
- package/dist/controller-target.d.ts +3 -0
- package/dist/controller-target.js +59 -0
- package/dist/date-input.d.ts +19 -0
- package/dist/date-input.js +58 -0
- package/dist/date-picker.d.ts +47 -0
- package/dist/date-picker.js +310 -0
- package/dist/date.d.ts +2 -0
- package/dist/date.js +60 -0
- package/dist/dialog-parts.d.ts +23 -0
- package/dist/dialog-parts.js +142 -0
- package/dist/dialog.d.ts +57 -0
- package/dist/dialog.js +32 -0
- package/dist/drawer.d.ts +70 -0
- package/dist/drawer.js +158 -0
- package/dist/editor-context.d.ts +4 -0
- package/dist/editor-context.js +9 -0
- package/dist/field-context.d.ts +47 -0
- package/dist/field-context.js +87 -0
- package/dist/field.d.ts +49 -0
- package/dist/field.js +169 -0
- package/dist/file-input.d.ts +46 -0
- package/dist/file-input.js +170 -0
- package/dist/form-inputs.d.ts +5 -0
- package/dist/form-inputs.js +22 -0
- package/dist/gesture.d.ts +1 -0
- package/dist/gesture.js +16 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +12 -0
- package/dist/hover-card.d.ts +33 -0
- package/dist/hover-card.js +84 -0
- package/dist/image-cropper.d.ts +333 -0
- package/dist/image-cropper.js +37 -0
- package/dist/image.d.ts +2 -0
- package/dist/image.js +6 -0
- package/dist/input.d.ts +22 -0
- package/dist/input.js +51 -0
- package/dist/label.d.ts +294 -0
- package/dist/label.js +9 -0
- package/dist/list-items.d.ts +15 -0
- package/dist/list-items.js +39 -0
- package/dist/list-text.d.ts +4 -0
- package/dist/list-text.js +15 -0
- package/dist/listbox.d.ts +56 -0
- package/dist/listbox.js +122 -0
- package/dist/menu.d.ts +116 -0
- package/dist/menu.js +426 -0
- package/dist/menubar-context.d.ts +4 -0
- package/dist/menubar-context.js +7 -0
- package/dist/menubar.d.ts +11 -0
- package/dist/menubar.js +32 -0
- package/dist/meter.d.ts +312 -0
- package/dist/meter.js +30 -0
- package/dist/navigation-menu.d.ts +61 -0
- package/dist/navigation-menu.js +169 -0
- package/dist/number-input.d.ts +44 -0
- package/dist/number-input.js +185 -0
- package/dist/number.d.ts +2 -0
- package/dist/number.js +40 -0
- package/dist/observe.d.ts +2 -0
- package/dist/observe.js +8 -0
- package/dist/otp-input.d.ts +29 -0
- package/dist/otp-input.js +94 -0
- package/dist/overlays.d.ts +26 -0
- package/dist/overlays.js +82 -0
- package/dist/pagination.d.ts +336 -0
- package/dist/pagination.js +71 -0
- package/dist/password-input.d.ts +23 -0
- package/dist/password-input.js +65 -0
- package/dist/popover.d.ts +54 -0
- package/dist/popover.js +151 -0
- package/dist/popup.d.ts +41 -0
- package/dist/popup.js +180 -0
- package/dist/preferences.d.ts +35 -0
- package/dist/preferences.js +93 -0
- package/dist/primitive.d.ts +2 -0
- package/dist/primitive.js +14 -0
- package/dist/progress.d.ts +312 -0
- package/dist/progress.js +30 -0
- package/dist/radio.d.ts +22 -0
- package/dist/radio.js +86 -0
- package/dist/rating.d.ts +20 -0
- package/dist/rating.js +94 -0
- package/dist/render-element.d.ts +35 -0
- package/dist/render-element.js +103 -0
- package/dist/resizable.d.ts +302 -0
- package/dist/resizable.js +23 -0
- package/dist/scroll-area.d.ts +324 -0
- package/dist/scroll-area.js +49 -0
- package/dist/segmented-control.d.ts +24 -0
- package/dist/segmented-control.js +78 -0
- package/dist/segmented-input.d.ts +15 -0
- package/dist/segmented-input.js +48 -0
- package/dist/select.d.ts +125 -0
- package/dist/select.js +330 -0
- package/dist/separator.d.ts +290 -0
- package/dist/separator.js +9 -0
- package/dist/shortcuts.d.ts +32 -0
- package/dist/shortcuts.js +61 -0
- package/dist/slider.d.ts +38 -0
- package/dist/slider.js +163 -0
- package/dist/switch.d.ts +15 -0
- package/dist/switch.js +39 -0
- package/dist/tabs.d.ts +331 -0
- package/dist/tabs.js +28 -0
- package/dist/text-control.d.ts +22 -0
- package/dist/text-control.js +24 -0
- package/dist/text-match.d.ts +2 -0
- package/dist/text-match.js +8 -0
- package/dist/textarea.d.ts +10 -0
- package/dist/textarea.js +48 -0
- package/dist/time-input.d.ts +19 -0
- package/dist/time-input.js +63 -0
- package/dist/timeline.d.ts +327 -0
- package/dist/timeline.js +31 -0
- package/dist/toast-parts.d.ts +63 -0
- package/dist/toast-parts.js +144 -0
- package/dist/toast.d.ts +2 -0
- package/dist/toast.js +22 -0
- package/dist/toggle.d.ts +23 -0
- package/dist/toggle.js +76 -0
- package/dist/token-input.d.ts +35 -0
- package/dist/token-input.js +100 -0
- package/dist/toolbar-context.d.ts +6 -0
- package/dist/toolbar-context.js +16 -0
- package/dist/toolbar.d.ts +38 -0
- package/dist/toolbar.js +63 -0
- package/dist/tooltip.d.ts +39 -0
- package/dist/tooltip.js +108 -0
- package/dist/tree.d.ts +51 -0
- package/dist/tree.js +150 -0
- package/dist/trigger-menu.d.ts +83 -0
- package/dist/trigger-menu.js +167 -0
- package/dist/use-primitive.d.ts +178 -0
- package/dist/use-primitive.js +674 -0
- package/dist/waterfall.d.ts +80 -0
- package/dist/waterfall.js +108 -0
- package/docs/README.md +89 -0
- package/docs/agents.md +1523 -0
- package/docs/components/accordion.md +1391 -0
- package/docs/components/alert-dialog.md +850 -0
- package/docs/components/autocomplete.md +1963 -0
- package/docs/components/avatar.md +252 -0
- package/docs/components/button.md +444 -0
- package/docs/components/calendar.md +1687 -0
- package/docs/components/carousel.md +1244 -0
- package/docs/components/checkbox.md +1069 -0
- package/docs/components/collapsible.md +567 -0
- package/docs/components/color-picker.md +736 -0
- package/docs/components/combobox.md +3647 -0
- package/docs/components/command.md +1573 -0
- package/docs/components/date-input.md +608 -0
- package/docs/components/date-picker.md +1266 -0
- package/docs/components/dialog.md +1469 -0
- package/docs/components/drawer.md +1197 -0
- package/docs/components/field.md +549 -0
- package/docs/components/file-input.md +609 -0
- package/docs/components/hover-card.md +514 -0
- package/docs/components/image-cropper.md +579 -0
- package/docs/components/input.md +301 -0
- package/docs/components/label.md +217 -0
- package/docs/components/listbox.md +1130 -0
- package/docs/components/menu.md +1286 -0
- package/docs/components/menubar.md +455 -0
- package/docs/components/meter.md +304 -0
- package/docs/components/navigation-menu.md +1581 -0
- package/docs/components/number-input.md +1086 -0
- package/docs/components/otp-input.md +565 -0
- package/docs/components/pagination.md +648 -0
- package/docs/components/password-input.md +737 -0
- package/docs/components/popover.md +1552 -0
- package/docs/components/preferences.md +220 -0
- package/docs/components/progress.md +443 -0
- package/docs/components/radio.md +846 -0
- package/docs/components/rating.md +458 -0
- package/docs/components/resizable.md +982 -0
- package/docs/components/scroll-area.md +980 -0
- package/docs/components/segmented-control.md +465 -0
- package/docs/components/select.md +2133 -0
- package/docs/components/separator.md +243 -0
- package/docs/components/slider.md +1169 -0
- package/docs/components/switch.md +613 -0
- package/docs/components/tabs.md +1014 -0
- package/docs/components/textarea.md +318 -0
- package/docs/components/time-input.md +231 -0
- package/docs/components/timeline.md +677 -0
- package/docs/components/toast.md +1395 -0
- package/docs/components/toggle.md +541 -0
- package/docs/components/token-input.md +1007 -0
- package/docs/components/toolbar.md +787 -0
- package/docs/components/tooltip.md +884 -0
- package/docs/components/tree.md +1346 -0
- package/docs/components/trigger-menu.md +1257 -0
- package/docs/components/waterfall.md +754 -0
- package/docs/controllers.md +89 -0
- package/docs/core.md +8027 -0
- package/docs/custom-parts.md +120 -0
- package/docs/first-component.md +83 -0
- package/docs/forms.md +148 -0
- package/docs/hooks/async-options.md +366 -0
- package/docs/hooks/shortcuts.md +221 -0
- package/docs/installation.md +45 -0
- package/docs/introduction.md +32 -0
- package/docs/lists-and-menus.md +170 -0
- package/docs/llms.txt +1844 -0
- package/docs/parts-and-composition.md +93 -0
- package/docs/popups.md +117 -0
- package/docs/rendering.md +121 -0
- package/docs/server-rendering.md +253 -0
- package/docs/state-and-events.md +105 -0
- package/docs/styling-and-animation.md +176 -0
- package/package.json +333 -3
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
# Image Cropper
|
|
2
|
+
|
|
3
|
+
Choose the part of an image to keep by moving and resizing a selection, or by panning and zooming the image under it.
|
|
4
|
+
|
|
5
|
+
Use an image cropper to choose the part of a photo to keep, like a profile picture or a cover image. The crop is a box in the image's own pixels, with a quarter-turn rotation and a flip, ready to pass to [`cropImage`](../core.md#images).
|
|
6
|
+
|
|
7
|
+
<ComponentExample
|
|
8
|
+
name="image-cropper/basic"
|
|
9
|
+
inspect
|
|
10
|
+
caption="Pick a ratio, drag the selection or one of its handles, then press Crop. Tab to the selection and use the arrow keys, with Shift to move further."
|
|
11
|
+
/>
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @ui-sdk/react @ui-sdk/core
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Composition
|
|
20
|
+
|
|
21
|
+
Use the following composition to build an `ImageCropper`:
|
|
22
|
+
|
|
23
|
+
```text
|
|
24
|
+
ImageCropper
|
|
25
|
+
├── ImageCropperViewport
|
|
26
|
+
│ ├── ImageCropperImage
|
|
27
|
+
│ └── ImageCropperSelection
|
|
28
|
+
│ ├── ImageCropperGrid
|
|
29
|
+
│ └── ImageCropperHandle
|
|
30
|
+
├── ImageCropperZoom
|
|
31
|
+
├── ImageCropperRotate
|
|
32
|
+
└── ImageCropperFlip
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **ImageCropper** holds the crop and the image's `src`, and is a group around the parts.
|
|
36
|
+
- **ImageCropperViewport** is the area the image shows in. Give it a size, and its padding keeps the image off its edges.
|
|
37
|
+
- **ImageCropperImage** is the image, turned and flipped. Give it `alt`.
|
|
38
|
+
- **ImageCropperSelection** shows the crop over the image. It's a slider you drag or move with the arrow keys.
|
|
39
|
+
- **ImageCropperGrid** draws lines across the selection, in thirds, for you to style.
|
|
40
|
+
- **ImageCropperHandle** resizes the selection from one edge or corner, its `position`, like `"se"` for the bottom right.
|
|
41
|
+
- **ImageCropperZoom** is a range input from 1, the largest crop, to `maxZoom`.
|
|
42
|
+
- **ImageCropperRotate** turns the image a quarter clockwise.
|
|
43
|
+
- **ImageCropperFlip** mirrors the image left to right, or top to bottom with `direction="vertical"`.
|
|
44
|
+
|
|
45
|
+
The viewport, image and selection are needed. The rest are optional.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Import the parts and put them together:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
"use client"
|
|
53
|
+
|
|
54
|
+
import {
|
|
55
|
+
ImageCropper,
|
|
56
|
+
ImageCropperHandle,
|
|
57
|
+
ImageCropperImage,
|
|
58
|
+
ImageCropperSelection,
|
|
59
|
+
ImageCropperViewport,
|
|
60
|
+
} from "@ui-sdk/react/image-cropper"
|
|
61
|
+
|
|
62
|
+
function CoverPhoto({ src }: { src: string }) {
|
|
63
|
+
return (
|
|
64
|
+
<ImageCropper aria-label="Cover photo" src={src} aspectRatio={3}>
|
|
65
|
+
<ImageCropperViewport>
|
|
66
|
+
<ImageCropperImage alt="Your cover photo" />
|
|
67
|
+
<ImageCropperSelection>
|
|
68
|
+
<ImageCropperHandle position="nw" />
|
|
69
|
+
<ImageCropperHandle position="se" />
|
|
70
|
+
</ImageCropperSelection>
|
|
71
|
+
</ImageCropperViewport>
|
|
72
|
+
</ImageCropper>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { CoverPhoto }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Name the cropper with `aria-label`. Until the image loads, the root has `data-loading` and `aria-busy`, and the selection is hidden. The crop then starts in the middle at `aspectRatio`, four fifths as large as the image allows so its edges show, or as large as it allows in a frame, unless you pass `defaultCrop`:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
<ImageCropper
|
|
83
|
+
aria-label="Photo"
|
|
84
|
+
src={src}
|
|
85
|
+
defaultCrop={{ x: 40, y: 40, width: 320, height: 240 }}
|
|
86
|
+
>
|
|
87
|
+
{/* … */}
|
|
88
|
+
</ImageCropper>
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Aspect Ratio
|
|
92
|
+
|
|
93
|
+
Set `aspectRatio` to keep the crop's width over its height, like `1` for a square or `16 / 9`. Changing it fits the crop to the new ratio about its middle. Without one, the crop is free, and holding Shift while dragging a handle keeps its current ratio. `minWidth` and `minHeight` stop it getting too small, in image pixels.
|
|
94
|
+
|
|
95
|
+
```tsx title="aspect-ratio.tsx"
|
|
96
|
+
"use client"
|
|
97
|
+
|
|
98
|
+
import * as React from "react"
|
|
99
|
+
import {
|
|
100
|
+
ImageCropper,
|
|
101
|
+
ImageCropperFlip,
|
|
102
|
+
ImageCropperGrid,
|
|
103
|
+
ImageCropperHandle,
|
|
104
|
+
ImageCropperImage,
|
|
105
|
+
ImageCropperSelection,
|
|
106
|
+
ImageCropperViewport,
|
|
107
|
+
} from "@ui-sdk/react/image-cropper"
|
|
108
|
+
|
|
109
|
+
const photo = `data:image/svg+xml,${encodeURIComponent(
|
|
110
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="640" height="420"><rect width="640" height="420" fill="#e5e5e5"/><circle cx="460" cy="150" r="56" fill="#fafafa"/><path d="M0 280 120 170l110 100 120-130 130 125 90-60 70 40v175H0z" fill="#a3a3a3"/><path d="M0 330l160-80 120 70 150-80 210 85v95H0z" fill="#525252"/><path d="M0 380q160-35 320-10t320-5v55H0z" fill="#262626"/></svg>`
|
|
111
|
+
)}`
|
|
112
|
+
|
|
113
|
+
const ratios = [
|
|
114
|
+
{ label: "Free", value: undefined },
|
|
115
|
+
{ label: "Square", value: 1 },
|
|
116
|
+
{ label: "Wide", value: 16 / 9 },
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
const handles = ["nw", "n", "ne", "e", "se", "s", "sw", "w"] as const
|
|
120
|
+
|
|
121
|
+
function Example() {
|
|
122
|
+
const [aspectRatio, setAspectRatio] = React.useState<number>()
|
|
123
|
+
return (
|
|
124
|
+
<div data-slot="stack">
|
|
125
|
+
<div data-slot="controls">
|
|
126
|
+
{ratios.map((ratio) => (
|
|
127
|
+
<button
|
|
128
|
+
key={ratio.label}
|
|
129
|
+
type="button"
|
|
130
|
+
aria-pressed={aspectRatio === ratio.value}
|
|
131
|
+
onClick={() => setAspectRatio(ratio.value)}
|
|
132
|
+
>
|
|
133
|
+
{ratio.label}
|
|
134
|
+
</button>
|
|
135
|
+
))}
|
|
136
|
+
</div>
|
|
137
|
+
<ImageCropper
|
|
138
|
+
aria-label="Photo"
|
|
139
|
+
src={photo}
|
|
140
|
+
aspectRatio={aspectRatio}
|
|
141
|
+
minWidth={64}
|
|
142
|
+
minHeight={64}
|
|
143
|
+
>
|
|
144
|
+
<ImageCropperViewport>
|
|
145
|
+
<ImageCropperImage alt="Mountains at sunset" />
|
|
146
|
+
<ImageCropperSelection>
|
|
147
|
+
<ImageCropperGrid />
|
|
148
|
+
{handles.map((position) => (
|
|
149
|
+
<ImageCropperHandle key={position} position={position} />
|
|
150
|
+
))}
|
|
151
|
+
</ImageCropperSelection>
|
|
152
|
+
</ImageCropperViewport>
|
|
153
|
+
<div data-slot="row">
|
|
154
|
+
<ImageCropperFlip>Flip</ImageCropperFlip>
|
|
155
|
+
<ImageCropperFlip direction="vertical">
|
|
156
|
+
Flip Vertically
|
|
157
|
+
</ImageCropperFlip>
|
|
158
|
+
</div>
|
|
159
|
+
</ImageCropper>
|
|
160
|
+
</div>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export { Example }
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Choose Square or Wide, then drag an edge. Flip mirrors the image and the crop with it.
|
|
168
|
+
|
|
169
|
+
## Frame Mode
|
|
170
|
+
|
|
171
|
+
Set `mode="frame"` for an avatar editor. The selection stays still and as large as it fits, and you pan the image under it with a drag, and zoom with the wheel, a pinch or the zoom input. Handles don't show in a frame.
|
|
172
|
+
|
|
173
|
+
`shape="circle"` rounds the selection, but the crop is still its box, so pass `shape: "circle"` to `cropImage` too. Here, Save Avatar crops a 128 pixel round file:
|
|
174
|
+
|
|
175
|
+
```tsx title="avatar.tsx"
|
|
176
|
+
"use client"
|
|
177
|
+
|
|
178
|
+
import * as React from "react"
|
|
179
|
+
import { cropImage } from "@ui-sdk/react/image"
|
|
180
|
+
import {
|
|
181
|
+
ImageCropper,
|
|
182
|
+
ImageCropperImage,
|
|
183
|
+
ImageCropperRotate,
|
|
184
|
+
ImageCropperSelection,
|
|
185
|
+
ImageCropperViewport,
|
|
186
|
+
ImageCropperZoom,
|
|
187
|
+
type ImageCrop,
|
|
188
|
+
} from "@ui-sdk/react/image-cropper"
|
|
189
|
+
|
|
190
|
+
const photo = `data:image/svg+xml,${encodeURIComponent(
|
|
191
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="640" height="420"><rect width="640" height="420" fill="#e5e5e5"/><circle cx="460" cy="150" r="56" fill="#fafafa"/><path d="M0 280 120 170l110 100 120-130 130 125 90-60 70 40v175H0z" fill="#a3a3a3"/><path d="M0 330l160-80 120 70 150-80 210 85v95H0z" fill="#525252"/><path d="M0 380q160-35 320-10t320-5v55H0z" fill="#262626"/></svg>`
|
|
192
|
+
)}`
|
|
193
|
+
|
|
194
|
+
function Example() {
|
|
195
|
+
const [crop, setCrop] = React.useState<ImageCrop>()
|
|
196
|
+
const [avatar, setAvatar] = React.useState<string>()
|
|
197
|
+
|
|
198
|
+
React.useEffect(
|
|
199
|
+
() => () => {
|
|
200
|
+
if (avatar) {
|
|
201
|
+
URL.revokeObjectURL(avatar)
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
[avatar]
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
async function save() {
|
|
208
|
+
if (!crop) {
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
const file = await cropImage(photo, crop, { width: 128, shape: "circle" })
|
|
212
|
+
setAvatar(URL.createObjectURL(file))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return (
|
|
216
|
+
<ImageCropper
|
|
217
|
+
aria-label="Profile photo"
|
|
218
|
+
src={photo}
|
|
219
|
+
mode="frame"
|
|
220
|
+
aspectRatio={1}
|
|
221
|
+
shape="circle"
|
|
222
|
+
maxZoom={4}
|
|
223
|
+
crop={crop}
|
|
224
|
+
onCropChange={setCrop}
|
|
225
|
+
>
|
|
226
|
+
<ImageCropperViewport>
|
|
227
|
+
<ImageCropperImage alt="Mountains at sunset" />
|
|
228
|
+
<ImageCropperSelection />
|
|
229
|
+
</ImageCropperViewport>
|
|
230
|
+
<div data-slot="row">
|
|
231
|
+
<ImageCropperZoom />
|
|
232
|
+
<ImageCropperRotate>Rotate</ImageCropperRotate>
|
|
233
|
+
</div>
|
|
234
|
+
<div data-slot="row">
|
|
235
|
+
<button type="button" onClick={save}>
|
|
236
|
+
Save Avatar
|
|
237
|
+
</button>
|
|
238
|
+
{avatar && (
|
|
239
|
+
<img data-slot="image-cropper-avatar" src={avatar} alt="Your new avatar" />
|
|
240
|
+
)}
|
|
241
|
+
</div>
|
|
242
|
+
</ImageCropper>
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export { Example }
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Drag the image or move the zoom, then press Save Avatar. The new avatar shows beside the button.
|
|
250
|
+
|
|
251
|
+
## Rotate and Flip
|
|
252
|
+
|
|
253
|
+
`ImageCropperRotate` turns the image a quarter clockwise, and R does the same from the keyboard, with Shift to turn it back. `ImageCropperFlip` mirrors the image. The crop turns and flips with the image, so it keeps the same part of the photo. Its `x` and `y` count from the top left of the image as it shows, turned and flipped, which is what `cropImage` takes.
|
|
254
|
+
|
|
255
|
+
## Disabled
|
|
256
|
+
|
|
257
|
+
Set `disabled`. The selection and handles can't be focused or dragged, and the zoom input and buttons are disabled.
|
|
258
|
+
|
|
259
|
+
```tsx
|
|
260
|
+
<ImageCropper aria-label="Photo" src={src} disabled>
|
|
261
|
+
{/* … */}
|
|
262
|
+
</ImageCropper>
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Controlled
|
|
266
|
+
|
|
267
|
+
Pass `crop` and update it in `onCropChange`. Each change carries a [reason](#reasons), like `drag`, `step-key` or `zoom-input`. Call `details.cancel()` in the callback to keep the current crop.
|
|
268
|
+
|
|
269
|
+
`onCropChange` runs on every step of a drag and once the image loads. `onCropCommit` runs once, when the drag, key press or wheel ends, so use it to save. A controller from `createImageCropperController()` sets the crop from anywhere:
|
|
270
|
+
|
|
271
|
+
```tsx
|
|
272
|
+
"use client"
|
|
273
|
+
|
|
274
|
+
import * as React from "react"
|
|
275
|
+
import {
|
|
276
|
+
createImageCropperController,
|
|
277
|
+
ImageCropper,
|
|
278
|
+
ImageCropperImage,
|
|
279
|
+
ImageCropperSelection,
|
|
280
|
+
ImageCropperViewport,
|
|
281
|
+
} from "@ui-sdk/react/image-cropper"
|
|
282
|
+
|
|
283
|
+
function Photo({ src }: { src: string }) {
|
|
284
|
+
const [controller] = React.useState(() => createImageCropperController())
|
|
285
|
+
return (
|
|
286
|
+
<>
|
|
287
|
+
<ImageCropper
|
|
288
|
+
aria-label="Photo"
|
|
289
|
+
src={src}
|
|
290
|
+
controller={controller}
|
|
291
|
+
onCropCommit={(crop, details) => console.log(crop, details.reason)}
|
|
292
|
+
>
|
|
293
|
+
<ImageCropperViewport>
|
|
294
|
+
<ImageCropperImage alt="Your photo" />
|
|
295
|
+
<ImageCropperSelection />
|
|
296
|
+
</ImageCropperViewport>
|
|
297
|
+
</ImageCropper>
|
|
298
|
+
<button type="button" onClick={() => controller.setZoom(2)}>
|
|
299
|
+
Zoom In
|
|
300
|
+
</button>
|
|
301
|
+
</>
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export { Photo }
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
See [State and Events](../state-and-events.md) for controlled state, cancellation and debugging.
|
|
309
|
+
|
|
310
|
+
## Custom Rendering
|
|
311
|
+
|
|
312
|
+
Every part takes `render`, to render a different element or component in its place. `render`, `className` and `children` can be functions of the part's state. Every part has the `crop`, the `zoom`, and whether it's `loading`, has an `error` or is `dragging`:
|
|
313
|
+
|
|
314
|
+
```tsx
|
|
315
|
+
<ImageCropper aria-label="Photo" src={src}>
|
|
316
|
+
{({ loading, error, zoom }) => (
|
|
317
|
+
<>
|
|
318
|
+
<ImageCropperViewport>
|
|
319
|
+
<ImageCropperImage alt="Your photo" />
|
|
320
|
+
<ImageCropperSelection />
|
|
321
|
+
</ImageCropperViewport>
|
|
322
|
+
{loading && <p>Loading…</p>}
|
|
323
|
+
{error && <p>The image didn't load.</p>}
|
|
324
|
+
<output>{Math.round(zoom * 100)}%</output>
|
|
325
|
+
</>
|
|
326
|
+
)}
|
|
327
|
+
</ImageCropper>
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
See [Parts and Composition](../parts-and-composition.md) for the rendering contract and [State](#state) for each part's fields.
|
|
331
|
+
|
|
332
|
+
## Styling
|
|
333
|
+
|
|
334
|
+
Parts are unstyled, apart from the positions they need: the viewport is `position: relative` with `overflow: hidden`, and the image and selection get their place as inline styles in pixels. Give the viewport a size, and the handles a size and a place on their edge or corner of the selection.
|
|
335
|
+
|
|
336
|
+
A box viewport fits the whole image, so set its `aspect-ratio` to `--image-cropper-aspect-ratio`. A wide `box-shadow` on the selection dims the image outside it. Show the grid only while `data-dragging` is on it.
|
|
337
|
+
|
|
338
|
+
Each part has a `data-slot`, such as `image-cropper-selection`, and its state attributes are listed under [Data Attributes](#data-attributes).
|
|
339
|
+
|
|
340
|
+
### ImageCropperViewport
|
|
341
|
+
- `--image-cropper-aspect-ratio`: The image's width over its height as it shows, turned, once it loads. Give the viewport `aspect-ratio: var(--image-cropper-aspect-ratio)` to fit the image.
|
|
342
|
+
|
|
343
|
+
## Data Attributes
|
|
344
|
+
|
|
345
|
+
### ImageCropper
|
|
346
|
+
`[data-slot="image-cropper"]`
|
|
347
|
+
|
|
348
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
349
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
350
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
351
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
352
|
+
- `data-error`: Present when the image failed to load.
|
|
353
|
+
- `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
|
|
354
|
+
|
|
355
|
+
### ImageCropperViewport
|
|
356
|
+
`[data-slot="image-cropper-viewport"]`
|
|
357
|
+
|
|
358
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
359
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
360
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
361
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
362
|
+
- `data-error`: Present when the image failed to load.
|
|
363
|
+
- `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
|
|
364
|
+
|
|
365
|
+
### ImageCropperImage
|
|
366
|
+
`[data-slot="image-cropper-image"]`
|
|
367
|
+
|
|
368
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
369
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
370
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
371
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
372
|
+
- `data-error`: Present when the image failed to load.
|
|
373
|
+
|
|
374
|
+
### ImageCropperSelection
|
|
375
|
+
`[data-slot="image-cropper-selection"]`
|
|
376
|
+
|
|
377
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
378
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
379
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
380
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
381
|
+
- `data-error`: Present when the image failed to load.
|
|
382
|
+
- `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
|
|
383
|
+
|
|
384
|
+
### ImageCropperGrid
|
|
385
|
+
`[data-slot="image-cropper-grid"]`
|
|
386
|
+
|
|
387
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
388
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
389
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
390
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
391
|
+
- `data-error`: Present when the image failed to load.
|
|
392
|
+
- `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
|
|
393
|
+
|
|
394
|
+
### ImageCropperHandle
|
|
395
|
+
`[data-slot="image-cropper-handle"]`
|
|
396
|
+
|
|
397
|
+
- `data-position` (`n`, `e`, `s`, `w`, `ne`, `nw`, `se`, `sw`): The edge or corner the handle resizes.
|
|
398
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
399
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
400
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
401
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
402
|
+
- `data-error`: Present when the image failed to load.
|
|
403
|
+
- `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
|
|
404
|
+
|
|
405
|
+
### ImageCropperZoom
|
|
406
|
+
`[data-slot="image-cropper-zoom"]`
|
|
407
|
+
|
|
408
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
409
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
410
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
411
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
412
|
+
- `data-error`: Present when the image failed to load.
|
|
413
|
+
|
|
414
|
+
### ImageCropperRotate
|
|
415
|
+
`[data-slot="image-cropper-rotate"]`
|
|
416
|
+
|
|
417
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
418
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
419
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
420
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
421
|
+
- `data-error`: Present when the image failed to load.
|
|
422
|
+
|
|
423
|
+
### ImageCropperFlip
|
|
424
|
+
`[data-slot="image-cropper-flip"]`
|
|
425
|
+
|
|
426
|
+
- `data-direction` (`horizontal`, `vertical`): Which way the button mirrors the image.
|
|
427
|
+
- `data-mode` (`box`, `frame`): How the crop changes.
|
|
428
|
+
- `data-shape` (`rectangle`, `circle`): The selection's shape.
|
|
429
|
+
- `data-loading`: Present until the image loads, while nothing can change.
|
|
430
|
+
- `data-disabled`: Present when the cropper is disabled.
|
|
431
|
+
- `data-error`: Present when the image failed to load.
|
|
432
|
+
|
|
433
|
+
## API Reference
|
|
434
|
+
|
|
435
|
+
Props and callbacks are the same as below, as React props.
|
|
436
|
+
|
|
437
|
+
### ImageCropper
|
|
438
|
+
|
|
439
|
+
- `src?` (`string`): The image's URL, like an object URL for a file someone picked.
|
|
440
|
+
- `crop?` (`ImageCrop`): The crop, in the image's own pixels, from the top left of the image as it shows once turned and flipped, with its `rotation`, `flipX` and `flipY`. Pass it straight to `cropImage`. Pass it to control the cropper.
|
|
441
|
+
- `defaultCrop?` (`ImageCrop`): The crop at first, when uncontrolled.
|
|
442
|
+
- `onCropChange?` (`((crop: Required<ImageCrop>, details: ImageCropperCropChangeDetails) => void)`): Runs while the crop changes: on every step of a drag, a key or the wheel, and once an image loads. `details.cancel()` stops it.
|
|
443
|
+
- `onCropCommit?` (`((crop: Required<ImageCrop>, details: ImageCropperCropCommitDetails) => void)`): Runs once an interaction that changed the crop ends: the pointer lifts, a key is pressed, the wheel pauses. For saving.
|
|
444
|
+
- `mode?` (`ImageCropperMode`): `"box"` moves and resizes a selection over the whole image. `"frame"` keeps the selection still and as large as it fits, and pans and zooms the image under it, like an avatar editor.
|
|
445
|
+
- `aspectRatio?` (`number`): Keeps the crop's width over its height, like `1` for a square or `16 / 9`. Changing it fits the crop to the new ratio.
|
|
446
|
+
- `shape?` (`ImageCropperShape`): The selection's shape, as `data-shape`. A circle is still a box to the crop, so pass `shape: "circle"` to `cropImage` too.
|
|
447
|
+
- `minWidth?` (`number`): The narrowest the crop gets, in image pixels.
|
|
448
|
+
- `minHeight?` (`number`): The shortest the crop gets, in image pixels.
|
|
449
|
+
- `maxZoom?` (`number`): How far the crop magnifies the image, where 1 is the largest crop the image allows.
|
|
450
|
+
- `disabled?` (`boolean`): Stops every change: the selection can't be focused, and the buttons and zoom are disabled.
|
|
451
|
+
- `controller?` (`object`): Sets the crop from anywhere. Create one with `createImageCropperController()`.
|
|
452
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
453
|
+
|
|
454
|
+
### ImageCropperHandle
|
|
455
|
+
|
|
456
|
+
- `position` (`ImageCropperHandlePosition`): The edge or corner it resizes, like `"nw"` for the top left corner.
|
|
457
|
+
|
|
458
|
+
### ImageCropperFlip
|
|
459
|
+
|
|
460
|
+
- `direction?` (`ImageCropperFlipDirection`): Which way it mirrors the image.
|
|
461
|
+
|
|
462
|
+
### State
|
|
463
|
+
|
|
464
|
+
What render functions, state-based class names and slots receive. Every part gets the cropper's state.
|
|
465
|
+
|
|
466
|
+
- `crop` (`Required<ImageCrop> | null`): The crop, or null until the image loads.
|
|
467
|
+
- `zoom` (`number`): How far the crop magnifies: 1 when it's as large as the image allows at its ratio.
|
|
468
|
+
- `loading` (`boolean`): Whether the image is still loading, so nothing can change yet.
|
|
469
|
+
- `error` (`boolean`): Whether the image failed to load.
|
|
470
|
+
- `dragging` (`boolean`): Whether a pointer is dragging the selection, a handle or the image.
|
|
471
|
+
- `mode` (`ImageCropperMode`): How the crop changes.
|
|
472
|
+
- `shape` (`ImageCropperShape`): The selection's shape.
|
|
473
|
+
- `disabled` (`boolean`): Whether the `disabled` option stops every change.
|
|
474
|
+
|
|
475
|
+
Handles add their own:
|
|
476
|
+
|
|
477
|
+
- `crop` (`Required<ImageCrop> | null`): The crop, or null until the image loads.
|
|
478
|
+
- `zoom` (`number`): How far the crop magnifies: 1 when it's as large as the image allows at its ratio.
|
|
479
|
+
- `loading` (`boolean`): Whether the image is still loading, so nothing can change yet.
|
|
480
|
+
- `error` (`boolean`): Whether the image failed to load.
|
|
481
|
+
- `dragging` (`boolean`): Whether a pointer is dragging the selection, a handle or the image.
|
|
482
|
+
- `mode` (`ImageCropperMode`): How the crop changes.
|
|
483
|
+
- `shape` (`ImageCropperShape`): The selection's shape.
|
|
484
|
+
- `disabled` (`boolean`): Whether the `disabled` option stops every change.
|
|
485
|
+
- `position` (`ImageCropperHandlePosition`): The edge or corner it resizes.
|
|
486
|
+
|
|
487
|
+
### Reasons
|
|
488
|
+
|
|
489
|
+
`details.reason` says why the crop changed.
|
|
490
|
+
|
|
491
|
+
- `image-load` (`Event | undefined`): An image loaded. No event when it was already in the browser's cache.
|
|
492
|
+
- `drag` (`PointerEvent`): A slider thumb or a resize handle was dragged, or an item was dragged to a new place in a list.
|
|
493
|
+
- `pinch`
|
|
494
|
+
- `wheel` (`WheelEvent`): The mouse wheel stepped a focused number input.
|
|
495
|
+
- `step-key` (`KeyboardEvent`): Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
|
|
496
|
+
- `zoom-key`
|
|
497
|
+
- `zoom-input`
|
|
498
|
+
- `rotate-key`
|
|
499
|
+
- `rotate-press`
|
|
500
|
+
- `flip-press`
|
|
501
|
+
- `aspect-ratio-change`
|
|
502
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
503
|
+
|
|
504
|
+
### Controller
|
|
505
|
+
|
|
506
|
+
Create one with `createImageCropperController()` and pass it as `controller`.
|
|
507
|
+
|
|
508
|
+
Each write method takes an optional `{ reason, event }`, runs the change callback like a drag, and returns `false` when nothing changed or the change is canceled.
|
|
509
|
+
|
|
510
|
+
- `getCrop()` (`() => Required<ImageCrop> | null`): The crop, or null until the image loads.
|
|
511
|
+
- `setCrop(crop, options?)` (`(crop: ImageCrop, options?: ImageCropperChangeOptions | undefined) => boolean`): Sets the crop, as a user would. Returns whether it changed.
|
|
512
|
+
- `rotate(direction?, options?)` (`(direction?: 1 | -1 | undefined, options?: ImageCropperChangeOptions | undefined) => boolean`): Turns the image a quarter clockwise, or counterclockwise with `-1`.
|
|
513
|
+
- `flip(direction?, options?)` (`(direction?: ImageCropperFlipDirection | undefined, options?: ImageCropperChangeOptions | undefined) => boolean`): Mirrors the image left to right, or top to bottom with `"vertical"`.
|
|
514
|
+
- `getZoom()` (`() => number`): How far the crop magnifies: 1 when it's as large as the image allows at its ratio.
|
|
515
|
+
- `setZoom(zoom, options?)` (`(zoom: number, options?: ImageCropperChangeOptions | undefined) => boolean`): Zooms to a level, about the crop's middle, within 1 and `maxZoom`.
|
|
516
|
+
- `focus()` (`() => void`): Moves focus to the selection.
|
|
517
|
+
|
|
518
|
+
### Events
|
|
519
|
+
|
|
520
|
+
Callbacks get the new crop first, then the details.
|
|
521
|
+
|
|
522
|
+
| Prop | On | Receives |
|
|
523
|
+
| -------------- | -------------- | ----------------------------------------------------------------- |
|
|
524
|
+
| `onCropChange` | `ImageCropper` | `(crop, details)`: the new crop and the [reason](#reasons) |
|
|
525
|
+
| `onCropCommit` | `ImageCropper` | `(crop, details)`: the crop once the interaction ends, its reason |
|
|
526
|
+
|
|
527
|
+
## Accessibility
|
|
528
|
+
|
|
529
|
+
No WAI-ARIA pattern covers an image cropper, so it's built from ones that do. The cropper is a group. The selection and each handle follow the [WAI-ARIA slider pattern](https://www.w3.org/WAI/ARIA/apg/patterns/slider/), and say the crop in words, like "300 by 300 pixels, at 50, 0". The zoom is a native range input that says its level, like "125%".
|
|
530
|
+
|
|
531
|
+
The selection, handles and zoom are named in English by default. Pass `aria-label` to name them in another language.
|
|
532
|
+
|
|
533
|
+
### Keyboard
|
|
534
|
+
|
|
535
|
+
| Key | Does |
|
|
536
|
+
| ------------------- | -------------------------------------------------------- |
|
|
537
|
+
| Arrow keys | Move the selection, or on a handle, move its edges |
|
|
538
|
+
| Shift with an arrow | Moves ten times as far |
|
|
539
|
+
| Plus or Minus | Zooms in or out, about the crop's middle |
|
|
540
|
+
| R | Turns the image a quarter clockwise, or back with Shift |
|
|
541
|
+
| Escape | During a drag, puts the crop back where the drag started |
|
|
542
|
+
|
|
543
|
+
Each key press commits, like the end of a drag.
|
|
544
|
+
|
|
545
|
+
### Tested Behavior
|
|
546
|
+
|
|
547
|
+
Every line below is an automated test.
|
|
548
|
+
|
|
549
|
+
**Loading**
|
|
550
|
+
|
|
551
|
+
- The crop starts once the image loads, as four fifths of the largest at the ratio.
|
|
552
|
+
|
|
553
|
+
**Box**
|
|
554
|
+
|
|
555
|
+
- A drag moves the selection over the image and commits once.
|
|
556
|
+
- A handle resizes from its corner, within the image.
|
|
557
|
+
- At a ratio, a handle keeps it.
|
|
558
|
+
- Escape during a drag puts the selection back.
|
|
559
|
+
|
|
560
|
+
**Keyboard**
|
|
561
|
+
|
|
562
|
+
- Arrows move the selection, ten times as far with Shift.
|
|
563
|
+
- Arrows on a handle move its edges, and the next Tab reaches it.
|
|
564
|
+
- Plus and minus zoom about the middle, and R turns the image.
|
|
565
|
+
|
|
566
|
+
**Controls**
|
|
567
|
+
|
|
568
|
+
- The zoom input zooms the crop.
|
|
569
|
+
- Rotate turns the image a quarter clockwise, and flip mirrors it.
|
|
570
|
+
- A controller sets the crop, and a canceled change keeps it.
|
|
571
|
+
- Disabled, nothing takes focus or changes.
|
|
572
|
+
|
|
573
|
+
**Frame**
|
|
574
|
+
|
|
575
|
+
- The selection stays put and a drag pans the image under it.
|
|
576
|
+
- Escape during a pan puts the image back.
|
|
577
|
+
- The wheel zooms at the pointer and commits after a pause.
|
|
578
|
+
|
|
579
|
+
Runs in React, Vue and web components.
|