Skip to content

Replace selector-observer - #5886

Merged
fregante merged 13 commits into
mainfrom
new-selector-observer
Aug 7, 2022
Merged

fregante merged 13 commits into
mainfrom
new-selector-observer

Conversation

@fregante

@fregante fregante commented Aug 3, 2022

Copy link
Copy Markdown
Member

Not tested.

If the WeakSet doesn't match existing elements, the function will also need to attach a temporary class because I removed our custom :not(rgh-etc) selectors

@fregante fregante changed the title Replace selector-observer (where constructor was used) Replace selector-observer Aug 3, 2022
@fregante

fregante commented Aug 3, 2022

Copy link
Copy Markdown
Member Author

Already found two pretty serious limitations:

  • it won't detect hidden elements (which can be an advantage sometimes)
  • you can't observe the same element twice (e.g. shorten-links applies to every markdown link, so that breaks a lot of features)

I think the "freedom" that an extremely-lightweight solution grants us is completely taken away from the fact that any overlapping selectors may at any time just break.

We could be careful about this, but there's no way we'd actually be successful at this. A 1-character bugfix could break another feature and we wouldn't know until much later.

@fregante fregante closed this Aug 3, 2022
@fregante
fregante deleted the new-selector-observer branch August 3, 2022 19:05
@fregante
fregante restored the new-selector-observer branch August 4, 2022 11:08
@fregante fregante reopened this Aug 4, 2022

@fregante fregante left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I found a solution to the multiple-observers problem and also the compatibility with any other native animations:

  • it doesn't matter what animation is triggered, the observer just needs any animation to start in order to find the element

However… I just realized that there's still another problem with this solution:

  1. an animation starts on an element
  2. the selector isn't matched yet
  3. a class is added
  4. the selector is matched
  5. the animation doesn't start again

I don’t think there's a solution here. However I slice it, there can only be one animation/trigger per element

Comment thread source/helpers/selector-observer.tsx Resolved
@fregante
fregante marked this pull request as ready for review August 5, 2022 07:27
@fregante

fregante commented Aug 5, 2022

Copy link
Copy Markdown
Member Author

Ok I found another solution for the issue I described, which also covers the possible issue I mentioned earlier:

If the WeakSet doesn't match existing elements, the function will also need to attach a temporary class because I removed our custom :not(rgh-etc) selectors

In short, like before, once the element is seen, it's marked as seen via class for the specific callback. So if a future selector is matched, the animation is started again.

The only situation where this workaround fails is on elements that have a permanent/native animation, which GitHub uses sparingly. I think that's an acceptable risk at this point.

@fregante

fregante commented Aug 5, 2022

Copy link
Copy Markdown
Member Author

I will merge this PR this weekend, reviews welcome before then.

Comment thread source/helpers/selector-observer.tsx Resolved
🎉  This is no longer accepted:

```
const cb = (a: HTMLParagraphElement) => {
	console.log(a);
}

observe('a', cb);
```
animation: 1ms ${animation};
}
`);
getTag().append(rule);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here's the style tag update in action, as I navigate GitHub:

Screen.Recording.4.mov

@kidonng

kidonng commented Aug 16, 2022

Copy link
Copy Markdown
Member
  • it doesn't matter what animation is triggered, the observer just needs any animation to start in order to find the element

Brilliant! I think it's an acceptable cost having to do matches and classList.add ourselves, for such a simple solution. Native animations not added by the observer are also sparse so not much performance is wasted on checking them.

The only situation where this workaround fails is on elements that have a permanent/native animation, which GitHub uses sparingly. I think that's an acceptable risk at this point.

Agreed, I don't think any of our targets are using animations. In the future we can also use @layer to avoid breaking existing animations, if any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Wrap selector-observer to add AbortSignal, better types, etc

2 participants