Skip to content

Dispatch cancelable events with createEventDispatcher #4623

@oreilles

Description

@oreilles

From the docs:

Component events created with createEventDispatcher create a CustomEvent. These events do not bubble and are not cancellable with event.preventDefault().

I understand why they couldn't bubble, but is there any reason why cancelable event aren't allowed ? It would only require adding one line in createEventDispatcher declaration (from /src/runtime/internal/lifecycle.ts)

export function createEventDispatcher() {
	const component = get_current_component();

	return (type: string, detail?: any): boolean => {
		const callbacks = component.$$.callbacks[type];

		if (callbacks) {
			const event = custom_event(type, detail);
			callbacks.slice().forEach(fn => {
				fn.call(component, event);
			});
			
			// Just add this line
			return !event.defaultPrevented
		}
	};
}

and a slightly different signature for custom_event (from /src/runtime/internal/dom.ts)

export function custom_event<T=any>(type: string, detail?: T, cancelable?: boolean){}
// or anything else

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions