@dialpad/dialtone
Version:
Dialpad's Dialtone design system monorepo
3 lines (2 loc) • 6.03 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=require("../../common/mixins/keyboard-list-navigation.cjs"),l=require("./dropdown-constants.cjs"),g=require("../../common/utils/index.cjs"),o=require("../../common/constants/index.cjs"),t=require("vue"),c=require("../../_plugin-vue_export-helper-BRilXfQE.cjs"),d=require("../list-item/list-item-constants.cjs"),E=require("../popover/popover.cjs"),f=require("../popover/popover-constants.cjs"),m={compatConfig:{MODE:3},name:"DtDropdown",components:{DtPopover:E.default},mixins:[p.default({indexKey:"highlightIndex",idKey:"highlightId",listElementKey:"getListElement",listItemRole:"menuitem",afterHighlightMethod:"afterHighlight",beginningOfListMethod:"beginningOfListMethod",endOfListMethod:"endOfListMethod",activeItemKey:"activeItemEl",focusOnKeyboardNavigation:!0})],inheritAttrs:!1,props:{open:{type:Boolean,default:null},openOnContext:{type:Boolean,default:!1},padding:{type:String,default:"small",validator:e=>Object.keys(l.DROPDOWN_PADDING_CLASSES).some(i=>i===e)},modal:{type:Boolean,default:!0},contentWidth:{type:String,default:null},maxHeight:{type:String,default:""},maxWidth:{type:String,default:""},listId:{type:String,default(){return g.getUniqueString()}},navigationType:{type:String,default:d.LIST_ITEM_NAVIGATION_TYPES.ARROW_KEYS,validator:e=>Object.values(d.LIST_ITEM_NAVIGATION_TYPES).includes(e)},fallbackPlacements:{type:Array,default:()=>["auto"]},placement:{type:String,default:"bottom"},onBeginningOfList:{type:Function,default:null},onEndOfList:{type:Function,default:null},listClass:{type:[String,Array,Object],default:""},appendTo:{type:[HTMLElement,String],default:"body",validator:e=>f.POPOVER_APPEND_TO_VALUES.includes(e)||e instanceof HTMLElement},tether:{type:Boolean,default:!0},transition:{type:String,default:"fade"}},emits:["keydown","highlight","update:open","opened","update:open"],data(){return{LIST_ITEM_NAVIGATION_TYPES:d.LIST_ITEM_NAVIGATION_TYPES,DROPDOWN_PADDING_CLASSES:l.DROPDOWN_PADDING_CLASSES,EVENT_KEYNAMES:o.EVENT_KEYNAMES,openedWithKeyboard:!1,isOpen:null}},computed:{dropdownListeners(){return{opened:e=>{this.updateInitialHighlightIndex(e)},keydown:e=>{switch(e.code){case o.EVENT_KEYNAMES.up:case o.EVENT_KEYNAMES.arrowup:this.onUpKeyPress(e),e.stopPropagation(),e.preventDefault();break;case o.EVENT_KEYNAMES.down:case o.EVENT_KEYNAMES.arrowdown:this.onDownKeyPress(e),e.stopPropagation(),e.preventDefault();break;case o.EVENT_KEYNAMES.space:case o.EVENT_KEYNAMES.spacebar:this.onSpaceKey();break;case o.EVENT_KEYNAMES.enter:this.onEnterKey();break;case o.EVENT_KEYNAMES.home:this.onHomeKeyPress(e),e.stopPropagation(),e.preventDefault();break;case o.EVENT_KEYNAMES.end:this.onEndKeyPress(e),e.stopPropagation(),e.preventDefault();break;default:this.onKeyPress(e);break}this.$emit("keydown",e)}}},beginningOfListMethod(){return this.onBeginningOfList||this.jumpToEnd},endOfListMethod(){return this.onEndOfList||this.jumpToBeginning},activeItemEl(){return this.getListElement().querySelector("#"+this.highlightId)},isArrowKeyNav(){return this.navigationType===this.LIST_ITEM_NAVIGATION_TYPES.ARROW_KEYS},listClasses(){return["d-dropdown-list",l.DROPDOWN_PADDING_CLASSES[this.padding],this.listClass,{"d-context-menu-list":this.openOnContext}]},shouldOpenWithArrowKeys(){return!this.openOnContext}},methods:{onMouseHighlight(e){const i=e.target.closest("li");i&&i.role&&this.highlightId!==i.id&&(this.setHighlightId(i.id),i.focus())},getListElement(){return this.$refs.listWrapper},clearHighlightIndex(){this.setHighlightIndex(-1)},afterHighlight(){this.highlightIndex!==this._itemsLength()-1&&this.$emit("highlight",this.highlightIndex)},updateInitialHighlightIndex(e){this.isOpen=e,e?(this.openedWithKeyboard&&this.isArrowKeyNav&&this.setHighlightIndex(0),this.$emit("opened",!0)):(this.clearHighlightIndex(),this.openedWithKeyboard=!1,this.$emit("opened",!1))},onSpaceKey(){this.open||(this.openedWithKeyboard=!0)},onEnterKey(){this.open||(this.openedWithKeyboard=!0)},onUpKeyPress(){if(!this.isOpen){this.openedWithKeyboard=!0;return}if(this.isArrowKeyNav)return this.onUpKey()},onDownKeyPress(){if(!this.isOpen){this.openedWithKeyboard=!0;return}if(this.isArrowKeyNav)return this.onDownKey()},onHomeKeyPress(){if(!(!this.isOpen||!this.isArrowKeyNav))return this.onHomeKey()},onEndKeyPress(){if(!(!this.isOpen||!this.isArrowKeyNav))return this.onEndKey()},onKeyPress(e){if(!(!this.isOpen||!this.isArrowKeyNav||!this.isValidLetter(e.key)))return e.stopPropagation(),e.preventDefault(),this.onNavigationKey(e.key)}}},y=["id"];function _(e,i,n,K,h,s){const u=t.resolveComponent("dt-popover");return t.openBlock(),t.createBlock(u,t.mergeProps({ref:"popover","content-width":n.contentWidth,open:n.open,placement:n.placement,"initial-focus-element":h.openedWithKeyboard?"first":"dialog","fallback-placements":n.fallbackPlacements,padding:"none",role:"menu","append-to":n.appendTo,modal:n.modal,"max-height":n.maxHeight,"max-width":n.maxWidth,"open-with-arrow-keys":s.shouldOpenWithArrowKeys,"open-on-context":n.openOnContext},e.$attrs,{tether:n.tether,transition:n.transition},t.toHandlers(s.dropdownListeners)),{anchor:t.withCtx(({attrs:r})=>[t.renderSlot(e.$slots,"anchor",t.mergeProps({ref:"anchor"},r))]),content:t.withCtx(({close:r})=>[t.createElementVNode("ul",{id:n.listId,ref:"listWrapper",class:t.normalizeClass(s.listClasses),"data-qa":"dt-dropdown-list-wrapper",onMouseleave:i[0]||(i[0]=(...a)=>s.clearHighlightIndex&&s.clearHighlightIndex(...a)),onMousemoveCapture:i[1]||(i[1]=(...a)=>s.onMouseHighlight&&s.onMouseHighlight(...a))},[t.renderSlot(e.$slots,"list",{close:r})],42,y)]),footerContent:t.withCtx(({close:r})=>[t.renderSlot(e.$slots,"footer",{close:r})]),_:3},16,["content-width","open","placement","initial-focus-element","fallback-placements","append-to","modal","max-height","max-width","open-with-arrow-keys","open-on-context","tether","transition"])}const S=c._(m,[["render",_]]);exports.default=S;
//# sourceMappingURL=dropdown.cjs.map