ion-segment-view.entry.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { r as registerInstance, c as createEvent, h, e as Host, f as getElement } from './index-527b9e34.js';
  5. const segmentViewIosCss = ":host{display:-ms-flexbox;display:flex;height:100%;overflow-x:scroll;-webkit-scroll-snap-type:x mandatory;-ms-scroll-snap-type:x mandatory;scroll-snap-type:x mandatory;scrollbar-width:none;-ms-overflow-style:none}:host::-webkit-scrollbar{display:none}:host(.segment-view-disabled){-ms-touch-action:none;touch-action:none;overflow-x:hidden}:host(.segment-view-scroll-disabled){pointer-events:none}:host(.segment-view-disabled){opacity:0.3}";
  6. const IonSegmentViewIosStyle0 = segmentViewIosCss;
  7. const segmentViewMdCss = ":host{display:-ms-flexbox;display:flex;height:100%;overflow-x:scroll;-webkit-scroll-snap-type:x mandatory;-ms-scroll-snap-type:x mandatory;scroll-snap-type:x mandatory;scrollbar-width:none;-ms-overflow-style:none}:host::-webkit-scrollbar{display:none}:host(.segment-view-disabled){-ms-touch-action:none;touch-action:none;overflow-x:hidden}:host(.segment-view-scroll-disabled){pointer-events:none}:host(.segment-view-disabled){opacity:0.3}";
  8. const IonSegmentViewMdStyle0 = segmentViewMdCss;
  9. const SegmentView = class {
  10. constructor(hostRef) {
  11. registerInstance(this, hostRef);
  12. this.ionSegmentViewScroll = createEvent(this, "ionSegmentViewScroll", 7);
  13. this.scrollEndTimeout = null;
  14. this.isTouching = false;
  15. this.disabled = false;
  16. this.isManualScroll = undefined;
  17. }
  18. handleScroll(ev) {
  19. var _a;
  20. const { scrollLeft, scrollWidth, clientWidth } = ev.target;
  21. const scrollRatio = scrollLeft / (scrollWidth - clientWidth);
  22. this.ionSegmentViewScroll.emit({
  23. scrollRatio,
  24. isManualScroll: (_a = this.isManualScroll) !== null && _a !== void 0 ? _a : true,
  25. });
  26. // Reset the timeout to check for scroll end
  27. this.resetScrollEndTimeout();
  28. }
  29. /**
  30. * Handle touch start event to know when the user is actively dragging the segment view.
  31. */
  32. handleScrollStart() {
  33. if (this.scrollEndTimeout) {
  34. clearTimeout(this.scrollEndTimeout);
  35. this.scrollEndTimeout = null;
  36. }
  37. this.isTouching = true;
  38. }
  39. /**
  40. * Handle touch end event to know when the user is no longer dragging the segment view.
  41. */
  42. handleTouchEnd() {
  43. this.isTouching = false;
  44. }
  45. /**
  46. * Reset the scroll end detection timer. This is called on every scroll event.
  47. */
  48. resetScrollEndTimeout() {
  49. if (this.scrollEndTimeout) {
  50. clearTimeout(this.scrollEndTimeout);
  51. this.scrollEndTimeout = null;
  52. }
  53. this.scrollEndTimeout = setTimeout(() => {
  54. this.checkForScrollEnd();
  55. },
  56. // Setting this to a lower value may result in inconsistencies in behavior
  57. // across browsers (particularly Firefox).
  58. // Ideally, all of this logic is removed once the scroll end event is
  59. // supported on all browsers (https://caniuse.com/?search=scrollend)
  60. 100);
  61. }
  62. /**
  63. * Check if the scroll has ended and the user is not actively touching.
  64. * If the conditions are met (active content is enabled and no active touch),
  65. * reset the scroll position and emit the scroll end event.
  66. */
  67. checkForScrollEnd() {
  68. // Only emit scroll end event if the active content is not disabled and
  69. // the user is not touching the segment view
  70. if (!this.isTouching) {
  71. this.isManualScroll = undefined;
  72. }
  73. }
  74. /**
  75. * @internal
  76. *
  77. * This method is used to programmatically set the displayed segment content
  78. * in the segment view. Calling this method will update the `value` of the
  79. * corresponding segment button.
  80. *
  81. * @param id: The id of the segment content to display.
  82. * @param smoothScroll: Whether to animate the scroll transition.
  83. */
  84. async setContent(id, smoothScroll = true) {
  85. const contents = this.getSegmentContents();
  86. const index = contents.findIndex((content) => content.id === id);
  87. if (index === -1)
  88. return;
  89. this.isManualScroll = false;
  90. this.resetScrollEndTimeout();
  91. const contentWidth = this.el.offsetWidth;
  92. this.el.scrollTo({
  93. top: 0,
  94. left: index * contentWidth,
  95. behavior: smoothScroll ? 'smooth' : 'instant',
  96. });
  97. }
  98. getSegmentContents() {
  99. return Array.from(this.el.querySelectorAll('ion-segment-content'));
  100. }
  101. render() {
  102. const { disabled, isManualScroll } = this;
  103. return (h(Host, { key: 'fa528d2d9ae0f00fc3067defe2a047dce77c814a', class: {
  104. 'segment-view-disabled': disabled,
  105. 'segment-view-scroll-disabled': isManualScroll === false,
  106. } }, h("slot", { key: '74dc8b4d073caeff1bab272d11b9ea3e1a215954' })));
  107. }
  108. get el() { return getElement(this); }
  109. };
  110. SegmentView.style = {
  111. ios: IonSegmentViewIosStyle0,
  112. md: IonSegmentViewMdStyle0
  113. };
  114. export { SegmentView as ion_segment_view };