@ui-sdk/vue 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.
Files changed (262) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +10 -2
  3. package/dist/accordion.d.ts +561 -0
  4. package/dist/accordion.js +24 -0
  5. package/dist/agent.d.ts +2 -0
  6. package/dist/agent.js +7 -0
  7. package/dist/alert-dialog.d.ts +231 -0
  8. package/dist/alert-dialog.js +33 -0
  9. package/dist/async-options.d.ts +18 -0
  10. package/dist/async-options.js +40 -0
  11. package/dist/autocomplete.d.ts +384 -0
  12. package/dist/autocomplete.js +344 -0
  13. package/dist/avatar.d.ts +40 -0
  14. package/dist/avatar.js +28 -0
  15. package/dist/button.d.ts +52 -0
  16. package/dist/button.js +43 -0
  17. package/dist/calendar-context.d.ts +10 -0
  18. package/dist/calendar-context.js +7 -0
  19. package/dist/calendar.d.ts +311 -0
  20. package/dist/calendar.js +349 -0
  21. package/dist/carousel.d.ts +236 -0
  22. package/dist/carousel.js +204 -0
  23. package/dist/checkbox.d.ts +211 -0
  24. package/dist/checkbox.js +151 -0
  25. package/dist/children.d.ts +41 -0
  26. package/dist/children.js +135 -0
  27. package/dist/collapsible.d.ts +54 -0
  28. package/dist/collapsible.js +18 -0
  29. package/dist/color-picker.d.ts +187 -0
  30. package/dist/color-picker.js +158 -0
  31. package/dist/color.d.ts +2 -0
  32. package/dist/color.js +21 -0
  33. package/dist/combobox.d.ts +517 -0
  34. package/dist/combobox.js +531 -0
  35. package/dist/command.d.ts +322 -0
  36. package/dist/command.js +345 -0
  37. package/dist/controller-target.d.ts +49 -0
  38. package/dist/controller-target.js +112 -0
  39. package/dist/create-parts.d.ts +126 -0
  40. package/dist/create-parts.js +206 -0
  41. package/dist/date-input.d.ts +133 -0
  42. package/dist/date-input.js +31 -0
  43. package/dist/date-picker.d.ts +328 -0
  44. package/dist/date-picker.js +354 -0
  45. package/dist/date.d.ts +2 -0
  46. package/dist/date.js +59 -0
  47. package/dist/dialog-parts.d.ts +259 -0
  48. package/dist/dialog-parts.js +300 -0
  49. package/dist/dialog.d.ts +251 -0
  50. package/dist/dialog.js +25 -0
  51. package/dist/drawer.d.ts +304 -0
  52. package/dist/drawer.js +373 -0
  53. package/dist/editor-context.d.ts +4 -0
  54. package/dist/editor-context.js +5 -0
  55. package/dist/field-context.d.ts +26 -0
  56. package/dist/field-context.js +44 -0
  57. package/dist/field.d.ts +154 -0
  58. package/dist/field.js +240 -0
  59. package/dist/file-input.d.ts +212 -0
  60. package/dist/file-input.js +255 -0
  61. package/dist/form-inputs.d.ts +16 -0
  62. package/dist/form-inputs.js +25 -0
  63. package/dist/gesture.d.ts +1 -0
  64. package/dist/gesture.js +15 -0
  65. package/dist/history.d.ts +2 -0
  66. package/dist/history.js +11 -0
  67. package/dist/hover-card.d.ts +137 -0
  68. package/dist/hover-card.js +153 -0
  69. package/dist/image-cropper.d.ts +74 -0
  70. package/dist/image-cropper.js +36 -0
  71. package/dist/image.d.ts +2 -0
  72. package/dist/image.js +5 -0
  73. package/dist/index.d.ts +59 -0
  74. package/dist/index.js +60 -0
  75. package/dist/input.d.ts +120 -0
  76. package/dist/input.js +73 -0
  77. package/dist/item-icon.d.ts +34 -0
  78. package/dist/item-icon.js +63 -0
  79. package/dist/label.d.ts +16 -0
  80. package/dist/label.js +8 -0
  81. package/dist/lifecycle.d.ts +11 -0
  82. package/dist/lifecycle.js +91 -0
  83. package/dist/list-items.d.ts +38 -0
  84. package/dist/list-items.js +59 -0
  85. package/dist/list-text.d.ts +7 -0
  86. package/dist/list-text.js +15 -0
  87. package/dist/listbox.d.ts +273 -0
  88. package/dist/listbox.js +126 -0
  89. package/dist/menu.d.ts +678 -0
  90. package/dist/menu.js +607 -0
  91. package/dist/menubar-context.d.ts +5 -0
  92. package/dist/menubar-context.js +5 -0
  93. package/dist/menubar.d.ts +61 -0
  94. package/dist/menubar.js +48 -0
  95. package/dist/meter.d.ts +41 -0
  96. package/dist/meter.js +37 -0
  97. package/dist/navigation-menu.d.ts +228 -0
  98. package/dist/navigation-menu.js +195 -0
  99. package/dist/number-input.d.ts +176 -0
  100. package/dist/number-input.js +231 -0
  101. package/dist/number.d.ts +2 -0
  102. package/dist/number.js +39 -0
  103. package/dist/nuxt/entries.json +977 -0
  104. package/dist/nuxt/module.d.ts +18 -0
  105. package/dist/nuxt/module.js +31 -0
  106. package/dist/nuxt/registrations.d.ts +22 -0
  107. package/dist/nuxt/registrations.js +22 -0
  108. package/dist/observe.d.ts +2 -0
  109. package/dist/observe.js +7 -0
  110. package/dist/otp-input.d.ts +142 -0
  111. package/dist/otp-input.js +130 -0
  112. package/dist/overlays.d.ts +40 -0
  113. package/dist/overlays.js +118 -0
  114. package/dist/pagination.d.ts +201 -0
  115. package/dist/pagination.js +146 -0
  116. package/dist/password-input.d.ts +144 -0
  117. package/dist/password-input.js +134 -0
  118. package/dist/popover.d.ts +211 -0
  119. package/dist/popover.js +258 -0
  120. package/dist/popup.d.ts +60 -0
  121. package/dist/popup.js +89 -0
  122. package/dist/preferences.d.ts +44 -0
  123. package/dist/preferences.js +102 -0
  124. package/dist/primitive.d.ts +2 -0
  125. package/dist/primitive.js +11 -0
  126. package/dist/progress.d.ts +41 -0
  127. package/dist/progress.js +37 -0
  128. package/dist/radio.d.ts +164 -0
  129. package/dist/radio.js +145 -0
  130. package/dist/rating.d.ts +149 -0
  131. package/dist/rating.js +146 -0
  132. package/dist/renderless.d.ts +4 -0
  133. package/dist/renderless.js +28 -0
  134. package/dist/resizable.d.ts +54 -0
  135. package/dist/resizable.js +22 -0
  136. package/dist/scroll-area.d.ts +72 -0
  137. package/dist/scroll-area.js +77 -0
  138. package/dist/segmented-control.d.ts +151 -0
  139. package/dist/segmented-control.js +110 -0
  140. package/dist/segmented-input.d.ts +76 -0
  141. package/dist/segmented-input.js +115 -0
  142. package/dist/select.d.ts +458 -0
  143. package/dist/select.js +468 -0
  144. package/dist/separator.d.ts +14 -0
  145. package/dist/separator.js +8 -0
  146. package/dist/shortcuts.d.ts +32 -0
  147. package/dist/shortcuts.js +56 -0
  148. package/dist/slider.d.ts +65 -0
  149. package/dist/slider.js +194 -0
  150. package/dist/switch.d.ts +103 -0
  151. package/dist/switch.js +71 -0
  152. package/dist/tabs.d.ts +338 -0
  153. package/dist/tabs.js +27 -0
  154. package/dist/text-control.d.ts +89 -0
  155. package/dist/text-control.js +51 -0
  156. package/dist/text-match.d.ts +2 -0
  157. package/dist/text-match.js +7 -0
  158. package/dist/textarea.d.ts +106 -0
  159. package/dist/textarea.js +68 -0
  160. package/dist/time-input.d.ts +131 -0
  161. package/dist/time-input.js +32 -0
  162. package/dist/timeline.d.ts +81 -0
  163. package/dist/timeline.js +30 -0
  164. package/dist/toast-parts.d.ts +203 -0
  165. package/dist/toast-parts.js +270 -0
  166. package/dist/toast.d.ts +2 -0
  167. package/dist/toast.js +21 -0
  168. package/dist/toggle-props.d.ts +22 -0
  169. package/dist/toggle-props.js +14 -0
  170. package/dist/toggle.d.ts +199 -0
  171. package/dist/toggle.js +129 -0
  172. package/dist/token-input.d.ts +136 -0
  173. package/dist/token-input.js +214 -0
  174. package/dist/toolbar-context.d.ts +7 -0
  175. package/dist/toolbar-context.js +20 -0
  176. package/dist/toolbar.d.ts +120 -0
  177. package/dist/toolbar.js +77 -0
  178. package/dist/tooltip.d.ts +160 -0
  179. package/dist/tooltip.js +224 -0
  180. package/dist/tree.d.ts +300 -0
  181. package/dist/tree.js +209 -0
  182. package/dist/trigger-menu.d.ts +452 -0
  183. package/dist/trigger-menu.js +254 -0
  184. package/dist/use-primitive.d.ts +132 -0
  185. package/dist/use-primitive.js +462 -0
  186. package/dist/waterfall.d.ts +201 -0
  187. package/dist/waterfall.js +171 -0
  188. package/docs/README.md +89 -0
  189. package/docs/agents.md +1523 -0
  190. package/docs/components/accordion.md +1369 -0
  191. package/docs/components/alert-dialog.md +799 -0
  192. package/docs/components/autocomplete.md +1857 -0
  193. package/docs/components/avatar.md +272 -0
  194. package/docs/components/button.md +435 -0
  195. package/docs/components/calendar.md +1589 -0
  196. package/docs/components/carousel.md +1171 -0
  197. package/docs/components/checkbox.md +1008 -0
  198. package/docs/components/collapsible.md +536 -0
  199. package/docs/components/color-picker.md +706 -0
  200. package/docs/components/combobox.md +3504 -0
  201. package/docs/components/command.md +1481 -0
  202. package/docs/components/date-input.md +536 -0
  203. package/docs/components/date-picker.md +1244 -0
  204. package/docs/components/dialog.md +1397 -0
  205. package/docs/components/drawer.md +1166 -0
  206. package/docs/components/field.md +538 -0
  207. package/docs/components/file-input.md +611 -0
  208. package/docs/components/hover-card.md +505 -0
  209. package/docs/components/image-cropper.md +556 -0
  210. package/docs/components/input.md +306 -0
  211. package/docs/components/label.md +203 -0
  212. package/docs/components/listbox.md +1004 -0
  213. package/docs/components/menu.md +1229 -0
  214. package/docs/components/menubar.md +443 -0
  215. package/docs/components/meter.md +315 -0
  216. package/docs/components/navigation-menu.md +1523 -0
  217. package/docs/components/number-input.md +1029 -0
  218. package/docs/components/otp-input.md +512 -0
  219. package/docs/components/pagination.md +613 -0
  220. package/docs/components/password-input.md +649 -0
  221. package/docs/components/popover.md +1448 -0
  222. package/docs/components/preferences.md +208 -0
  223. package/docs/components/progress.md +423 -0
  224. package/docs/components/radio.md +785 -0
  225. package/docs/components/rating.md +416 -0
  226. package/docs/components/resizable.md +910 -0
  227. package/docs/components/scroll-area.md +915 -0
  228. package/docs/components/segmented-control.md +435 -0
  229. package/docs/components/select.md +2015 -0
  230. package/docs/components/separator.md +229 -0
  231. package/docs/components/slider.md +1084 -0
  232. package/docs/components/switch.md +565 -0
  233. package/docs/components/tabs.md +944 -0
  234. package/docs/components/textarea.md +299 -0
  235. package/docs/components/time-input.md +224 -0
  236. package/docs/components/timeline.md +654 -0
  237. package/docs/components/toast.md +1224 -0
  238. package/docs/components/toggle.md +500 -0
  239. package/docs/components/token-input.md +953 -0
  240. package/docs/components/toolbar.md +739 -0
  241. package/docs/components/tooltip.md +818 -0
  242. package/docs/components/tree.md +1306 -0
  243. package/docs/components/trigger-menu.md +1223 -0
  244. package/docs/components/waterfall.md +717 -0
  245. package/docs/controllers.md +71 -0
  246. package/docs/core.md +8027 -0
  247. package/docs/custom-parts.md +89 -0
  248. package/docs/first-component.md +83 -0
  249. package/docs/forms.md +122 -0
  250. package/docs/installation.md +72 -0
  251. package/docs/introduction.md +32 -0
  252. package/docs/lists-and-menus.md +122 -0
  253. package/docs/llms.txt +1502 -0
  254. package/docs/parts-and-composition.md +85 -0
  255. package/docs/popups.md +108 -0
  256. package/docs/rendering.md +56 -0
  257. package/docs/server-rendering.md +180 -0
  258. package/docs/state-and-events.md +100 -0
  259. package/docs/styling-and-animation.md +148 -0
  260. package/docs/utilities/async-options.md +348 -0
  261. package/docs/utilities/shortcuts.md +159 -0
  262. package/package.json +352 -3
@@ -0,0 +1,556 @@
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/vue @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 `max-zoom`.
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
+ ```vue
52
+ <script setup lang="ts">
53
+ import {
54
+ ImageCropper,
55
+ ImageCropperHandle,
56
+ ImageCropperImage,
57
+ ImageCropperSelection,
58
+ ImageCropperViewport,
59
+ } from "@ui-sdk/vue/image-cropper"
60
+
61
+ defineProps<{ src: string }>()
62
+ </script>
63
+
64
+ <template>
65
+ <ImageCropper aria-label="Cover photo" :src="src" :aspect-ratio="3">
66
+ <ImageCropperViewport>
67
+ <ImageCropperImage alt="Your cover photo" />
68
+ <ImageCropperSelection>
69
+ <ImageCropperHandle position="nw" />
70
+ <ImageCropperHandle position="se" />
71
+ </ImageCropperSelection>
72
+ </ImageCropperViewport>
73
+ </ImageCropper>
74
+ </template>
75
+ ```
76
+
77
+ 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 `default-crop`:
78
+
79
+ ```vue
80
+ <ImageCropper
81
+ aria-label="Photo"
82
+ :src="src"
83
+ :default-crop="{ x: 40, y: 40, width: 320, height: 240 }"
84
+ >
85
+ <!-- … -->
86
+ </ImageCropper>
87
+ ```
88
+
89
+ ## Aspect Ratio
90
+
91
+ Set `aspect-ratio` 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. `min-width` and `min-height` stop it getting too small, in image pixels.
92
+
93
+ ```vue title="aspect-ratio.vue"
94
+ <script setup lang="ts">
95
+ import { ref } from "vue"
96
+ import {
97
+ ImageCropper,
98
+ ImageCropperFlip,
99
+ ImageCropperGrid,
100
+ ImageCropperHandle,
101
+ ImageCropperImage,
102
+ ImageCropperSelection,
103
+ ImageCropperViewport,
104
+ } from "@ui-sdk/vue/image-cropper"
105
+
106
+ const photo = `data:image/svg+xml,${encodeURIComponent(
107
+ `<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>`
108
+ )}`
109
+
110
+ const ratios = [
111
+ { label: "Free", value: undefined },
112
+ { label: "Square", value: 1 },
113
+ { label: "Wide", value: 16 / 9 },
114
+ ]
115
+
116
+ const handles = ["nw", "n", "ne", "e", "se", "s", "sw", "w"] as const
117
+
118
+ const aspectRatio = ref<number>()
119
+ </script>
120
+
121
+ <template>
122
+ <div data-slot="stack">
123
+ <div data-slot="controls">
124
+ <button
125
+ v-for="ratio in ratios"
126
+ :key="ratio.label"
127
+ type="button"
128
+ :aria-pressed="aspectRatio === ratio.value"
129
+ @click="aspectRatio = ratio.value"
130
+ >
131
+ {{ ratio.label }}
132
+ </button>
133
+ </div>
134
+ <ImageCropper
135
+ aria-label="Photo"
136
+ :src="photo"
137
+ :aspect-ratio="aspectRatio"
138
+ :min-width="64"
139
+ :min-height="64"
140
+ >
141
+ <ImageCropperViewport>
142
+ <ImageCropperImage alt="Mountains at sunset" />
143
+ <ImageCropperSelection>
144
+ <ImageCropperGrid />
145
+ <ImageCropperHandle
146
+ v-for="position in handles"
147
+ :key="position"
148
+ :position="position"
149
+ />
150
+ </ImageCropperSelection>
151
+ </ImageCropperViewport>
152
+ <div data-slot="row">
153
+ <ImageCropperFlip>Flip</ImageCropperFlip>
154
+ <ImageCropperFlip direction="vertical"
155
+ >Flip Vertically</ImageCropperFlip
156
+ >
157
+ </div>
158
+ </ImageCropper>
159
+ </div>
160
+ </template>
161
+ ```
162
+
163
+ Choose Square or Wide, then drag an edge. Flip mirrors the image and the crop with it.
164
+
165
+ ## Frame Mode
166
+
167
+ 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.
168
+
169
+ `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:
170
+
171
+ ```vue title="avatar.vue"
172
+ <script setup lang="ts">
173
+ import { onBeforeUnmount, ref } from "vue"
174
+ import { cropImage } from "@ui-sdk/vue/image"
175
+ import {
176
+ ImageCropper,
177
+ ImageCropperImage,
178
+ ImageCropperRotate,
179
+ ImageCropperSelection,
180
+ ImageCropperViewport,
181
+ ImageCropperZoom,
182
+ type ImageCrop,
183
+ } from "@ui-sdk/vue/image-cropper"
184
+
185
+ const photo = `data:image/svg+xml,${encodeURIComponent(
186
+ `<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>`
187
+ )}`
188
+
189
+ const crop = ref<ImageCrop>()
190
+ const avatar = ref<string>()
191
+
192
+ async function save() {
193
+ if (!crop.value) {
194
+ return
195
+ }
196
+ const file = await cropImage(photo, crop.value, {
197
+ width: 128,
198
+ shape: "circle",
199
+ })
200
+ if (avatar.value) {
201
+ URL.revokeObjectURL(avatar.value)
202
+ }
203
+ avatar.value = URL.createObjectURL(file)
204
+ }
205
+
206
+ onBeforeUnmount(() => avatar.value && URL.revokeObjectURL(avatar.value))
207
+ </script>
208
+
209
+ <template>
210
+ <ImageCropper
211
+ v-model:crop="crop"
212
+ aria-label="Profile photo"
213
+ :src="photo"
214
+ mode="frame"
215
+ :aspect-ratio="1"
216
+ shape="circle"
217
+ :max-zoom="4"
218
+ >
219
+ <ImageCropperViewport>
220
+ <ImageCropperImage alt="Mountains at sunset" />
221
+ <ImageCropperSelection />
222
+ </ImageCropperViewport>
223
+ <div data-slot="row">
224
+ <ImageCropperZoom />
225
+ <ImageCropperRotate>Rotate</ImageCropperRotate>
226
+ </div>
227
+ <div data-slot="row">
228
+ <button type="button" @click="save">Save Avatar</button>
229
+ <img v-if="avatar" data-slot="image-cropper-avatar" :src="avatar" alt="Your new avatar" />
230
+ </div>
231
+ </ImageCropper>
232
+ </template>
233
+ ```
234
+
235
+ Drag the image or move the zoom, then press Save Avatar. The new avatar shows beside the button.
236
+
237
+ ## Rotate and Flip
238
+
239
+ `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.
240
+
241
+ ## Disabled
242
+
243
+ Set `disabled`. The selection and handles can't be focused or dragged, and the zoom input and buttons are disabled.
244
+
245
+ ```vue
246
+ <ImageCropper aria-label="Photo" :src="src" disabled>
247
+ <!-- … -->
248
+ </ImageCropper>
249
+ ```
250
+
251
+ ## Controlled
252
+
253
+ Bind `v-model:crop`, and listen to `@crop-change` for the details. Each change carries a [reason](#reasons), like `drag`, `step-key` or `zoom-input`. Call `details.cancel()` in the listener to keep the current crop.
254
+
255
+ `@crop-change` fires on every step of a drag and once the image loads. `@crop-commit` fires once, when the drag, key press or wheel ends, so use it to save. A controller from `createImageCropperController()` sets the crop from anywhere:
256
+
257
+ ```vue
258
+ <script setup lang="ts">
259
+ import {
260
+ createImageCropperController,
261
+ ImageCropper,
262
+ ImageCropperImage,
263
+ ImageCropperSelection,
264
+ ImageCropperViewport,
265
+ } from "@ui-sdk/vue/image-cropper"
266
+
267
+ defineProps<{ src: string }>()
268
+
269
+ const controller = createImageCropperController()
270
+ </script>
271
+
272
+ <template>
273
+ <ImageCropper
274
+ aria-label="Photo"
275
+ :src="src"
276
+ :controller="controller"
277
+ @crop-commit="(crop, details) => console.log(crop, details.reason)"
278
+ >
279
+ <ImageCropperViewport>
280
+ <ImageCropperImage alt="Your photo" />
281
+ <ImageCropperSelection />
282
+ </ImageCropperViewport>
283
+ </ImageCropper>
284
+ <button type="button" @click="controller.setZoom(2)">Zoom In</button>
285
+ </template>
286
+ ```
287
+
288
+ See [State and Events](../state-and-events.md) for controlled state, cancellation and debugging.
289
+
290
+ ## Custom Rendering
291
+
292
+ Every part takes `render`, to render a different element or component in its place, and its default slot gets its `state`. Every part has the `crop`, the `zoom`, and whether it's `loading`, has an `error` or is `dragging`:
293
+
294
+ ```vue
295
+ <ImageCropper v-slot="{ state }" aria-label="Photo" :src="src">
296
+ <ImageCropperViewport>
297
+ <ImageCropperImage alt="Your photo" />
298
+ <ImageCropperSelection />
299
+ </ImageCropperViewport>
300
+ <p v-if="state.loading">Loading…</p>
301
+ <p v-if="state.error">The image didn't load.</p>
302
+ <output>{{ Math.round(state.zoom * 100) }}%</output>
303
+ </ImageCropper>
304
+ ```
305
+
306
+ See [Parts and Composition](../parts-and-composition.md) for the rendering contract and [State](#state) for each part's fields.
307
+
308
+ ## Styling
309
+
310
+ 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.
311
+
312
+ 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.
313
+
314
+ Each part has a `data-slot`, such as `image-cropper-selection`, and its state attributes are listed under [Data Attributes](#data-attributes).
315
+
316
+ ### ImageCropperViewport
317
+ - `--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.
318
+
319
+ ## Data Attributes
320
+
321
+ ### ImageCropper
322
+ `[data-slot="image-cropper"]`
323
+
324
+ - `data-mode` (`box`, `frame`): How the crop changes.
325
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
326
+ - `data-loading`: Present until the image loads, while nothing can change.
327
+ - `data-disabled`: Present when the cropper is disabled.
328
+ - `data-error`: Present when the image failed to load.
329
+ - `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
330
+
331
+ ### ImageCropperViewport
332
+ `[data-slot="image-cropper-viewport"]`
333
+
334
+ - `data-mode` (`box`, `frame`): How the crop changes.
335
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
336
+ - `data-loading`: Present until the image loads, while nothing can change.
337
+ - `data-disabled`: Present when the cropper is disabled.
338
+ - `data-error`: Present when the image failed to load.
339
+ - `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
340
+
341
+ ### ImageCropperImage
342
+ `[data-slot="image-cropper-image"]`
343
+
344
+ - `data-mode` (`box`, `frame`): How the crop changes.
345
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
346
+ - `data-loading`: Present until the image loads, while nothing can change.
347
+ - `data-disabled`: Present when the cropper is disabled.
348
+ - `data-error`: Present when the image failed to load.
349
+
350
+ ### ImageCropperSelection
351
+ `[data-slot="image-cropper-selection"]`
352
+
353
+ - `data-mode` (`box`, `frame`): How the crop changes.
354
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
355
+ - `data-loading`: Present until the image loads, while nothing can change.
356
+ - `data-disabled`: Present when the cropper is disabled.
357
+ - `data-error`: Present when the image failed to load.
358
+ - `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
359
+
360
+ ### ImageCropperGrid
361
+ `[data-slot="image-cropper-grid"]`
362
+
363
+ - `data-mode` (`box`, `frame`): How the crop changes.
364
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
365
+ - `data-loading`: Present until the image loads, while nothing can change.
366
+ - `data-disabled`: Present when the cropper is disabled.
367
+ - `data-error`: Present when the image failed to load.
368
+ - `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
369
+
370
+ ### ImageCropperHandle
371
+ `[data-slot="image-cropper-handle"]`
372
+
373
+ - `data-position` (`n`, `e`, `s`, `w`, `ne`, `nw`, `se`, `sw`): The edge or corner the handle resizes.
374
+ - `data-mode` (`box`, `frame`): How the crop changes.
375
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
376
+ - `data-loading`: Present until the image loads, while nothing can change.
377
+ - `data-disabled`: Present when the cropper is disabled.
378
+ - `data-error`: Present when the image failed to load.
379
+ - `data-dragging`: Present while a pointer drags the selection, the image or, on a handle, that handle. Show the grid with it.
380
+
381
+ ### ImageCropperZoom
382
+ `[data-slot="image-cropper-zoom"]`
383
+
384
+ - `data-mode` (`box`, `frame`): How the crop changes.
385
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
386
+ - `data-loading`: Present until the image loads, while nothing can change.
387
+ - `data-disabled`: Present when the cropper is disabled.
388
+ - `data-error`: Present when the image failed to load.
389
+
390
+ ### ImageCropperRotate
391
+ `[data-slot="image-cropper-rotate"]`
392
+
393
+ - `data-mode` (`box`, `frame`): How the crop changes.
394
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
395
+ - `data-loading`: Present until the image loads, while nothing can change.
396
+ - `data-disabled`: Present when the cropper is disabled.
397
+ - `data-error`: Present when the image failed to load.
398
+
399
+ ### ImageCropperFlip
400
+ `[data-slot="image-cropper-flip"]`
401
+
402
+ - `data-direction` (`horizontal`, `vertical`): Which way the button mirrors the image.
403
+ - `data-mode` (`box`, `frame`): How the crop changes.
404
+ - `data-shape` (`rectangle`, `circle`): The selection's shape.
405
+ - `data-loading`: Present until the image loads, while nothing can change.
406
+ - `data-disabled`: Present when the cropper is disabled.
407
+ - `data-error`: Present when the image failed to load.
408
+
409
+ ## API Reference
410
+
411
+ Props are the same as below. `onCropChange` is `v-model:crop`, or `@crop-change` for the details, and `onCropCommit` is `@crop-commit`.
412
+
413
+ ### ImageCropper
414
+
415
+ - `src?` (`string`): The image's URL, like an object URL for a file someone picked.
416
+ - `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.
417
+ - `defaultCrop?` (`ImageCrop`): The crop at first, when uncontrolled.
418
+ - `@crop-change` (`((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.
419
+ - `@crop-commit` (`((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.
420
+ - `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.
421
+ - `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.
422
+ - `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.
423
+ - `minWidth?` (`number`): The narrowest the crop gets, in image pixels.
424
+ - `minHeight?` (`number`): The shortest the crop gets, in image pixels.
425
+ - `maxZoom?` (`number`): How far the crop magnifies the image, where 1 is the largest crop the image allows.
426
+ - `disabled?` (`boolean`): Stops every change: the selection can't be focused, and the buttons and zoom are disabled.
427
+ - `controller?` (`object`): Sets the crop from anywhere. Create one with `createImageCropperController()`.
428
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
429
+
430
+ ### ImageCropperHandle
431
+
432
+ - `position` (`ImageCropperHandlePosition`): The edge or corner it resizes, like `"nw"` for the top left corner.
433
+
434
+ ### ImageCropperFlip
435
+
436
+ - `direction?` (`ImageCropperFlipDirection`): Which way it mirrors the image.
437
+
438
+ ### State
439
+
440
+ What render functions, state-based class names and slots receive. Every part gets the cropper's state.
441
+
442
+ - `crop` (`Required<ImageCrop> | null`): The crop, or null until the image loads.
443
+ - `zoom` (`number`): How far the crop magnifies: 1 when it's as large as the image allows at its ratio.
444
+ - `loading` (`boolean`): Whether the image is still loading, so nothing can change yet.
445
+ - `error` (`boolean`): Whether the image failed to load.
446
+ - `dragging` (`boolean`): Whether a pointer is dragging the selection, a handle or the image.
447
+ - `mode` (`ImageCropperMode`): How the crop changes.
448
+ - `shape` (`ImageCropperShape`): The selection's shape.
449
+ - `disabled` (`boolean`): Whether the `disabled` option stops every change.
450
+
451
+ Handles add their own:
452
+
453
+ - `crop` (`Required<ImageCrop> | null`): The crop, or null until the image loads.
454
+ - `zoom` (`number`): How far the crop magnifies: 1 when it's as large as the image allows at its ratio.
455
+ - `loading` (`boolean`): Whether the image is still loading, so nothing can change yet.
456
+ - `error` (`boolean`): Whether the image failed to load.
457
+ - `dragging` (`boolean`): Whether a pointer is dragging the selection, a handle or the image.
458
+ - `mode` (`ImageCropperMode`): How the crop changes.
459
+ - `shape` (`ImageCropperShape`): The selection's shape.
460
+ - `disabled` (`boolean`): Whether the `disabled` option stops every change.
461
+ - `position` (`ImageCropperHandlePosition`): The edge or corner it resizes.
462
+
463
+ ### Reasons
464
+
465
+ `details.reason` says why the crop changed.
466
+
467
+ - `image-load` (`Event | undefined`): An image loaded. No event when it was already in the browser's cache.
468
+ - `drag` (`PointerEvent`): A slider thumb or a resize handle was dragged, or an item was dragged to a new place in a list.
469
+ - `pinch`
470
+ - `wheel` (`WheelEvent`): The mouse wheel stepped a focused number input.
471
+ - `step-key` (`KeyboardEvent`): Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
472
+ - `zoom-key`
473
+ - `zoom-input`
474
+ - `rotate-key`
475
+ - `rotate-press`
476
+ - `flip-press`
477
+ - `aspect-ratio-change`
478
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
479
+
480
+ ### Controller
481
+
482
+ Create one with `createImageCropperController()` and pass it as `controller`.
483
+
484
+ Each write method takes an optional `{ reason, event }`, fires the change event like a drag, and returns `false` when nothing changed or the change is canceled.
485
+
486
+ - `getCrop()` (`() => Required<ImageCrop> | null`): The crop, or null until the image loads.
487
+ - `setCrop(crop, options?)` (`(crop: ImageCrop, options?: ImageCropperChangeOptions | undefined) => boolean`): Sets the crop, as a user would. Returns whether it changed.
488
+ - `rotate(direction?, options?)` (`(direction?: 1 | -1 | undefined, options?: ImageCropperChangeOptions | undefined) => boolean`): Turns the image a quarter clockwise, or counterclockwise with `-1`.
489
+ - `flip(direction?, options?)` (`(direction?: ImageCropperFlipDirection | undefined, options?: ImageCropperChangeOptions | undefined) => boolean`): Mirrors the image left to right, or top to bottom with `"vertical"`.
490
+ - `getZoom()` (`() => number`): How far the crop magnifies: 1 when it's as large as the image allows at its ratio.
491
+ - `setZoom(zoom, options?)` (`(zoom: number, options?: ImageCropperChangeOptions | undefined) => boolean`): Zooms to a level, about the crop's middle, within 1 and `maxZoom`.
492
+ - `focus()` (`() => void`): Moves focus to the selection.
493
+
494
+ ### Events
495
+
496
+ Events get the new crop first, then the details. `v-model` only updates when the change event wasn't canceled.
497
+
498
+ | Binding | On | Receives |
499
+ | -------------- | -------------- | ----------------------------------------------------------------- |
500
+ | `v-model:crop` | `ImageCropper` | The crop |
501
+ | `@crop-change` | `ImageCropper` | `(crop, details)`: the new crop and the [reason](#reasons) |
502
+ | `@crop-commit` | `ImageCropper` | `(crop, details)`: the crop once the interaction ends, its reason |
503
+
504
+ ## Accessibility
505
+
506
+ 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%".
507
+
508
+ The selection, handles and zoom are named in English by default. Pass `aria-label` to name them in another language.
509
+
510
+ ### Keyboard
511
+
512
+ | Key | Does |
513
+ | ------------------- | -------------------------------------------------------- |
514
+ | Arrow keys | Move the selection, or on a handle, move its edges |
515
+ | Shift with an arrow | Moves ten times as far |
516
+ | Plus or Minus | Zooms in or out, about the crop's middle |
517
+ | R | Turns the image a quarter clockwise, or back with Shift |
518
+ | Escape | During a drag, puts the crop back where the drag started |
519
+
520
+ Each key press commits, like the end of a drag.
521
+
522
+ ### Tested Behavior
523
+
524
+ Every line below is an automated test.
525
+
526
+ **Loading**
527
+
528
+ - The crop starts once the image loads, as four fifths of the largest at the ratio.
529
+
530
+ **Box**
531
+
532
+ - A drag moves the selection over the image and commits once.
533
+ - A handle resizes from its corner, within the image.
534
+ - At a ratio, a handle keeps it.
535
+ - Escape during a drag puts the selection back.
536
+
537
+ **Keyboard**
538
+
539
+ - Arrows move the selection, ten times as far with Shift.
540
+ - Arrows on a handle move its edges, and the next Tab reaches it.
541
+ - Plus and minus zoom about the middle, and R turns the image.
542
+
543
+ **Controls**
544
+
545
+ - The zoom input zooms the crop.
546
+ - Rotate turns the image a quarter clockwise, and flip mirrors it.
547
+ - A controller sets the crop, and a canceled change keeps it.
548
+ - Disabled, nothing takes focus or changes.
549
+
550
+ **Frame**
551
+
552
+ - The selection stays put and a drag pans the image under it.
553
+ - Escape during a pan puts the image back.
554
+ - The wheel zooms at the pointer and commits after a pause.
555
+
556
+ Runs in React, Vue and web components.