Files
ui/docs/app/components/content/ComponentExample.vue
2024-08-06 11:22:01 +02:00

56 lines
1.3 KiB
Vue

<script setup lang="ts">
import { camelCase } from 'scule'
const props = defineProps<{
name: string
class?: any
props?: { [key: string]: any }
/**
* Whether to format the code with Prettier
* @defaultValue false
*/
prettier?: boolean
}>()
const { $prettier } = useNuxtApp()
const camelName = camelCase(props.name)
const data = await fetchComponentExample(camelName)
const componentProps = reactive({ ...(props.props || {}) })
const code = computed(() => `\`\`\`vue\n${data?.code ?? ''}\n\`\`\``)
const { data: ast } = await useAsyncData(`component-example-${camelName}`, async () => {
if (!props.prettier) {
return parseMarkdown(code.value)
}
let formatted = ''
try {
formatted = await $prettier.format(code.value, {
trailingComma: 'none',
semi: false,
singleQuote: true
})
} catch (e) {
formatted = code.value
}
return parseMarkdown(formatted)
}, { watch: [code] })
</script>
<template>
<div class="my-5">
<div>
<div class="flex border border-b-0 border-gray-300 dark:border-gray-700 relative p-4 rounded-t-md" :class="[props.class]">
<component :is="camelName" v-bind="componentProps" />
</div>
</div>
<MDCRenderer v-if="ast" :body="ast.body" :data="ast.data" class="[&>div>pre]:!rounded-t-none [&>div]:!mt-0" />
</div>
</template>