Skip to content

Update events documentation #139

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

Merged
merged 20 commits into from
Feb 23, 2021
Merged

Update events documentation #139

merged 20 commits into from
Feb 23, 2021

Conversation

sorvell
Copy link
Member

@sorvell sorvell commented Jan 30, 2021

Fixes #66, #67, #68, #69.

@aomarks Just need a quick review of the change to litdev-example.

@aomarks
Copy link
Member

aomarks commented Jan 30, 2021

@sorvell I just sent out #138 which provides another way to do this, within the project.json. I kinda like your approach here for its simplicity -- the advantage of mine is that it automatically fixes the total height to prevent pre-upgrade layout shift, plus it's one fewer DOM element. WDYT?

@nicolejadeyee nicolejadeyee added this to the Website complete milestone Feb 10, 2021

## Where to add your event listeners
## Listening to events
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This intro section talking about addEventListener seems unnecessary, since it's not the primary way to add events, and putting it up front makes it seem that way.

I would just add the MDN link to the "Adding event listeners to the component or its shadow root" section below:

To be notified of an event dispatched from the component's slotted children as well as children rendered into shadow DOM via the component template, you can add a listener to the component itself using the standard addEventListener DOM method (See EventTarget.addEventListener() on MDN for full details on this API).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was suggesting removing this discussion altogether (since it's primarily about how to imperatively call addEventListener to nodes, which is not the primary way to add events in Lit, and putting this section up front makes it feel that way.

Since the only time you should have to call addEventListener imperatively today is when adding listeners to the shadow root or host, and you already have a section on that, I'd removed this preamble and go directly into "Adding event listeners in the element template".


If your component adds an event listener to anything except itself or its children–for example, to `Window`, `Document`, or some element in the main DOM–you should add the listener in `connectedCallback` and remove it in `disconnectedCallback`.
See [Working with events in shadow DOM](#shadowdom) for more information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest removing newline between paragraphs, since this cross-reference is related to the previous paragraph.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


Whether or not an event bubbles depends on the value of its `bubbles` property. To check if a particular event bubbles:
To make a custom event pass through shadow DOM boundaries, you must set both the `composed` and `bubbles` flags to `true`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exactly correct, the options are distinct. IOW composed: true events can either bubble or not. If not bubbling, they will just re-target on every shadow host up the tree, just not bubble through each node in the composed tree. afaiu.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


To make a custom event pass through shadow DOM boundaries, you must set both the `composed` and `bubbles` flags to `true`:
For example, the `preventDefault()` method can be called to indicate the event's standard action should not occur. For example, calling `preventDefault()` in a listener for the click event of a checkbox prevents the checkbox from becoming checked when clicked.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consecutive use of "For example"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Steven Orvell and others added 9 commits February 22, 2021 15:40
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
Co-authored-by: Kevin Schaaf <kschaaf@google.com>
@sorvell sorvell requested a review from kevinpschaaf February 23, 2021 00:06
Copy link
Member

@kevinpschaaf kevinpschaaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple more suggestions, I think you can consider them and then go ahead and merge.


## Where to add your event listeners
## Listening to events
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was suggesting removing this discussion altogether (since it's primarily about how to imperatively call addEventListener to nodes, which is not the primary way to add events in Lit, and putting this section up front makes it feel that way.

Since the only time you should have to call addEventListener imperatively today is when adding listeners to the shadow root or host, and you already have a section on that, I'd removed this preamble and go directly into "Adding event listeners in the element template".

@@ -300,7 +289,7 @@ See the [MDN documentation on composedPath](https://developer.mozilla.org/en-US/

Events exist primarily to communicate changes from the event dispatcher to the event listener, but events can also be used to communicate information from the listener back to the dispatcher.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title of this section and its contents don't really connect for me. Based on the title, I expected it to focus on a listener setting data on to the event that the dispatcher then retrieves synchronously after dispatch, but it talks mostly about preventDefault, which sorta feels random, and skims over the details of actual communication between dispatcher and listener. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded.

Steve Orvell and others added 2 commits February 22, 2021 18:35
@sorvell sorvell merged commit a5ebd5b into master Feb 23, 2021
@aomarks aomarks deleted the events branch October 8, 2021 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Components--Events-- Edit for Consistency
4 participants