feat: add codesandbox

This commit is contained in:
zernonia 2023-10-21 21:14:21 +08:00
parent 511ce84942
commit 616ffdca5e
5 changed files with 43 additions and 40 deletions

View File

@ -3,26 +3,19 @@ import { onMounted, ref } from 'vue'
import { Icon } from '@iconify/vue' import { Icon } from '@iconify/vue'
import { makeCodeSandboxParams } from '../utils/codeeditor' import { makeCodeSandboxParams } from '../utils/codeeditor'
import Tooltip from './Tooltip.vue' import Tooltip from './Tooltip.vue'
import { Button } from '@/lib/registry/new-york/ui/button'
import { type Style } from '@/lib/registry/styles'
const props = defineProps<{ const props = defineProps<{
name: string name: string
code: string
style: Style
}>() }>()
const sources = ref<Record<string, string>>({}) const sources = ref<Record<string, string>>({})
onMounted(() => { onMounted(() => {
// props.files?.forEach((file) => { sources.value['App.vue'] = props.code
// if (file.endsWith('.vue')) {
// import(`../components/demo/${props.name}/${file.replace('.vue', '')}.vue?raw`).then(
// res => (sources.value[file] = res.default),
// )
// }
// else if (file.endsWith('.js')) {
// import(`../components/demo/${props.name}/${file.replace('.js', '')}.js?raw`).then(
// res => (sources.value[file] = res.default),
// )
// }
// })
}) })
</script> </script>
@ -31,12 +24,12 @@ onMounted(() => {
<input type="hidden" name="query" value="file=src/App.vue"> <input type="hidden" name="query" value="file=src/App.vue">
<input type="hidden" name="environment" value="server"> <input type="hidden" name="environment" value="server">
<input type="hidden" name="hidedevtools" value="1"> <input type="hidden" name="hidedevtools" value="1">
<input type="hidden" name="parameters" :value="makeCodeSandboxParams(name, sources)"> <input type="hidden" name="parameters" :value="makeCodeSandboxParams(name, style, sources)">
<Tooltip :content="`Open ${name} in CodeSandbox`"> <Tooltip :content="`Open ${name} in CodeSandbox`">
<button type="submit"> <Button :variant="'ghost'" :size="'icon'" type="submit">
<Icon icon="ph-codesandbox-logo" /> <Icon icon="ph-codesandbox-logo" />
</button> </Button>
</Tooltip> </Tooltip>
</form> </form>
</template> </template>

View File

@ -2,6 +2,7 @@
import StyleSwitcher from './StyleSwitcher.vue' import StyleSwitcher from './StyleSwitcher.vue'
import ComponentLoader from './ComponentLoader.vue' import ComponentLoader from './ComponentLoader.vue'
import Stackblitz from './Stackblitz.vue' import Stackblitz from './Stackblitz.vue'
import CodeSandbox from './CodeSandbox.vue'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/lib/registry/default/ui/tabs' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/lib/registry/default/ui/tabs'
import { useConfigStore } from '@/stores/config' import { useConfigStore } from '@/stores/config'
import { cn } from '@/lib/utils' import { cn } from '@/lib/utils'
@ -40,7 +41,11 @@ const { style } = useConfigStore()
<TabsContent value="preview" class="relative rounded-md border"> <TabsContent value="preview" class="relative rounded-md border">
<div class="flex items-center justify-between p-4"> <div class="flex items-center justify-between p-4">
<StyleSwitcher /> <StyleSwitcher />
<Stackblitz :key="style" :style="style" :name="name" :code="decodeURIComponent(sfcTsCode ?? '')" />
<div class="flex items-center gap-x-1">
<Stackblitz :key="style" :style="style" :name="name" :code="decodeURIComponent(sfcTsCode ?? '')" />
<CodeSandbox :key="style" :style="style" :name="name" :code="decodeURIComponent(sfcTsCode ?? '')" />
</div>
</div> </div>
<div <div
:class="cn('preview flex min-h-[350px] w-full justify-center p-6 lg:p-10', { :class="cn('preview flex min-h-[350px] w-full justify-center p-6 lg:p-10', {

View File

@ -3,6 +3,7 @@ import { onMounted, ref } from 'vue'
import { Icon } from '@iconify/vue' import { Icon } from '@iconify/vue'
import { makeStackblitzParams } from '../utils/codeeditor' import { makeStackblitzParams } from '../utils/codeeditor'
import Tooltip from './Tooltip.vue' import Tooltip from './Tooltip.vue'
import { Button } from '@/lib/registry/new-york/ui/button'
import { type Style } from '@/lib/registry/styles' import { type Style } from '@/lib/registry/styles'
const props = defineProps<{ const props = defineProps<{
@ -25,9 +26,9 @@ function handleClick() {
<template> <template>
<div> <div>
<Tooltip :content="`Open ${name} in Stackblitz`"> <Tooltip :content="`Open ${name} in Stackblitz`">
<button @click="handleClick"> <Button :variant="'ghost'" :size="'icon'" @click="handleClick">
<Icon icon="simple-icons:stackblitz" /> <Icon icon="simple-icons:stackblitz" />
</button> </Button>
</Tooltip> </Tooltip>
</div> </div>
</template> </template>

View File

@ -119,26 +119,29 @@ watch(() => $route.path, (n) => {
</div> </div>
</Button> </Button>
<div <div class="flex items-center gap-x-1">
v-for="link in links" <Button
:key="link.name" v-for="link in links"
class="flex items-center space-x-1" :key="link.name"
> as="a"
<a :href="link.href" target="_blank" class="text-foreground"> :href="link.href" target="_blank"
<component :is="link.icon" class="w-5 h-5" /> :variant="'ghost'" :size="'icon'"
</a> >
</div> <component :is="link.icon" class="w-[20px] h-[20px]" />
</Button>
<button <Button
class="flex items-center justify-center" class="flex items-center justify-center"
aria-label="Toggle dark mode" aria-label="Toggle dark mode"
@click="toggleDark()" :variant="'ghost'"
> :size="'icon'" @click="toggleDark()"
<component >
:is="isDark ? RadixIconsSun : RadixIconsMoon" <component
class="w-5 h-5 text-foreground" :is="isDark ? RadixIconsSun : RadixIconsMoon"
/> class="w-[20px] h-[20px] text-foreground"
</button> />
</Button>
</div>
</div> </div>
</div> </div>
</header> </header>

View File

@ -86,6 +86,7 @@ function constructFiles(componentName: string, style: Style, sources: Record<str
'tailwindcss-animate': 'latest', 'tailwindcss-animate': 'latest',
[iconPackage]: 'latest', [iconPackage]: 'latest',
'shadcn-vue': 'latest', 'shadcn-vue': 'latest',
'typescript': 'latest',
} }
const devDependencies = { const devDependencies = {
@ -119,8 +120,8 @@ function constructFiles(componentName: string, style: Style, sources: Record<str
const files = { const files = {
'package.json': { 'package.json': {
content: { content: {
name: `radix-vue-${componentName.toLowerCase().replace(/ /g, '-')}`, name: `shadcn-vue-${componentName.toLowerCase().replace(/ /g, '-')}`,
scripts: { start: `npx shadcn-vue@latest add ${registryDependencies.join(' ')} -y && vite` }, scripts: { start: `shadcn-vue add ${registryDependencies.join(' ')} -y && vite` },
dependencies, dependencies,
devDependencies, devDependencies,
}, },
@ -201,7 +202,7 @@ createApp(App).mount('#app')`,
} }
#app { #app {
@apply w-full flex items-center justify-center; @apply w-full flex items-center justify-center px-12;
}`, }`,
isBinary: false, isBinary: false,
}, },