@ionic/core
Version:
Base components for Ionic
4 lines • 10.1 kB
JavaScript
/*!
* (C) Ionic http://ionicframework.com - MIT License
*/
import{d as t,H as s,i as e,w as i,h as o,f as n,t as r}from"./p-Ct2aBEue.js";import{r as a}from"./p-B4IBdPVJ.js";import{i as h}from"./p-Dojwmvde.js";import{c as l,h as c}from"./p-byZM6qHV.js";import{b as d}from"./p-HthJZjZ6.js";const u=t(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.ionChange=e(this,"ionChange",7),this.ionSelect=e(this,"ionSelect",7),this.ionStyle=e(this,"ionStyle",7),this.segmentViewEl=null,this.activated=!1,this.disabled=!1,this.scrollable=!1,this.swipeGesture=!0,this.selectOnFocus=!1,this.onClick=t=>{const s=t.target,e=this.checked;"ION-SEGMENT"!==s.tagName&&(this.value=s.value,s!==e&&this.emitValueChange(),this.segmentViewEl?(this.updateSegmentView(),this.scrollable&&e&&this.checkButton(e,s)):!this.scrollable&&this.swipeGesture||(e?this.checkButton(e,s):this.setCheckedClasses()))},this.onSlottedItemsChange=()=>{this.valueChanged(this.value)},this.getSegmentButton=t=>{const s=this.getButtons().filter((t=>!t.disabled)),e=s.findIndex((t=>t===document.activeElement));switch(t){case"first":return s[0];case"last":return s[s.length-1];case"next":return s[e+1]??s[0];case"previous":return s[e-1]??s[s.length-1];default:return null}}}colorChanged(t,s){(void 0===s&&void 0!==t||void 0!==s&&void 0===t)&&this.emitStyle()}swipeGestureChanged(){this.gestureChanged()}valueChanged(t,s){if(this.segmentViewEl&&void 0===t)this.value=this.getButtons()[0].value;else{if(void 0!==s&&void 0!==t){const e=this.getButtons(),i=e.find((t=>t.value===s)),o=e.find((s=>s.value===t));i&&o&&(this.segmentViewEl?!1!==this.triggerScrollOnValueChange&&this.updateSegmentView():this.checkButton(i,o))}else void 0!==t&&void 0===s&&this.segmentViewEl&&this.updateSegmentView();this.ionSelect.emit({value:t}),this.segmentViewEl||this.scrollActiveButtonIntoView(),this.triggerScrollOnValueChange=void 0}}disabledChanged(){if(this.gestureChanged(),this.segmentViewEl)this.segmentViewEl.disabled=this.disabled;else{const t=this.getButtons();for(const s of t)s.disabled=this.disabled}}gestureChanged(){this.gesture&&this.gesture.enable(!this.scrollable&&!this.disabled&&this.swipeGesture)}connectedCallback(){this.emitStyle(),this.segmentViewEl=this.getSegmentView()}disconnectedCallback(){this.segmentViewEl=null}componentWillLoad(){this.emitStyle()}async componentDidLoad(){this.segmentViewEl=this.getSegmentView(),this.setCheckedClasses(),a((()=>{this.scrollActiveButtonIntoView(!1)})),this.gesture=(await import("./p-D0YpjgON.js")).createGesture({el:this.el,gestureName:"segment",gesturePriority:100,threshold:0,passive:!1,onStart:t=>this.onStart(t),onMove:t=>this.onMove(t),onEnd:t=>this.onEnd(t)}),this.gestureChanged(),this.disabled&&this.disabledChanged(),this.updateSegmentView(!1)}onStart(t){this.valueBeforeGesture=this.value,this.activate(t)}onMove(t){this.setNextIndex(t)}onEnd(t){this.setActivated(!1),this.setNextIndex(t,!0),t.event.stopImmediatePropagation();const s=this.value;void 0!==s&&this.valueBeforeGesture!==s&&(this.emitValueChange(),this.updateSegmentView()),this.valueBeforeGesture=void 0}emitValueChange(){const{value:t}=this;this.ionChange.emit({value:t})}getButtons(){return Array.from(this.el.querySelectorAll("ion-segment-button"))}get checked(){return this.getButtons().find((t=>t.value===this.value))}setActivated(t){this.getButtons().forEach((s=>{s.classList.toggle("segment-button-activated",t)})),this.activated=t}activate(t){const s=t.event.target,e=this.getButtons().find((t=>t.value===this.value));"ION-SEGMENT-BUTTON"===s.tagName&&(e||(this.value=s.value,this.setCheckedClasses()),this.value===s.value&&this.setActivated(!0))}getIndicator(t){return(t.shadowRoot||t).querySelector(".segment-button-indicator")}checkButton(t,s){const e=this.getIndicator(t),o=this.getIndicator(s);if(null===e||null===o)return;const n=e.getBoundingClientRect(),r=o.getBoundingClientRect(),a=`translate3d(${n.left-r.left}px, 0, 0) scaleX(${n.width/r.width})`;i((()=>{o.classList.remove("segment-button-indicator-animated"),o.style.setProperty("transform",a),o.getBoundingClientRect(),o.classList.add("segment-button-indicator-animated"),o.style.setProperty("transform",""),this.scrollActiveButtonIntoView(!0)})),this.value=s.value,this.setCheckedClasses()}setCheckedClasses(){const t=this.getButtons(),s=t.findIndex((t=>t.value===this.value))+1;for(const s of t)s.classList.remove("segment-button-after-checked");s<t.length&&t[s].classList.add("segment-button-after-checked")}getSegmentView(){const t=this.getButtons().find((t=>t.contentId)),s=document.querySelector(`ion-segment-content[id="${t?.contentId}"]`);return s?.closest("ion-segment-view")}handleSegmentViewScroll(t){const{scrollRatio:s,isManualScroll:e}=t.detail;if(!e)return;const i=t.target,o=this.segmentViewEl,n=this.el;if(t.composedPath().includes(o)||i?.contains(n)){const t=this.getButtons();if(!t.length)return;const e=t.findIndex((t=>t.value===this.value)),i=t[e],o=Math.round(s*(t.length-1));if(t[o]?.disabled)return;void 0!==this.lastNextIndex&&this.lastNextIndex===o||(this.lastNextIndex=o,this.triggerScrollOnValueChange=!1,this.checkButton(i,t[o]),this.emitValueChange())}}updateSegmentView(t=!0){const s=this.getButtons().find((t=>t.value===this.value));if(!s?.contentId)return;const e=this.segmentViewEl;e&&e.setContent(s.contentId,t)}scrollActiveButtonIntoView(t=!0){const{scrollable:s,value:e,el:i}=this;if(s){const s=this.getButtons().find((t=>t.value===e));if(void 0!==s){const e=i.getBoundingClientRect(),o=s.getBoundingClientRect();i.scrollTo({top:0,left:i.scrollLeft+(o.x-e.x-e.width/2+o.width/2),behavior:t?"smooth":"instant"})}}}setNextIndex(t,s=!1){const e=h(this.el),i=this.activated,o=this.getButtons(),n=o.findIndex((t=>t.value===this.value)),r=o[n];let a,l;if(-1===n)return;const c=r.getBoundingClientRect(),d=c.left,u=c.width,m=t.currentX,g=c.top+c.height/2,b=this.el.getRootNode().elementFromPoint(m,g);if(i&&!s){if(e?m>d+u:m<d){const t=n-1;t>=0&&(l=t)}else if((e?m<d:m>d+u)&&i&&!s){const t=n+1;t<o.length&&(l=t)}void 0===l||o[l].disabled||(a=o[l])}if(!i&&s&&(a=b),null!=a){if("ION-SEGMENT"===a.tagName)return!1;r!==a&&this.checkButton(r,a)}return!0}emitStyle(){this.ionStyle.emit({segment:!0})}onKeyDown(t){const s=h(this.el);let e,i=this.selectOnFocus;switch(t.key){case"ArrowRight":t.preventDefault(),e=this.getSegmentButton(s?"previous":"next");break;case"ArrowLeft":t.preventDefault(),e=this.getSegmentButton(s?"next":"previous");break;case"Home":t.preventDefault(),e=this.getSegmentButton("first");break;case"End":t.preventDefault(),e=this.getSegmentButton("last");break;case" ":case"Enter":t.preventDefault(),e=document.activeElement,i=!0}if(e){if(i){const t=this.checked;this.checkButton(t||e,e),e!==t&&this.emitValueChange()}e.setFocus()}}render(){const t=d(this);return o(n,{key:"f74bd72f10f966129528497aa47b2c683a62d59e",role:"tablist",onClick:this.onClick,class:l(this.color,{[t]:!0,"in-toolbar":c("ion-toolbar",this.el),"in-toolbar-color":c("ion-toolbar[color]",this.el),"segment-activated":this.activated,"segment-disabled":this.disabled,"segment-scrollable":this.scrollable})},o("slot",{key:"79b0c6cce9d83d147b190ae5d77df7de29a6b42d",onSlotchange:this.onSlottedItemsChange}))}get el(){return this}static get watchers(){return{color:[{colorChanged:0}],swipeGesture:[{swipeGestureChanged:0}],value:[{valueChanged:0}],disabled:[{disabledChanged:0}]}}static get style(){return{ios:":host{--ripple-color:currentColor;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;display:grid;grid-auto-columns:1fr;position:relative;-ms-flex-align:stretch;align-items:stretch;-ms-flex-pack:center;justify-content:center;width:100%;background:var(--background);font-family:var(--ion-font-family, inherit);text-align:center;contain:paint;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}:host(.segment-scrollable){-ms-flex-pack:start;justify-content:start;width:auto;overflow-x:auto;grid-auto-columns:minmax(-webkit-min-content, 1fr);grid-auto-columns:minmax(min-content, 1fr)}:host(.segment-scrollable::-webkit-scrollbar){display:none}:host{--background:rgba(var(--ion-text-color-rgb, 0, 0, 0), 0.065);border-radius:8px;overflow:hidden;z-index:0}:host(.ion-color){background:rgba(var(--ion-color-base-rgb), 0.065)}:host(.in-toolbar){-webkit-margin-start:auto;margin-inline-start:auto;-webkit-margin-end:auto;margin-inline-end:auto;margin-top:0;margin-bottom:0;width:auto}:host(.in-toolbar:not(.ion-color)){background:var(--ion-toolbar-segment-background, var(--background))}:host(.in-toolbar-color:not(.ion-color)){background:rgba(var(--ion-color-contrast-rgb), 0.11)}",md:":host{--ripple-color:currentColor;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased;display:grid;grid-auto-columns:1fr;position:relative;-ms-flex-align:stretch;align-items:stretch;-ms-flex-pack:center;justify-content:center;width:100%;background:var(--background);font-family:var(--ion-font-family, inherit);text-align:center;contain:paint;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}:host(.segment-scrollable){-ms-flex-pack:start;justify-content:start;width:auto;overflow-x:auto;grid-auto-columns:minmax(-webkit-min-content, 1fr);grid-auto-columns:minmax(min-content, 1fr)}:host(.segment-scrollable::-webkit-scrollbar){display:none}:host{--background:transparent;grid-auto-columns:minmax(auto, 360px)}:host(.in-toolbar){min-height:var(--min-height)}:host(.segment-scrollable) ::slotted(ion-segment-button){min-width:auto}"}}},[289,"ion-segment",{color:[513],disabled:[4],scrollable:[4],swipeGesture:[4,"swipe-gesture"],value:[1032],selectOnFocus:[4,"select-on-focus"],activated:[32]},[[16,"ionSegmentViewScroll","handleSegmentViewScroll"],[0,"keydown","onKeyDown"]],{color:[{colorChanged:0}],swipeGesture:[{swipeGestureChanged:0}],value:[{valueChanged:0}],disabled:[{disabledChanged:0}]}]),m=u,g=function(){"undefined"!=typeof customElements&&["ion-segment"].forEach((t=>{"ion-segment"===t&&(customElements.get(r(t))||customElements.define(r(t),u))}))};export{m as IonSegment,g as defineCustomElement}