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

Commit

Permalink
refactor: modal component binding types
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Jun 27, 2023
1 parent 34616f4 commit 361008c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions components/App/ModalWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ onKeyStroke('Escape', () => emit('close'))
<span class="i-carbon-close h-6 w-6" aria-hidden="true" />
</button>
</div>

<slot />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion components/App/Modals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { modals } = useModals(props.scope)

<TransitionGroup name="content-fade">
<component
:is="(modal.component as any)"
:is="modal.component"
v-for="(modal, i) in modals"
:key="`${modal.component}${i}`"
v-bind="modal.bindings"
Expand Down
17 changes: 11 additions & 6 deletions composables/modals.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { nanoid } from 'nanoid'
import type { Ref } from 'vue'
import type { ComponentPublicInstance, Ref } from 'vue'

type Component = abstract new (...args: any) => any
type ComponentConstructor<
T extends ComponentPublicInstance<Props> = ComponentPublicInstance<any>,
Props = any
> = new (...args: any[]) => T

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

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

const scopes: Record<string, Ref<Modal[]>> = {}

export function useModals(scope = '') {
const modals = (scopes[scope] = scopes[scope] ?? ref<Modal[]>([]))

async function open<T extends Component>(
async function open<T extends ComponentConstructor>(
component: T,
bindings: Bindings<T>
) {
Expand Down

0 comments on commit 361008c

Please sign in to comment.