Skip to content

Commit c203ed4

Browse files
committed
feat(mosaic): crop avatars and logos before upload
1 parent 6b52ec4 commit c203ed4

16 files changed

Lines changed: 501 additions & 24 deletions

‎.changeset/mosaic-avatar-crop.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/mosaic': minor
3+
---
4+
5+
Picking a profile picture in `UserProfile`, or a logo in `OrganizationProfile` or `CreateOrganization`, now opens a dialog to crop it before it uploads. Users can drag, zoom, rotate a quarter turn and reset, then save a 512px square PNG. Cancelling uploads nothing. GIFs upload as picked, so their animation is kept.

‎packages/mosaic/AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Most problems a feature hits already have a shared answer. Find the line for you
8585
- An inline message row; height is undefined until measured → `useHeldMessage`, `useMessageHeight`, `FeedbackBody` (`src/utils/feedback.tsx`) + `src/styles/feedback.styles.ts`
8686
- Data read from a Clerk resource that should be cached across mounts → A query hook in `src/queries/`, called from the model (`src/queries/use-user-sessions-query.ts`)
8787
- The seconds left on a resend cooldown, for display → `useResendSeconds` (`src/hooks/`), from a `resendAvailableAt` time kept in the machine
88-
- An avatar or logo row: picker, drop target, Upload button or manage menu → `AvatarUpload` (`src/blocks/avatar-upload/`), laid out by the caller
88+
- An avatar or logo row: picker, drop target, crop dialog, Upload button or manage menu → `AvatarUpload` (`src/blocks/avatar-upload/`), laid out by the caller
8989
- Any other file picker or drop zone, such as a metadata or key file → `FileUpload` (`src/primitives/file-upload/`), see its `README.md`
9090
- Cropping an image to a square: pan, zoom, rotate, then draw the result → `ImageCropper` and `cropImage` (`src/primitives/image-cropper/`), see its `README.md`
9191
- An inline notice inside content: info, warning, error or success → `Banner` (`src/components/banner/`); a form-level error on a card uses `Card.Banner`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { screen, waitFor, within } from '@testing-library/react';
2+
import type { UserEvent } from '@testing-library/user-event';
3+
import { expect } from 'vitest';
4+
5+
export async function imageFile(name: string, { width = 200, height = 100, type = 'image/png' } = {}): Promise<File> {
6+
const canvas = new OffscreenCanvas(width, height);
7+
const context = canvas.getContext('2d');
8+
if (!context) {
9+
throw new Error('expected a 2D canvas');
10+
}
11+
context.fillStyle = 'rgb(255 0 0)';
12+
context.fillRect(0, 0, width, height);
13+
const blob = await canvas.convertToBlob({ type });
14+
return new File([blob], name, { type });
15+
}
16+
17+
export async function saveCrop(actor: UserEvent, title: string): Promise<void> {
18+
const dialog = await screen.findByRole('dialog', { name: title });
19+
await waitFor(() => expect(within(dialog).getByRole('button', { name: 'Rotate' })).toBeEnabled());
20+
await actor.click(within(dialog).getByRole('button', { name: 'Save' }));
21+
await waitFor(() => expect(dialog).not.toBeInTheDocument());
22+
}

‎packages/mosaic/src/blocks/avatar-upload/avatar-upload.styles.ts‎

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import * as stylex from '@stylexjs/stylex';
22

3-
import { durationVars, easingVars, space } from '../../tokens.stylex';
3+
import { colorVars, durationVars, easingVars, radiusVars, shadowVars, space } from '../../tokens.stylex';
44
import { avatarUploadDropzoneScope } from './avatar-upload.markers.stylex';
55

6+
const SCRIM = 'color-mix(in oklab, oklch(0 0 0) 55%, transparent)';
7+
const RING = 'oklch(1 0 0 / 0.85)';
8+
const THUMB = '16px';
9+
const TRACK = '4px';
10+
611
export const styles = stylex.create({
712
dropzone: {
813
alignItems: 'center',
@@ -45,4 +50,105 @@ export const styles = stylex.create({
4550
[stylex.when.ancestor('[data-drag-rejected]', avatarUploadDropzoneScope)]: 'inline-block',
4651
},
4752
},
53+
cropContent: {
54+
display: 'flex',
55+
flexDirection: 'column',
56+
rowGap: space['4'],
57+
},
58+
stage: {
59+
position: 'relative',
60+
},
61+
frame: {
62+
borderRadius: radiusVars['--cl-radius-lg'],
63+
overflow: 'hidden',
64+
aspectRatio: '1',
65+
backgroundColor: colorVars['--cl-color-surface-subtle'],
66+
cursor: {
67+
default: 'grab',
68+
':where([data-disabled])': 'default',
69+
':where([data-dragging])': 'grabbing',
70+
},
71+
position: 'relative',
72+
userSelect: 'none',
73+
width: '100%',
74+
},
75+
area: {
76+
inset: '7%',
77+
position: 'absolute',
78+
},
79+
mask: {
80+
inset: 0,
81+
boxShadow: `0 0 0 100vmax ${SCRIM}, inset 0 0 0 2px ${RING}`,
82+
pointerEvents: 'none',
83+
position: 'absolute',
84+
},
85+
circleMask: {
86+
borderRadius: '50%',
87+
},
88+
rotate: {
89+
insetBlockStart: space['2'],
90+
insetInlineEnd: space['2'],
91+
position: 'absolute',
92+
},
93+
clockwise: {
94+
transform: 'scaleX(-1)',
95+
},
96+
zoomRow: {
97+
alignItems: 'center',
98+
columnGap: space['2'],
99+
display: 'flex',
100+
},
101+
zoom: {
102+
margin: 0,
103+
borderRadius: radiusVars['--cl-radius-full'],
104+
appearance: 'none',
105+
backgroundColor: 'transparent',
106+
cursor: {
107+
default: 'pointer',
108+
':disabled': 'default',
109+
},
110+
flexGrow: 1,
111+
opacity: {
112+
default: 1,
113+
':disabled': 0.5,
114+
},
115+
height: THUMB,
116+
minWidth: 0,
117+
'::-moz-range-thumb': {
118+
borderColor: colorVars['--cl-color-border'],
119+
borderRadius: radiusVars['--cl-radius-full'],
120+
borderStyle: 'solid',
121+
borderWidth: '1px',
122+
backgroundColor: 'oklch(1 0 0)',
123+
boxShadow: shadowVars['--cl-shadow-sm'],
124+
height: THUMB,
125+
width: THUMB,
126+
},
127+
'::-moz-range-track': {
128+
borderRadius: radiusVars['--cl-radius-full'],
129+
backgroundColor: colorVars['--cl-color-neutral-alpha-300'],
130+
height: TRACK,
131+
},
132+
'::-webkit-slider-runnable-track': {
133+
borderRadius: radiusVars['--cl-radius-full'],
134+
backgroundColor: colorVars['--cl-color-neutral-alpha-300'],
135+
height: TRACK,
136+
},
137+
'::-webkit-slider-thumb': {
138+
borderColor: colorVars['--cl-color-border'],
139+
borderRadius: radiusVars['--cl-radius-full'],
140+
borderStyle: 'solid',
141+
borderWidth: '1px',
142+
appearance: 'none',
143+
backgroundColor: 'oklch(1 0 0)',
144+
boxShadow: shadowVars['--cl-shadow-sm'],
145+
height: THUMB,
146+
marginTop: `calc((${TRACK} - ${THUMB}) / 2)`,
147+
width: THUMB,
148+
},
149+
},
150+
footerActions: {
151+
columnGap: space['2'],
152+
display: 'flex',
153+
},
48154
});

0 commit comments

Comments
 (0)