@getmicdrop/svelte-components 5.6.1 → 5.7.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/dist/calendar/AboutShow/AboutShow.svelte +172 -172
- package/dist/calendar/Calendar/MiniMonthCalendar.svelte +782 -782
- package/dist/calendar/FAQs/FAQs.svelte +75 -75
- package/dist/calendar/MonthSwitcher/MonthSwitcher.svelte +126 -126
- package/dist/calendar/OrderSummary/OrderSummary.svelte +367 -367
- package/dist/calendar/PublicCard/PublicCard.svelte +134 -134
- package/dist/calendar/ShowCard/ShowCard.svelte +157 -157
- package/dist/calendar/ShowTimeCard/ShowTimeCard.svelte +61 -61
- package/dist/components/Heading.svelte +57 -0
- package/dist/components/Heading.svelte.d.ts +12 -0
- package/dist/components/Heading.svelte.d.ts.map +1 -0
- package/dist/components/Layout/AppShell.svelte +104 -104
- package/dist/components/Layout/ContentSection.svelte +80 -80
- package/dist/components/Layout/Grid.svelte +4 -4
- package/dist/components/Layout/Heading.svelte +81 -81
- package/dist/components/Layout/PageContainer.svelte +69 -69
- package/dist/components/Layout/Responsive.svelte +75 -75
- package/dist/components/Layout/Section.svelte +80 -80
- package/dist/components/Layout/ShowOnDesktop.svelte +37 -37
- package/dist/components/Layout/ShowOnMobile.svelte +37 -37
- package/dist/components/Layout/Sidebar.svelte +108 -108
- package/dist/components/Layout/Stack.spec.js +1 -1
- package/dist/components/Layout/Stack.svelte +6 -6
- package/dist/components/Layout/Text.svelte +87 -87
- package/dist/components/Layout/TwoColumn.svelte +108 -108
- package/dist/components/Layout/index.d.ts +11 -0
- package/dist/components/Layout/index.d.ts.map +1 -0
- package/dist/components/Layout/index.js +14 -0
- package/dist/components/Text.svelte +40 -0
- package/dist/components/Text.svelte.d.ts +11 -0
- package/dist/components/Text.svelte.d.ts.map +1 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +12 -0
- package/dist/constants/validation.js +91 -91
- package/dist/constants/validation.spec.js +64 -64
- package/dist/datetime/__tests__/format.test.js +1 -1
- package/dist/datetime/__tests__/parse.test.js +1 -1
- package/dist/datetime/__tests__/timezone.test.js +1 -1
- package/dist/datetime/parse.js +1 -1
- package/dist/forms/createFormStore.svelte.js +0 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +10 -1
- package/dist/patterns/data/DataGrid.svelte +45 -45
- package/dist/patterns/data/DataList.svelte +24 -24
- package/dist/patterns/data/DataTable.svelte +36 -36
- package/dist/patterns/forms/FormActions.spec.js +95 -95
- package/dist/patterns/forms/FormActions.stories.svelte +97 -97
- package/dist/patterns/forms/FormActions.svelte +46 -46
- package/dist/patterns/forms/FormGrid.svelte +33 -33
- package/dist/patterns/forms/FormSection.svelte +32 -32
- package/dist/patterns/forms/FormValidationSummary.stories.svelte +83 -83
- package/dist/patterns/forms/FormValidationSummary.svelte +74 -74
- package/dist/patterns/layout/Sidebar.svelte +39 -39
- package/dist/patterns/navigation/BottomNav.stories.svelte +117 -117
- package/dist/patterns/navigation/BottomNav.svelte +74 -74
- package/dist/patterns/navigation/Header.stories.svelte +77 -77
- package/dist/patterns/navigation/Header.svelte +193 -193
- package/dist/patterns/page/PageHeader.svelte +18 -18
- package/dist/patterns/page/PageLayout.svelte +40 -40
- package/dist/patterns/page/PageLoader.spec.js +57 -57
- package/dist/patterns/page/PageLoader.stories.svelte +137 -137
- package/dist/patterns/page/PageLoader.svelte +24 -24
- package/dist/patterns/page/SectionHeader.svelte +29 -29
- package/dist/presets/badges.js +112 -112
- package/dist/presets/buttons.js +76 -76
- package/dist/presets/index.js +9 -9
- package/dist/primitives/Accordion/Accordion.stories.svelte +75 -75
- package/dist/primitives/Accordion/Accordion.svelte +42 -42
- package/dist/primitives/Accordion/AccordionItem.svelte +95 -95
- package/dist/primitives/Alert/Alert.spec.js +173 -173
- package/dist/primitives/Alert/Alert.stories.svelte +88 -88
- package/dist/primitives/Alert/Alert.svelte +27 -27
- package/dist/primitives/Avatar/Avatar.stories.svelte +94 -94
- package/dist/primitives/Avatar/Avatar.svelte +66 -66
- package/dist/primitives/Badges/Badge.spec.js +144 -144
- package/dist/primitives/Badges/Badge.stories.svelte +86 -86
- package/dist/primitives/Badges/Badge.svelte +79 -79
- package/dist/primitives/BottomSheet/BottomSheet.spec.js +136 -136
- package/dist/primitives/BottomSheet/BottomSheet.stories.svelte +83 -83
- package/dist/primitives/BottomSheet/BottomSheet.svelte +100 -100
- package/dist/primitives/Breadcrumb/Breadcrumb.spec.js +122 -122
- package/dist/primitives/Breadcrumb/Breadcrumb.stories.svelte +23 -23
- package/dist/primitives/Breadcrumb/Breadcrumb.svelte +89 -89
- package/dist/primitives/Button/Button.spec.js +223 -223
- package/dist/primitives/Button/Button.stories.svelte +76 -76
- package/dist/primitives/Button/Button.svelte +270 -270
- package/dist/primitives/Button/ButtonSaveDemo.spec.js +146 -146
- package/dist/primitives/Button/ButtonSaveDemo.svelte +25 -25
- package/dist/primitives/Button/ButtonVariantShowcase.svelte +129 -129
- package/dist/primitives/Card.spec.js +49 -49
- package/dist/primitives/Card.stories.svelte +22 -22
- package/dist/primitives/Card.svelte +28 -28
- package/dist/primitives/Checkbox/Checkbox.stories.svelte +84 -84
- package/dist/primitives/Checkbox/Checkbox.svelte +88 -88
- package/dist/primitives/DarkModeToggle.spec.js +390 -390
- package/dist/primitives/DarkModeToggle.stories.svelte +57 -57
- package/dist/primitives/DarkModeToggle.svelte +136 -136
- package/dist/primitives/Drawer/Drawer.stories.svelte +80 -80
- package/dist/primitives/Drawer/Drawer.svelte +120 -120
- package/dist/primitives/Dropdown/Dropdown.stories.svelte +137 -137
- package/dist/primitives/Dropdown/Dropdown.svelte +14 -14
- package/dist/primitives/Dropdown/DropdownDivider.svelte +9 -9
- package/dist/primitives/Dropdown/DropdownItem.svelte +80 -80
- package/dist/primitives/Helper/Helper.svelte +33 -33
- package/dist/primitives/Icons/ArrowLeft.svelte +8 -8
- package/dist/primitives/Icons/ArrowRight.svelte +8 -8
- package/dist/primitives/Icons/Availability.svelte +14 -14
- package/dist/primitives/Icons/Back.svelte +14 -14
- package/dist/primitives/Icons/CheckCircle.svelte +6 -6
- package/dist/primitives/Icons/CheckCircleOutline.svelte +15 -15
- package/dist/primitives/Icons/ChevronLeft.svelte +4 -4
- package/dist/primitives/Icons/ChevronRight.svelte +4 -4
- package/dist/primitives/Icons/Copy.svelte +15 -15
- package/dist/primitives/Icons/Cross.svelte +5 -5
- package/dist/primitives/Icons/DownArrow.svelte +8 -8
- package/dist/primitives/Icons/ErrorCircle.svelte +6 -6
- package/dist/primitives/Icons/FacebookIcon.svelte +2 -2
- package/dist/primitives/Icons/Home.svelte +15 -15
- package/dist/primitives/Icons/Icon.spec.js +169 -169
- package/dist/primitives/Icons/Icon.stories.svelte +100 -100
- package/dist/primitives/Icons/Icon.svelte +52 -52
- package/dist/primitives/Icons/IconGallery.stories.svelte +235 -235
- package/dist/primitives/Icons/Info.svelte +7 -7
- package/dist/primitives/Icons/InstagramIcon.svelte +4 -4
- package/dist/primitives/Icons/LogoInstagram.svelte +2 -2
- package/dist/primitives/Icons/Message.svelte +15 -15
- package/dist/primitives/Icons/MoonIcon.svelte +5 -5
- package/dist/primitives/Icons/More.svelte +21 -21
- package/dist/primitives/Icons/MoreHori.spec.js +61 -61
- package/dist/primitives/Icons/MoreHori.svelte +22 -22
- package/dist/primitives/Icons/Notification.svelte +14 -14
- package/dist/primitives/Icons/Payment.svelte +14 -14
- package/dist/primitives/Icons/Profile.svelte +21 -21
- package/dist/primitives/Icons/Reload.svelte +29 -29
- package/dist/primitives/Icons/Shows.svelte +21 -21
- package/dist/primitives/Icons/Signout.svelte +21 -21
- package/dist/primitives/Icons/SunIcon.svelte +8 -8
- package/dist/primitives/Icons/TiktokIcon.svelte +2 -2
- package/dist/primitives/Icons/TwitterIcon.svelte +2 -2
- package/dist/primitives/Icons/WarningIcon.spec.js +18 -18
- package/dist/primitives/Icons/WarningIcon.svelte +5 -5
- package/dist/primitives/Input/Input.spec.js +573 -573
- package/dist/primitives/Input/Input.stories.svelte +139 -139
- package/dist/primitives/Input/Select.spec.js +212 -212
- package/dist/primitives/Input/Select.stories.svelte +112 -112
- package/dist/primitives/Input/Select.svelte +128 -128
- package/dist/primitives/Input/Textarea.stories.svelte +137 -137
- package/dist/primitives/Input/Textarea.svelte +35 -35
- package/dist/primitives/Label/Label.svelte +37 -37
- package/dist/primitives/Modal/Modal.spec.js +99 -99
- package/dist/primitives/Modal/Modal.stories.svelte +86 -86
- package/dist/primitives/Pagination/Pagination.stories.svelte +76 -76
- package/dist/primitives/Pagination/Pagination.svelte +261 -261
- package/dist/primitives/Radio/Radio.stories.svelte +80 -80
- package/dist/primitives/Radio/Radio.svelte +67 -67
- package/dist/primitives/Skeleton/CardPlaceholder.svelte +87 -87
- package/dist/primitives/Skeleton/ImagePlaceholder.svelte +59 -59
- package/dist/primitives/Skeleton/ListPlaceholder.svelte +76 -76
- package/dist/primitives/Skeleton/Skeleton.stories.svelte +151 -151
- package/dist/primitives/Skeleton/Skeleton.svelte +26 -26
- package/dist/primitives/Spinner/Spinner.spec.js +71 -71
- package/dist/primitives/Spinner/Spinner.stories.svelte +29 -29
- package/dist/primitives/Spinner/Spinner.svelte +20 -20
- package/dist/primitives/Tabs/TabItem.svelte +49 -49
- package/dist/primitives/Tabs/Tabs.stories.svelte +112 -112
- package/dist/primitives/Tabs/Tabs.svelte +123 -123
- package/dist/primitives/Toggle.spec.js +143 -143
- package/dist/primitives/Toggle.stories.svelte +92 -92
- package/dist/primitives/Tooltip/Tooltip.svelte +83 -83
- package/dist/primitives/Typography/Typography.svelte +53 -53
- package/dist/primitives/ValidationError.spec.js +103 -103
- package/dist/primitives/ValidationError.stories.svelte +69 -69
- package/dist/primitives/ValidationError.svelte +29 -29
- package/dist/primitives/index.js +91 -91
- package/dist/recipes/CropImage/CropImage.spec.js +208 -208
- package/dist/recipes/CropImage/CropImage.stories.svelte +104 -104
- package/dist/recipes/CropImage/CropImage.svelte +238 -238
- package/dist/recipes/ImageUploader/ImageUploader.stories.svelte +125 -125
- package/dist/recipes/ImageUploader/ImageUploader.svelte +804 -804
- package/dist/recipes/Toaster/Toaster.stories.svelte +62 -62
- package/dist/recipes/feedback/EmptyState/EmptyState.svelte +1 -1
- package/dist/recipes/feedback/ErrorDisplay.spec.js +69 -69
- package/dist/recipes/feedback/ErrorDisplay.stories.svelte +101 -101
- package/dist/recipes/feedback/ErrorDisplay.svelte +1 -1
- package/dist/recipes/feedback/StatusIndicator/StatusIndicator.spec.js +133 -133
- package/dist/recipes/feedback/StatusIndicator/StatusIndicator.svelte +157 -157
- package/dist/recipes/fields/CheckboxField.svelte +85 -85
- package/dist/recipes/fields/FormField.svelte +58 -58
- package/dist/recipes/fields/RadioGroup.svelte +95 -95
- package/dist/recipes/fields/SelectField.svelte +80 -80
- package/dist/recipes/fields/TextareaField.svelte +97 -97
- package/dist/recipes/fields/ToggleField.svelte +60 -60
- package/dist/recipes/fields/index.js +7 -7
- package/dist/recipes/inputs/MultiSelect.spec.js +258 -258
- package/dist/recipes/inputs/MultiSelect.stories.svelte +133 -133
- package/dist/recipes/inputs/OTPInput.spec.js +251 -251
- package/dist/recipes/inputs/OTPInput.stories.svelte +162 -162
- package/dist/recipes/inputs/OTPInput.svelte +29 -29
- package/dist/recipes/inputs/PasswordInput.svelte +22 -22
- package/dist/recipes/inputs/PasswordStrengthIndicator/PasswordStrengthIndicator.svelte +117 -117
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.stories.svelte +123 -123
- package/dist/recipes/inputs/Search.svelte +37 -37
- package/dist/recipes/inputs/SelectDropdown.svelte +57 -57
- package/dist/recipes/modals/AlertModal.svelte +130 -130
- package/dist/recipes/modals/ConfirmationModal.spec.js +206 -206
- package/dist/recipes/modals/ConfirmationModal.stories.svelte +119 -119
- package/dist/recipes/modals/ConfirmationModal.svelte +152 -152
- package/dist/recipes/modals/InputModal.svelte +182 -182
- package/dist/recipes/modals/ModalStateManager.spec.js +100 -100
- package/dist/recipes/modals/ModalStateManager.svelte +77 -77
- package/dist/recipes/modals/ModalTestWrapper.svelte +65 -65
- package/dist/recipes/modals/StatusModal.svelte +206 -206
- package/dist/services/EventService.js +75 -75
- package/dist/services/EventService.spec.js +217 -217
- package/dist/services/ShowService.spec.js +345 -345
- package/dist/stores/auth.js +36 -44
- package/dist/stores/auth.spec.js +139 -139
- package/dist/stores/formDataStore.d.ts.map +1 -1
- package/dist/stores/formDataStore.js +0 -8
- package/dist/stores/formSave.d.ts.map +1 -1
- package/dist/stores/formSave.js +0 -8
- package/dist/stores/navigation.d.ts.map +1 -1
- package/dist/stores/navigation.js +0 -8
- package/dist/stores/toaster.js +13 -13
- package/dist/stories/ButtonAuditReview.stories.svelte +14 -14
- package/dist/stories/ButtonAuditReview.svelte +427 -427
- package/dist/stories/PatternsGallery.stories.svelte +19 -19
- package/dist/stories/PatternsGallery.svelte +206 -206
- package/dist/stories/PrimitivesGallery.stories.svelte +19 -19
- package/dist/stories/PrimitivesGallery.svelte +725 -725
- package/dist/stories/RecipesGallery.stories.svelte +19 -19
- package/dist/stories/RecipesGallery.svelte +271 -271
- package/dist/stories/button-audit-manifest.json +11186 -11186
- package/dist/tailwind/preset.cjs +82 -82
- package/dist/telemetry.js +405 -405
- package/dist/telemetry.spec.js +1169 -1169
- package/dist/tokens/tokens.css +87 -87
- package/dist/tokens/typography-base.css +163 -163
- package/dist/utils/apiConfig.spec.js +219 -219
- package/dist/utils/transitions.js +62 -62
- package/dist/utils/utils.js +354 -354
- package/package.json +1 -1
- package/dist/stores/auth.svelte.d.ts +0 -39
- package/dist/stores/auth.svelte.d.ts.map +0 -1
- package/dist/stores/auth.svelte.js +0 -60
- package/dist/stores/formDataStore.svelte.d.ts +0 -47
- package/dist/stores/formDataStore.svelte.d.ts.map +0 -1
- package/dist/stores/formDataStore.svelte.js +0 -84
- package/dist/stores/formSave.svelte.d.ts +0 -33
- package/dist/stores/formSave.svelte.d.ts.map +0 -1
- package/dist/stores/formSave.svelte.js +0 -113
- package/dist/stores/navigation.svelte.d.ts +0 -35
- package/dist/stores/navigation.svelte.d.ts.map +0 -1
- package/dist/stores/navigation.svelte.js +0 -69
|
@@ -1,238 +1,238 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import Cropper from 'svelte-easy-crop';
|
|
3
|
-
import { CloseOutline, MinusOutline, PlusOutline } from "../../primitives/Icons";
|
|
4
|
-
import Button from '../../primitives/Button/Button.svelte';
|
|
5
|
-
import { typography } from '../../tokens/typography';
|
|
6
|
-
|
|
7
|
-
interface Props {
|
|
8
|
-
/** Whether to show the modal */
|
|
9
|
-
showModal?: boolean;
|
|
10
|
-
/** Image source to crop */
|
|
11
|
-
imageSrc?: string;
|
|
12
|
-
/** Callback when save is clicked with cropped image */
|
|
13
|
-
onSave?: (croppedImage: File) => void;
|
|
14
|
-
/** Callback when cancel is clicked */
|
|
15
|
-
onCancel?: () => void;
|
|
16
|
-
/** Whether image is currently uploading */
|
|
17
|
-
isUploadingImage?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let {
|
|
21
|
-
showModal = $bindable(false),
|
|
22
|
-
imageSrc = $bindable(''),
|
|
23
|
-
onSave = () => {},
|
|
24
|
-
onCancel = () => {},
|
|
25
|
-
isUploadingImage = false
|
|
26
|
-
}: Props = $props();
|
|
27
|
-
|
|
28
|
-
let crop = $state({ x: 0, y: 0 });
|
|
29
|
-
let zoom = $state(1);
|
|
30
|
-
let croppedAreaPixels = $state<{ x: number; y: number; width: number; height: number } | null>(null);
|
|
31
|
-
let isLoading = $state(true);
|
|
32
|
-
let isSaving = $state(false);
|
|
33
|
-
|
|
34
|
-
$effect(() => {
|
|
35
|
-
if (showModal && imageSrc) {
|
|
36
|
-
crop = { x: 0, y: 0 };
|
|
37
|
-
zoom = 1;
|
|
38
|
-
croppedAreaPixels = null;
|
|
39
|
-
isLoading = true;
|
|
40
|
-
isSaving = false; // Reset saving state when modal opens with new image
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
function handleCropComplete(e: { pixels: { x: number; y: number; width: number; height: number } }) {
|
|
45
|
-
croppedAreaPixels = e.pixels;
|
|
46
|
-
isLoading = false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async function handleSave() {
|
|
50
|
-
if (!croppedAreaPixels || isSaving) return;
|
|
51
|
-
|
|
52
|
-
isSaving = true;
|
|
53
|
-
const croppedImage = await getCroppedImg(imageSrc, croppedAreaPixels);
|
|
54
|
-
onSave(croppedImage);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function closeModal() {
|
|
58
|
-
showModal = false;
|
|
59
|
-
imageSrc = '';
|
|
60
|
-
crop = { x: 0, y: 0 };
|
|
61
|
-
zoom = 1;
|
|
62
|
-
croppedAreaPixels = null;
|
|
63
|
-
isSaving = false;
|
|
64
|
-
onCancel();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function createImage(url: string): Promise<HTMLImageElement> {
|
|
68
|
-
return new Promise((resolve, reject) => {
|
|
69
|
-
const image = new Image();
|
|
70
|
-
image.addEventListener('load', () => resolve(image));
|
|
71
|
-
image.addEventListener('error', (error) => reject(error));
|
|
72
|
-
// Only set crossOrigin for non-blob URLs (blob URLs don't need CORS)
|
|
73
|
-
if (!url.startsWith('blob:')) {
|
|
74
|
-
image.setAttribute('crossOrigin', 'anonymous');
|
|
75
|
-
}
|
|
76
|
-
image.src = url;
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function supportsWebP(): boolean {
|
|
81
|
-
const canvas = document.createElement('canvas');
|
|
82
|
-
canvas.width = 1;
|
|
83
|
-
canvas.height = 1;
|
|
84
|
-
return canvas.toDataURL('image/webp').indexOf('data:image/webp') === 0;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function getCroppedImg(imageSrc: string, pixelCrop: { x: number; y: number; width: number; height: number }): Promise<File> {
|
|
88
|
-
const image = await createImage(imageSrc);
|
|
89
|
-
const canvas = document.createElement('canvas');
|
|
90
|
-
const ctx = canvas.getContext('2d')!;
|
|
91
|
-
|
|
92
|
-
const maxSize = 1000;
|
|
93
|
-
canvas.width = maxSize;
|
|
94
|
-
canvas.height = maxSize;
|
|
95
|
-
|
|
96
|
-
ctx.imageSmoothingEnabled = true;
|
|
97
|
-
ctx.imageSmoothingQuality = 'high';
|
|
98
|
-
|
|
99
|
-
ctx.drawImage(
|
|
100
|
-
image,
|
|
101
|
-
pixelCrop.x,
|
|
102
|
-
pixelCrop.y,
|
|
103
|
-
pixelCrop.width,
|
|
104
|
-
pixelCrop.height,
|
|
105
|
-
0,
|
|
106
|
-
0,
|
|
107
|
-
maxSize,
|
|
108
|
-
maxSize
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
const useWebP = supportsWebP();
|
|
112
|
-
const mimeType = useWebP ? 'image/webp' : 'image/jpeg';
|
|
113
|
-
const quality = useWebP ? 0.80 : 0.85;
|
|
114
|
-
const extension = useWebP ? 'webp' : 'jpg';
|
|
115
|
-
|
|
116
|
-
return new Promise((resolve) => {
|
|
117
|
-
canvas.toBlob(
|
|
118
|
-
(blob) => {
|
|
119
|
-
const file = new File([blob!], `cropped-image.${extension}`, {
|
|
120
|
-
type: mimeType,
|
|
121
|
-
});
|
|
122
|
-
resolve(file);
|
|
123
|
-
},
|
|
124
|
-
mimeType,
|
|
125
|
-
quality
|
|
126
|
-
);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
</script>
|
|
130
|
-
|
|
131
|
-
{#if showModal}
|
|
132
|
-
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
133
|
-
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
134
|
-
<div
|
|
135
|
-
class="fixed inset-0 z-50 flex items-center justify-center overflow-y-auto overflow-x-hidden bg-gray-900/50 dark:bg-gray-900/80 p-4"
|
|
136
|
-
onclick={(e) => { if (e.target === e.currentTarget) closeModal(); }}
|
|
137
|
-
>
|
|
138
|
-
<div class="relative w-full max-w-lg max-h-full">
|
|
139
|
-
<div class="relative bg-white rounded-lg shadow-xl dark:bg-gray-800">
|
|
140
|
-
<div class="flex items-center justify-between p-4 border-b border-gray-200 rounded-t-lg dark:border-gray-600">
|
|
141
|
-
<h3 class={typography.h2}>
|
|
142
|
-
Crop image
|
|
143
|
-
</h3>
|
|
144
|
-
<Button
|
|
145
|
-
variant="icon"
|
|
146
|
-
size="sm"
|
|
147
|
-
onclick={closeModal}
|
|
148
|
-
aria-label="Close modal"
|
|
149
|
-
>
|
|
150
|
-
<CloseOutline class="w-3 h-3" />
|
|
151
|
-
</Button>
|
|
152
|
-
</div>
|
|
153
|
-
|
|
154
|
-
<div class="p-4 md:p-6 space-y-4">
|
|
155
|
-
<div class="relative w-full aspect-square sm:aspect-[4/3] max-h-80 sm:max-h-96 bg-gray-800 rounded-lg overflow-hidden">
|
|
156
|
-
{#if isLoading}
|
|
157
|
-
<div class="absolute inset-0 flex items-center justify-center bg-gray-200 dark:bg-gray-700 z-10">
|
|
158
|
-
<span class={typography.smMuted}>Loading...</span>
|
|
159
|
-
</div>
|
|
160
|
-
{/if}
|
|
161
|
-
<Cropper
|
|
162
|
-
image={imageSrc}
|
|
163
|
-
bind:crop
|
|
164
|
-
bind:zoom
|
|
165
|
-
aspect={1}
|
|
166
|
-
minZoom={1}
|
|
167
|
-
maxZoom={3}
|
|
168
|
-
showGrid={true}
|
|
169
|
-
oncropcomplete={handleCropComplete}
|
|
170
|
-
/>
|
|
171
|
-
</div>
|
|
172
|
-
|
|
173
|
-
<p class={`${typography.smMuted} text-center`}>
|
|
174
|
-
Drag to position. Pinch or use slider to zoom.
|
|
175
|
-
</p>
|
|
176
|
-
|
|
177
|
-
<div class="flex items-center justify-center gap-3 max-w-xs mx-auto">
|
|
178
|
-
<Button
|
|
179
|
-
variant="outline"
|
|
180
|
-
size="sm"
|
|
181
|
-
class="!rounded-full !w-9 !h-9 !p-0 shrink-0"
|
|
182
|
-
onclick={() => (zoom = Math.max(1, zoom - 0.2))}
|
|
183
|
-
aria-label="Zoom out"
|
|
184
|
-
>
|
|
185
|
-
<MinusOutline class="w-4 h-4" />
|
|
186
|
-
</Button>
|
|
187
|
-
|
|
188
|
-
<input
|
|
189
|
-
type="range"
|
|
190
|
-
min="1"
|
|
191
|
-
max="3"
|
|
192
|
-
step="0.1"
|
|
193
|
-
bind:value={zoom}
|
|
194
|
-
class="flex-1 w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700 accent-blue-600"
|
|
195
|
-
aria-label="Zoom level"
|
|
196
|
-
/>
|
|
197
|
-
|
|
198
|
-
<Button
|
|
199
|
-
variant="outline"
|
|
200
|
-
size="sm"
|
|
201
|
-
class="!rounded-full !w-9 !h-9 !p-0 shrink-0"
|
|
202
|
-
onclick={() => (zoom = Math.min(3, zoom + 0.2))}
|
|
203
|
-
aria-label="Zoom in"
|
|
204
|
-
>
|
|
205
|
-
<PlusOutline class="w-4 h-4" />
|
|
206
|
-
</Button>
|
|
207
|
-
</div>
|
|
208
|
-
</div>
|
|
209
|
-
|
|
210
|
-
<div class="flex items-center justify-end gap-3 p-4 border-t border-gray-200 rounded-b-lg dark:border-gray-600">
|
|
211
|
-
<Button
|
|
212
|
-
type="button"
|
|
213
|
-
variant="alternative"
|
|
214
|
-
size="md"
|
|
215
|
-
minWidth="sm"
|
|
216
|
-
onclick={closeModal}
|
|
217
|
-
disabled={isSaving || isUploadingImage}
|
|
218
|
-
>
|
|
219
|
-
Cancel
|
|
220
|
-
</Button>
|
|
221
|
-
<Button
|
|
222
|
-
type="button"
|
|
223
|
-
size="md"
|
|
224
|
-
minWidth="sm"
|
|
225
|
-
onclick={handleSave}
|
|
226
|
-
disabled={isLoading || isSaving || isUploadingImage}
|
|
227
|
-
>
|
|
228
|
-
{#if isSaving || isUploadingImage}
|
|
229
|
-
Uploading...
|
|
230
|
-
{:else}
|
|
231
|
-
Save
|
|
232
|
-
{/if}
|
|
233
|
-
</Button>
|
|
234
|
-
</div>
|
|
235
|
-
</div>
|
|
236
|
-
</div>
|
|
237
|
-
</div>
|
|
238
|
-
{/if}
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import Cropper from 'svelte-easy-crop';
|
|
3
|
+
import { CloseOutline, MinusOutline, PlusOutline } from "../../primitives/Icons";
|
|
4
|
+
import Button from '../../primitives/Button/Button.svelte';
|
|
5
|
+
import { typography } from '../../tokens/typography';
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
/** Whether to show the modal */
|
|
9
|
+
showModal?: boolean;
|
|
10
|
+
/** Image source to crop */
|
|
11
|
+
imageSrc?: string;
|
|
12
|
+
/** Callback when save is clicked with cropped image */
|
|
13
|
+
onSave?: (croppedImage: File) => void;
|
|
14
|
+
/** Callback when cancel is clicked */
|
|
15
|
+
onCancel?: () => void;
|
|
16
|
+
/** Whether image is currently uploading */
|
|
17
|
+
isUploadingImage?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let {
|
|
21
|
+
showModal = $bindable(false),
|
|
22
|
+
imageSrc = $bindable(''),
|
|
23
|
+
onSave = () => {},
|
|
24
|
+
onCancel = () => {},
|
|
25
|
+
isUploadingImage = false
|
|
26
|
+
}: Props = $props();
|
|
27
|
+
|
|
28
|
+
let crop = $state({ x: 0, y: 0 });
|
|
29
|
+
let zoom = $state(1);
|
|
30
|
+
let croppedAreaPixels = $state<{ x: number; y: number; width: number; height: number } | null>(null);
|
|
31
|
+
let isLoading = $state(true);
|
|
32
|
+
let isSaving = $state(false);
|
|
33
|
+
|
|
34
|
+
$effect(() => {
|
|
35
|
+
if (showModal && imageSrc) {
|
|
36
|
+
crop = { x: 0, y: 0 };
|
|
37
|
+
zoom = 1;
|
|
38
|
+
croppedAreaPixels = null;
|
|
39
|
+
isLoading = true;
|
|
40
|
+
isSaving = false; // Reset saving state when modal opens with new image
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function handleCropComplete(e: { pixels: { x: number; y: number; width: number; height: number } }) {
|
|
45
|
+
croppedAreaPixels = e.pixels;
|
|
46
|
+
isLoading = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function handleSave() {
|
|
50
|
+
if (!croppedAreaPixels || isSaving) return;
|
|
51
|
+
|
|
52
|
+
isSaving = true;
|
|
53
|
+
const croppedImage = await getCroppedImg(imageSrc, croppedAreaPixels);
|
|
54
|
+
onSave(croppedImage);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function closeModal() {
|
|
58
|
+
showModal = false;
|
|
59
|
+
imageSrc = '';
|
|
60
|
+
crop = { x: 0, y: 0 };
|
|
61
|
+
zoom = 1;
|
|
62
|
+
croppedAreaPixels = null;
|
|
63
|
+
isSaving = false;
|
|
64
|
+
onCancel();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function createImage(url: string): Promise<HTMLImageElement> {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const image = new Image();
|
|
70
|
+
image.addEventListener('load', () => resolve(image));
|
|
71
|
+
image.addEventListener('error', (error) => reject(error));
|
|
72
|
+
// Only set crossOrigin for non-blob URLs (blob URLs don't need CORS)
|
|
73
|
+
if (!url.startsWith('blob:')) {
|
|
74
|
+
image.setAttribute('crossOrigin', 'anonymous');
|
|
75
|
+
}
|
|
76
|
+
image.src = url;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function supportsWebP(): boolean {
|
|
81
|
+
const canvas = document.createElement('canvas');
|
|
82
|
+
canvas.width = 1;
|
|
83
|
+
canvas.height = 1;
|
|
84
|
+
return canvas.toDataURL('image/webp').indexOf('data:image/webp') === 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function getCroppedImg(imageSrc: string, pixelCrop: { x: number; y: number; width: number; height: number }): Promise<File> {
|
|
88
|
+
const image = await createImage(imageSrc);
|
|
89
|
+
const canvas = document.createElement('canvas');
|
|
90
|
+
const ctx = canvas.getContext('2d')!;
|
|
91
|
+
|
|
92
|
+
const maxSize = 1000;
|
|
93
|
+
canvas.width = maxSize;
|
|
94
|
+
canvas.height = maxSize;
|
|
95
|
+
|
|
96
|
+
ctx.imageSmoothingEnabled = true;
|
|
97
|
+
ctx.imageSmoothingQuality = 'high';
|
|
98
|
+
|
|
99
|
+
ctx.drawImage(
|
|
100
|
+
image,
|
|
101
|
+
pixelCrop.x,
|
|
102
|
+
pixelCrop.y,
|
|
103
|
+
pixelCrop.width,
|
|
104
|
+
pixelCrop.height,
|
|
105
|
+
0,
|
|
106
|
+
0,
|
|
107
|
+
maxSize,
|
|
108
|
+
maxSize
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const useWebP = supportsWebP();
|
|
112
|
+
const mimeType = useWebP ? 'image/webp' : 'image/jpeg';
|
|
113
|
+
const quality = useWebP ? 0.80 : 0.85;
|
|
114
|
+
const extension = useWebP ? 'webp' : 'jpg';
|
|
115
|
+
|
|
116
|
+
return new Promise((resolve) => {
|
|
117
|
+
canvas.toBlob(
|
|
118
|
+
(blob) => {
|
|
119
|
+
const file = new File([blob!], `cropped-image.${extension}`, {
|
|
120
|
+
type: mimeType,
|
|
121
|
+
});
|
|
122
|
+
resolve(file);
|
|
123
|
+
},
|
|
124
|
+
mimeType,
|
|
125
|
+
quality
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
{#if showModal}
|
|
132
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
133
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
134
|
+
<div
|
|
135
|
+
class="fixed inset-0 z-50 flex items-center justify-center overflow-y-auto overflow-x-hidden bg-gray-900/50 dark:bg-gray-900/80 p-4"
|
|
136
|
+
onclick={(e) => { if (e.target === e.currentTarget) closeModal(); }}
|
|
137
|
+
>
|
|
138
|
+
<div class="relative w-full max-w-lg max-h-full">
|
|
139
|
+
<div class="relative bg-white rounded-lg shadow-xl dark:bg-gray-800">
|
|
140
|
+
<div class="flex items-center justify-between p-4 border-b border-gray-200 rounded-t-lg dark:border-gray-600">
|
|
141
|
+
<h3 class={typography.h2}>
|
|
142
|
+
Crop image
|
|
143
|
+
</h3>
|
|
144
|
+
<Button
|
|
145
|
+
variant="icon"
|
|
146
|
+
size="sm"
|
|
147
|
+
onclick={closeModal}
|
|
148
|
+
aria-label="Close modal"
|
|
149
|
+
>
|
|
150
|
+
<CloseOutline class="w-3 h-3" />
|
|
151
|
+
</Button>
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
<div class="p-4 md:p-6 space-y-4">
|
|
155
|
+
<div class="relative w-full aspect-square sm:aspect-[4/3] max-h-80 sm:max-h-96 bg-gray-800 rounded-lg overflow-hidden">
|
|
156
|
+
{#if isLoading}
|
|
157
|
+
<div class="absolute inset-0 flex items-center justify-center bg-gray-200 dark:bg-gray-700 z-10">
|
|
158
|
+
<span class={typography.smMuted}>Loading...</span>
|
|
159
|
+
</div>
|
|
160
|
+
{/if}
|
|
161
|
+
<Cropper
|
|
162
|
+
image={imageSrc}
|
|
163
|
+
bind:crop
|
|
164
|
+
bind:zoom
|
|
165
|
+
aspect={1}
|
|
166
|
+
minZoom={1}
|
|
167
|
+
maxZoom={3}
|
|
168
|
+
showGrid={true}
|
|
169
|
+
oncropcomplete={handleCropComplete}
|
|
170
|
+
/>
|
|
171
|
+
</div>
|
|
172
|
+
|
|
173
|
+
<p class={`${typography.smMuted} text-center`}>
|
|
174
|
+
Drag to position. Pinch or use slider to zoom.
|
|
175
|
+
</p>
|
|
176
|
+
|
|
177
|
+
<div class="flex items-center justify-center gap-3 max-w-xs mx-auto">
|
|
178
|
+
<Button
|
|
179
|
+
variant="outline"
|
|
180
|
+
size="sm"
|
|
181
|
+
class="!rounded-full !w-9 !h-9 !p-0 shrink-0"
|
|
182
|
+
onclick={() => (zoom = Math.max(1, zoom - 0.2))}
|
|
183
|
+
aria-label="Zoom out"
|
|
184
|
+
>
|
|
185
|
+
<MinusOutline class="w-4 h-4" />
|
|
186
|
+
</Button>
|
|
187
|
+
|
|
188
|
+
<input
|
|
189
|
+
type="range"
|
|
190
|
+
min="1"
|
|
191
|
+
max="3"
|
|
192
|
+
step="0.1"
|
|
193
|
+
bind:value={zoom}
|
|
194
|
+
class="flex-1 w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700 accent-blue-600"
|
|
195
|
+
aria-label="Zoom level"
|
|
196
|
+
/>
|
|
197
|
+
|
|
198
|
+
<Button
|
|
199
|
+
variant="outline"
|
|
200
|
+
size="sm"
|
|
201
|
+
class="!rounded-full !w-9 !h-9 !p-0 shrink-0"
|
|
202
|
+
onclick={() => (zoom = Math.min(3, zoom + 0.2))}
|
|
203
|
+
aria-label="Zoom in"
|
|
204
|
+
>
|
|
205
|
+
<PlusOutline class="w-4 h-4" />
|
|
206
|
+
</Button>
|
|
207
|
+
</div>
|
|
208
|
+
</div>
|
|
209
|
+
|
|
210
|
+
<div class="flex items-center justify-end gap-3 p-4 border-t border-gray-200 rounded-b-lg dark:border-gray-600">
|
|
211
|
+
<Button
|
|
212
|
+
type="button"
|
|
213
|
+
variant="alternative"
|
|
214
|
+
size="md"
|
|
215
|
+
minWidth="sm"
|
|
216
|
+
onclick={closeModal}
|
|
217
|
+
disabled={isSaving || isUploadingImage}
|
|
218
|
+
>
|
|
219
|
+
Cancel
|
|
220
|
+
</Button>
|
|
221
|
+
<Button
|
|
222
|
+
type="button"
|
|
223
|
+
size="md"
|
|
224
|
+
minWidth="sm"
|
|
225
|
+
onclick={handleSave}
|
|
226
|
+
disabled={isLoading || isSaving || isUploadingImage}
|
|
227
|
+
>
|
|
228
|
+
{#if isSaving || isUploadingImage}
|
|
229
|
+
Uploading...
|
|
230
|
+
{:else}
|
|
231
|
+
Save
|
|
232
|
+
{/if}
|
|
233
|
+
</Button>
|
|
234
|
+
</div>
|
|
235
|
+
</div>
|
|
236
|
+
</div>
|
|
237
|
+
</div>
|
|
238
|
+
{/if}
|