chunk.764ZPE4Y.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {
  2. clamp
  3. } from "./chunk.T56CG5BM.js";
  4. import {
  5. i
  6. } from "./chunk.DA3UDEH5.js";
  7. import {
  8. event,
  9. watch
  10. } from "./chunk.XX234VRK.js";
  11. import {
  12. T,
  13. e,
  14. h,
  15. n,
  16. o,
  17. r
  18. } from "./chunk.5PIDMFOE.js";
  19. import {
  20. __decorateClass
  21. } from "./chunk.IHGPZX35.js";
  22. // _uyihdawu1:/Users/claviska/Projects/shoelace/src/components/image-comparer/image-comparer.scss
  23. var image_comparer_default = ":host {\n position: relative;\n box-sizing: border-box;\n}\n:host *, :host *:before, :host *:after {\n box-sizing: inherit;\n}\n\n[hidden] {\n display: none !important;\n}\n\n:host {\n --divider-width: 2px;\n --handle-size: 2.5rem;\n display: inline-block;\n position: relative;\n}\n\n.image-comparer {\n max-width: 100%;\n max-height: 100%;\n overflow: hidden;\n}\n\n.image-comparer__before,\n.image-comparer__after {\n pointer-events: none;\n}\n.image-comparer__before ::slotted(img),\n.image-comparer__before ::slotted(svg),\n.image-comparer__after ::slotted(img),\n.image-comparer__after ::slotted(svg) {\n display: block;\n max-width: 100% !important;\n height: auto;\n}\n\n.image-comparer__after {\n position: absolute;\n top: 0;\n left: 0;\n height: 100%;\n width: 100%;\n}\n\n.image-comparer__divider {\n display: flex;\n align-items: center;\n justify-content: center;\n position: absolute;\n top: 0;\n width: var(--divider-width);\n height: 100%;\n background-color: var(--sl-color-white);\n transform: translateX(calc(var(--divider-width) / -2));\n cursor: ew-resize;\n}\n\n.image-comparer__handle {\n display: flex;\n align-items: center;\n justify-content: center;\n position: absolute;\n top: calc(50% - (var(--handle-size) / 2));\n width: var(--handle-size);\n height: var(--handle-size);\n background-color: var(--sl-color-white);\n border-radius: var(--sl-border-radius-circle);\n font-size: calc(var(--handle-size) * 0.5);\n color: var(--sl-color-gray-500);\n cursor: inherit;\n z-index: 10;\n}\n.image-comparer__handle:focus {\n outline: none;\n box-shadow: 0 0 0 1px var(--sl-color-primary-500), 0 0 0 var(--sl-focus-ring-width) var(--sl-focus-ring-color-primary);\n}";
  24. // src/components/image-comparer/image-comparer.ts
  25. var SlImageComparer = class extends h {
  26. constructor() {
  27. super(...arguments);
  28. this.position = 50;
  29. }
  30. handleDrag(event2) {
  31. const { width } = this.base.getBoundingClientRect();
  32. function drag(event3, container, onMove) {
  33. const move = (event4) => {
  34. const dims = container.getBoundingClientRect();
  35. const defaultView = container.ownerDocument.defaultView;
  36. const offsetX = dims.left + defaultView.pageXOffset;
  37. const offsetY = dims.top + defaultView.pageYOffset;
  38. const x = (event4.changedTouches ? event4.changedTouches[0].pageX : event4.pageX) - offsetX;
  39. const y = (event4.changedTouches ? event4.changedTouches[0].pageY : event4.pageY) - offsetY;
  40. onMove(x, y);
  41. };
  42. move(event3);
  43. const stop = () => {
  44. document.removeEventListener("mousemove", move);
  45. document.removeEventListener("touchmove", move);
  46. document.removeEventListener("mouseup", stop);
  47. document.removeEventListener("touchend", stop);
  48. };
  49. document.addEventListener("mousemove", move);
  50. document.addEventListener("touchmove", move);
  51. document.addEventListener("mouseup", stop);
  52. document.addEventListener("touchend", stop);
  53. }
  54. this.handle.focus();
  55. event2.preventDefault();
  56. drag(event2, this.base, (x) => {
  57. this.position = Number(clamp(x / width * 100, 0, 100).toFixed(2));
  58. });
  59. }
  60. handleKeyDown(event2) {
  61. if (["ArrowLeft", "ArrowRight", "Home", "End"].includes(event2.key)) {
  62. const incr = event2.shiftKey ? 10 : 1;
  63. let newPosition = this.position;
  64. event2.preventDefault();
  65. if (event2.key === "ArrowLeft")
  66. newPosition = newPosition - incr;
  67. if (event2.key === "ArrowRight")
  68. newPosition = newPosition + incr;
  69. if (event2.key === "Home")
  70. newPosition = 0;
  71. if (event2.key === "End")
  72. newPosition = 100;
  73. newPosition = clamp(newPosition, 0, 100);
  74. this.position = newPosition;
  75. }
  76. }
  77. handlePositionChange() {
  78. this.slChange.emit();
  79. }
  80. render() {
  81. return T`
  82. <div part="base" class="image-comparer" @keydown=${this.handleKeyDown}>
  83. <div class="image-comparer__image">
  84. <div part="before" class="image-comparer__before">
  85. <slot name="before"></slot>
  86. </div>
  87. <div
  88. part="after"
  89. class="image-comparer__after"
  90. style=${i({ clipPath: `inset(0 ${100 - this.position}% 0 0)` })}
  91. >
  92. <slot name="after"></slot>
  93. </div>
  94. </div>
  95. <div
  96. part="divider"
  97. class="image-comparer__divider"
  98. style=${i({ left: this.position + "%" })}
  99. @mousedown=${this.handleDrag}
  100. @touchstart=${this.handleDrag}
  101. >
  102. <div
  103. part="handle"
  104. class="image-comparer__handle"
  105. role="scrollbar"
  106. aria-valuenow=${this.position}
  107. aria-valuemin="0"
  108. aria-valuemax="100"
  109. tabindex="0"
  110. >
  111. <slot name="handle-icon">
  112. <sl-icon class="image-comparer__handle-icon" name="grip-vertical" library="system"></sl-icon>
  113. </slot>
  114. </div>
  115. </div>
  116. </div>
  117. `;
  118. }
  119. };
  120. SlImageComparer.styles = r(image_comparer_default);
  121. __decorateClass([
  122. o(".image-comparer")
  123. ], SlImageComparer.prototype, "base", 2);
  124. __decorateClass([
  125. o(".image-comparer__handle")
  126. ], SlImageComparer.prototype, "handle", 2);
  127. __decorateClass([
  128. e({ type: Number, reflect: true })
  129. ], SlImageComparer.prototype, "position", 2);
  130. __decorateClass([
  131. event("sl-change")
  132. ], SlImageComparer.prototype, "slChange", 2);
  133. __decorateClass([
  134. watch("position")
  135. ], SlImageComparer.prototype, "handlePositionChange", 1);
  136. SlImageComparer = __decorateClass([
  137. n("sl-image-comparer")
  138. ], SlImageComparer);
  139. var image_comparer_default2 = SlImageComparer;
  140. export {
  141. image_comparer_default2 as image_comparer_default
  142. };