UNPKG

tdesign-vue-next

Version:
320 lines (316 loc) 13.5 kB
/** * tdesign v1.11.5 * (c) 2025 tdesign * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { defineComponent, ref, computed, watch, onUpdated, onMounted, onBeforeUnmount, createVNode, Teleport, mergeProps, nextTick } from 'vue'; import { CloseIcon } from 'tdesign-icons-vue-next'; import { usePrefixClass } from '../hooks/useConfig.js'; import { useGlobalIcon } from '../hooks/useGlobalIcon.js'; import { isServer } from '../utils/dom.js'; import { g as getScrollbarWidth } from '../_chunks/dep-3bde6c25.js'; import props from './props.js'; import { useAction } from '../dialog/hooks/useAction.js'; import { u as useTNodeJSX, a as useContent } from '../_chunks/dep-7c56a7f5.js'; import { useDrag } from './hooks/index.js'; import useTeleport from '../hooks/useTeleport.js'; import { useConfig } from '../config-provider/hooks/useConfig.js'; import '../_chunks/dep-caecb55d.js'; import 'lodash-es'; import '../_chunks/dep-56c3d46e.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import '@babel/runtime/helpers/slicedToArray'; import '@babel/runtime/helpers/typeof'; 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 '../_chunks/dep-d60f6309.js'; import '../loading/props.js'; import '../utils/render-tnode.js'; import '../config-provider/utils/context.js'; import '../_chunks/dep-c75b9b8e.js'; import 'dayjs'; import '../utils/withInstall.js'; import '../button/props.js'; import '../hooks/useRipple.js'; import '../hooks/useKeepAnimation.js'; import '../hooks/useDisabled.js'; var key = 1; var _Drawer = defineComponent({ name: "TDrawer", 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 _useGlobalIcon = useGlobalIcon({ CloseIcon: CloseIcon }), CloseIcon$1 = _useGlobalIcon.CloseIcon; 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 || _props2$onConfirm.call(props2, { e: e }); }; var cancelBtnAction = function cancelBtnAction(e) { var _props2$onCancel; (_props2$onCancel = props2.onCancel) === null || _props2$onCancel === 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 () { return [COMPONENT_NAME.value, "".concat(COMPONENT_NAME.value, "--").concat(props2.placement), _defineProperty(_defineProperty(_defineProperty({}, "".concat(COMPONENT_NAME.value, "--open"), isVisible.value), "".concat(COMPONENT_NAME.value, "--attach"), props2.showInAttachedElement), "".concat(COMPONENT_NAME.value, "--without-mask"), !props2.showOverlay)]; }); 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" && isVisible.value) { var _props2$onEscKeydown; (_props2$onEscKeydown = props2.onEscKeydown) === null || _props2$onEscKeydown === 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 || _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) { var _props2$onBeforeOpen, _props2$onBeforeClose; if (isServer) return; if (value) { if (!props2.showInAttachedElement && props2.preventScrollThrough) { styleEl.value && document.head.appendChild(styleEl.value); } } else { clearStyleFunc(); } value ? (_props2$onBeforeOpen = props2.onBeforeOpen) === null || _props2$onBeforeOpen === void 0 ? void 0 : _props2$onBeforeOpen.call(props2) : (_props2$onBeforeClose = props2.onBeforeClose) === null || _props2$onBeforeClose === void 0 ? void 0 : _props2$onBeforeClose.call(props2); 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 || _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 || _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 closeDrawer = function closeDrawer(params) { var _props2$onClose; (_props2$onClose = props2.onClose) === null || _props2$onClose === void 0 || _props2$onClose.call(props2, params); context.emit("update:visible", false); }; onUpdated(function () { updatePushMode(); }); onMounted(function () { var hasScrollBar = window.innerWidth > document.documentElement.clientWidth; 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": handleEscKeydown, "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(CloseIcon$1, 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