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

Handle appear events #115

Merged
merged 1 commit into from
Jul 2, 2024
Merged

Handle appear events #115

merged 1 commit into from
Jul 2, 2024

Conversation

peterszerzo
Copy link
Collaborator

Also includes some refactoring that fixes a click but related to the once feature.

@peterszerzo peterszerzo requested a review from jakub-nlx as a code owner July 2, 2024 13:44
packages/journey-manager/src/index.ts Outdated Show resolved Hide resolved
try {
return {
...step,
elements: await find(step.query),
Copy link
Collaborator

Choose a reason for hiding this comment

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

For appear on mutation observer, I'd imagine you want the sync version rather than waiting up to a second for the element to appear.

Comment on lines 530 to 531
mutations.forEach(async (mutation) => {
const targets = await withElements(appearSteps);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
mutations.forEach(async (mutation) => {
const targets = await withElements(appearSteps);
const targets = await withElements(appearSteps);
mutations.forEach(async (mutation) => {

Seems like a performance win, no?

Comment on lines 536 to 539
[...mutation.addedNodes].some((addedNode) => {
if (element.contains(addedNode)) {
sendStep(stepId, once ?? false);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this is required at all. Don't we want basically the same logic like we do for URL events? If the DOM changed at all, and the query didn't succeed previously but now it did, then fire the appear event? Or perhaps more elaborately, we store the count of elements, then if the count is now larger than the old count we trigger the appear event?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see this the same way from a semantic perspective. If, by 'query succeeds', you mean both query and URL condition combined, then it can happen that the step fires even though no 'appear' took place, but purely from the technicality of the combined condition wasn't satisfied before but now is.

Practical example: we want to play something on voice when an error message first appears, as opposed to when the URL is finally compatible but the error had been around on the page for 2 minutes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well then pre-filter the array based on URL condition, then reset it when those change?

The above code as far as I read it just triggers when an element we are targeting has a child inserted into the DOM. That could be for all sorts of reasons and doesn't necessarily mean it "appeared". For one, we are not checking its visibility, so it might not actually be visible to the user at all.

Also, why the contains check? That could trigger an element that simply has changeable children?

Copy link
Collaborator Author

@peterszerzo peterszerzo Jul 2, 2024

Choose a reason for hiding this comment

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

I had the condition backwards, just changed to addedNode.contains(element). This way we make sure if the user selected an element, but that is inserted as a result of its parent being inserted, then the event still fires. Otherwise, the user has to target the exact element that has been added to the DOM.

Appear intentionally does not care about visibility. There is a separate 'enters viewport' that takes care of that, and they are intentionally separate because sometimes an error notification is not visible, but you want to react to it immediately anyway.

At this point I'm tempted to say if you have a different implementation in mind, feel free to implement it as a separate PR, and we can compare.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I had the condition backwards, just changed to addedNode.contains(element).

OK, that I think alleviates most of my concern here.

Appear intentionally does not care about visibility.

I believe that isn't strictly true, since the accessibility queries do care about elements being visible (at least in the they don't have display: none set sense).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe that isn't strictly true, since the accessibility queries do care about elements being visible (at least in the they don't have display: none set sense).

I'm ok with this edge case, but good to know for debugging.

@peterszerzo peterszerzo force-pushed the appear-handling branch 4 times, most recently from 69da7ef to fe2b581 Compare July 2, 2024 15:07
}
return prev;
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return null;

not necessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Explicit returns read clearer to me.

@peterszerzo peterszerzo merged commit 76d729c into main Jul 2, 2024
4 checks passed
@peterszerzo peterszerzo deleted the appear-handling branch July 2, 2024 16:50
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.

2 participants