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

Make reading element dimensions work regardless of transforms #633

Merged

Conversation

javiergonzalezGenially
Copy link
Contributor

@javiergonzalezGenially javiergonzalezGenially commented Nov 16, 2023

When using the carousel inside a container with a scale transform applied the carousel doesn't work properly because it uses to measure nodes getBoundingClientRect, which returns a rect which is already scaled.
The proposed fix (tested working in the playground) uses instead offsetLeft/offsetWidth etc, which are unscaled.

@javiergonzalezGenially javiergonzalezGenially changed the title fix when container is scaled fix when the container has a scale transform Nov 16, 2023
@davidjerleke
Copy link
Owner

davidjerleke commented Nov 16, 2023

Hi @javiergonzalezGenially,

A friendly reminder: Please start a new discussion before starting any work to avoid doing unnecessary work.

Thank you for your contribution! I don't think this approach will work with a RTL setup? It needs to be modified to work with that. Basically we need to create our own artificial offsetRight value.

Best,
David

@javiergonzalezGenially
Copy link
Contributor Author

javiergonzalezGenially commented Nov 16, 2023

Sorry, I didn't know a discussion was necessary.

I just tried adding dir="rtl" to the node: <div class="embla" dir="rtl"> and adding the option direction: 'rtl' and it seems to work correctly. Is there anything else I need to check?

@davidjerleke
Copy link
Owner

Thanks @javiergonzalezGenially,

One more thing I have in mind at the time of writing: Have you checked if all tests in embla-carousel work as expected? Or do we need to update them?

Best,
David

@davidjerleke davidjerleke added the feature request New feature or request label Nov 16, 2023
@davidjerleke davidjerleke changed the title fix when the container has a scale transform Make reading element dimensions work regardless of transforms Nov 16, 2023
@davidjerleke
Copy link
Owner

Sorry, I didn't know a discussion was necessary.

@javiergonzalezGenially I have to update the contribution guidelines but the reason is actually there for you. Now this PR/initiative is great and is actually on my TODO, but sometimes devs do significant amount of work on something that isn't aligned with the Embla vision without starting a discussion. So they practically waste their time which could be avoided with a discussion 🙂.

@javiergonzalezGenially
Copy link
Contributor Author

@davidjerleke just made a commit that fixed the unit tests :)

@davidjerleke
Copy link
Owner

Thanks a ton @javiergonzalezGenially. I will test this as soon as possible. Thank you very much for contributing ⭐!

@davidjerleke davidjerleke self-assigned this Nov 16, 2023
@javiergonzalezGenially
Copy link
Contributor Author

Thanks to you for creating the lib! I don't want to seem too demanding, but if you could release a rc with the fix at some point would be really appreciated ;)

@davidjerleke
Copy link
Owner

davidjerleke commented Nov 17, 2023

@javiergonzalezGenially sure. I will release an RC after I’ve tested this and I’ve reviewed all code. I think this is a great feature to test with RC before the stable v8 release.

@davidjerleke
Copy link
Owner

@javiergonzalezGenially I will have to change some stuff on this PR before we can merge this because I found cases where the measurements fail. Especially when setting containScroll: false.

It seems to be caused by the fact that getBoundingClientRect().left returns the left distance relative to the document while node.offsetLeft gives us the left distance relative to its parent. The same goes for the top values.

Best,
David

@javiergonzalezGenially
Copy link
Contributor Author

@davidjerleke sure, maybe you can use https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent to get the relative parent element and sum all the offsets until that offsetParent returns null?

@davidjerleke
Copy link
Owner

@javiergonzalezGenially thanks for the idea! I’m looking into possible solutions. I have to take bundle size, performance, readability and complexity into account so will try to find a good solution.

@javiergonzalezGenially
Copy link
Contributor Author

javiergonzalezGenially commented Nov 20, 2023

Sure, I wasn't talking of anything complex, just something like:

function getDocumentOffset(element) {
  let top = 0, left = 0;
  let curElement = element;
  while (curElement) {
    top += curElement.offsetTop;
    left += curElement.offsetLeft;
    curElement = curElement.offsetParent;
  }
  return { offsetTop: top, offsetLeft: left, offsetWidth: element.offsetWidth, offsetHeight: element.offsetHeight };
}

After that it should be a matter of changing getNodeRect to do

const { offsetTop, offsetLeft, offsetWidth, offsetHeight } = getDocumentOffset(node)

instead of

const { offsetTop, offsetLeft, offsetWidth, offsetHeight } = node

@davidjerleke
Copy link
Owner

@javiergonzalezGenially thanks for the code snippet. I probably wasn’t clear: I didn’t mean that your suggestion was complex, I just wanted to explain that I want to consider more than one solution, what I will be looking into and their pros and cons.

Sorry if I wasn’t clear 🙂.

Cheers!

@javiergonzalezGenially
Copy link
Contributor Author

Ah! sure, no problem :)

@davidjerleke
Copy link
Owner

@javiergonzalezGenially I think this is ready to merge. I will let you know when the experimental RC is out which will be v8.0.0-rc15.

@davidjerleke
Copy link
Owner

davidjerleke commented Nov 25, 2023

@davidjerleke davidjerleke linked an issue Nov 25, 2023 that may be closed by this pull request
1 task
@davidjerleke davidjerleke added the core This is related to the core package label Nov 27, 2023
@davidjerleke
Copy link
Owner

@davidjerleke davidjerleke merged commit c528a98 into davidjerleke:master Nov 28, 2023
@davidjerleke davidjerleke added the resolved This issue is resolved label Nov 28, 2023
@davidjerleke
Copy link
Owner

davidjerleke commented Nov 28, 2023

@javiergonzalezGenially I've released v8.0.0-rc15 which includes this PR.

@javiergonzalezGenially
Copy link
Contributor Author

Thanks a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core This is related to the core package feature request New feature or request resolved This issue is resolved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make reading element dimensions work regardless of transforms
2 participants