chunk.XAZN5AQ5.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // src/internal/offset.ts
  2. function getOffset(element, parent) {
  3. return {
  4. top: Math.round(element.getBoundingClientRect().top - parent.getBoundingClientRect().top),
  5. left: Math.round(element.getBoundingClientRect().left - parent.getBoundingClientRect().left)
  6. };
  7. }
  8. // src/internal/scroll.ts
  9. var locks = new Set();
  10. function lockBodyScrolling(lockingEl) {
  11. locks.add(lockingEl);
  12. document.body.classList.add("sl-scroll-lock");
  13. }
  14. function unlockBodyScrolling(lockingEl) {
  15. locks.delete(lockingEl);
  16. if (locks.size === 0) {
  17. document.body.classList.remove("sl-scroll-lock");
  18. }
  19. }
  20. function scrollIntoView(element, container, direction = "vertical", behavior = "smooth") {
  21. const offset = getOffset(element, container);
  22. const offsetTop = offset.top + container.scrollTop;
  23. const offsetLeft = offset.left + container.scrollLeft;
  24. const minX = container.scrollLeft;
  25. const maxX = container.scrollLeft + container.offsetWidth;
  26. const minY = container.scrollTop;
  27. const maxY = container.scrollTop + container.offsetHeight;
  28. if (direction === "horizontal" || direction === "both") {
  29. if (offsetLeft < minX) {
  30. container.scrollTo({ left: offsetLeft, behavior });
  31. } else if (offsetLeft + element.clientWidth > maxX) {
  32. container.scrollTo({ left: offsetLeft - container.offsetWidth + element.clientWidth, behavior });
  33. }
  34. }
  35. if (direction === "vertical" || direction === "both") {
  36. if (offsetTop < minY) {
  37. container.scrollTo({ top: offsetTop, behavior });
  38. } else if (offsetTop + element.clientHeight > maxY) {
  39. container.scrollTo({ top: offsetTop - container.offsetHeight + element.clientHeight, behavior });
  40. }
  41. }
  42. }
  43. export {
  44. getOffset,
  45. lockBodyScrolling,
  46. unlockBodyScrolling,
  47. scrollIntoView
  48. };