histoire 0.4.4 → 0.5.0

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.
Files changed (77) hide show
  1. package/dist/bundled/HomeView.js +1 -0
  2. package/dist/bundled/MobileOverlay.js +3 -1
  3. package/dist/bundled/SandboxVue3.js +649 -0
  4. package/dist/bundled/SearchPane.js +2 -0
  5. package/dist/bundled/StoryView.js +551 -622
  6. package/dist/bundled/global-components.js +16 -6
  7. package/dist/bundled/index2.js +114 -43
  8. package/dist/bundled/sandbox.js +3 -11
  9. package/dist/bundled/state.js +55 -0
  10. package/dist/bundled/story.js +1 -1
  11. package/dist/client/app/App.vue +18 -0
  12. package/dist/client/app/codegen/serialize-js.js +1 -1
  13. package/dist/client/app/codegen/vue3.d.ts +2 -0
  14. package/dist/client/app/codegen/vue3.js +37 -12
  15. package/dist/client/app/components/app/InitialLoading.vue +46 -0
  16. package/dist/client/app/components/app/MobileOverlay.vue +1 -1
  17. package/dist/client/app/components/base/BaseSelect.vue +72 -0
  18. package/dist/client/app/components/exposed/Story.vue +2 -1
  19. package/dist/client/app/components/exposed/Variant.vue +4 -4
  20. package/dist/client/app/components/panel/ControlsComponentPropItem.vue +96 -0
  21. package/dist/client/app/components/panel/ControlsComponentProps.vue +33 -0
  22. package/dist/client/app/components/panel/StatePresets.vue +195 -0
  23. package/dist/client/app/components/panel/StoryControls.vue +31 -2
  24. package/dist/client/app/components/sandbox/SandboxVue3.vue +70 -2
  25. package/dist/client/app/components/story/StoryVariantSinglePreviewRemote.vue +2 -14
  26. package/dist/client/app/index.js +5 -0
  27. package/dist/client/app/sandbox.js +2 -11
  28. package/dist/client/app/style/transitions.pcss +12 -0
  29. package/dist/client/app/types.d.ts +11 -0
  30. package/dist/client/app/util/mapping.js +11 -1
  31. package/dist/client/app/util/state.d.ts +5 -0
  32. package/dist/client/app/util/state.js +55 -0
  33. package/dist/client/app/util/variant.d.ts +1 -1
  34. package/dist/node/build.js +4 -6
  35. package/dist/node/collect/index.js +6 -0
  36. package/dist/node/collect/worker.js +0 -4
  37. package/dist/node/commands/preview.js +1 -1
  38. package/dist/node/config.d.ts +23 -23
  39. package/dist/node/plugin.d.ts +2 -2
  40. package/dist/node/server.js +66 -47
  41. package/dist/node/vite.d.ts +1 -1
  42. package/dist/node/vite.js +10 -2
  43. package/dist/style.css +162 -50
  44. package/package.json +4 -4
  45. package/src/client/app/App.vue +18 -0
  46. package/src/client/app/codegen/serialize-js.ts +1 -1
  47. package/src/client/app/codegen/vue3.ts +40 -12
  48. package/src/client/app/components/app/InitialLoading.vue +46 -0
  49. package/src/client/app/components/app/MobileOverlay.vue +1 -1
  50. package/src/client/app/components/base/BaseSelect.vue +72 -0
  51. package/src/client/app/components/exposed/Story.vue +2 -1
  52. package/src/client/app/components/exposed/Variant.vue +4 -4
  53. package/src/client/app/components/panel/ControlsComponentPropItem.vue +96 -0
  54. package/src/client/app/components/panel/ControlsComponentProps.vue +33 -0
  55. package/src/client/app/components/panel/StatePresets.vue +195 -0
  56. package/src/client/app/components/panel/StoryControls.vue +31 -2
  57. package/src/client/app/components/sandbox/SandboxVue3.vue +70 -2
  58. package/src/client/app/components/story/StoryVariantSinglePreviewRemote.vue +2 -14
  59. package/src/client/app/index.ts +6 -0
  60. package/src/client/app/sandbox.ts +2 -10
  61. package/src/client/app/style/transitions.pcss +12 -0
  62. package/src/client/app/types.ts +13 -0
  63. package/src/client/app/util/mapping.ts +12 -1
  64. package/src/client/app/util/state.ts +61 -0
  65. package/src/client/app/util/variant.ts +1 -1
  66. package/src/node/build.ts +4 -6
  67. package/src/node/collect/index.ts +7 -0
  68. package/src/node/collect/worker.ts +0 -4
  69. package/src/node/commands/preview.ts +1 -1
  70. package/src/node/server.ts +72 -49
  71. package/src/node/vite.ts +11 -2
  72. package/vite.config.ts +25 -0
  73. package/dist/bundled/dark.js +0 -4
  74. package/dist/bundled/reactivity.js +0 -96
  75. package/dist/client/app/util/reactivity.d.ts +0 -1
  76. package/dist/client/app/util/reactivity.js +0 -16
  77. package/src/client/app/util/reactivity.ts +0 -22
@@ -4,6 +4,8 @@ import { Icon } from '@iconify/vue'
4
4
  import type { Story, Variant } from '../../types'
5
5
  import SandboxVue3 from '../sandbox/SandboxVue3.vue'
6
6
  import BaseEmpty from '../base/BaseEmpty.vue'
7
+ import StatePresets from './StatePresets.vue'
8
+ import ControlsComponentProps from './ControlsComponentProps.vue'
7
9
 
8
10
  defineProps({
9
11
  variant: {
@@ -19,21 +21,48 @@ defineProps({
19
21
  </script>
20
22
 
21
23
  <template>
22
- <div data-test-id="story-controls">
24
+ <div
25
+ data-test-id="story-controls"
26
+ class="htw-flex htw-flex-col htw-divide-y htw-divide-gray-100 dark:htw-divide-gray-750"
27
+ >
28
+ <!-- Toolbar -->
29
+ <div
30
+ class="htw-h-9 htw-flex-none htw-px-2 htw-flex htw-items-center"
31
+ >
32
+ <StatePresets
33
+ :story="story"
34
+ :variant="variant"
35
+ />
36
+ </div>
37
+
23
38
  <!-- Custom controls -->
24
39
  <SandboxVue3
25
40
  v-if="variant.slots().controls || story.slots().controls"
26
41
  slot-name="controls"
27
42
  :variant="variant"
28
43
  :story="story"
44
+ class="htw-flex-none"
29
45
  />
30
46
 
31
- <BaseEmpty v-else>
47
+ <BaseEmpty v-else-if="!variant.state?._hPropDefs?.length">
32
48
  <Icon
33
49
  icon="carbon:audio-console"
34
50
  class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
35
51
  />
36
52
  <span>No controls available for this story</span>
37
53
  </BaseEmpty>
54
+
55
+ <!-- Auto props -->
56
+ <div
57
+ v-if="variant.state?._hPropDefs?.length"
58
+ >
59
+ <ControlsComponentProps
60
+ v-for="(def, index) of variant.state._hPropDefs"
61
+ :key="index"
62
+ :variant="variant"
63
+ :definition="def"
64
+ class="htw-flex-none htw-my-2"
65
+ />
66
+ </div>
38
67
  </div>
39
68
  </template>
@@ -1,6 +1,8 @@
1
1
  <script lang="ts" setup>
2
2
  import { App, createApp, onMounted, onUnmounted, PropType, ref, watch } from 'vue'
3
- import { Story, Variant } from '../../types'
3
+ import { Story, Variant, PropDefinition, AutoPropComponentDefinition } from '../../types'
4
+ import { getTagName } from '../../codegen/vue3'
5
+ import { applyStateToVariant } from '../../util/state'
4
6
  // @ts-expect-error virtual module id
5
7
  import * as setup from '$histoire-setup'
6
8
 
@@ -34,15 +36,81 @@ async function mountVariant () {
34
36
 
35
37
  await props.variant.initState()
36
38
 
39
+ let lastPropsTypesSnapshot: string
40
+
37
41
  app = createApp({
38
42
  name: 'SandboxVue3',
39
43
 
40
44
  render: () => {
41
- return props.variant.slots()?.[props.slotName]?.({
45
+ const vnodes = props.variant.slots()?.[props.slotName]?.({
42
46
  state: props.variant.state ?? {},
43
47
  }) ?? props.story.slots()?.[props.slotName]?.({
44
48
  state: props.variant.state ?? {},
45
49
  })
50
+
51
+ // Auto detect props
52
+ if (props.slotName === 'default') {
53
+ const propsTypes: AutoPropComponentDefinition[] = vnodes.filter(vnode => typeof vnode.type === 'object').map((vnode, index) => {
54
+ const propDefs: PropDefinition[] = []
55
+ for (const key in vnode.type.props) {
56
+ const prop = vnode.type.props[key]
57
+ let types
58
+ let defaultValue
59
+ if (prop) {
60
+ const rawTypes = Array.isArray(prop.type) ? prop.type : [prop.type]
61
+ types = rawTypes.map(t => {
62
+ switch (t) {
63
+ case String:
64
+ return 'string'
65
+ case Number:
66
+ return 'number'
67
+ case Boolean:
68
+ return 'boolean'
69
+ case Object:
70
+ return 'object'
71
+ case Array:
72
+ return 'array'
73
+ default:
74
+ return 'unknown'
75
+ }
76
+ })
77
+ defaultValue = typeof prop.default === 'function' ? prop.default.toString() : prop.default
78
+ }
79
+ propDefs.push({
80
+ name: key,
81
+ types,
82
+ required: prop?.required,
83
+ default: defaultValue,
84
+ })
85
+
86
+ // Props overrides
87
+ if (props.variant.state?._hPropState?.[index]?.[key]) {
88
+ vnode.props[key] = props.variant.state._hPropState[index][key]
89
+ }
90
+ }
91
+
92
+ return {
93
+ name: getTagName(vnode),
94
+ index,
95
+ props: propDefs,
96
+ } as AutoPropComponentDefinition
97
+ }).filter(def => def.props.length)
98
+
99
+ const snapshot = JSON.stringify(propsTypes)
100
+ if (!lastPropsTypesSnapshot || lastPropsTypesSnapshot !== snapshot) {
101
+ applyStateToVariant(props.variant, {
102
+ _hPropDefs: propsTypes,
103
+ })
104
+ if (!props.variant.state._hPropState) {
105
+ applyStateToVariant(props.variant, {
106
+ _hPropState: {},
107
+ })
108
+ }
109
+ lastPropsTypesSnapshot = snapshot
110
+ }
111
+ }
112
+
113
+ return vnodes
46
114
  },
47
115
  })
48
116
 
@@ -6,11 +6,11 @@ import { STATE_SYNC, PREVIEW_SETTINGS_SYNC, SANDBOX_READY, EVENT_SEND } from '..
6
6
  import type { Story, Variant } from '../../types'
7
7
  import HatchedPattern from '../misc/HatchedPattern.vue'
8
8
  import CheckerboardPattern from '../misc/CheckerboardPattern.vue'
9
- import { toRawDeep } from '../../util/reactivity'
10
9
  import { getSandboxUrl } from '../sandbox/lib'
11
10
  import { usePreviewSettingsStore } from '../../stores/preview-settings'
12
11
  import { HstEvent, useEventsStore } from '../../stores/events'
13
12
  import StoryResponsivePreview from './StoryResponsivePreview.vue'
13
+ import { applyStateToVariant, toRawDeep } from '../../util/state'
14
14
 
15
15
  const props = defineProps<{
16
16
  story: Story
@@ -65,19 +65,7 @@ useEventListener(window, 'message', (event) => {
65
65
 
66
66
  function updateVariantState (state: any) {
67
67
  synced = true
68
- if (props.variant.state) {
69
- for (const key in state) {
70
- if (typeof props.variant.state[key] === 'object') {
71
- Object.assign(props.variant.state[key], state[key])
72
- } else {
73
- // eslint-disable-next-line vue/no-mutating-props
74
- props.variant.state[key] = state[key]
75
- }
76
- }
77
- } else {
78
- // eslint-disable-next-line vue/no-mutating-props
79
- props.variant.state = state
80
- }
68
+ applyStateToVariant(props.variant, state)
81
69
  }
82
70
 
83
71
  function logEvent (event: HstEvent) {
@@ -26,4 +26,9 @@ export function mountMainApp() {
26
26
  app.use(router);
27
27
  registerGlobalComponents(app);
28
28
  app.mount('#app');
29
+ // @ts-expect-error vite hot api
30
+ if (import.meta.hot) {
31
+ // @ts-expect-error vite hot api
32
+ import.meta.hot.send('histoire:mount', {});
33
+ }
29
34
  }
@@ -10,7 +10,7 @@ import { PREVIEW_SETTINGS_SYNC, STATE_SYNC, SANDBOX_READY } from './util/const.j
10
10
  import { applyPreviewSettings } from './util/preview-settings.js';
11
11
  import { isDark } from './util/dark.js';
12
12
  import { histoireConfig } from './util/config.js';
13
- import { toRawDeep } from './util/reactivity';
13
+ import { applyStateToVariant, toRawDeep } from './util/state';
14
14
  const query = parseQuery(window.location.search);
15
15
  const file = ref(mapFile(files.find(f => f.id === query.storyId)));
16
16
  const app = createApp({
@@ -25,16 +25,7 @@ const app = createApp({
25
25
  if (!mounted)
26
26
  return;
27
27
  synced = true;
28
- if (variant.value.state) {
29
- for (const key in event.data.state) {
30
- if (typeof variant.value.state[key] === 'object') {
31
- Object.assign(variant.value.state[key], event.data.state[key]);
32
- }
33
- else {
34
- variant.value.state[key] = event.data.state[key];
35
- }
36
- }
37
- }
28
+ applyStateToVariant(variant.value, event.data.state);
38
29
  }
39
30
  else if (event.data?.type === PREVIEW_SETTINGS_SYNC) {
40
31
  applyPreviewSettings(event.data.settings);
@@ -25,6 +25,18 @@
25
25
  }
26
26
 
27
27
  .__histoire-fade {
28
+ &-enter-active,
29
+ &-leave-active {
30
+ transition: opacity .15s;
31
+ }
32
+
33
+ &-enter-from,
34
+ &-leave-to {
35
+ opacity: 0;
36
+ }
37
+ }
38
+
39
+ .__histoire-fade-bottom {
28
40
  &-enter-active,
29
41
  &-leave-active {
30
42
  transition: transform .15s, opacity .15s;
@@ -71,3 +71,14 @@ export interface PreviewSettings {
71
71
  backgroundColor: string;
72
72
  checkerboard: boolean;
73
73
  }
74
+ export interface PropDefinition {
75
+ name: string;
76
+ types?: string[];
77
+ required?: boolean;
78
+ default?: any;
79
+ }
80
+ export interface AutoPropComponentDefinition {
81
+ name: string;
82
+ index: number;
83
+ props: PropDefinition[];
84
+ }
@@ -1,4 +1,14 @@
1
1
  import { markRaw } from 'vue';
2
+ const copiedFromExistingVariant = [
3
+ 'state',
4
+ 'slots',
5
+ 'initState',
6
+ 'source',
7
+ 'responsiveDisabled',
8
+ 'setupApp',
9
+ 'configReady',
10
+ 'previewReady',
11
+ ];
2
12
  export function mapFile(file, existingFile) {
3
13
  const result = {
4
14
  ...file,
@@ -16,7 +26,7 @@ export function mapFile(file, existingFile) {
16
26
  const variant = result.story.variants[index];
17
27
  const existingVariant = existingFile.story.variants[index];
18
28
  if (existingVariant) {
19
- for (const key in existingVariant) {
29
+ for (const key of copiedFromExistingVariant) {
20
30
  if (typeof existingVariant[key] !== 'undefined') {
21
31
  if (key === 'state') {
22
32
  variant.state = existingVariant.state;
@@ -0,0 +1,5 @@
1
+ import type { Variant } from '../types';
2
+ export declare function toRawDeep(val: any): any;
3
+ export declare function clone(data: any): any;
4
+ export declare function omit(data: any, keys: string[]): {};
5
+ export declare function applyStateToVariant(variant: Variant, state: any): void;
@@ -0,0 +1,55 @@
1
+ import { unref, isRef } from 'vue';
2
+ const isObject = (val) => val !== null && typeof val === 'object';
3
+ export function toRawDeep(val) {
4
+ const unwrappedValue = isRef(val) ? unref(val) : val;
5
+ if (!isObject(unwrappedValue)) {
6
+ return unwrappedValue;
7
+ }
8
+ if (Array.isArray(unwrappedValue)) {
9
+ return unwrappedValue.map(toRawDeep);
10
+ }
11
+ return toRawObject(unwrappedValue);
12
+ }
13
+ const toRawObject = (obj) => Object.keys(obj).reduce((raw, key) => {
14
+ raw[key] = toRawDeep(obj[key]);
15
+ return raw;
16
+ }, {});
17
+ export function clone(data) {
18
+ try {
19
+ return structuredClone(data);
20
+ }
21
+ catch (e) {
22
+ console.warn(e, `Fallback to JSON cloning`);
23
+ try {
24
+ return JSON.parse(JSON.stringify(data));
25
+ }
26
+ catch (e) {
27
+ console.error(e);
28
+ }
29
+ return data;
30
+ }
31
+ }
32
+ export function omit(data, keys) {
33
+ const copy = {};
34
+ for (const key in data) {
35
+ if (!keys.includes(key)) {
36
+ copy[key] = data[key];
37
+ }
38
+ }
39
+ return copy;
40
+ }
41
+ export function applyStateToVariant(variant, state) {
42
+ if (variant.state) {
43
+ for (const key in state) {
44
+ if (variant.state[key] && !key.startsWith('_h') && typeof variant.state[key] === 'object' && !Array.isArray(variant.state[key])) {
45
+ Object.assign(variant.state[key], state[key]);
46
+ }
47
+ else {
48
+ variant.state[key] = state[key];
49
+ }
50
+ }
51
+ }
52
+ else {
53
+ variant.state = state;
54
+ }
55
+ }
@@ -1,5 +1,5 @@
1
1
  import { Ref } from 'vue';
2
- import { Variant } from '../types';
2
+ import type { Variant } from '../types';
3
3
  export declare function useCurrentVariantRoute(variant: Ref<Variant>): {
4
4
  isActive: import("vue").ComputedRef<boolean>;
5
5
  targetRoute: import("vue").ComputedRef<{
@@ -23,7 +23,7 @@ export async function build(ctx) {
23
23
  const startTime = performance.now();
24
24
  await findAllStories(ctx);
25
25
  const server = await createViteServer({
26
- plugins: await createVitePlugins(ctx),
26
+ plugins: await createVitePlugins(true, ctx),
27
27
  });
28
28
  await server.pluginContainer.buildStart({});
29
29
  const moduleLoader = useModuleLoader({
@@ -45,15 +45,13 @@ export async function build(ctx) {
45
45
  server,
46
46
  throws: true,
47
47
  }, ctx);
48
- for (const storyFile of ctx.storyFiles) {
49
- await executeStoryFile(storyFile);
50
- }
48
+ await Promise.all(ctx.storyFiles.map(storyFile => executeStoryFile(storyFile)));
51
49
  await destroyCollectStories();
52
50
  const storyCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ? 1 : 0), 0);
53
51
  const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0);
54
52
  const emptyStoryCount = ctx.storyFiles.length - storyCount;
55
53
  const results = await viteBuild({
56
- plugins: await createVitePlugins(ctx),
54
+ plugins: await createVitePlugins(false, ctx),
57
55
  build: {
58
56
  rollupOptions: {
59
57
  input: [
@@ -76,7 +74,7 @@ export async function build(ctx) {
76
74
  });
77
75
  const result = Array.isArray(results) ? results[0] : results;
78
76
  const resolvedViteConfig = await resolveViteConfig({
79
- plugins: await createVitePlugins(ctx),
77
+ plugins: await createVitePlugins(false, ctx),
80
78
  }, 'build');
81
79
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
82
80
  // Preload
@@ -4,6 +4,7 @@ import { relative } from 'pathe';
4
4
  import pc from 'picocolors';
5
5
  import Tinypool from 'tinypool';
6
6
  import { createBirpc } from 'birpc';
7
+ import { cpus } from 'os';
7
8
  import { createPath } from '../tree.js';
8
9
  export function useCollectStories(options, ctx) {
9
10
  const { server } = options;
@@ -14,10 +15,15 @@ export function useCollectStories(options, ctx) {
14
15
  ],
15
16
  },
16
17
  });
18
+ const threadsCount = ctx.mode === 'dev'
19
+ ? Math.max(cpus().length / 2, 1)
20
+ : Math.max(cpus().length - 1, 1);
17
21
  const threadPool = new Tinypool({
18
22
  filename: new URL('./worker.js', import.meta.url).href,
19
23
  // WebContainer compatibility (Stackblitz)
20
24
  useAtomics: typeof process.versions.webcontainer !== 'string',
25
+ maxThreads: threadsCount,
26
+ minThreads: threadsCount,
21
27
  });
22
28
  function clearCache() {
23
29
  server.moduleGraph.invalidateAll();
@@ -13,10 +13,6 @@ export default async (payload) => {
13
13
  root: payload.root,
14
14
  base: payload.base,
15
15
  fetchModule(id) {
16
- // Alias
17
- if (id.endsWith('vue.runtime.esm-bundler.js')) {
18
- id = 'vue';
19
- }
20
16
  return rpc.fetchModule(id);
21
17
  },
22
18
  resolveId(id, importer) {
@@ -8,7 +8,7 @@ export async function previewCommand(options) {
8
8
  mode: 'build',
9
9
  });
10
10
  const viteConfig = await resolveViteConfig({
11
- plugins: await createVitePlugins(ctx),
11
+ plugins: await createVitePlugins(false, ctx),
12
12
  }, 'build');
13
13
  const { baseUrl } = await startPreview(viteConfig, options.port, ctx);
14
14
  console.log(`Preview server listening on ${pc.cyan(baseUrl)}`);