Skip to content

Commit

Permalink
fix: 修复层级显示问题,并提供全局挂载点
Browse files Browse the repository at this point in the history
  • Loading branch information
BBBboys committed Oct 10, 2024
1 parent 74984ce commit cdb617f
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 26 deletions.
17 changes: 17 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
export {}

/* prettier-ignore */
declare module 'vue' {
export interface GlobalComponents {
ElButton: typeof import('element-plus/es')['ElButton']
ElConfigProvider: typeof import('element-plus/es')['ElConfigProvider']
ElDivider: typeof import('element-plus/es')['ElDivider']
ElLink: typeof import('element-plus/es')['ElLink']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
}
}
8 changes: 7 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<!doctype html>
<html lang="en" class="dark">


<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover" />
<!-- 开启顶部安全区适配 -->
<van-nav-bar safe-area-inset-top />
<!-- 开启底部安全区适配 -->
<van-number-keyboard safe-area-inset-bottom />
<title>example</title>
</head>

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
"sass": "^1.79.4",
"semantic-release": "^21.0.1",
"typescript": "^5.5.3",
"unplugin-vue-components": "^0.27.4",
"unplugin-vue-router": "^0.10.8",
"vant": "~4.9.7",
"vite": "^5.4.1",
"vite-plugin-dts": "^4.2.2",
"vue": "^3.5.3",
"vue-router": "^4.4.5",
"vue-tsc": "^2.0.29",
"vue3-command-dialog": "~1.1.5"
"vue3-command-dialog": "~1.1.6"
},
"peerDependencies": {
"element-plus": "2.x",
Expand Down
51 changes: 46 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/components/Core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export function CommandDialogProvider(parentInstance: ComponentInternalInstance
container.className = "command-commponent-container"

appendToElement.appendChild(container);
const zIndex = getMaxZIndex(container);
const MaxZIndex = getMaxZIndex(container);
// 设置节点层级,尽量让其显示出来
container.style.position = 'relative';
container.style.zIndex = String(zIndex + 1);
container.style.zIndex = String(Math.max(MaxZIndex + 1, 9999));

const hide = () => {
config.visible.value = false;
Expand Down
12 changes: 11 additions & 1 deletion src/components/ElementPlusDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import type { ICommandDialogArrtsProviderConfig } from "./Core";
import { CommandDialogProvider } from "./Core";
import { busName2EventName, eventName2BusName } from "./utils";
import { EVENT_NAME } from "./type";
// 引入dialog组件的样式
import "element-plus/es/components/dialog/style/css";


export type IElementPlusConfig = {
// 目标ui库目标组件的插槽
Expand All @@ -26,6 +29,13 @@ export type IElementPlusConfig = {

} & ICommandDialogArrtsProviderConfig & Record<string, any>;


// ElementPlusDialog全局挂载点
let mountNode: HTMLElement | undefined = void 0
export const setMountNode = (node: HTMLElement | undefined) => {
mountNode = node
}

export const createElementPlusDialog = (immediately = true) => {
// 我们需要捕获使用命令式组件的的组件实例,我们会用它来获取上下文
const parentInstance = getCurrentInstance();
Expand Down Expand Up @@ -95,7 +105,7 @@ export const createElementPlusDialog = (immediately = true) => {
})),
{
provideProps: config.provideProps || {},
appendTo: config.appendTo,
appendTo: mountNode || config.appendTo,
visible,
}
);
Expand Down
10 changes: 9 additions & 1 deletion src/components/VantUiPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { ICommandDialogArrtsProviderConfig } from "./Core";
import { CommandDialogProvider } from "./Core";
import { EVENT_NAME } from "./type";
import { merge } from "lodash-es";
// 引入vant Popup组件的样式
import "vant/es/popup/style";

export type IVantUiConfig = {
// 目标ui库目标组件的插槽
Expand All @@ -18,6 +20,12 @@ export type IVantUiConfig = {

} & ICommandDialogArrtsProviderConfig & Record<string, any>;

// VantUiPopup全局挂载点
let mountNode: HTMLElement | undefined = void 0
export const setMountNode = (node: HTMLElement | undefined) => {
mountNode = node
}

// 默认属性
const defaultProps: IVantUiConfig = {
overlay: true,
Expand Down Expand Up @@ -78,7 +86,7 @@ export const createVantUiPopup = (immediately = true) => {
})),
{
provideProps: config.provideProps || {},
appendTo: config.appendTo,
appendTo: mountNode || config.appendTo,
visible,
}
);
Expand Down
9 changes: 0 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ import { createApp } from "vue";
import { router } from "./router";
import App from "./App.vue";

import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import "element-plus/theme-chalk/dark/css-vars.css";
import Vant from "vant";
import "vant/lib/index.css";

const app = createApp(App);

app.provide("main", "来自main的🩷");

app.use(ElementPlus);
app.use(Vant);

app.use(router);
app.mount("#app");
3 changes: 2 additions & 1 deletion src/pages/example/base/compatible-native-attributes.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="tsx" setup>
import { createElementPlusDialog } from "vue3-command-dialog";
import { createElementPlusDialog } from "../../../components";
import { ref, h, defineComponent } from "vue";
const CommandDialog = createElementPlusDialog();
Expand Down
5 changes: 3 additions & 2 deletions src/pages/example/base/components/Content.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script lang="tsx" setup>
import { getCommandDialogConsumer } from "vue3-command-dialog";
import Base from "../index.vue";
import { getCommandDialogConsumer } from "../../../../components";
import { getCurrentInstance, inject, provide, ref } from "vue";
import { useRoute } from "vue-router";
import Base from "../index.vue";
const nested = useRoute().query.nested === "true";
Expand Down
2 changes: 1 addition & 1 deletion src/pages/example/base/confirm.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="tsx" setup>
import { createElementPlusDialog, getCommandDialogConsumer, EVENT_NAME } from "vue3-command-dialog";
import { createElementPlusDialog, EVENT_NAME, getCommandDialogConsumer } from "../../../components";
import { defineComponent } from "vue";
const CommandDialog = createElementPlusDialog();
Expand Down
3 changes: 1 addition & 2 deletions src/pages/example/base/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="tsx" setup>
// 实际应该书写为:
import { createElementPlusDialog, createVantUiPopup, createVantUiTitlePopupOnBottom } from "vue3-command-dialog";
import { createElementPlusDialog, createVantUiPopup, createVantUiTitlePopupOnBottom } from "../../../components";
import Content from "./components/Content.vue";
import { provide } from "vue";
Expand Down
6 changes: 6 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import VueRouter from "unplugin-vue-router/vite";
import dts from "vite-plugin-dts";
import Components from "unplugin-vue-components/vite";
import { VantResolver, ElementPlusResolver } from "unplugin-vue-components/resolvers";

export default defineConfig({
plugins: [
Expand All @@ -13,6 +15,10 @@ export default defineConfig({
dts({
tsconfigPath: "./tsconfig.types.json",
}),
// vant 组件自动按需引入
Components({
resolvers: [VantResolver(), ElementPlusResolver()],
}),
],
server: {
port: 7263,
Expand Down

0 comments on commit cdb617f

Please sign in to comment.