Skip to content

Commit

Permalink
fix: init state sometimes not being taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 12, 2022
1 parent 9d5b969 commit fb9c775
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { App, createApp, onMounted, onUnmounted, PropType, ref } from 'vue'
import { App, createApp, onMounted, onUnmounted, PropType, ref, watch } from 'vue'
import { Story, Variant } from '../../types'
const props = defineProps({
Expand All @@ -21,8 +21,11 @@ const props = defineProps({
const sandbox = ref<HTMLDivElement>()
let app: App
let mounting = false
async function mountVariant () {
mounting = true
onMounted(async () => {
await props.variant.initState()
app = createApp({
Expand All @@ -35,10 +38,22 @@ onMounted(async () => {
const target = document.createElement('div')
sandbox.value.appendChild(target)
app.mount(target)
}
onMounted(async () => {
if (props.variant.initState) {
await mountVariant()
}
})
watch(() => props.variant.initState, value => {
if (value && !mounting) {
mountVariant()
}
})
onUnmounted(() => {
app.unmount()
app?.unmount()
})
</script>

Expand Down
2 changes: 1 addition & 1 deletion packages/histoire/src/client/app/util/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function mapVariant (variant: Variant): Variant {
return {
...variant,
state: {},
initState: () => ({}),
initState: null,
slots: () => ({}),
}
}

0 comments on commit fb9c775

Please sign in to comment.