Skip to content

Commit

Permalink
Merge pull request #425 from storyblok/feature/403-defineexpose-not-w…
Browse files Browse the repository at this point in the history
…orking-with-storyblokcomponent

feat: 403 defineexpose not working with storyblokcomponent
  • Loading branch information
alvarosabu authored Sep 29, 2023
2 parents f99b3f9 + 40355de commit 772ff72
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
19 changes: 15 additions & 4 deletions lib/StoryblokComponent.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<template>
<component :is="componentName" v-bind="{ ...$props, ...$attrs }"></component>
</template>

<script setup lang="ts">
import { ref, resolveDynamicComponent, inject } from "vue";
import type { SbBlokData, SbVueSDKOptions } from "./types";
Expand All @@ -10,6 +6,13 @@ export interface SbComponentProps {
blok: SbBlokData;
}
const props = defineProps<SbComponentProps>();
const blokRef = ref();
defineExpose({
value: blokRef,
});
const componentFound: boolean =
typeof resolveDynamicComponent(props.blok.component) !== "string";
Expand All @@ -34,3 +37,11 @@ if (!componentFound) {
}
}
</script>

<template>
<component
ref="blokRef"
:is="componentName"
v-bind="{ ...$props, ...$attrs }"
></component>
</template>
16 changes: 12 additions & 4 deletions playground/components/Feature.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<script setup>
defineProps({ blok: Object });
function featureMethod() {
console.log("Hi from Feature.vue");
}
defineExpose({
featureMethod,
});
</script>

<template>
<div v-editable="blok" class="py-2" data-test="feature">
<h1 class="text-lg">{{ blok.name }}</h1>
</div>
</template>

<script setup>
defineProps({ blok: Object });
</script>
22 changes: 17 additions & 5 deletions playground/components/Grid.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
<script setup>
import { ref, watchEffect } from "vue";
defineProps({ blok: Object });
const blokRefs = ref([]);
watchEffect(() => {
if (blokRefs.value.length > 0) {
blokRefs.value.forEach((blokRef) => {
blokRef.value.featureMethod();
});
}
});
</script>

<template>
<div v-editable="blok" class="flex py-8 mb-6" data-test="grid">
<div v-for="blok in blok.columns" :key="blok._uid" class="flex-auto px-6">
<StoryblokComponent :blok="blok" />
<StoryblokComponent ref="blokRefs" :blok="blok" />
</div>
</div>
</template>

<script setup>
defineProps({ blok: Object });
</script>
8 changes: 4 additions & 4 deletions playground/components/Page.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script setup>
defineProps({ blok: Object });
</script>

<template>
<div v-editable="blok" class="px-6" data-test="page">
<StoryblokComponent
Expand All @@ -7,7 +11,3 @@
/>
</div>
</template>

<script setup>
defineProps({ blok: Object });
</script>
8 changes: 4 additions & 4 deletions playground/components/Teaser.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script setup>
defineProps({ blok: Object });
</script>

<template>
<div
v-editable="blok"
Expand All @@ -8,7 +12,3 @@
{{ blok.headline }}
</div>
</template>

<script setup>
defineProps({ blok: Object });
</script>

0 comments on commit 772ff72

Please sign in to comment.