Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
feat: add scopes & use nanoid
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmarrec committed Sep 30, 2022
1 parent 678b0d2 commit b65ff32
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
"eslint": "latest",
"nuxt": "^3.0.0-rc.11",
"typescript": "latest"
},
"dependencies": {
"nanoid": "^4.0.0"
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

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

20 changes: 12 additions & 8 deletions src/runtime/components/NuxtModals.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<script setup>
import { useModals } from '#imports'
defineProps({
const props = defineProps({
scope: {
type: String,
required: false,
},
transition: {
type: String,
default: 'modal',
default: 'default',
},
})
const { modals } = useModals()
const { modals } = useModals(props.scope)
</script>

<template>
Expand All @@ -21,14 +25,14 @@ const { modals } = useModals()
</TransitionGroup>
</template>

<style lang="postcss">
.modal-enter-active,
.modal-leave-active {
<style lang="postcss" scoped>
.default-enter-active,
.default-leave-active {
transition: opacity 0.3s ease-out;
}
.modal-enter-from,
.modal-leave-to {
.default-enter-from,
.default-leave-to {
opacity: 0;
}
</style>
15 changes: 7 additions & 8 deletions src/runtime/composables/useModals.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { nanoid } from 'nanoid'
import type { Ref } from 'vue'
import { markRaw, ref } from 'vue'

type Component = abstract new (...args: any) => any

interface Modal {
id: number
id: string
component: Component
bindings: Record<string, any>
}

type Bindings<T extends Component> = InstanceType<T>['$props']
type ReturnValue<T extends Component> = Parameters<Bindings<T>['onClose']>[0]

const modals = ref<Modal[]>([])
let index = 0
const scopes: Record<string, Ref<Modal[]>> = {}

export function useModals () {
function generateId (): number {
return index++
}
export function useModals (scope = '') {
const modals = scopes[scope] = scopes[scope] ?? ref<Modal[]>([])

async function open <T extends Component> (component: T, bindings: Bindings<T>): Promise<ReturnValue<T>> {
return new Promise((resolve) => {
const id = generateId()
const id = nanoid()
modals.value.push({
id,
component: markRaw(component),
Expand Down

0 comments on commit b65ff32

Please sign in to comment.