|
1 | | -const controllers = new Set<string>() |
| 1 | +const controllers = new WeakSet<Element>() |
2 | 2 |
|
3 | 3 | /* |
4 | 4 | * Bind `[data-action]` elements from the DOM to their actions. |
5 | 5 | * |
6 | 6 | */ |
7 | 7 | export function bind(controller: HTMLElement): void { |
8 | | - controllers.add(controller.tagName.toLowerCase()) |
| 8 | + controllers.add(controller) |
9 | 9 | if (controller.shadowRoot) { |
10 | 10 | bindElements(controller.shadowRoot) |
11 | 11 | listenForBind(controller.shadowRoot) |
@@ -72,14 +72,14 @@ function bindElements(root: Element | ShadowRoot) { |
72 | 72 | function handleEvent(event: Event) { |
73 | 73 | const el = event.currentTarget as Element |
74 | 74 | for (const binding of bindings(el)) { |
75 | | - if (event.type === binding.type && controllers.has(binding.tag)) { |
76 | | - type EventDispatcher = Element & Record<string, (ev: Event) => unknown> |
77 | | - const controller = el.closest(binding.tag) as EventDispatcher |
78 | | - if (controller && typeof controller[binding.method] === 'function') { |
| 75 | + if (event.type === binding.type) { |
| 76 | + type EventDispatcher = HTMLElement & Record<string, (ev: Event) => unknown> |
| 77 | + const controller = el.closest<EventDispatcher>(binding.tag)! |
| 78 | + if (controllers.has(controller) && typeof controller[binding.method] === 'function') { |
79 | 79 | controller[binding.method](event) |
80 | 80 | } |
81 | 81 | const root = el.getRootNode() |
82 | | - if (root instanceof ShadowRoot && root.host.matches(binding.tag)) { |
| 82 | + if (root instanceof ShadowRoot && controllers.has(root.host) && root.host.matches(binding.tag)) { |
83 | 83 | const shadowController = root.host as EventDispatcher |
84 | 84 | if (typeof shadowController[binding.method] === 'function') { |
85 | 85 | shadowController[binding.method](event) |
|
0 commit comments