Skip to content

Commit c7e7c09

Browse files
committed
fix: event attributes support
1 parent 575cbf4 commit c7e7c09

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/ModernCropper.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
crossorigin = 'anonymous',
3838
3939
resetOnInitialAttributes = true,
40+
useEventListeners = false,
4041
4142
passThrough,
4243
} = defineProps<{
@@ -49,6 +50,11 @@ const {
4950
* @default true
5051
*/
5152
resetOnInitialAttributes?: boolean
53+
/**
54+
* Use event listeners instead of direct property assignment for event attributes (onchange etc)
55+
* @default false
56+
*/
57+
useEventListeners?: boolean
5258
5359
passThrough?: PassThroughOptions
5460
}>()
@@ -185,6 +191,16 @@ function _setElementAttributes(element: HTMLElement, attributes: Record<any, any
185191
if (value === false || value === null)
186192
return element.removeAttribute(attribute)
187193
194+
// Handle event attributes
195+
if (/^on[a-z]+$/.test(attribute)) {
196+
if (useEventListeners)
197+
element.addEventListener(attribute.replace(/^on/, ''), value)
198+
else
199+
// @ts-expect-error index signature unknown
200+
element[attribute] = value
201+
return
202+
}
203+
188204
element.setAttribute(attribute.replaceAll(/([a-z])([A-Z])/g, (_match, p1: string, p2: string) => `${p1}-${p2.toLowerCase()}`), value)
189205
})
190206

0 commit comments

Comments
 (0)