@dialpad/dialtone
Version:
Dialpad's Dialtone design system monorepo
1 lines • 6.49 kB
Source Map (JSON)
{"version":3,"file":"scroller.vue.cjs","sources":["../../../components/scroller/scroller.vue"],"sourcesContent":["<template>\n <component\n :is=\"dynamic ? DynamicScroller : CoreScroller\"\n ref=\"scroller\"\n data-qa=\"dt-scroller\"\n :items=\"items\"\n :item-size=\"itemSize\"\n :min-item-size=\"minItemSize\"\n :direction=\"direction\"\n :key-field=\"keyField\"\n :list-tag=\"listTag\"\n :item-tag=\"itemTag\"\n :style=\"computedStyle\"\n tabindex=\"0\"\n @user-position=\"$emit('user-position', $event)\"\n >\n <template\n #default=\"{ item, index, active }\"\n >\n <slot\n v-bind=\"{\n item: item,\n index,\n active,\n }\"\n />\n </template>\n </component>\n</template>\n\n<script setup>\nimport CoreScroller from './modules/core_scroller.vue';\nimport DynamicScroller from './modules/dynamic_scroller.vue';\nimport { provide, computed, watch, ref } from 'vue';\n\ndefineOptions({\n name: 'DtScroller',\n});\n\nconst props = defineProps({\n /**\n * The direction of the scroller.\n * @values vertical, horizontal\n */\n direction: {\n type: String,\n default: 'vertical',\n validator: (value) => ['vertical', 'horizontal'].includes(value),\n },\n\n /**\n * Indicates if the items need to react to changes in their size.\n * If disabled the itemSize prop is required and you will get improved performance.\n * If enabled the minItemSize prop is required and you\n * will have reduced performance but the ability to reactively size list items\n * @values true, false\n */\n dynamic: {\n type: Boolean,\n default: false,\n },\n\n /**\n * Display height (or width in horizontal mode) of the items in pixels\n * used to calculate the scroll size and position.\n * Required if DYNAMIC is false\n */\n itemSize: {\n type: Number,\n default: null,\n },\n\n /**\n * The tag to use for the items.\n */\n itemTag: {\n type: String,\n default: 'div',\n },\n\n /**\n * The items to render.\n * If the items are simple arrays, the index will be used as the key.\n * If the items are objects, the keyField will be used as the key.\n * @example items: [ 'item1', 'item2', 'item3' ]\n * @example items: [ { id: 1, name: 'item1' }, { id: 2, name: 'item2' }, { id: 3, name: 'item3' } ]\n */\n items: {\n type: Array,\n required: true,\n },\n\n /**\n * The key field to use for the items.\n * If the items are objects, the scroller needs to be able to identify them.\n * By default it will look for an id field on the items.\n * This can be configured with this prop if you are using another field name.\n */\n keyField: {\n type: String,\n default: 'id',\n },\n\n /**\n * The tag to use for the list.\n */\n listTag: {\n type: String,\n default: 'div',\n },\n\n /**\n * Minimum size used if the height (or width in horizontal mode) of a item is unknown.\n * Is required for the initial render of items in DYNAMIC size mode.\n */\n minItemSize: {\n type: [Number, String],\n default: null,\n },\n\n /**\n * The height of the scroller.\n * Can be a number (in pixels) or a string (in CSS units).\n */\n scrollerHeight: {\n type: [String, Number],\n default: '100%',\n },\n\n /**\n * The width of the scroller.\n * Can be a number (in pixels) or a string (in CSS units).\n */\n scrollerWidth: {\n type: [String, Number],\n default: '100%',\n },\n});\n\nconst emits = defineEmits([\n /**\n * Describe when the scroller changes from start/middle/end\n * @param {string} position The position of the scroller.\n * @values start, middle, end\n */\n 'user-position',\n]);\n\nprovide('emit', emits);\n\nconst scroller = ref(null);\n\nconst computedStyle = computed(() => {\n return {\n width: typeof props.scrollerWidth === 'number' ? `${props.scrollerWidth}px` : props.scrollerWidth,\n height: typeof props.scrollerHeight === 'number' ? `${props.scrollerHeight}px` : props.scrollerHeight,\n };\n});\n\nwatch(props, () => {\n validateProps();\n}, { deep: true, immediate: true });\n\nfunction scrollToBottom () {\n if (scroller.value) scroller.value.scrollToBottom();\n}\n\nfunction scrollToItem (index) {\n if (scroller.value) scroller.value.scrollToItem(index);\n}\n\nfunction updateItems () {\n if (!scroller.value) return;\n if (props.dynamic) {\n scroller.value.dynamicScrollerUpdateItems();\n } else {\n scroller.value._updateVisibleItems(true);\n }\n}\n\nfunction updateItemsFromBottom () {\n if (!scroller.value) return;\n if (props.dynamic) {\n scroller.value.dynamicScrollerUpdateItemsFromBottom();\n } else {\n scroller.value._updateVisibleItems(false, true);\n }\n}\n\nfunction validateProps () {\n if (props.dynamic && !props.minItemSize) {\n console.error('scroller error: \\'minItemSize\\' is required on \\'dynamic\\' mode.');\n }\n\n if (!props.dynamic && !props.itemSize) {\n console.error('scroller error: \\'itemSize\\' is required.');\n }\n}\n\ndefineExpose({\n scrollToBottom,\n scrollToItem,\n updateItems,\n updateItemsFromBottom,\n});\n</script>\n"],"names":["provide","ref","computed","watch"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,UAAM,QAAQ;AAoGd,UAAM,QAAQ;AASdA,QAAAA,QAAQ,QAAQ,KAAK;AAErB,UAAM,WAAWC,IAAAA,IAAI,IAAI;AAEzB,UAAM,gBAAgBC,IAAQ,SAAC,MAAM;AACnC,aAAO;AAAA,QACL,OAAO,OAAO,MAAM,kBAAkB,WAAW,GAAG,MAAM,aAAa,OAAO,MAAM;AAAA,QACpF,QAAQ,OAAO,MAAM,mBAAmB,WAAW,GAAG,MAAM,cAAc,OAAO,MAAM;AAAA,MAC3F;AAAA,IACA,CAAC;AAEDC,QAAK,MAAC,OAAO,MAAM;AACjB;IACF,GAAG,EAAE,MAAM,MAAM,WAAW,KAAM,CAAA;AAElC,aAAS,iBAAkB;AACzB,UAAI,SAAS,MAAO,UAAS,MAAM,eAAc;AAAA,IACnD;AAEA,aAAS,aAAc,OAAO;AAC5B,UAAI,SAAS,MAAO,UAAS,MAAM,aAAa,KAAK;AAAA,IACvD;AAEA,aAAS,cAAe;AACtB,UAAI,CAAC,SAAS,MAAO;AACrB,UAAI,MAAM,SAAS;AACjB,iBAAS,MAAM;MACnB,OAAS;AACL,iBAAS,MAAM,oBAAoB,IAAI;AAAA,MACxC;AAAA,IACH;AAEA,aAAS,wBAAyB;AAChC,UAAI,CAAC,SAAS,MAAO;AACrB,UAAI,MAAM,SAAS;AACjB,iBAAS,MAAM;MACnB,OAAS;AACL,iBAAS,MAAM,oBAAoB,OAAO,IAAI;AAAA,MAC/C;AAAA,IACH;AAEA,aAAS,gBAAiB;AACxB,UAAI,MAAM,WAAW,CAAC,MAAM,aAAa;AACvC,gBAAQ,MAAM,8DAAkE;AAAA,MACjF;AAED,UAAI,CAAC,MAAM,WAAW,CAAC,MAAM,UAAU;AACrC,gBAAQ,MAAM,yCAA2C;AAAA,MAC1D;AAAA,IACH;AAEA,aAAa;AAAA,MACX;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}