Closed tolu closed 1 year ago
Thank you very much for the PR and your input on this!
I've added some suggestions to modify your implementation to be more in line with other core-components using MutationObserver in a similar manner. Give me a heads-up if you have questions.
Ref https://github.com/nrkno/core-components/issues/637 I suspect running npm run test
may fail, unfortunately, but running npm run lint
should give you some hints to conform to the standardjs codestyle used across the repo.
If you're up for making one, it would be awesome to have a test-case to cover this scenario.
thanks for the feedback, completely agree on all accounts 😅
I'll update, fix and add a test 👍
@skjalgepalg edits made, test added 🙏
I was a little fast on the trigger there I noticed (forgot to fix all suggestions) should be in order now. Thanks for all the good feedback 🙏
just let me know if there is anything else 🙏
Have given this some more thought and suggest we address the following:
scroll.change
when DOM is manipulated feels misleading to the intention of that event.
scroll.DOMChange
(very open for input on the name) event when a MutationRecord is type childlist
. scroll.change
in onConnected, but further updates to DOM will trigger scroll.DOMChangescroll.DOMChange
to customEvents
in core-scroll.jsx gives us onScrollDOMChange
for React use-cases.Examples in docs on static.nrk.no could use a touch-up, will add an example of DOM changes similar to what we use in core-toggle and move more work on that side to a separate issue.
I have solutions to the above that I can (maybe?) commit to this PR branch, or possibly as a PR to your fork @tolu, what would be most convenient for you?
Edit: Opted to just add to the PR branch to keep from fragmenting the discussion more than necessary. Hope that's ok
I can understand what you mean but the fact that scrollLeft
and scrollRight
do change when children are added/removed makes me think dispatching scroll.change
is really not misleading by any stretch 🤔
Adding a separate event also adds complexity for consumers and so increases the chance to make mistakes...
It is of course up to you but I'd go for simplicity over correctness in this case
Very good point with the complexity part. Reverted the scroll.DOMChange
-event, while keeping the handling to the onDOMChange
function. Restored the test as well
Adding children to a
<CoreScroll>
after the component has rendered (becauseasync
) does not trigger thescroll.change
event and subsequently (forjsx
anyway) does not set the correct state for the scroll buttons (given the example implementation seen below).Currently
scroll.change
is triggered on first render, when bothscrollLeft
andscrollLeft
is 0 resulting in not displaying any scroll-buttons if the child list is empty. This PR adds a mutation observer that triggerthis.handleEvent
so that buttons can be re-initialised.Another solution is of course to solve this from the outside using a
ref
. I do feel like this behavior (dispatchingscroll.change
) when children change is a better solution since it solves all use cases where children can be dynamically added or removed.I'm happy to update the PR with all or any requested changes 🙌