UNPKG

xdesign-vue-next

Version:

XDesign Component for vue-next

344 lines (340 loc) 14.2 kB
/** * xdesign v1.0.6 * (c) 2023 xdesign * @license MIT */ import { _ as __unplugin_components_2 } from '../_chunks/dep-ae5efda0.js'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, ref, computed, watch, onUpdated, onMounted, onBeforeUnmount, createVNode, Teleport, mergeProps, nextTick } from 'vue'; import { usePrefixClass } from '../hooks/useConfig.js'; import { isServer } from '../utils/dom.js'; import { getScrollbarWidth } from '../_common/js/utils/getScrollbarWidth.js'; import props from './props.js'; import { useAction } from '../dialog/hooks.js'; import { useTNodeJSX, useContent } from '../hooks/tnode.js'; import { useDrag } from './hooks.js'; import useTeleport from '../hooks/useTeleport.js'; import { useConfig } from '../config-provider/useConfig.js'; import '../_chunks/dep-3a1cce9f.js'; import 'lodash/isString'; import 'lodash/isFunction'; import 'lodash/isArray'; import '../utils/easing.js'; import '../_common/js/utils/helper.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import '@babel/runtime/helpers/slicedToArray'; import 'lodash/isNull'; import 'lodash/isUndefined'; import 'lodash/isNumber'; import '@babel/runtime/helpers/typeof'; import 'lodash/isObject'; import 'lodash/omit'; import '../button/index.js'; import '../button/button.js'; import '../loading/index.js'; import '../loading/directive.js'; import '../loading/plugin.js'; import '../loading/loading.js'; import '../loading/icon/gradient.js'; import '../_common/js/loading/circle-adapter.js'; import '../_common/js/utils/set-style.js'; import '../utils/render-tnode.js'; import 'lodash/isEmpty'; import 'lodash/camelCase'; import 'lodash/kebabCase'; import '../loading/props.js'; import 'lodash/cloneDeep'; import '../config-provider/context.js'; import 'lodash/mergeWith'; import 'lodash/merge'; import '../_common/js/global-config/default-config.js'; import '../_common/js/global-config/locale/en_US.js'; import '../utils/withInstall.js'; import '../button/props.js'; import '../hooks/useRipple.js'; import '../hooks/useKeepAnimation.js'; import '../utils/set-style.js'; import '../hooks/useDisabled.js'; import 'lodash/isBoolean'; var key = 1; var _Drawer = defineComponent({ name: "XDrawer", inheritAttrs: false, props: props, emits: ["update:visible"], setup: function setup(props2, context) { var destroyOnCloseVisible = ref(false); var isVisible = ref(false); var styleEl = ref(); var styleTimer = ref(); var _useConfig = useConfig("drawer"), globalConfig = _useConfig.globalConfig; var renderTNodeJSX = useTNodeJSX(); var renderContent = useContent(); var COMPONENT_NAME = usePrefixClass("drawer"); var _useDrag = useDrag(props2), draggedSizeValue = _useDrag.draggedSizeValue, enableDrag = _useDrag.enableDrag, draggableLineStyles = _useDrag.draggableLineStyles; var teleportElement = useTeleport(function () { return props2.attach; }); var confirmBtnAction = function confirmBtnAction(e) { var _props2$onConfirm; (_props2$onConfirm = props2.onConfirm) === null || _props2$onConfirm === void 0 ? void 0 : _props2$onConfirm.call(props2, { e: e }); }; var cancelBtnAction = function cancelBtnAction(e) { var _props2$onCancel; (_props2$onCancel = props2.onCancel) === null || _props2$onCancel === void 0 ? void 0 : _props2$onCancel.call(props2, { e: e }); closeDrawer({ trigger: "cancel", e: e }); }; var _useAction = useAction({ confirmBtnAction: confirmBtnAction, cancelBtnAction: cancelBtnAction }), getConfirmBtn = _useAction.getConfirmBtn, getCancelBtn = _useAction.getCancelBtn; var drawerEle = ref(null); var drawerClasses = computed(function () { var _ref; return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "--").concat(props2.placement), (_ref = {}, _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--open"), isVisible.value), _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--attach"), props2.showInAttachedElement), _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--without-mask"), !props2.showOverlay), _ref)]; }); var sizeValue = computed(function () { var _props2$size; if (draggedSizeValue.value) return draggedSizeValue.value; var size = (_props2$size = props2.size) !== null && _props2$size !== void 0 ? _props2$size : globalConfig.value.size; var defaultSize = isNaN(Number(size)) ? size : "".concat(size, "px"); return { small: "300px", medium: "500px", large: "760px" }[size] || defaultSize; }); var wrapperStyles = computed(function () { return { transform: isVisible.value ? "translateX(0)" : void 0, width: ["left", "right"].includes(props2.placement) ? sizeValue.value : "", height: ["top", "bottom"].includes(props2.placement) ? sizeValue.value : "" }; }); var wrapperClasses = computed(function () { return ["".concat(COMPONENT_NAME.value, "__content-wrapper"), "".concat(COMPONENT_NAME.value, "__content-wrapper--").concat(props2.placement)]; }); var parentNode = computed(function () { return drawerEle.value && drawerEle.value.parentNode; }); var modeAndPlacement = computed(function () { return [props2.mode, props2.placement].join(); }); var footerStyle = computed(function () { return { display: "flex", justifyContent: props2.placement === "right" ? "flex-start" : "flex-end" }; }); var handleEscKeydown = function handleEscKeydown(e) { var _props2$closeOnEscKey; if ((_props2$closeOnEscKey = props2.closeOnEscKeydown) !== null && _props2$closeOnEscKey !== void 0 ? _props2$closeOnEscKey : globalConfig.value.closeOnEscKeydown && e.key === "Escape") { var _props2$onEscKeydown; (_props2$onEscKeydown = props2.onEscKeydown) === null || _props2$onEscKeydown === void 0 ? void 0 : _props2$onEscKeydown.call(props2, { e: e }); closeDrawer({ trigger: "esc", e: e }); } }; var clearStyleFunc = function clearStyleFunc() { clearTimeout(styleTimer.value); styleTimer.value = setTimeout(function () { var _styleEl$value, _styleEl$value$remove; (_styleEl$value = styleEl.value) === null || _styleEl$value === void 0 || (_styleEl$value = _styleEl$value.parentNode) === null || _styleEl$value === void 0 || (_styleEl$value$remove = _styleEl$value.removeChild) === null || _styleEl$value$remove === void 0 ? void 0 : _styleEl$value$remove.call(_styleEl$value, styleEl.value); }, 150); }; var handlePushMode = function handlePushMode() { if (props2.mode !== "push") return; nextTick(function () { if (!parentNode.value) return; parentNode.value.style.cssText = "transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;"; }); }; var updatePushMode = function updatePushMode() { if (!parentNode.value) return; if (props2.mode !== "push" || !parentNode.value) return; var marginStr = { left: "margin: 0 0 0 ".concat(sizeValue.value), right: "margin: 0 0 0 -".concat(sizeValue.value), top: "margin: ".concat(sizeValue.value, " 0 0 0"), bottom: "margin: -".concat(sizeValue.value, " 0 0 0") }[props2.placement]; if (isVisible.value) { parentNode.value.style.cssText += marginStr; } else { parentNode.value.style.cssText = parentNode.value.style.cssText.replace(/margin:.+;/, ""); } }; var getDefaultFooter = function getDefaultFooter() { var confirmBtn = getConfirmBtn({ confirmBtn: props2.confirmBtn, globalConfirm: globalConfig.value.confirm, className: "".concat(COMPONENT_NAME.value, "__confirm") }); var cancelBtn = getCancelBtn({ cancelBtn: props2.cancelBtn, globalCancel: globalConfig.value.cancel, className: "".concat(COMPONENT_NAME.value, "__cancel") }); return createVNode("div", { "style": footerStyle.value }, [props2.placement === "right" ? confirmBtn : null, cancelBtn, props2.placement !== "right" ? confirmBtn : null]); }; watch(modeAndPlacement, function () { handlePushMode(); }, { immediate: true }); watch(function () { return props2.visible; }, function (value) { if (isServer) return; if (value) { if (!props2.showInAttachedElement && props2.preventScrollThrough) { styleEl.value && document.head.appendChild(styleEl.value); } } else { clearStyleFunc(); } if (props2.destroyOnClose) { if (value) { destroyOnCloseVisible.value = false; setTimeout(function () { isVisible.value = true; }); } else { isVisible.value = false; setTimeout(function () { destroyOnCloseVisible.value = true; }, 200); } } else if (destroyOnCloseVisible.value === true && value) { destroyOnCloseVisible.value = false; setTimeout(function () { isVisible.value = true; }); } else { isVisible.value = value; } }, { immediate: true }); var handleCloseBtnClick = function handleCloseBtnClick(e) { var _props2$onCloseBtnCli; (_props2$onCloseBtnCli = props2.onCloseBtnClick) === null || _props2$onCloseBtnCli === void 0 ? void 0 : _props2$onCloseBtnCli.call(props2, { e: e }); closeDrawer({ trigger: "close-btn", e: e }); }; var handleWrapperClick = function handleWrapperClick(e) { var _props2$onOverlayClic, _props2$closeOnOverla; (_props2$onOverlayClic = props2.onOverlayClick) === null || _props2$onOverlayClic === void 0 ? void 0 : _props2$onOverlayClic.call(props2, { e: e }); if ((_props2$closeOnOverla = props2.closeOnOverlayClick) !== null && _props2$closeOnOverla !== void 0 ? _props2$closeOnOverla : globalConfig.value.closeOnOverlayClick) { closeDrawer({ trigger: "overlay", e: e }); } }; var onKeyDown = function onKeyDown(e) { var _props2$closeOnEscKey2; if (((_props2$closeOnEscKey2 = props2.closeOnEscKeydown) !== null && _props2$closeOnEscKey2 !== void 0 ? _props2$closeOnEscKey2 : globalConfig.value.closeOnEscKeydown) && e.key === "Escape") { var _props2$onEscKeydown2; (_props2$onEscKeydown2 = props2.onEscKeydown) === null || _props2$onEscKeydown2 === void 0 ? void 0 : _props2$onEscKeydown2.call(props2, { e: e }); closeDrawer({ trigger: "esc", e: e }); } }; var closeDrawer = function closeDrawer(params) { var _props2$onClose; (_props2$onClose = props2.onClose) === null || _props2$onClose === void 0 ? void 0 : _props2$onClose.call(props2, params); context.emit("update:visible", false); }; onUpdated(function () { updatePushMode(); }); onMounted(function () { var hasScrollBar = document.documentElement.scrollHeight > document.documentElement.clientHeight; var scrollWidth = hasScrollBar ? getScrollbarWidth() : 0; styleEl.value = document.createElement("style"); styleEl.value.dataset.id = "td_drawer_".concat(+new Date(), "_").concat(key += 1); styleEl.value.innerHTML = "\n html body {\n overflow-y: hidden;\n transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;\n ".concat(props2.mode === "push" ? "" : "width: calc(100% - ".concat(scrollWidth, "px);"), "\n }\n "); if (isVisible.value && !props2.showInAttachedElement && props2.preventScrollThrough) { document.head.appendChild(styleEl.value); } window.addEventListener("keydown", handleEscKeydown); }); onBeforeUnmount(function () { clearStyleFunc(); window.removeEventListener("keydown", handleEscKeydown); }); return function () { if (destroyOnCloseVisible.value) return; var body = renderContent("body", "default"); var headerContent = renderTNodeJSX("header"); var defaultFooter = getDefaultFooter(); return createVNode(Teleport, { "disabled": !props2.attach || !teleportElement.value, "to": teleportElement.value }, { "default": function _default() { return [createVNode("div", mergeProps({ "ref": drawerEle, "class": drawerClasses.value, "style": { zIndex: props2.zIndex }, "onKeydown": onKeyDown, "tabindex": 0 }, context.attrs), [props2.showOverlay && createVNode("div", { "class": "".concat(COMPONENT_NAME.value, "__mask"), "onClick": handleWrapperClick }, null), createVNode("div", { "class": wrapperClasses.value, "style": wrapperStyles.value }, [headerContent && createVNode("div", { "class": "".concat(COMPONENT_NAME.value, "__header") }, [headerContent]), props2.closeBtn && createVNode("div", { "class": "".concat(COMPONENT_NAME.value, "__close-btn"), "onClick": handleCloseBtnClick }, [renderTNodeJSX("closeBtn", createVNode(__unplugin_components_2, null, null))]), createVNode("div", { "class": ["".concat(COMPONENT_NAME.value, "__body"), "narrow-scrollbar"] }, [body]), props2.footer && createVNode("div", { "class": "".concat(COMPONENT_NAME.value, "__footer") }, [renderTNodeJSX("footer", defaultFooter)]), props2.sizeDraggable && createVNode("div", { "style": draggableLineStyles.value, "onMousedown": enableDrag }, null)])])]; } }); }; } }); export { _Drawer as default }; //# sourceMappingURL=drawer.js.map