@dialpad/dialtone-vue
Version:
Vue component library for Dialpad's design system Dialtone
3 lines (2 loc) • 5.62 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const a=require("../../common/mixins/keyboard-list-navigation.cjs"),o=require("./dropdown-constants.cjs"),l=require("../../common/utils/index.cjs"),n=require("../../common/constants/index.cjs"),d=require("../../_plugin-vue2_normalizer-e_CkxkSV.cjs"),s=require("../list-item/list-item-constants.cjs"),h=require("../popover/popover.cjs"),p=require("../popover/popover-constants.cjs"),u={name:"DtDropdown",components:{DtPopover:h.default},mixins:[a.default({indexKey:"highlightIndex",idKey:"highlightId",listElementKey:"getListElement",listItemRole:"menuitem",afterHighlightMethod:"afterHighlight",beginningOfListMethod:"beginningOfListMethod",endOfListMethod:"endOfListMethod",activeItemKey:"activeItemEl",focusOnKeyboardNavigation:!0})],props:{open:{type:Boolean,default:null},openOnContext:{type:Boolean,default:!1},padding:{type:String,default:"small",validator:e=>Object.keys(o.DROPDOWN_PADDING_CLASSES).some(t=>t===e)},modal:{type:Boolean,default:!0},contentWidth:{type:String,default:null},maxHeight:{type:String,default:""},maxWidth:{type:String,default:""},listId:{type:String,default(){return l.getUniqueString()}},navigationType:{type:String,default:s.LIST_ITEM_NAVIGATION_TYPES.ARROW_KEYS,validator:e=>Object.values(s.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=>p.POPOVER_APPEND_TO_VALUES.includes(e)||e instanceof HTMLElement},tether:{type:Boolean,default:!0},transition:{type:String,default:"fade"}},emits:["highlight","opened","update:open"],data(){return{LIST_ITEM_NAVIGATION_TYPES:s.LIST_ITEM_NAVIGATION_TYPES,DROPDOWN_PADDING_CLASSES:o.DROPDOWN_PADDING_CLASSES,EVENT_KEYNAMES:n.EVENT_KEYNAMES,openedWithKeyboard:!1,isOpen:null}},computed:{dropdownListeners(){return{...this.$listeners,opened:e=>{this.updateInitialHighlightIndex(e)},keydown:e=>{switch(e.code){case n.EVENT_KEYNAMES.up:case n.EVENT_KEYNAMES.arrowup:this.onUpKeyPress(e),e.stopPropagation(),e.preventDefault();break;case n.EVENT_KEYNAMES.down:case n.EVENT_KEYNAMES.arrowdown:this.onDownKeyPress(e),e.stopPropagation(),e.preventDefault();break;case n.EVENT_KEYNAMES.space:case n.EVENT_KEYNAMES.spacebar:this.onSpaceKey();break;case n.EVENT_KEYNAMES.enter:this.onEnterKey();break;case n.EVENT_KEYNAMES.home:this.onHomeKeyPress(e),e.stopPropagation(),e.preventDefault();break;case n.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",o.DROPDOWN_PADDING_CLASSES[this.padding],this.listClass,{"d-context-menu-list":this.openOnContext}]},shouldOpenWithArrowKeys(){return!this.openOnContext}},methods:{onMouseHighlight(e){const t=e.target.closest("li");t&&t.role&&this.highlightId!==t.id&&(this.setHighlightId(t.id),t.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)}}};var g=function(){var t=this,r=t._self._c;return r("dt-popover",t._g({ref:"popover",attrs:{"content-width":t.contentWidth,open:t.open,placement:t.placement,"initial-focus-element":t.openedWithKeyboard?"first":"dialog","fallback-placements":t.fallbackPlacements,padding:"none",role:"menu","append-to":t.appendTo,modal:t.modal,"max-height":t.maxHeight,"max-width":t.maxWidth,"open-with-arrow-keys":t.shouldOpenWithArrowKeys,"open-on-context":t.openOnContext,tether:t.tether,transition:t.transition},scopedSlots:t._u([{key:"anchor",fn:function({attrs:i}){return[t._t("anchor",null,null,i)]}},{key:"content",fn:function({close:i}){return[r("ul",{ref:"listWrapper",class:t.listClasses,attrs:{id:t.listId,"data-qa":"dt-dropdown-list-wrapper"},on:{mouseleave:t.clearHighlightIndex,"!mousemove":function(_){return t.onMouseHighlight.apply(null,arguments)}}},[t._t("list",null,{close:i})],2)]}},{key:"footerContent",fn:function({close:i}){return[t._t("footer",null,{close:i})]}}],null,!0)},t.dropdownListeners))},c=[],f=d.n(u,g,c);const E=f.exports;exports.default=E;
//# sourceMappingURL=dropdown.cjs.map