histoire 0.4.6 → 0.5.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.
Files changed (55) hide show
  1. package/client.d.ts +2 -0
  2. package/dist/bundled/HomeView.js +1 -0
  3. package/dist/bundled/MobileOverlay.js +3 -1
  4. package/dist/bundled/SandboxVue3.js +649 -0
  5. package/dist/bundled/SearchPane.js +2 -0
  6. package/dist/bundled/StoryView.js +307 -618
  7. package/dist/bundled/global-components.js +5 -5
  8. package/dist/bundled/index2.js +111 -43
  9. package/dist/bundled/sandbox.js +2 -2
  10. package/dist/bundled/state.js +16 -83
  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/exposed/Story.vue +2 -1
  18. package/dist/client/app/components/exposed/Variant.vue +4 -4
  19. package/dist/client/app/components/panel/ControlsComponentPropItem.vue +96 -0
  20. package/dist/client/app/components/panel/ControlsComponentProps.vue +33 -0
  21. package/dist/client/app/components/panel/StatePresets.vue +8 -6
  22. package/dist/client/app/components/panel/StoryControls.vue +18 -3
  23. package/dist/client/app/components/sandbox/SandboxVue3.vue +70 -2
  24. package/dist/client/app/style/transitions.pcss +12 -0
  25. package/dist/client/app/types.d.ts +19 -7
  26. package/dist/client/app/util/state.d.ts +1 -0
  27. package/dist/client/app/util/state.js +11 -2
  28. package/dist/node/build.js +1 -3
  29. package/dist/node/collect/index.js +6 -0
  30. package/dist/node/config.d.ts +23 -23
  31. package/dist/node/plugin.d.ts +2 -2
  32. package/dist/node/server.js +16 -5
  33. package/dist/style.css +92 -13
  34. package/index.d.ts +3 -0
  35. package/package.json +3 -3
  36. package/src/client/app/App.vue +18 -0
  37. package/src/client/app/codegen/serialize-js.ts +1 -1
  38. package/src/client/app/codegen/vue3.ts +40 -12
  39. package/src/client/app/components/app/InitialLoading.vue +46 -0
  40. package/src/client/app/components/app/MobileOverlay.vue +1 -1
  41. package/src/client/app/components/exposed/Story.vue +2 -1
  42. package/src/client/app/components/exposed/Variant.vue +4 -4
  43. package/src/client/app/components/panel/ControlsComponentPropItem.vue +96 -0
  44. package/src/client/app/components/panel/ControlsComponentProps.vue +33 -0
  45. package/src/client/app/components/panel/StatePresets.vue +8 -6
  46. package/src/client/app/components/panel/StoryControls.vue +18 -3
  47. package/src/client/app/components/sandbox/SandboxVue3.vue +70 -2
  48. package/src/client/app/style/transitions.pcss +12 -0
  49. package/src/client/app/types.ts +22 -7
  50. package/src/client/app/util/state.ts +12 -2
  51. package/src/components.d.ts +143 -0
  52. package/src/node/build.ts +1 -3
  53. package/src/node/collect/index.ts +7 -0
  54. package/src/node/server.ts +20 -5
  55. package/dist/bundled/dark.js +0 -4
@@ -1,5 +1,4 @@
1
- // @ts-nocheck
2
- // @TODO remove no-check and fix vnode type errors
1
+ // @TODO remove @ts-ignore
3
2
  import { vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic, Text } from 'vue';
4
3
  import { pascal } from 'case';
5
4
  import { createAutoBuildingObject, indent } from './util';
@@ -9,14 +8,16 @@ export async function generateSourceCode(variant) {
9
8
  const vnode = variant.slots().default?.({ state: variant.state ?? {} }) ?? [];
10
9
  const list = Array.isArray(vnode) ? vnode : [vnode];
11
10
  const lines = [];
12
- for (const vnode of list) {
13
- lines.push(...(await printVNode(vnode)).lines);
11
+ for (const n in list) {
12
+ const vnode = list[n];
13
+ lines.push(...(await printVNode(vnode, variant.state?._hPropState?.[n])).lines);
14
14
  }
15
15
  return lines.join('\n');
16
16
  }
17
- async function printVNode(vnode) {
17
+ async function printVNode(vnode, propsOverrides = null) {
18
18
  if (vnode.type === Text) {
19
19
  return {
20
+ // @ts-ignore
20
21
  lines: [vnode.children],
21
22
  isText: true,
22
23
  };
@@ -24,7 +25,9 @@ async function printVNode(vnode) {
24
25
  const lines = [];
25
26
  if (typeof vnode.type === 'object' || typeof vnode.type === 'string') {
26
27
  // Wait for async component
28
+ // @ts-ignore
27
29
  if (vnode.type?.__asyncLoader && !vnode.type.__asyncResolved) {
30
+ // @ts-ignore
28
31
  await vnode.type.__asyncLoader();
29
32
  }
30
33
  const attrs = [];
@@ -78,8 +81,10 @@ async function printVNode(vnode) {
78
81
  }
79
82
  }
80
83
  genDirective('model', dir, valueCode);
84
+ // @ts-ignore
81
85
  }
82
86
  else if (dir.instance._ || dir.instance.$) {
87
+ // @ts-ignore
83
88
  const target = dir.instance.$ ?? dir.instance._;
84
89
  let dirName;
85
90
  for (const directives of [target.directives, target.appContext.directives]) {
@@ -99,11 +104,8 @@ async function printVNode(vnode) {
99
104
  }
100
105
  }
101
106
  // Attributes
102
- for (const prop in vnode.props) {
103
- if (skipProps.includes(prop)) {
104
- continue;
105
- }
106
- const value = vnode.props[prop];
107
+ function addAttr(prop, value) {
108
+ // @ts-ignore
107
109
  if (typeof value !== 'string' || vnode.dynamicProps?.includes(prop)) {
108
110
  let directive = ':';
109
111
  if (prop.startsWith('on')) {
@@ -112,6 +114,7 @@ async function printVNode(vnode) {
112
114
  const arg = directive === '@' ? `${prop[2].toLowerCase()}${prop.slice(3)}` : prop;
113
115
  // v-model on component
114
116
  const vmodelListener = `onUpdate:${prop}`;
117
+ // @ts-ignore
115
118
  if (directive === ':' && vnode.dynamicProps?.includes(vmodelListener)) {
116
119
  // Listener
117
120
  skipProps.push(vmodelListener);
@@ -132,7 +135,7 @@ async function printVNode(vnode) {
132
135
  modifiers,
133
136
  value,
134
137
  }, valueCode);
135
- continue;
138
+ return;
136
139
  }
137
140
  let serialized;
138
141
  if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
@@ -172,6 +175,18 @@ async function printVNode(vnode) {
172
175
  attrs.push([`${prop}="${value}"`]);
173
176
  }
174
177
  }
178
+ for (const prop in vnode.props) {
179
+ if (skipProps.includes(prop) || (propsOverrides && prop in propsOverrides)) {
180
+ continue;
181
+ }
182
+ const value = vnode.props[prop];
183
+ addAttr(prop, value);
184
+ }
185
+ if (propsOverrides) {
186
+ for (const prop in propsOverrides) {
187
+ addAttr(prop, propsOverrides[prop]);
188
+ }
189
+ }
175
190
  if (attrs.length > 1) {
176
191
  multilineAttrs = true;
177
192
  }
@@ -192,6 +207,7 @@ async function printVNode(vnode) {
192
207
  else if (Array.isArray(vnode.children)) {
193
208
  let isAllChildText;
194
209
  for (const child of vnode.children) {
210
+ // @ts-ignore
195
211
  const result = await printVNode(child);
196
212
  if (result.isText) {
197
213
  if (isAllChildText === undefined) {
@@ -226,6 +242,7 @@ async function printVNode(vnode) {
226
242
  return () => false;
227
243
  }
228
244
  });
245
+ // @ts-ignore
229
246
  const children = vnode.children[key](autoObject.proxy);
230
247
  const slotLines = [];
231
248
  for (const child of children) {
@@ -286,6 +303,7 @@ async function printVNode(vnode) {
286
303
  }
287
304
  }
288
305
  else if (vnode?.shapeFlag & 1 << 4) {
306
+ // @ts-ignore
289
307
  for (const child of vnode.children) {
290
308
  lines.push(...(await printVNode(child)).lines);
291
309
  }
@@ -294,18 +312,24 @@ async function printVNode(vnode) {
294
312
  lines,
295
313
  };
296
314
  }
297
- function getTagName(vnode) {
315
+ export function getTagName(vnode) {
298
316
  if (typeof vnode.type === 'string') {
299
317
  return vnode.type;
318
+ // @ts-ignore
300
319
  }
301
320
  else if (vnode.type?.__asyncResolved) {
321
+ // @ts-ignore
302
322
  const asyncComp = vnode.type?.__asyncResolved;
303
323
  return asyncComp.name ?? getNameFromFile(asyncComp.__file);
324
+ // @ts-ignore
304
325
  }
305
326
  else if (vnode.type?.name) {
327
+ // @ts-ignore
306
328
  return vnode.type.name;
329
+ // @ts-ignore
307
330
  }
308
331
  else if (vnode.type?.__file) {
332
+ // @ts-ignore
309
333
  return getNameFromFile(vnode.type.__file);
310
334
  }
311
335
  return 'Anonymous';
@@ -321,6 +345,7 @@ function serializeAndCleanJs(value) {
321
345
  const isAutoBuildingObject = !!value?.__autoBuildingObject;
322
346
  const result = serializeJs(value);
323
347
  if (isAutoBuildingObject) {
348
+ // @ts-ignore
324
349
  return [cleanupExpression(result.__autoBuildingObjectGetKey)];
325
350
  }
326
351
  else {
@@ -0,0 +1,46 @@
1
+ <script lang="ts" setup>
2
+ import { reactive } from 'vue'
3
+
4
+ const progress = reactive({
5
+ loaded: 0,
6
+ total: 0,
7
+ })
8
+
9
+ const maxCols = window.innerWidth / 20
10
+
11
+ // @ts-expect-error vite hot api
12
+ if (import.meta.hot) {
13
+ // @ts-expect-error vite hot api
14
+ import.meta.hot.on('histoire:stories-loading-progress', (data) => {
15
+ progress.loaded = data.loadedFileCount
16
+ progress.total = data.totalFileCount
17
+ })
18
+ }
19
+ </script>
20
+
21
+ <template>
22
+ <div class="htw-fixed htw-inset-0 htw-bg-white dark:htw-bg-gray-700 htw-flex htw-flex-col htw-gap-6 htw-items-center htw-justify-center">
23
+ <transition name="__histoire-fade">
24
+ <div
25
+ v-if="progress.total > 0"
26
+ class="htw-grid htw-gap-2"
27
+ :style="{
28
+ gridTemplateColumns: `repeat(${Math.min(Math.ceil(Math.sqrt(progress.total)), maxCols)}, minmax(0, 1fr))`,
29
+ }"
30
+ >
31
+ <div
32
+ v-for="n in progress.total"
33
+ :key="n"
34
+ class="htw-bg-primary-500/10 htw-rounded-full"
35
+ >
36
+ <div
37
+ class="htw-w-3 htw-h-3 htw-bg-primary-500 htw-rounded-full htw-duration-150 htw-ease-out"
38
+ :class="{
39
+ 'htw-transition-transform htw-scale-0': n >= progress.loaded,
40
+ }"
41
+ />
42
+ </div>
43
+ </div>
44
+ </transition>
45
+ </div>
46
+ </template>
@@ -10,7 +10,7 @@ const emits = defineEmits<{(e: 'close'): void}>()
10
10
  </script>
11
11
 
12
12
  <template>
13
- <transition name="__histoire-fade">
13
+ <transition name="__histoire-fade-bottom">
14
14
  <div
15
15
  v-if="opened"
16
16
  class="htw-absolute htw-z-10 htw-bg-white dark:htw-bg-gray-700 htw-w-screen htw-h-screen htw-inset-0 htw-overflow-hidden htw-flex htw-flex-col"
@@ -46,7 +46,8 @@ export default defineComponent({
46
46
  for (const key in storyComponent.devtoolsRawSetupState) {
47
47
  addImplicitState(key, storyComponent.devtoolsRawSetupState[key])
48
48
  }
49
- provide('implicitState', () => implicitState)
49
+ // Shallow copy to prevent sharing object with different variants
50
+ provide('implicitState', () => ({ ...implicitState }))
50
51
 
51
52
  function updateStory () {
52
53
  Object.assign(attrs.story, {
@@ -1,6 +1,7 @@
1
1
  <script lang="ts">
2
2
  import { defineComponent, getCurrentInstance, inject, PropType, useAttrs, watch } from 'vue'
3
3
  import { Variant } from '../../types'
4
+ import { applyStateToVariant } from '../../util/state'
4
5
 
5
6
  // const logLocation = location.href.includes('__sandbox') ? '[Sandbox]' : '[Host]'
6
7
 
@@ -41,9 +42,7 @@ export default defineComponent({
41
42
  const implicitState = inject<() => any>('implicitState')
42
43
 
43
44
  watch(() => implicitState, value => {
44
- if (typeof props.initState !== 'function') {
45
- attrs.variant.state = value()
46
- }
45
+ applyStateToVariant(attrs.variant, value())
47
46
  }, {
48
47
  immediate: true,
49
48
  })
@@ -52,7 +51,8 @@ export default defineComponent({
52
51
  Object.assign(attrs.variant, {
53
52
  initState: async () => {
54
53
  if (typeof props.initState === 'function') {
55
- attrs.variant.state = await props.initState()
54
+ const state = await props.initState()
55
+ applyStateToVariant(attrs.variant, state)
56
56
  }
57
57
  },
58
58
  slots: () => vm.proxy.$slots,
@@ -0,0 +1,96 @@
1
+ <script lang="ts" setup>
2
+ import { computed, ref } from 'vue'
3
+ import { Icon } from '@iconify/vue'
4
+ import type { Variant, PropDefinition, AutoPropComponentDefinition } from '../../types'
5
+
6
+ const props = defineProps<{
7
+ variant: Variant
8
+ component: AutoPropComponentDefinition
9
+ definition: PropDefinition
10
+ }>()
11
+
12
+ const comp = computed(() => {
13
+ switch (props.definition.types?.[0]) {
14
+ case 'string':
15
+ return 'HstText'
16
+ case 'number':
17
+ return 'HstNumber'
18
+ case 'boolean':
19
+ return 'HstCheckbox'
20
+ case 'object':
21
+ default:
22
+ return 'HstTextarea'
23
+ }
24
+ })
25
+
26
+ const isJSON = computed(() => comp.value === 'HstTextarea')
27
+
28
+ const invalidValue = ref('')
29
+
30
+ const model = computed({
31
+ get: () => {
32
+ if (invalidValue.value) {
33
+ return invalidValue.value
34
+ }
35
+ let val = props.variant.state._hPropState[props.component.index]?.[props.definition.name]
36
+ if (val && isJSON.value) {
37
+ val = JSON.stringify(val, null, 2)
38
+ }
39
+ return val
40
+ },
41
+ set: (value) => {
42
+ invalidValue.value = ''
43
+ if (isJSON.value) {
44
+ try {
45
+ value = JSON.parse(value)
46
+ } catch (e) {
47
+ invalidValue.value = value
48
+ return
49
+ }
50
+ }
51
+ if (!props.variant.state._hPropState[props.component.index]) {
52
+ // eslint-disable-next-line vue/no-mutating-props
53
+ props.variant.state._hPropState[props.component.index] = {}
54
+ }
55
+ // eslint-disable-next-line vue/no-mutating-props
56
+ props.variant.state._hPropState[props.component.index][props.definition.name] = value
57
+ },
58
+ })
59
+
60
+ function reset () {
61
+ if (props.variant.state._hPropState[props.component.index]) {
62
+ delete props.variant.state._hPropState[props.component.index][props.definition.name]
63
+ }
64
+ }
65
+
66
+ const canReset = computed(() => props.variant.state?._hPropState?.[props.component.index] && props.definition.name in props.variant.state._hPropState[props.component.index])
67
+ </script>
68
+
69
+ <template>
70
+ <component
71
+ :is="comp"
72
+ v-if="comp"
73
+ v-model="model"
74
+ :title="definition.name"
75
+ :placeholder="isJSON ? 'Enter JSON' : null"
76
+ >
77
+ <template #actions>
78
+ <Icon
79
+ v-if="invalidValue"
80
+ v-tooltip="'JSON error'"
81
+ icon="carbon:warning-alt"
82
+ class="htw-text-orange-500"
83
+ />
84
+
85
+ <Icon
86
+ v-tooltip="'Remove override'"
87
+ icon="carbon:erase"
88
+ class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
89
+ :class="[
90
+ canReset ? 'htw-opacity-50 hover:htw-opacity-100' : 'htw-opacity-25 htw-pointer-events-none',
91
+ ]"
92
+ @click.stop="reset()"
93
+ />
94
+ </template>
95
+ </component>
96
+ </template>
@@ -0,0 +1,33 @@
1
+ <script lang="ts" setup>
2
+ import type { AutoPropComponentDefinition, Variant } from '../../types'
3
+ import { Icon } from '@iconify/vue'
4
+ import ControlsComponentPropItem from './ControlsComponentPropItem.vue'
5
+
6
+ defineProps<{
7
+ variant: Variant
8
+ definition: AutoPropComponentDefinition
9
+ }>()
10
+ </script>
11
+
12
+ <template>
13
+ <div>
14
+ <div class="htw-font-mono htw-p-2 htw-flex htw-items-center htw-gap-1">
15
+ <Icon
16
+ v-tooltip="'Auto-detected props'"
17
+ icon="carbon:flash"
18
+ class="htw-w-4 htw-h-4 htw-text-primary-500 htw-flex-none"
19
+ />
20
+ <div>
21
+ <span class="htw-opacity-30">&lt;</span>{{ definition.name }}<span class="htw-opacity-30">&gt;</span>
22
+ </div>
23
+ </div>
24
+
25
+ <ControlsComponentPropItem
26
+ v-for="prop of definition.props"
27
+ :key="prop.name"
28
+ :variant="variant"
29
+ :component="definition"
30
+ :definition="prop"
31
+ />
32
+ </div>
33
+ </template>
@@ -3,7 +3,7 @@ import { ref, computed, nextTick, onMounted } from 'vue'
3
3
  import { Icon } from '@iconify/vue'
4
4
  import { useStorage, onClickOutside, useTimeoutFn } from '@vueuse/core'
5
5
  import BaseSelect from '../base/BaseSelect.vue'
6
- import { applyStateToVariant, clone, toRawDeep } from '../../util/state'
6
+ import { applyStateToVariant, clone, omit, toRawDeep } from '../../util/state'
7
7
  import type { Story, Variant } from '../../types'
8
8
 
9
9
  const DEFAULT_ID = 'default'
@@ -15,7 +15,9 @@ const props = defineProps<{
15
15
 
16
16
  const saveId = computed(() => `${props.story.id}:${props.variant.id}`)
17
17
 
18
- const defaultState = clone(toRawDeep(props.variant.state))
18
+ const omitKeys = ['_hPropDefs']
19
+
20
+ const defaultState = clone(omit(toRawDeep(props.variant.state), omitKeys))
19
21
 
20
22
  const selectedOption = useStorage<string>(
21
23
  `_histoire-presets/${saveId.value}/selected`,
@@ -37,14 +39,14 @@ const presetsOptions = computed(() => {
37
39
 
38
40
  function resetState () {
39
41
  selectedOption.value = DEFAULT_ID
40
- applyStateToVariant(props.variant, defaultState)
42
+ applyStateToVariant(props.variant, clone(defaultState))
41
43
  }
42
44
 
43
45
  function applyPreset (id) {
44
46
  if (id === DEFAULT_ID) {
45
47
  resetState()
46
48
  } else if (presetStates.value.has(id)) {
47
- applyStateToVariant(props.variant, presetStates.value.get(id).state)
49
+ applyStateToVariant(props.variant, clone(presetStates.value.get(id).state))
48
50
  }
49
51
  }
50
52
 
@@ -62,7 +64,7 @@ const isEditing = ref(false)
62
64
  async function createPreset () {
63
65
  const id = (new Date()).getTime().toString()
64
66
 
65
- presetStates.value.set(id, { state: clone(toRawDeep(props.variant.state)), label: 'New preset' })
67
+ presetStates.value.set(id, { state: clone(omit(toRawDeep(props.variant.state), omitKeys)), label: 'New preset' })
66
68
  selectedOption.value = id
67
69
  isEditing.value = true
68
70
  await nextTick()
@@ -78,7 +80,7 @@ async function savePreset () {
78
80
  if (!canEdit.value) return
79
81
 
80
82
  const preset = presetStates.value.get(selectedOption.value)
81
- preset.state = clone(toRawDeep(props.variant.state))
83
+ preset.state = clone(omit(toRawDeep(props.variant.state), omitKeys))
82
84
  savedNotif.value = true
83
85
  savedTimeout.start()
84
86
  }
@@ -5,6 +5,7 @@ import type { Story, Variant } from '../../types'
5
5
  import SandboxVue3 from '../sandbox/SandboxVue3.vue'
6
6
  import BaseEmpty from '../base/BaseEmpty.vue'
7
7
  import StatePresets from './StatePresets.vue'
8
+ import ControlsComponentProps from './ControlsComponentProps.vue'
8
9
 
9
10
  defineProps({
10
11
  variant: {
@@ -22,11 +23,11 @@ defineProps({
22
23
  <template>
23
24
  <div
24
25
  data-test-id="story-controls"
25
- class="htw-flex htw-flex-col"
26
+ class="htw-flex htw-flex-col htw-divide-y htw-divide-gray-100 dark:htw-divide-gray-750"
26
27
  >
27
28
  <!-- Toolbar -->
28
29
  <div
29
- class="htw-h-9 htw-flex-none htw-border-b htw-border-solid htw-border-gray-100 dark:htw-border-gray-750 htw-px-2 htw-flex htw-items-center"
30
+ class="htw-h-9 htw-flex-none htw-px-2 htw-flex htw-items-center"
30
31
  >
31
32
  <StatePresets
32
33
  :story="story"
@@ -40,14 +41,28 @@ defineProps({
40
41
  slot-name="controls"
41
42
  :variant="variant"
42
43
  :story="story"
44
+ class="htw-flex-none"
43
45
  />
44
46
 
45
- <BaseEmpty v-else>
47
+ <BaseEmpty v-else-if="!variant.state?._hPropDefs?.length">
46
48
  <Icon
47
49
  icon="carbon:audio-console"
48
50
  class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
49
51
  />
50
52
  <span>No controls available for this story</span>
51
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>
52
67
  </div>
53
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
 
@@ -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;
@@ -6,18 +6,19 @@ export interface StoryFile {
6
6
  story: Story;
7
7
  path: string[];
8
8
  }
9
+ export declare type StoryLayout = {
10
+ type: 'single';
11
+ iframe?: boolean;
12
+ } | {
13
+ type: 'grid';
14
+ width?: number | string;
15
+ };
9
16
  export interface Story {
10
17
  id: string;
11
18
  title: string;
12
19
  group?: string;
13
20
  variants: Variant[];
14
- layout?: {
15
- type: 'single';
16
- iframe?: boolean;
17
- } | {
18
- type: 'grid';
19
- width?: number | string;
20
- };
21
+ layout?: StoryLayout;
21
22
  icon?: string;
22
23
  iconColor?: string;
23
24
  docsOnly?: boolean;
@@ -71,3 +72,14 @@ export interface PreviewSettings {
71
72
  backgroundColor: string;
72
73
  checkerboard: boolean;
73
74
  }
75
+ export interface PropDefinition {
76
+ name: string;
77
+ types?: string[];
78
+ required?: boolean;
79
+ default?: any;
80
+ }
81
+ export interface AutoPropComponentDefinition {
82
+ name: string;
83
+ index: number;
84
+ props: PropDefinition[];
85
+ }
@@ -1,4 +1,5 @@
1
1
  import type { Variant } from '../types';
2
2
  export declare function toRawDeep(val: any): any;
3
3
  export declare function clone(data: any): any;
4
+ export declare function omit(data: any, keys: string[]): {};
4
5
  export declare function applyStateToVariant(variant: Variant, state: any): void;
@@ -19,7 +19,7 @@ export function clone(data) {
19
19
  return structuredClone(data);
20
20
  }
21
21
  catch (e) {
22
- console.error(e);
22
+ console.warn(e, `Fallback to JSON cloning`);
23
23
  try {
24
24
  return JSON.parse(JSON.stringify(data));
25
25
  }
@@ -29,10 +29,19 @@ export function clone(data) {
29
29
  return data;
30
30
  }
31
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
+ }
32
41
  export function applyStateToVariant(variant, state) {
33
42
  if (variant.state) {
34
43
  for (const key in state) {
35
- if (typeof variant.state[key] === 'object') {
44
+ if (variant.state[key] && !key.startsWith('_h') && typeof variant.state[key] === 'object' && !Array.isArray(variant.state[key])) {
36
45
  Object.assign(variant.state[key], state[key]);
37
46
  }
38
47
  else {