@aurelia/storybook 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,18 +13,8 @@ interface AureliaStoryResult {
13
13
  props?: Record<string, any>;
14
14
  }
15
15
 
16
- /**
17
- * Merges multiple sources into a single object.
18
- * Sources can be story parameters, args, or story.props.
19
- */
20
- function mergeStoryProps(
21
- ...sources: Array<Record<string, any> | undefined>
22
- ): Record<string, any> {
23
- return Object.assign({}, ...sources);
24
- }
25
-
26
16
  // Track Aurelia apps for cleanup
27
- const appMap = new Map<HTMLElement, Aurelia>();
17
+ const appMap = new Map<HTMLElement, any>();
28
18
 
29
19
  async function teardown(element: HTMLElement) {
30
20
  if (appMap.has(element)) {
@@ -36,7 +26,9 @@ async function teardown(element: HTMLElement) {
36
26
  }
37
27
  }
38
28
 
39
- export const render: ArgsStoryFn<AureliaRenderer> = (args, { id, component: Component }) => {
29
+ export const render: ArgsStoryFn<AureliaRenderer> = (args, context) => {
30
+ const { id, component: Component } = context;
31
+
40
32
  if (!Component) {
41
33
  throw new Error(
42
34
  `Unable to render story ${id} as the component annotation is missing from the default export`
@@ -56,14 +48,34 @@ export async function renderToCanvas(
56
48
  forceRemount,
57
49
  }: RenderContext<AureliaRenderer>,
58
50
  canvasElement: HTMLElement,
59
- bootstrapAppFn?: typeof bootstrapAureliaApp
51
+ bootstrapAppFn?: typeof createAureliaApp
60
52
  ) {
61
- const appBootstrapFn = bootstrapAppFn || bootstrapAureliaApp;
53
+ // Store reference to the original storybook root element
54
+ const rootElement = canvasElement;
55
+
56
+ // Ensure we have (or create) a single container inside the root where the Aurelia app actually renders
57
+ let hostElement: HTMLElement;
58
+ if (rootElement.id === 'storybook-root') {
59
+ hostElement = rootElement.querySelector('.aurelia-story-container') as HTMLElement;
60
+ if (!hostElement) {
61
+ hostElement = document.createElement('div');
62
+ hostElement.className = 'aurelia-story-container';
63
+ hostElement.style.height = '100%';
64
+ rootElement.appendChild(hostElement);
65
+ }
66
+ } else {
67
+ hostElement = rootElement;
68
+ }
62
69
 
70
+ // All app instances are now tracked by the *root* element, ensuring we only ever have one per story iframe
71
+ const appBootstrapFn = bootstrapAppFn ?? createAureliaApp;
63
72
  const { parameters, component, args } = storyContext;
64
- let app = appMap.get(canvasElement);
65
-
73
+
74
+ let app = appMap.get(rootElement);
66
75
  const story = storyFn() as AureliaStoryResult;
76
+
77
+ // Temporary debug logging
78
+ console.log(`[DEBUG] Story: ${name}, forceRemount: ${forceRemount}, hasExistingApp: ${!!app}, canvasId: ${canvasElement.className}`);
67
79
 
68
80
  if (!story) {
69
81
  showError({
@@ -78,50 +90,40 @@ export async function renderToCanvas(
78
90
 
79
91
  showMain();
80
92
 
81
- let mergedProps;
82
- // Use full merge (including story.props) when bootstrapping a new app or force remounting.
83
93
  if (!app || forceRemount) {
84
- mergedProps = mergeStoryProps(parameters?.args, args, story.props);
85
- if (app) {
86
- await teardown(canvasElement);
94
+ if (forceRemount && app) {
95
+ await teardown(rootElement);
96
+ app = undefined;
87
97
  }
88
- app = appBootstrapFn(
98
+ // Clear container before mounting new app
99
+ hostElement.innerHTML = '';
100
+
101
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
102
+
103
+ const aureliaApp = appBootstrapFn(
89
104
  story,
90
105
  mergedProps,
91
- canvasElement,
106
+ hostElement,
92
107
  component as Constructable
93
- ) as Aurelia;
94
- await app.start();
95
- appMap.set(canvasElement, app);
108
+ );
109
+ await aureliaApp.start();
110
+ appMap.set(rootElement, aureliaApp);
111
+ app = aureliaApp;
96
112
  } else {
97
- // Update the existing app viewModel only with parameters and args (exclude story.props).
98
- mergedProps = mergeStoryProps(parameters?.args, args);
99
- if (app.root?.controller?.viewModel) {
113
+ // update existing app props
114
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
115
+ if (app?.root?.controller?.viewModel) {
100
116
  Object.assign(app.root.controller.viewModel, mergedProps);
101
117
  }
102
118
  }
103
119
 
104
- // Set up story change listener for cleanup
105
- const channel = storyContext.viewMode === 'story' ? storyContext.channel : null;
106
- let onStoryChange: () => void;
107
- if (channel) {
108
- onStoryChange = () => {
109
- // When the story changes, clean up the Aurelia app
110
- teardown(canvasElement);
111
- };
112
- channel.on(STORY_CHANGED, onStoryChange);
113
- }
114
-
115
- // Return teardown function that also unsubscribes from STORY_CHANGED
120
+ // Return cleanup fn
116
121
  return async () => {
117
- if (channel && onStoryChange) {
118
- channel.off(STORY_CHANGED, onStoryChange);
119
- }
120
- await teardown(canvasElement);
122
+ await teardown(rootElement);
121
123
  };
122
124
  }
123
125
 
124
- export function bootstrapAureliaApp(
126
+ export function createAureliaApp(
125
127
  story: AureliaStoryResult,
126
128
  args: Record<string, any>,
127
129
  domElement: HTMLElement,
@@ -146,7 +148,7 @@ export function bootstrapAureliaApp(
146
148
 
147
149
  const App = CustomElement.define(
148
150
  {
149
- name: 'au-storybook',
151
+ name: 'sb-app',
150
152
  template,
151
153
  containerless: true,
152
154
  },
package/src/preview.ts CHANGED
@@ -1,50 +1 @@
1
- import { renderToCanvas, bootstrapAureliaApp } from './preview/render';
2
- import Aurelia from 'aurelia';
3
-
4
- // Track the current story's cleanup function
5
- let currentCleanup: (() => void) | null = null;
6
-
7
- export const render = (args: any, context: any) => {
8
- // Clean up previous story if exists
9
- if (currentCleanup) {
10
- currentCleanup();
11
- currentCleanup = null;
12
- }
13
-
14
- // Create a container element
15
- const container = document.createElement('div');
16
-
17
- // Get the story function result
18
- const story = context.storyFn();
19
-
20
- // Bootstrap Aurelia app immediately
21
- if (story && (story.Component || story.template)) {
22
- const app = bootstrapAureliaApp(
23
- story,
24
- args,
25
- container,
26
- story.Component || context.component
27
- ) as Aurelia;
28
-
29
- // Start the app asynchronously
30
- const startPromise = app.start();
31
- if (startPromise && typeof startPromise.catch === 'function') {
32
- startPromise.catch((error: any) => {
33
- console.error('Failed to start Aurelia app:', error);
34
- });
35
- }
36
-
37
- // Set cleanup function
38
- currentCleanup = () => {
39
- const stopPromise = app.stop();
40
- if (stopPromise && typeof stopPromise.catch === 'function') {
41
- stopPromise.catch((error: any) => {
42
- console.error('Failed to stop Aurelia app:', error);
43
- });
44
- }
45
- };
46
- }
47
-
48
- // Return the container element immediately
49
- return container;
50
- };
1
+ export { renderToCanvas, render } from './preview/render';