diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 69f5cc07b1..255c071ac4 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -630,14 +630,10 @@ export function toggle_class(element, name, toggle) { element.classList[toggle ? 'add' : 'remove'](name); } -export interface CustomEventOpts { - bubbles?: boolean; - cancelable?: boolean; -} -export function custom_event(type: string, detail?: T, opts: CustomEventOpts = {}) { +export function custom_event(type: string, detail?: T, { bubbles = false, cancelable = false } = {}): CustomEvent { const e: CustomEvent = document.createEvent('CustomEvent'); - e.initCustomEvent(type, opts.bubbles, opts.cancelable, detail); + e.initCustomEvent(type, bubbles, cancelable, detail); return e; } diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index 610557ed18..aac8ebe0b7 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -1,4 +1,4 @@ -import { custom_event, CustomEventOpts } from './dom'; +import { custom_event } from './dom'; export let current_component; @@ -32,17 +32,20 @@ export function createEventDispatcher< >(): >(type: EventKey, detail?: EventMap[EventKey]) => void { const component = get_current_component(); - return (type: string, detail?: any, opts?: CustomEventOpts) => { + return (type: string, detail?: any, { cancelable = false } = {}): boolean => { const callbacks = component.$$.callbacks[type]; if (callbacks) { // TODO are there situations where events could be dispatched // in a server (non-DOM) environment? - const event = custom_event(type, detail, opts); + const event = custom_event(type, detail, { cancelable }); callbacks.slice().forEach(fn => { fn.call(component, event); }); + return !event.defaultPrevented; } + + return true; }; }