An open API service indexing awesome lists of open source software.

https://github.com/ztrehagem/vue-modal

Stacking-managed styleless modal library for Vue.js.
https://github.com/ztrehagem/vue-modal

component modal vue vue3 vuejs

Last synced: 3 months ago
JSON representation

Stacking-managed styleless modal library for Vue.js.

Awesome Lists containing this project

README

        

# @ztrehagem/vue-modal

![](https://img.shields.io/npm/v/@ztrehagem/vue-modal/latest)
![](https://img.shields.io/npm/dependency-version/@ztrehagem/vue-modal/peer/vue)
![](https://img.shields.io/npm/types/@ztrehagem/vue-modal)
![](https://img.shields.io/github/license/ztrehagem/vue-modal)

Stacking-managed styleless modal library for Vue.js.

## Features

The modals are managed as a stack structure with functions such as push and pop.
Only the top of the stack is always rendered.
State of stacked modals are kept for each instance by using ``.
In addition, multiple instances of the same modal component can be in the stack.

## Installation

```sh
npm install @ztrehagem/vue-modal
```

## Usage

1. Declare all modal names and their argument schema.
In this example, there is a modal named `hello` with an argument `{ nickname: string }`.

```tsx
export interface ModalTypes {
hello: { nickname: string };
}
```

2. Implement modal components declared above.

```tsx
// HelloModal.tsx
import { useModal, VueModal } from "@ztrehagem/vue-modal";

export default defineComponent({
props: {
args: {
type: Object as PropType,
required: true,
},
},

setup(props) {
const modal = useModal();

const closeModal = (e: Event) => {
e.preventDefault();
modal.pop();
};

return () => (

Hello, {props.args.nickname}!


closeModal


);
},
});
```

3. Implement calling those modals, and mount `` and `` in the root component.

```tsx
// App.tsx
import {
useModal,
VueModalBackdrop,
VueModalRenderer,
} from "@ztrehagem/vue-modal";

export default defineComponent({
setup() {
const modal = useModal();

const nickname = ref("");

const openModal = (e: Event) => {
e.preventDefault();
modal.push("hello", { nickname: nickname.value });
};

return () => (



openModal




);
},
});
```

4. Create the ModalManager instance with the ModalTypes, and provide components for each name declared. Then install the instance to the vue app instance.

```ts
import { ModalManager } from "@ztrehagem/vue-modal";
import HelloModal from "./HelloModal";
import App from "./App";

// Styles for and .
// If you don't use these library components, this is not required.
import "@ztrehagem/vue-modal/style.css";

const modalManager = new ModalManager();
modalManager.addComponent("hello", HelloModal);

// Dynamic import is available through async components.
const asyncHelloModal = defineAsyncComponent(() => import("./HelloModal"));
modalManager.addComponent("hello", asyncHelloModal);

const app = createApp(App);
app.use(modalManager);
app.mount("#app");
```

See [src](./src) in this repo for example usage.

### Library Components

- [``](src/lib/VueModalRenderer.tsx) renders modal components with `` and ``.
- [``](src/lib/VueModal.tsx) provides default styles for CSS transition of modal components. It is optional to use.
- [``](src/lib/VueModalBackdrop.tsx) provides default styles for backdrop of modals. It is also optional to use.

## API

### class `ModalManager`

- see [src/lib/ModalManager.ts](src/lib/ModalManager.ts)