Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a way to add listener only to some nodes #185

Closed
marcosvega91 opened this issue Jun 17, 2020 · 3 comments
Closed

add a way to add listener only to some nodes #185

marcosvega91 opened this issue Jun 17, 2020 · 3 comments
Labels
feature New feature or request project:events Tickets related to the /events route

Comments

@marcosvega91
Copy link
Member

Summary 💡

We should add event listeners to only some elements if a data attribute is used in markup

Examples 🌈

as suggested by Kent we can lister for a [data-listen-only] attribute. If some elements have this attribute we could attach event listener only on those elements.

Motivation 🔦

Because the user can be interested only on some elements

@marcosvega91 marcosvega91 added feature New feature or request project:events Tickets related to the /events route labels Jun 17, 2020
@marcosvega91
Copy link
Member Author

My idea is to do something similar

diff --git a/src/components/DomEvents.js b/src/components/DomEvents.js
index f5cd663..971a9b5 100644
--- a/src/components/DomEvents.js
+++ b/src/components/DomEvents.js
@@ -62,11 +62,14 @@ function addLoggingEvents(node, log) {
       if (event.target === event.currentTarget) {
         return;
       }
+      const onlyNode = node.querySelector('[data-listen-only]');
 
-      log({
-        event: eventType,
-        target: getElementData(event.target),
-      });
+      if (!onlyNode || event.target.matches('[data-listen-only]')) {
+        log({
+          event: eventType,
+          target: getElementData(event.target),
+        });
+      }
     };
   }
   const eventListeners = [];

I'm worried about performance issue of retrieving every time the node.
@smeijer What do you think?

@smeijer
Copy link
Member

smeijer commented Jun 17, 2020

@marcosvega91, I think it makes sense to add this to #171.

The way that I see it, is that there is no difference in filtering on Event or on Target.

We need to think of a way to make the element selections, but I would prefer to have it be done in a visual way, and not with attributes.

The reasoning for that is, that attributes will not work when using devtools. They'll require the developer/tester to update the markup. Which is not always possible.

@marcosvega91
Copy link
Member Author

Yes right I didn't think about devtools. Ok I'll close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request project:events Tickets related to the /events route
Projects
None yet
Development

No branches or pull requests

2 participants