Skip to content

EDU-133: Vue and Nuxt Gen 2 - Custom components - Child blocks (all 3 examples) #3961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/sdks-tests/src/snippet-tests/advanced-child.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ test.describe('Advanced child sub components', () => {
'angular-16-ssr',
'gen1-remix',
'gen1-react',
'vue',
'nuxt',
'svelte',
'sveltekit',
'qwik-city',
Expand Down Expand Up @@ -44,6 +46,8 @@ test.describe('Advanced child sub components', () => {
'angular-16-ssr',
'gen1-remix',
'gen1-react',
'vue',
'nuxt',
'svelte',
'sveltekit',
'qwik-city',
Expand Down
4 changes: 4 additions & 0 deletions packages/sdks-tests/src/snippet-tests/custom-child.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ test.describe('Div with Hero class, and text', () => {
'angular-16-ssr',
'gen1-remix',
'gen1-react',
'vue',
'nuxt',
'svelte',
'sveltekit',
'qwik-city',
Expand All @@ -29,6 +31,8 @@ test.describe('Div with Hero class, and text', () => {
'angular-16-ssr',
'gen1-remix',
'gen1-react',
'vue',
'nuxt',
'svelte',
'sveltekit',
'qwik-city',
Expand Down
10 changes: 7 additions & 3 deletions packages/sdks-tests/src/snippet-tests/editable-regions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ test.describe('Editable regions in custom components', () => {
packageName,
}) => {
test.skip(
!['react', 'angular-16', 'angular-16-ssr', 'svelte', 'sveltekit'].includes(packageName)
!['react', 'angular-16', 'angular-16-ssr', 'vue', 'nuxt', 'svelte', 'sveltekit'].includes(
packageName
)
);

await page.goto('/editable-region');
Expand All @@ -31,10 +33,12 @@ test.describe('Editable regions in custom components', () => {
'angular-16-ssr',
'gen1-remix',
'gen1-react',
'svelte',
'sveltekit',
'vue',
'nuxt',
'qwik-city',
'hydrogen',
'svelte',
'sveltekit',
].includes(packageName)
);

Expand Down
24 changes: 24 additions & 0 deletions packages/sdks/snippets/nuxt/components/CustomColumns.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<!-- Left Column -->
<Blocks
:parent="builderBlock.id"
:path="'column1'"
:blocks="column1"
/>
<!-- Right Column -->
<Blocks
:parent="builderBlock.id"
:path="'column2'"
:blocks="column2"
/>
</template>

<script setup lang="ts">
import { Blocks, type BuilderBlock } from '@builder.io/sdk-vue';

const { column1, column2, builderBlock } = defineProps<{
column1: BuilderBlock[];
column2: BuilderBlock[];
builderBlock: BuilderBlock;
}>();
</script>
25 changes: 25 additions & 0 deletions packages/sdks/snippets/nuxt/components/CustomColumnsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { RegisteredComponent } from '@builder.io/sdk-vue';
import CustomColumns from './CustomColumns.vue';

export const CustomColumnsInfo: RegisteredComponent = {
component: CustomColumns,
name: 'MyColumns', // you can define your custom name for the component
inputs: [
{
name: 'column1',
type: 'uiBlocks',
defaultValue: [],
},
{
name: 'column2',
type: 'uiBlocks',
defaultValue: [],
},
],

shouldReceiveBuilderProps: {
builderBlock: true,
},
};

export default CustomColumnsInfo;
5 changes: 5 additions & 0 deletions packages/sdks/snippets/nuxt/components/CustomHero.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>This is text from your component</div>
<!-- The slot will render any child blocks from Builder -->
<slot />
</template>
21 changes: 21 additions & 0 deletions packages/sdks/snippets/nuxt/components/CustomHeroInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { RegisteredComponent } from '@builder.io/sdk-vue';
import CustomHero from './CustomHero.vue';

const CustomHeroInfo: RegisteredComponent = {
component: CustomHero,
name: 'CustomHero',
canHaveChildren: true,
defaultChildren: [
{
'@type': '@builder.io/sdk:Element',
component: {
name: 'Text',
options: {
text: 'This is Builder text',
},
},
},
],
};

export default CustomHeroInfo;
36 changes: 36 additions & 0 deletions packages/sdks/snippets/nuxt/components/CustomTabs.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<div class="dynamics-slots" v-if="tabList.length">
<button
v-for="(tab, index) in tabList"
:key="index"
:class="{ 'active': activeTab === index }"
@click="activeTab = index"
>
{{ tab.tabName }}
</button>

<Blocks
:blocks="tabList[activeTab].blocks"
:path="`tabList.${activeTab}.blocks`"
:parent="builderBlock.id"
/>
</div>
</template>

<script setup lang="ts">
import { ref, defineProps } from 'vue';
import { Blocks, type BuilderBlock } from '@builder.io/sdk-vue';

const activeTab = ref(0);

const { builderBlock, tabList } = defineProps<{
builderBlock: BuilderBlock,
tabList: Array<{
tabName: string;
blocks: BuilderBlock[];
}>
}>();

</script>


29 changes: 29 additions & 0 deletions packages/sdks/snippets/nuxt/components/CustomTabsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { RegisteredComponent } from '@builder.io/sdk-vue';
import CustomTabs from './CustomTabs.vue';

export const CustomTabsInfo: RegisteredComponent = {
component: CustomTabs,
name: 'TabFields',
inputs: [
{
name: 'tabList',
type: 'list',
subFields: [
{
name: 'tabName',
type: 'string',
},
{
name: 'blocks',
type: 'uiBlocks',
defaultValue: [],
},
],
},
],
shouldReceiveBuilderProps: {
builderBlock: true,
},
};

export default CustomTabsInfo;
35 changes: 35 additions & 0 deletions packages/sdks/snippets/nuxt/pages/advanced-child.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script setup lang="ts">
import { Content, fetchOneEntry, isPreviewing } from '@builder.io/sdk-vue';
import CustomTabsInfo from '../components/CustomTabsInfo';
import { useRoute } from 'nuxt/app';

const route = useRoute();
const model = 'advanced-child';
const apiKey = 'ee9f13b4981e489a9a1209887695ef2b';

const { data: content, pending } = await useAsyncData(
`builder-${model}-${route.path}`,
() => fetchOneEntry({
model,
apiKey,
userAttributes: {
urlPath: route.path,
},
})
);
</script>

<template>
<div>
<div v-if="pending">Loading...</div>
<div v-else-if="!content && !isPreviewing()">404 - Not Found</div>
<Content
v-else
:content="content"
:model="model"
:apiKey="apiKey"
:customComponents="[CustomTabsInfo]"
/>
</div>
</template>

33 changes: 33 additions & 0 deletions packages/sdks/snippets/nuxt/pages/custom-child.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script setup lang="ts">
import { fetchOneEntry, Content, isPreviewing } from '@builder.io/sdk-vue';
import CustomHeroInfo from '../components/CustomHeroInfo';
import { useRoute } from 'nuxt/app';

const route = useRoute();
const model = 'custom-child';
const apiKey = 'ee9f13b4981e489a9a1209887695ef2b';

const { data: content, pending } = await useAsyncData(
`builder-${model}-${route.path}`,
() => fetchOneEntry({
model,
apiKey,
userAttributes: {
urlPath: route.path,
},
})
);
</script>

<template>
<div v-if="pending">Loading...</div>
<div v-else-if="!content && !isPreviewing()">404 - Not Found</div>
<Content
v-else
:content="content"
:model="model"
:apiKey="apiKey"
:customComponents="[CustomHeroInfo]"
/>
</template>

34 changes: 34 additions & 0 deletions packages/sdks/snippets/nuxt/pages/editable-region.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template>
<div>
<div v-if="pending">Loading...</div>
<div v-else-if="!content && !isPreviewing()">404 - Not Found</div>
<Content
v-else
:content="content"
:model="model"
:apiKey="apikey"
:customComponents="[CustomColumnsInfo]"
/>
</div>
</template>

<script setup lang="ts">
import { Content, fetchOneEntry, isPreviewing } from '@builder.io/sdk-vue';
import CustomColumnsInfo from '../components/CustomColumnsInfo';
import { useRoute } from 'nuxt/app';

const route = useRoute();
const model = 'editable-regions';
const apiKey = 'ee9f13b4981e489a9a1209887695ef2b';

const { data: content, pending } = await useAsyncData(
`builder-${model}-${route.path}`,
() => fetchOneEntry({
model,
apiKey,
userAttributes: {urlPath: route.path},
})
);

</script>

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<!-- Left Column -->
<Blocks
:parent="builderBlock.id"
:path="'column1'"
:blocks="column1"
/>
<!-- Right Column -->
<Blocks
:parent="builderBlock.id"
:path="'column2'"
:blocks="column2"
/>
</template>

<script setup lang="ts">
import { Blocks, type BuilderBlock } from '@builder.io/sdk-vue';

const { column1, column2, builderBlock } = defineProps<{
column1: BuilderBlock[];
column2: BuilderBlock[];
builderBlock: BuilderBlock;
}>();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import CustomColumns from '@/components/custom-components/custom-columns/CustomColumns.vue';
import type { RegisteredComponent } from '@builder.io/sdk-vue';

export const CustomColumnsInfo: RegisteredComponent = {
component: CustomColumns,
name: 'MyColumns', // you can define your custom name for the component
inputs: [
{
name: 'column1',
type: 'uiBlocks',
defaultValue: [],
},
{
name: 'column2',
type: 'uiBlocks',
defaultValue: [],
},
],

shouldReceiveBuilderProps: {
builderBlock: true,
},
};

export default CustomColumnsInfo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<section>
<div>This is text from your component</div>
<!-- The slot will render any child blocks from Builder -->
<slot />
</section>
</template>

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import CustomHero from '@/components/custom-components/custom-hero/CustomHero.vue';
import type { RegisteredComponent } from '@builder.io/sdk-vue';

const customHeroInfo: RegisteredComponent = {
component: CustomHero,
name: 'CustomHero',
canHaveChildren: true,
defaultChildren: [
{
'@type': '@builder.io/sdk:Element',
component: {
name: 'Text',
options: {
text: 'This is Builder text',
},
},
},
],
};

export default customHeroInfo;
Loading
Loading