Skip to content

Commit 53f29e7

Browse files
committed
refactor: enhanced WebPreview loading slot functionality
1 parent da5aa11 commit 53f29e7

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

packages/elements/__tests__/web-preview.spec.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ describe('webPreviewBody', () => {
148148
expect(iframe).toHaveAttribute('sandbox')
149149
})
150150

151-
it('renders loading component', () => {
151+
it('renders loading slot', () => {
152152
const screen = render(
153153
<WebPreview>
154-
<WebPreviewBody loading={<div>Loading...</div>} />
154+
<WebPreviewBody>
155+
{{
156+
loading: () => <div>Loading...</div>,
157+
}}
158+
</WebPreviewBody>
155159
</WebPreview>,
156160
)
157161
expect(screen.getByText('Loading...')).toBeInTheDocument()

packages/elements/src/web-preview/WebPreview.vue

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import {
66
provideWebPreviewContext,
77
} from './context'
88
9-
interface Props {
9+
interface Props extends /* @vue-ignore */ HTMLAttributes {
1010
class?: HTMLAttributes['class']
1111
defaultUrl?: string
12-
url?: string
13-
consoleOpen?: boolean
1412
}
1513
1614
const props = withDefaults(defineProps<Props>(), {
@@ -25,7 +23,7 @@ const emit = defineEmits<{
2523
}>()
2624
2725
const url = ref(props.defaultUrl)
28-
const consoleOpen = ref(props.consoleOpen ?? false)
26+
const consoleOpen = ref(false)
2927
3028
function setUrl(value: string) {
3129
url.value = value
@@ -46,13 +44,17 @@ provideWebPreviewContext({
4644
setConsoleOpen,
4745
})
4846
49-
const classes = computed(() =>
50-
cn('flex size-full flex-col rounded-lg border bg-card', props.class),
51-
)
47+
const vBind = computed(() => {
48+
const { class: _, ...rest } = props
49+
return {
50+
class: cn('flex size-full flex-col rounded-lg border bg-card', props.class),
51+
...rest,
52+
}
53+
})
5254
</script>
5355

5456
<template>
55-
<div :class="classes" v-bind="$attrs">
57+
<div v-bind="vBind">
5658
<slot />
5759
</div>
5860
</template>
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<script setup lang="ts">
2-
import type { HTMLAttributes, VNodeChild } from 'vue'
2+
import type { IframeHTMLAttributes, VNodeChild } from 'vue'
33
import { cn } from '@repo/shadcn-vue/lib/utils'
44
import { computed, useAttrs } from 'vue'
55
import { useWebPreviewContext } from './context'
66
7-
interface Props {
8-
class?: HTMLAttributes['class']
7+
interface Props extends /* @vue-ignore */ IframeHTMLAttributes {
8+
class?: IframeHTMLAttributes['class']
99
src?: string
10-
loading?: VNodeChild
1110
}
1211
1312
const props = defineProps<Props>()
14-
const attrs = useAttrs()
1513
14+
defineSlots<{
15+
loading: () => VNodeChild
16+
}>()
17+
18+
const attrs = useAttrs()
1619
const { url } = useWebPreviewContext()
1720
1821
const frameSrc = computed(() => (props.src ?? url.value) || undefined)
@@ -27,8 +30,6 @@ const frameSrc = computed(() => (props.src ?? url.value) || undefined)
2730
title="Preview"
2831
v-bind="attrs"
2932
/>
30-
<slot name="loading">
31-
<component :is="props.loading" v-if="props.loading" />
32-
</slot>
33+
<slot name="loading" />
3334
</div>
3435
</template>

packages/elements/src/web-preview/WebPreviewNavigation.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { HTMLAttributes } from 'vue'
33
import { cn } from '@repo/shadcn-vue/lib/utils'
44
5-
interface Props {
5+
interface Props extends /* @vue-ignore */ HTMLAttributes {
66
class?: HTMLAttributes['class']
77
}
88

0 commit comments

Comments
 (0)