Skip to content
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

chore: use nuxt island to render storybook stories #1728

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion components/JabRefLogo.stories.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import JabRefLogo from './JabrefLogo.vue'
import JabRefLogo from './JabrefLogo.island.vue'
</script>

<template>
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions components/Storybook.island.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script setup lang="ts">
const props = defineProps()
</script>

<template>
<div>
Propsu:
<pre v-html="JSON.stringify(props, null, 2)" />
</div>
</template>
4 changes: 4 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default defineNuxtConfig({
global: 'global.ts',
},

experimental: {
componentIslands: true,
},

nitro: {
// Prevent 'reflect-metadata' from being treeshaked (since we don't explicitly use the import it would otherwise be removed)
moduleSideEffects: ['reflect-metadata'],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"pinia": "^2.0.35",
"redis": "^4.6.7",
"reflect-metadata": "^0.1.13",
"storybook-nuxt": "link:storybook-nuxt",
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"tsyringe": "^4.7.0",
Expand Down
1 change: 0 additions & 1 deletion pages/_storybook/external-iframe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function renderToCanvas(

mount(element, { element: domElement, app: useNuxtApp().vueApp })
}

definePageMeta({ layout: false, alias: '/iframe.html' })

export default defineComponent({
Expand Down
76 changes: 76 additions & 0 deletions storybook-nuxt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "storybook-nuxt",
"version": "7.0.0-beta.35",
"description": "Storybook for HTML and Vite: Develop HTML in isolation with Hot Reloading.",
"keywords": [
"storybook"
],
"homepage": "https://github.com/storybookjs/storybook/tree/main/frameworks/html-vite",
"bugs": {
"url": "https://github.com/storybookjs/storybook/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/storybookjs/storybook.git",
"directory": "frameworks/html-vite"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/storybook"
},
"license": "MIT",
"exports": {
".": {
"node": "./dist/index.cjs",
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./preset": {
"require": "./dist/preset.cjs",
"import": "./dist/preset.mjs",
"types": "./dist/preset.d.ts"
},
"./html": {
"require": "./dist/renderer.cjs",
"import": "./dist/renderer.mjs",
"types": "./dist/preset.d.ts"
},
"./html/preview": {
"require": "./dist/renderer.cjs",
"import": "./dist/renderer.mjs",
"types": "./dist/preset.d.ts"
},
"./package.json": "./package.json"
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist/**/*",
"template/**/*",
"README.md",
"*.js",
"*.d.ts"
],
"scripts": {
"check": "tsc --noEmit",
"prep": "../../../scripts/prepare/bundle.ts"
},
"dependencies": {},
"devDependencies": {},
"engines": {
"node": "^14.18 || >=16"
},
"publishConfig": {
"access": "public"
},
"bundler": {
"entries": [
"./src/index.ts",
"./src/preset.ts"
],
"platform": "node"
},
"gitHead": "e6d5c50c88bc67468a815efc1048b36ebc2d4bdd"
}
1 change: 1 addition & 0 deletions storybook-nuxt/preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/preset.cjs')
4 changes: 4 additions & 0 deletions storybook-nuxt/preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const core = {
builder: '@storybook/builder-vite',
renderer: 'storybook-nuxt/html',
}
31 changes: 31 additions & 0 deletions storybook-nuxt/renderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function renderToCanvas(
{ storyFn, kind, name, showMain, showError, forceRemount },
canvasElement
) {
const element = storyFn()
showMain()
if (typeof element === 'string') {
canvasElement.innerHTML = element
simulatePageLoad(canvasElement)
} else if (element instanceof Node) {
if (canvasElement.firstChild === element && forceRemount === false) {
return
}

canvasElement.innerHTML = ''
canvasElement.appendChild(element)
simulateDOMContentLoaded()
} else {
showError({
title: `Expecting an HTML snippet or DOM node from the story: "${name}" of "${kind}", but got ${JSON.stringify(
element
)}.`,
description: `
Did you forget to return the HTML snippet from the story?
Use "() => <your snippet or node>" or when defining the story.
`,
})
}
}

export { renderToCanvas }
Loading