@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
3 lines (2 loc) • 13.4 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import{a as P,b as H}from"./I3D4XCLD.js";import{a as T}from"./3PITBECR.js";import{a as C}from"./S5D4KC2T.js";import{a as m,c as u,d as k,e as S,f as A,g as n,h as O}from"./D26KXFR4.js";import{a as x}from"./DCQRXTMY.js";import{a as D}from"./IJ3XVWSG.js";import"./AP2LDRUV.js";import"./WKR7R7AZ.js";import{a as f}from"./YGX7CUXN.js";import{a as h,b as d}from"./ENDXE7YY.js";import{a as E}from"./D7Q3AXKP.js";import"./RH6ZIP65.js";import{e as y}from"./YJF4NBEX.js";import"./TQRVWHPS.js";import{C as p}from"./KDWR7M23.js";import"./OAOQ5BXS.js";import{F as a,G as I,H as o,S as g,g as b,h as c,p as r}from"./C4ZX7VYR.js";var $={trigger:"trigger"},v={content:"content",wrapper:"wrapper",triggerContainer:"trigger-container"},z=b`:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:inline-block}.wrapper{inline-size:max-content;display:none;max-inline-size:100vw;max-block-size:100vh;inset-block-start:0;left:0;z-index:var(--calcite-floating-ui-z-index)}-style{.wrapper{opacity:0;inset-block-start:0;left:0}}:host([top-layer-disabled]) .wrapper{--calcite-floating-ui-z-index: var(--calcite-z-index-dropdown)}.wrapper[popover]{padding:0;margin:0;border:none;background-color:transparent;overflow:visible;display:none}.wrapper:popover-open{display:block}.wrapper .calcite-floating-ui-anim{position:relative;transition-duration:var(--calcite-floating-ui-transition);transition-property:inset-block-start,left,opacity,display;transition-behavior:allow-discrete;opacity:0;box-shadow:0 0 16px #00000029;z-index:var(--calcite-z-index);border-radius:.25rem}.wrapper[data-placement^=bottom] .calcite-floating-ui-anim{inset-block-start:-5px}.wrapper[data-placement^=top] .calcite-floating-ui-anim{inset-block-start:5px}.wrapper[data-placement^=left] .calcite-floating-ui-anim{left:5px}.wrapper[data-placement^=right] .calcite-floating-ui-anim{left:-5px}.wrapper[data-placement] .calcite-floating-ui-anim--active{opacity:1;inset-block-start:0;left:0}-style{.wrapper[data-placement] .calcite-floating-ui-anim--active{opacity:0}}.content{max-height:45vh;width:auto;overflow-y:auto;overflow-x:hidden;inline-size:var(--calcite-dropdown-width, var(--calcite-internal-dropdown-width));background-color:var(--calcite-dropdown-background-color, var(--calcite-color-foreground-1))}.trigger-container{position:relative;display:flex;height:100%;flex:1 1 auto;word-wrap:break-word;word-break:break-word}.width-s{--calcite-internal-dropdown-width: 12rem}.width-m{--calcite-internal-dropdown-width: 14rem}.width-l{--calcite-internal-dropdown-width: 16rem}{:host([open]) .wrapper{border:var(--calcite-border-width-sm) solid canvasText}}:host([hidden]){display:none}[hidden]{display:none}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}.interaction-container{display:contents}`,M=H({click:!0,hover:!0}),w=class extends I{constructor(){super(),this.referenceElementController=P({manager:M})(this),this.direction=y(),this.focusLastDropdownItem=!1,this.activeItemIndex=-1,this.groups=[],this.items=[],this.mutationObserver=h("mutation",()=>this.updateItems()),this.transitionProp="opacity",this.resizeObserver=h("resize",e=>this.resizeObserverCallback(e)),this.onReferenceElementKeyDown=e=>this.keyDownHandler(e),this.focusSetter=E()(this),this.interactiveContainer=D(this),this.topLayer=C({target:()=>this.floatingEl})(this),this.closeOnSelectDisabled=!1,this.disabled=!1,this.maxItems=0,this.offsetDistance=0,this.offsetSkidding=0,this.open=!1,this.overlayPositioning="absolute",this.placement=m,this.scale="m",this.selectedItems=[],this.topLayerDisabled=!1,this.type="click",this.calciteDropdownBeforeClose=r({cancelable:!1}),this.calciteDropdownBeforeOpen=r({cancelable:!1}),this.calciteDropdownClose=r({cancelable:!1}),this.calciteDropdownOpen=r({cancelable:!1}),this.calciteDropdownSelect=r({cancelable:!1}),this.listenOn(window,"click",this.closeCalciteDropdownOnClick),this.listenOn(window,"calciteDropdownOpen",this.closeCalciteDropdownOnOpenEvent),this.listen("pointerenter",this.pointerEnterHandler),this.listen("pointerleave",this.pointerLeaveHandler),this.listen("calciteInternalDropdownItemSelect",this.handleItemSelect)}static{this.properties={activeDescendantElement:[16,{},{state:!0}],referenceEl:[16,{},{state:!0}],closeOnSelectDisabled:[7,{},{reflect:!0,type:Boolean}],disabled:[7,{},{reflect:!0,type:Boolean}],flipPlacements:[0,{},{attribute:!1}],maxItems:[11,{},{reflect:!0,type:Number}],offsetDistance:[11,{},{type:Number,reflect:!0}],offsetSkidding:[11,{},{reflect:!0,type:Number}],open:[7,{},{reflect:!0,type:Boolean}],overlayPositioning:[3,{},{reflect:!0}],placement:[3,{},{reflect:!0}],referenceElement:1,scale:[3,{},{reflect:!0}],selectedItems:[0,{},{attribute:!1}],topLayerDisabled:[7,{},{reflect:!0,type:Boolean}],type:[3,{},{reflect:!0}],widthScale:[3,{},{reflect:!0}],width:[3,{},{reflect:!0}]}}static{this.shadowRootOptions={mode:"open",delegatesFocus:!0}}static{this.styles=z}get referenceElementType(){return this.referenceElement?this.type:null}async reposition(e=!1){let{filteredFlipPlacements:t,floatingEl:i,offsetDistance:s,offsetSkidding:l,overlayPositioning:L,placement:F,referenceEl:B}=this;return S(this,{direction:this.direction,floatingEl:i,referenceEl:B,offsetDistance:s,offsetSkidding:l,overlayPositioning:L,placement:F,flipPlacements:t,type:"menu"},e)}async setFocus(e){return this.focusSetter(()=>this.referenceEl instanceof HTMLElement?this.referenceEl:this.floatingEl,e)}connectedCallback(){super.connectedCallback(),this.mutationObserver?.observe(this.el,{childList:!0,subtree:!0}),this.setFilteredPlacements(),this.updateItems(),n(this)}willUpdate(e){e.has("open")&&(this.hasUpdated||this.open!==!1)&&this.openHandler(),e.has("disabled")&&(this.hasUpdated||this.disabled!==!1)&&this.handleDisabledChange(this.disabled),e.has("flipPlacements")&&this.flipPlacementsHandler(),e.has("maxItems")&&this.hasUpdated&&this.setMaxScrollerHeight(),this.hasUpdated&&(e.has("offsetDistance")&&this.offsetDistance!==0||e.has("offsetSkidding")&&this.offsetSkidding!==0||e.has("overlayPositioning")&&this.overlayPositioning!=="absolute"||e.has("placement")&&this.placement!==m)&&this.reposition(!0),e.has("scale")&&(this.hasUpdated||this.scale!=="m")&&this.handlePropsChange(),e.has("referenceElement")&&!this.referenceElement&&this.open&&this.topLayer.hide()}updated(e){e.has("referenceEl")&&this.referenceElementType&&n(this)}loaded(){this.updateSelectedItems(),n(this)}disconnectedCallback(){super.disconnectedCallback(),this.mutationObserver?.disconnect(),this.resizeObserver?.disconnect(),O(this)}openHandler(){this.disabled||(x(this),this.reposition(!0))}handleDisabledChange(e){e||(this.open=!1)}flipPlacementsHandler(){this.setFilteredPlacements(),this.reposition(!0)}handlePropsChange(){this.updateItems(),this.updateGroupProps()}closeCalciteDropdownOnClick(e){this.referenceElementType||this.disabled||!this.open||e.composedPath().includes(this.el)||this.closeCalciteDropdown()}closeCalciteDropdownOnOpenEvent(e){this.referenceElementType||e.composedPath().includes(this.el)||this.closeCalciteDropdown()}pointerEnterHandler(){this.referenceElementType||this.disabled||this.type!=="hover"||(this.open=!0)}pointerLeaveHandler(){this.referenceElementType||this.disabled||this.type!=="hover"||this.closeCalciteDropdown()}getTraversableItems(){return this.items.filter(e=>!e.disabled&&!e.hidden)}async handleItemSelect(e){this.updateSelectedItems(),this.syncActiveItemFromTraversableItems(),e.stopPropagation(),this.calciteDropdownSelect.emit(),await this.setFocus(),this.closeOnSelectDisabled||this.closeCalciteDropdown()}setFilteredPlacements(){let{el:e,flipPlacements:t}=this;this.filteredFlipPlacements=t?k(t,e):null}updateItems(){this.items=this.groups.map(e=>Array.from(e?.querySelectorAll("calcite-dropdown-item"))).reduce((e,t)=>[...e,...t],[]),this.updateSelectedItems(),this.syncActiveItemFromTraversableItems(),this.reposition(!0),this.items.forEach(e=>e.scale=this.scale)}updateGroups(e){let t=e.target.assignedElements({flatten:!0}).filter(i=>i?.matches("calcite-dropdown-group"));this.groups=t,this.updateItems(),this.updateGroupProps()}updateGroupProps(){this.groups.forEach((e,t)=>{e.scale=this.scale,e.position=t})}resizeObserverCallback(e){e.forEach(({target:t})=>{t===this.referenceEl?this.setDropdownWidth():t===this.scrollerEl&&this.setMaxScrollerHeight()})}setDropdownWidth(){let{referenceEl:e,scrollerEl:t}=this;!t||!(e instanceof HTMLElement)||(t.style.minWidth=`${e.clientWidth}px`)}setMaxScrollerHeight(){let{maxItems:e,items:t,scrollerEl:i}=this;if(!i)return;let s=t.length>=e&&e>0?this.getYDistanceFromScroller(t.at(e-1)):0;i.style.maxBlockSize=s>0?`${s}px`:"",this.reposition(!0)}setScrollerAndTransitionEl(e){d(this.resizeObserver,this.scrollerEl,e),this.scrollerEl=e,this.transitionEl=e}onBeforeOpen(){this.setInitialActiveItem(),this.calciteDropdownBeforeOpen.emit(),this.topLayer.show()}onOpen(){this.calciteDropdownOpen.emit()}onBeforeClose(){this.calciteDropdownBeforeClose.emit()}onClose(){this.calciteDropdownClose.emit(),A(this),this.topLayer.hide()}setReferenceEl(e){let t=this.referenceEl instanceof HTMLElement?this.referenceEl:null,i=e instanceof HTMLElement?e:null;d(this.resizeObserver,t,i),this.referenceEl=e,n(this)}setFloatingEl(e){this.floatingEl=e,n(this)}keyDownHandler(e){if(!(this.referenceEl instanceof HTMLElement)||!e.composedPath().includes(this.referenceEl))return;let{defaultPrevented:t,key:i}=e;if(!t){if(this.open&&i==="Escape"){this.closeCalciteDropdown(),e.preventDefault();return}if(!this.open&&f(i)){this.open=!0,e.preventDefault();return}if(!this.open&&(i==="ArrowDown"||i==="ArrowUp")){e.preventDefault(),this.focusLastDropdownItem=i==="ArrowUp",this.open=!0;return}if(this.open){if(i==="Tab"){this.closeCalciteDropdown();return}if(i==="ArrowDown"){e.preventDefault(),this.navigateActiveItem("next");return}if(i==="ArrowUp"){e.preventDefault(),this.navigateActiveItem("previous");return}if(i==="Home"){e.preventDefault(),this.navigateActiveItem("first");return}if(i==="End"){e.preventDefault(),this.navigateActiveItem("last");return}f(i)&&(e.preventDefault(),this.activateActiveItem())}}}updateSelectedItems(){this.selectedItems=this.items.filter(e=>e.selected)}getYDistanceFromScroller(e){let t=e.getBoundingClientRect();return e.offsetTop+t.height}closeCalciteDropdown(){this.open=!1,this.setActiveItemByIndex(-1)}async setInitialActiveItem(){let e=this.getTraversableItems(),t=this.focusLastDropdownItem?e.at(-1):e[0];if(this.focusLastDropdownItem=!1,!t){this.setActiveItemByIndex(-1);return}let i=e.findIndex(s=>s===t);this.setActiveItemByIndex(i),await this.scrollActiveItemIntoView(t)}syncActiveItemFromTraversableItems(){let e=this.getTraversableItems();if(!e.length){this.setActiveItemByIndex(-1);return}if(this.activeItemIndex<0||this.activeItemIndex>=e.length){this.setActiveItemByIndex(0);return}this.updateActiveDescendantElement(e[this.activeItemIndex])}setActiveItemByIndex(e){this.activeItemIndex=e;let t=this.getTraversableItems(),i=e>=0?t[e]:null;this.updateActiveDescendantElement(i)}updateActiveDescendantElement(e){this.items.forEach(t=>{t.activeDescendant=t===e}),this.activeDescendantElement=e??null}navigateActiveItem(e){let t=this.getTraversableItems();if(!t.length)return;let i=t.length,s=this.activeItemIndex;s<0||s>=i?s=e==="previous"||e==="last"?i-1:0:e==="next"?s=(s+1)%i:e==="previous"?s=(s-1+i)%i:e==="first"?s=0:e==="last"&&(s=i-1);let l=t[s];this.setActiveItemByIndex(s),this.scrollActiveItemIntoView(l)}async scrollActiveItemIntoView(e){e&&(await this.updateComplete,await p(),await p(),e.scrollIntoView({block:"nearest"}))}activateActiveItem(){let e=this.getTraversableItems(),t=e[this.activeItemIndex]||e[0];t&&(this.setActiveItemByIndex(e.findIndex(i=>i===t)),t.activateItem())}openHoverDropdown(){this.open||this.disabled||this.type!=="hover"||(this.open=!0)}closeHoverDropdown(e){if(!this.open||this.disabled||this.type!=="hover")return;let t=e.relatedTarget;t&&(this.el.contains(t)||this.referenceEl!=null&&this.referenceEl instanceof HTMLElement&&this.referenceEl.contains(t))||this.closeCalciteDropdown()}toggleClickDropdown(){this.disabled||this.type!=="click"||(this.open=!this.open)}render(){let{open:e}=this;return this.interactiveContainer({disabled:this.disabled,children:c`${this.referenceElementType?null:c`<div class=${o(v.triggerContainer)} =${this.toggleClickDropdown} =${this.openHoverDropdown} =${this.closeHoverDropdown} =${this.keyDownHandler} ${a(this.setReferenceEl)}><slot .ariaActiveDescendantElement=${this.activeDescendantElement??null} .ariaControlsElements=${this.scrollerEl?[this.scrollerEl]:void 0} .ariaExpanded=${e} aria-haspopup=menu name=${$.trigger}></slot></div>`}<div class=${o({[v.wrapper]:!0,[T("width",this.width,this.widthScale)]:!!(this.width||this.widthScale)})} .inert=${!e} popover=manual ${a(this.setFloatingEl)}><div .ariaLabelledByElements=${this.referenceEl instanceof HTMLElement?[this.referenceEl]:void 0} class=${o({[v.content]:!0,[u.animation]:!0,[u.animationActive]:e})} role=menu ${a(this.setScrollerAndTransitionEl)}><slot =${this.updateGroups}></slot></div></div>`})}};g("calcite-dropdown",w);export{w as Dropdown};