-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Nuxt App created only when story vue app rendered first time, (#830
) <!-- ☝️ PR title should follow conventional commits (https://conventionalcommits.org). In particular, the title should start with one of the following types: - docs: 📖 Documentation (updates to the documentation or readme) - fix: 🐞 Bug fix (a non-breaking change that fixes an issue) - feat: ✨ New feature/enhancement (a non-breaking change that adds functionality or improves existing one) - feat!/fix!:⚠️ Breaking change (fix or feature that would cause existing functionality to change) - chore: 🧹 Chore (updates to the build process or auxiliary tools and libraries) --> ### 🔗 Linked issue <!-- If it resolves an open issue, please link the issue here. For example "Resolves #123" --> ### 📚 Description The issue arose because, in the setup function, the code was checking if the Nuxt context with storyContext.id was already used by the Nuxt app. This prevented the recreation of the Nuxt app, which led to a bug where revisiting the same story caused its Vue app to lack a Nuxt instance. The bug specifically occurred when using modules that register plugins with the Vue app, such as i18n. On the first render, the story was correctly displayed with the registered plugin (e.g., translations). However, on subsequent renders, the Vue app couldn't find the registered plugin. Solution: Use canvasElement.id as a unique key for differentiation. Create a new Nuxt app each time a story’s Vue app is rendered. Remove the check for context.tryUse() since a new Vue app is always created and mounted for each story. This ensures that every story render gets a fresh and fully initialized Nuxt app. Changes: I added i18n example to playground and example/showcase I configured Vite bundler to alias "vue" to "vue/dist/vue.esm-bundler.js, to solve the runtime compilation issue for custom string template
- Loading branch information
Showing
15 changed files
with
1,264 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Meta, StoryObj } from '@storybook/vue3' | ||
|
||
import MyComponent from './MyI18n.vue' | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/vue/writing-stories/introduction | ||
|
||
const meta = { | ||
title: 'Modules/I18n', | ||
component: MyComponent, | ||
argTypes: { | ||
lang: { control: 'select', options: ['en', 'fr', 'ar'] }, | ||
}, | ||
|
||
// This component will have an automatically generated docsPage entry: https://storybook.js.org/docs/vue/writing-docs/autodocs | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof MyComponent> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
/* | ||
*👇 Render functions are a framework specific feature to allow you control on how the component renders. | ||
* See https://storybook.js.org/docs/vue/api/csf | ||
* to learn how to use render functions. | ||
*/ | ||
|
||
export const FrenchStory: Story = { | ||
args: { lang: 'fr' }, | ||
} | ||
|
||
export const EnglishStory: Story = { | ||
args: { lang: 'en' }, | ||
} | ||
|
||
export const ArabicStory: Story = { | ||
args: { lang: 'ar' }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<script lang="ts" setup> | ||
import '../assets/button.css' | ||
const props = defineProps<{ | ||
lang: string | ||
message?: string | ||
}>() | ||
const { t, locale } = useI18n() | ||
const rtl = computed(() => props.lang === 'ar') | ||
locale.value = props.lang | ||
watch( | ||
() => props.lang, | ||
(lang: unknown) => { | ||
locale.value = lang | ||
}, | ||
) | ||
</script> | ||
|
||
<template> | ||
<div class="storybook lang-selector"> | ||
<button | ||
class="storybook-button storybook-button--small" | ||
@click="locale = 'en'" | ||
> | ||
en | ||
</button> | ||
<button | ||
class="storybook-button storybook-button--small" | ||
@click="locale = 'fr'" | ||
> | ||
fr | ||
</button> | ||
<button | ||
class="storybook-button storybook-button--small" | ||
@click="locale = 'ar'" | ||
> | ||
ar | ||
</button> | ||
</div> | ||
<div class="storybook welcome" :style="{ direction: rtl ? 'rtl' : 'ltr' }"> | ||
<div>{{ t('welcome', { name: 'Storybook' }) }}</div> | ||
<div>{{ t('welcome', { name: 'Nuxt' }) }}</div> | ||
<div>{{ t('welcome', { name: 'I18n' }) }}</div> | ||
</div> | ||
|
||
<p>language : {{ lang }}</p> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { Meta, StoryObj } from '@storybook/vue3' | ||
|
||
import MyNuxtWelcome from './MyWelcome.vue' | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/vue/writing-stories/introduction | ||
|
||
const meta = { | ||
title: 'Example/NuxtWelcome', | ||
component: MyNuxtWelcome, | ||
// This component will have an automatically generated docsPage entry: https://storybook.js.org/docs/vue/writing-docs/autodocs | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof MyNuxtWelcome> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
export const NuxtWelcomeStory: Story = { | ||
args: {}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<template> | ||
<h1>Welcome Nuxt to Storybook</h1> | ||
<NuxtWelcome /> | ||
</template> | ||
|
||
<style> | ||
.readmore { | ||
-webkit-text-size-adjust: 100%; | ||
tab-size: 4; | ||
box-sizing: border-box; | ||
border-width: 0; | ||
border-style: solid; | ||
border-color: #e0e0e0; | ||
--tw-ring-inset: var(--tw-empty,); | ||
--tw-ring-offset-width: 0px; | ||
--tw-ring-offset-color: #fff; | ||
--tw-ring-color: rgba(14, 165, 233, 0.5); | ||
--tw-ring-offset-shadow: 0 0 #0000; | ||
--tw-ring-shadow: 0 0 #0000; | ||
--tw-shadow: 0 0 #0000; | ||
font-family: | ||
'Nunito Sans', | ||
-apple-system, | ||
'.SFNSText-Regular', | ||
'San Francisco', | ||
BlinkMacSystemFont, | ||
'Segoe UI', | ||
'Helvetica Neue', | ||
Helvetica, | ||
Arial, | ||
sans-serif; | ||
margin: 0; | ||
-webkit-font-smoothing: antialiased; | ||
-webkit-tap-highlight-color: rgba(0, 0, 0, 0); | ||
line-height: 24px; | ||
color: #029cfd; | ||
text-decoration: none; | ||
font-size: 14px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default defineI18nConfig(() => ({ | ||
legacy: false, | ||
locale: 'en', | ||
defaultLocale: 'en', | ||
messages: { | ||
en: { | ||
welcome: 'Welcome to Storybook ❤️ {name} ', | ||
}, | ||
fr: { | ||
welcome: 'Bienvenue a Storybook ❤️ {name} ', | ||
}, | ||
ar: { | ||
welcome: ' ناكست ❤️ {name} ❤️ مرحبا بكم في ستوري بوك ', | ||
}, | ||
}, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default defineI18nConfig(() => ({ | ||
legacy: false, | ||
locale: 'en', | ||
defaultLocale: 'en', | ||
messages: { | ||
en: { | ||
welcome: 'Welcome to Storybook ❤️ {name} ', | ||
}, | ||
fr: { | ||
welcome: 'Bienvenue a Storybook ❤️ {name} ', | ||
}, | ||
ar: { | ||
welcome: ' ناكست ❤️ {name} ❤️ مرحبا بكم في ستوري بوك ', | ||
}, | ||
}, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
devtools: { enabled: true }, | ||
modules: ['../packages/nuxt-module/src/module', '@nuxt/test-utils/module'], | ||
modules: [ | ||
'../packages/nuxt-module/src/module', | ||
'@nuxt/test-utils/module', | ||
'@nuxtjs/i18n', | ||
], | ||
|
||
storybook: { | ||
// Very verbose logs for debugging | ||
logLevel: Number.POSITIVE_INFINITY, | ||
}, | ||
|
||
i18n: { | ||
locales: ['en', 'fr', 'ar'], | ||
defaultLocale: 'en', | ||
}, | ||
|
||
compatibilityDate: '2024-08-03', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Meta, StoryObj } from '@storybook/vue3' | ||
|
||
import MyComponent from './MyI18n.vue' | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/vue/writing-stories/introduction | ||
|
||
const meta = { | ||
title: 'Plugins/I18n', | ||
component: MyComponent, | ||
argTypes: { | ||
lang: { control: 'select', options: ['en', 'fr', 'ar'] }, | ||
}, | ||
|
||
// This component will have an automatically generated docsPage entry: https://storybook.js.org/docs/vue/writing-docs/autodocs | ||
tags: ['autodocs'], | ||
} satisfies Meta<typeof MyComponent> | ||
|
||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
/* | ||
*👇 Render functions are a framework specific feature to allow you control on how the component renders. | ||
* See https://storybook.js.org/docs/vue/api/csf | ||
* to learn how to use render functions. | ||
*/ | ||
|
||
export const FrenchStory: Story = { | ||
args: { lang: 'fr' }, | ||
} | ||
|
||
export const EnglishStory: Story = { | ||
args: { lang: 'en' }, | ||
} | ||
|
||
export const ArabicStory: Story = { | ||
args: { lang: 'ar' }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<script lang="ts" setup> | ||
import './button.css' | ||
const props = defineProps<{ | ||
lang: string | ||
message?: string | ||
}>() | ||
const { t, locale } = useI18n() | ||
const rtl = computed(() => props.lang === 'ar') | ||
locale.value = props.lang | ||
watch( | ||
() => props.lang, | ||
(lang: unknown) => { | ||
locale.value = lang | ||
}, | ||
) | ||
</script> | ||
|
||
<template> | ||
<div class="storybook lang-selector"> | ||
<button | ||
class="storybook-button storybook-button--small" | ||
@click="locale = 'en'" | ||
> | ||
en | ||
</button> | ||
<button | ||
class="storybook-button storybook-button--small" | ||
@click="locale = 'fr'" | ||
> | ||
fr | ||
</button> | ||
<button | ||
class="storybook-button storybook-button--small" | ||
@click="locale = 'ar'" | ||
> | ||
ar | ||
</button> | ||
</div> | ||
<div class="storybook welcome" :style="{ direction: rtl ? 'rtl' : 'ltr' }"> | ||
<div>{{ t('welcome', { name: 'Storybook' }) }}</div> | ||
<div>{{ t('welcome', { name: 'Nuxt' }) }}</div> | ||
<div>{{ t('welcome', { name: 'I18n' }) }}</div> | ||
</div> | ||
|
||
<p>language : {{ lang }}</p> | ||
</template> |
Oops, something went wrong.