histoire 0.2.3 → 0.3.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 (178) hide show
  1. package/dist/client/app/App.vue +9 -4
  2. package/dist/client/app/codegen/const.d.ts +1 -0
  3. package/dist/client/app/codegen/const.js +3 -0
  4. package/dist/client/app/codegen/vue3.js +82 -21
  5. package/dist/client/app/components/app/AppHeader.vue +2 -2
  6. package/dist/client/app/components/app/Breadcrumb.vue +1 -1
  7. package/dist/client/app/components/base/BaseEmpty.vue +17 -1
  8. package/dist/client/app/components/base/BaseListItemLink.vue +1 -1
  9. package/dist/client/app/components/base/BaseTab.vue +1 -1
  10. package/dist/client/app/components/exposed/Story.vue +22 -1
  11. package/dist/client/app/components/exposed/Variant.vue +13 -1
  12. package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -4
  13. package/dist/client/app/components/sandbox/lib.js +2 -1
  14. package/dist/client/app/components/search/SearchItem.vue +1 -0
  15. package/dist/client/app/components/search/SearchLoading.vue +16 -0
  16. package/dist/client/app/components/search/SearchModal.vue +12 -148
  17. package/dist/client/app/components/search/SearchPane.vue +210 -0
  18. package/dist/client/app/components/search/search-data.d.ts +2 -0
  19. package/dist/client/app/components/search/search-data.js +7 -0
  20. package/dist/client/app/components/story/StoryControls.vue +5 -0
  21. package/dist/client/app/components/story/StoryDocs.vue +18 -11
  22. package/dist/client/app/components/story/StoryEvent.vue +53 -0
  23. package/dist/client/app/components/story/StoryEvents.vue +45 -0
  24. package/dist/client/app/components/story/StoryResponsivePreview.vue +41 -8
  25. package/dist/client/app/components/story/StorySidePanel.vue +28 -3
  26. package/dist/client/app/components/story/StorySourceCode.vue +24 -14
  27. package/dist/client/app/components/story/StoryView.vue +16 -2
  28. package/dist/client/app/components/story/toolbar/StoryVariantNewTab.vue +1 -1
  29. package/dist/client/app/components/tree/StoryGroup.vue +75 -0
  30. package/dist/client/app/components/{story → tree}/StoryList.vue +8 -2
  31. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListFolder.vue +10 -8
  32. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListItem.vue +7 -4
  33. package/dist/client/app/router.d.ts +1 -0
  34. package/dist/client/app/router.js +3 -1
  35. package/dist/client/app/sandbox.js +11 -1
  36. package/dist/client/app/stores/events.d.ts +29 -0
  37. package/dist/client/app/stores/events.js +25 -0
  38. package/dist/client/app/stores/folder.d.ts +10 -0
  39. package/dist/client/app/stores/folder.js +47 -0
  40. package/dist/client/app/stores/story.d.ts +20 -8
  41. package/dist/client/app/stores/story.js +20 -28
  42. package/dist/client/app/style/floating-vue.pcss +3 -1
  43. package/dist/client/app/style/main.pcss +5 -0
  44. package/dist/client/app/style/sandbox.css +2 -0
  45. package/dist/client/app/types.d.ts +9 -1
  46. package/dist/client/app/util/const.d.ts +1 -0
  47. package/dist/client/app/util/const.js +1 -0
  48. package/dist/client/app/util/events.d.ts +1 -0
  49. package/dist/client/app/util/events.js +31 -0
  50. package/dist/client/app/util/mapping.js +2 -2
  51. package/dist/client/index.d.ts +2 -1
  52. package/dist/client/index.js +2 -1
  53. package/dist/client/server/Story.server.vue +21 -4
  54. package/dist/node/alias.d.ts +2 -1
  55. package/dist/node/alias.js +3 -2
  56. package/dist/node/build.js +74 -21
  57. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
  58. package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
  59. package/dist/node/collect/index.js +5 -3
  60. package/dist/node/commands/preview.js +4 -25
  61. package/dist/node/config.d.ts +111 -5
  62. package/dist/node/config.js +40 -23
  63. package/dist/node/context.d.ts +3 -3
  64. package/dist/node/context.js +1 -1
  65. package/dist/node/index.d.ts +1 -0
  66. package/dist/node/index.js +1 -0
  67. package/dist/node/load.d.ts +12 -0
  68. package/dist/node/load.js +42 -0
  69. package/dist/node/plugin.d.ts +67 -11
  70. package/dist/node/plugin.js +42 -245
  71. package/dist/node/preview.d.ts +8 -0
  72. package/dist/node/preview.js +41 -0
  73. package/dist/node/search.d.ts +2 -0
  74. package/dist/node/search.js +68 -0
  75. package/dist/node/server.js +43 -21
  76. package/dist/node/stories.d.ts +2 -0
  77. package/dist/node/stories.js +9 -4
  78. package/dist/node/tree.d.ts +16 -12
  79. package/dist/node/tree.js +45 -5
  80. package/dist/node/types.d.ts +11 -0
  81. package/dist/node/util/find-up.d.ts +1 -0
  82. package/dist/node/util/find-up.js +20 -0
  83. package/dist/node/vite.d.ts +13 -0
  84. package/dist/node/vite.js +278 -0
  85. package/dist/style.css +179 -36
  86. package/package.json +5 -3
  87. package/scripts/copy.mjs +2 -1
  88. package/src/client/app/App.vue +9 -4
  89. package/src/client/app/codegen/const.ts +3 -0
  90. package/src/client/app/codegen/vue3.ts +80 -22
  91. package/src/client/app/components/app/AppHeader.vue +2 -2
  92. package/src/client/app/components/app/Breadcrumb.vue +1 -1
  93. package/src/client/app/components/base/BaseEmpty.vue +17 -1
  94. package/src/client/app/components/base/BaseListItemLink.vue +1 -1
  95. package/src/client/app/components/base/BaseTab.vue +1 -1
  96. package/src/client/app/components/exposed/Story.vue +22 -1
  97. package/src/client/app/components/exposed/Variant.vue +13 -1
  98. package/src/client/app/components/sandbox/SandboxVue3.vue +6 -4
  99. package/src/client/app/components/sandbox/lib.ts +3 -1
  100. package/src/client/app/components/search/SearchItem.vue +1 -0
  101. package/src/client/app/components/search/SearchLoading.vue +16 -0
  102. package/src/client/app/components/search/SearchModal.vue +12 -148
  103. package/src/client/app/components/search/SearchPane.vue +210 -0
  104. package/src/client/app/components/search/search-data.ts +9 -0
  105. package/src/client/app/components/story/StoryControls.vue +5 -0
  106. package/src/client/app/components/story/StoryDocs.vue +18 -11
  107. package/src/client/app/components/story/StoryEvent.vue +53 -0
  108. package/src/client/app/components/story/StoryEvents.vue +45 -0
  109. package/src/client/app/components/story/StoryResponsivePreview.vue +41 -8
  110. package/src/client/app/components/story/StorySidePanel.vue +28 -3
  111. package/src/client/app/components/story/StorySourceCode.vue +24 -14
  112. package/src/client/app/components/story/StoryView.vue +16 -2
  113. package/src/client/app/components/story/toolbar/StoryVariantNewTab.vue +1 -1
  114. package/src/client/app/components/tree/StoryGroup.vue +75 -0
  115. package/src/client/app/components/{story → tree}/StoryList.vue +8 -2
  116. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListFolder.vue +10 -8
  117. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListItem.vue +7 -4
  118. package/src/client/app/router.ts +4 -1
  119. package/src/client/app/sandbox.ts +12 -2
  120. package/src/client/app/stores/events.ts +36 -0
  121. package/src/client/app/stores/folder.ts +59 -0
  122. package/src/client/app/stores/story.ts +20 -35
  123. package/src/client/app/style/floating-vue.pcss +3 -1
  124. package/src/client/app/style/main.pcss +5 -0
  125. package/src/client/app/style/sandbox.css +2 -0
  126. package/src/client/app/types.ts +10 -1
  127. package/src/client/app/util/const.ts +1 -0
  128. package/src/client/app/util/events.ts +30 -0
  129. package/src/client/app/util/mapping.ts +2 -2
  130. package/src/client/index.ts +2 -1
  131. package/src/client/server/Story.server.vue +21 -4
  132. package/src/node/alias.ts +4 -2
  133. package/src/node/build.ts +86 -21
  134. package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
  135. package/src/node/collect/index.ts +6 -4
  136. package/src/node/commands/preview.ts +4 -33
  137. package/src/node/config.ts +62 -29
  138. package/src/node/context.ts +3 -3
  139. package/src/node/index.ts +1 -0
  140. package/src/node/load.ts +63 -0
  141. package/src/node/plugin.ts +75 -252
  142. package/src/node/preview.ts +56 -0
  143. package/src/node/search.ts +75 -0
  144. package/src/node/server.ts +49 -23
  145. package/src/node/stories.ts +9 -4
  146. package/src/node/tree.spec.ts +35 -17
  147. package/src/node/tree.ts +64 -10
  148. package/src/node/types.ts +12 -0
  149. package/src/node/util/find-up.ts +24 -0
  150. package/src/node/vite.ts +296 -0
  151. package/tailwind.config.cjs +1 -0
  152. package/tsconfig.json +2 -0
  153. package/dist/client/app/codegen/serialize-js.ts +0 -119
  154. package/dist/client/app/codegen/util.ts +0 -79
  155. package/dist/client/app/codegen/vue3.ts +0 -276
  156. package/dist/client/app/components/sandbox/lib.ts +0 -8
  157. package/dist/client/app/global-components.ts +0 -13
  158. package/dist/client/app/index.ts +0 -28
  159. package/dist/client/app/router.ts +0 -17
  160. package/dist/client/app/sandbox.ts +0 -94
  161. package/dist/client/app/stores/preview-settings.ts +0 -17
  162. package/dist/client/app/stores/story.ts +0 -62
  163. package/dist/client/app/types.ts +0 -70
  164. package/dist/client/app/util/config.ts +0 -6
  165. package/dist/client/app/util/const.ts +0 -3
  166. package/dist/client/app/util/dark.ts +0 -4
  167. package/dist/client/app/util/env.ts +0 -1
  168. package/dist/client/app/util/keyboard.ts +0 -99
  169. package/dist/client/app/util/mapping.ts +0 -48
  170. package/dist/client/app/util/preview-settings.ts +0 -8
  171. package/dist/client/app/util/reactivity.ts +0 -22
  172. package/dist/client/app/util/responsive.ts +0 -3
  173. package/dist/client/app/util/scroll.ts +0 -31
  174. package/dist/client/app/util/tooltip.ts +0 -13
  175. package/dist/client/app/util/variant.ts +0 -20
  176. package/dist/client/index.ts +0 -12
  177. package/dist/client/server/index.ts +0 -32
  178. package/dist/client/shim.d.ts +0 -5
@@ -39,7 +39,7 @@ onKeyboardShortcut(['ctrl+shift+d', 'meta+shift+d'], (event) => {
39
39
  <div class="htw-ml-auto htw-flex-none htw-flex">
40
40
  <a
41
41
  v-tooltip="makeTooltip('Search', ({ isMac }) => isMac ? 'meta+k' : 'ctrl+k')"
42
- class="htw-p-2 sm:htw-p-1 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-cursor-pointer"
42
+ class="htw-p-2 sm:htw-p-1 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-cursor-pointer htw-text-gray-900 dark:htw-text-gray-100"
43
43
  data-test-id="search-btn"
44
44
  @click="$emit('search')"
45
45
  >
@@ -51,7 +51,7 @@ onKeyboardShortcut(['ctrl+shift+d', 'meta+shift+d'], (event) => {
51
51
 
52
52
  <a
53
53
  v-tooltip="makeTooltip('Toggle dark mode', ({ isMac }) => isMac ? 'meta+shift+d' : 'ctrl+shift+d')"
54
- class="htw-p-2 sm:htw-p-1 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-cursor-pointer"
54
+ class="htw-p-2 sm:htw-p-1 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-cursor-pointer htw-text-gray-900 dark:htw-text-gray-100"
55
55
  @click="toggleDark()"
56
56
  >
57
57
  <Icon
@@ -4,7 +4,7 @@ import { Icon } from '@iconify/vue'
4
4
  import { useStoryStore } from '../../stores/story'
5
5
  import { Story, Tree } from '../../types'
6
6
  import MobileOverlay from './MobileOverlay.vue'
7
- import StoryList from '../story/StoryList.vue'
7
+ import StoryList from '../tree/StoryList.vue'
8
8
 
9
9
  const props = defineProps<{
10
10
  tree: Tree
@@ -1,5 +1,21 @@
1
1
  <template>
2
- <div class="htw-flex htw-flex-col htw-items-center htw-justify-center htw-space-y-4 htw-py-12 htw-h-full htw-text-center htw-text-gray-400 htw-text-lg">
2
+ <div class="htw-base-empty htw-flex htw-flex-col htw-items-center htw-justify-center htw-space-y-4 htw-py-12 htw-h-full htw-text-center htw-text-gray-400 htw-text-lg">
3
3
  <slot />
4
4
  </div>
5
5
  </template>
6
+
7
+ <style scoped>
8
+ .htw-base-empty {
9
+ animation: htw-base-empty .3s 1s both;
10
+ }
11
+
12
+ @keyframes htw-base-empty {
13
+ 0% {
14
+ opacity: 0;
15
+ }
16
+
17
+ 100% {
18
+ opacity: 1;
19
+ }
20
+ }
21
+ </style>
@@ -36,7 +36,7 @@ export default defineComponent({
36
36
  >
37
37
  <a
38
38
  :href="href"
39
- class="htw-flex htw-items-center htw-gap-2"
39
+ class="htw-flex htw-items-center htw-gap-2 htw-text-gray-900 dark:htw-text-gray-100"
40
40
  :class="[
41
41
  $attrs.class,
42
42
  (isActive != null ? isActive : linkIsActive)
@@ -27,7 +27,7 @@ export default defineComponent({
27
27
  <a
28
28
  v-bind="$attrs"
29
29
  :href="href"
30
- class="htw-px-4 htw-h-full htw-inline-flex htw-items-center hover:htw-bg-primary-50 dark:hover:htw-bg-primary-900 htw-relative"
30
+ class="htw-px-4 htw-h-full htw-inline-flex htw-items-center hover:htw-bg-primary-50 dark:hover:htw-bg-primary-900 htw-relative htw-text-gray-900 dark:htw-text-gray-100"
31
31
  :class="{
32
32
  'htw-text-primary-500 dark:htw-text-primary-400': matched != null ? matched : (exact && isExactActive) || (!exact && isActive),
33
33
  }"
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { computed, defineComponent, provide, useAttrs, VNode, h, PropType } from 'vue'
2
+ import { computed, defineComponent, provide, useAttrs, VNode, h, PropType, getCurrentInstance } from 'vue'
3
3
  import { Story } from '../../types.js'
4
4
  import Variant from './Variant.vue'
5
5
 
@@ -25,6 +25,27 @@ export default defineComponent({
25
25
  const story = computed(() => attrs.story)
26
26
  provide('story', story)
27
27
 
28
+ const storyComponent: any = getCurrentInstance().parent
29
+ // Allows tracking reactivity with watchers to sync state
30
+ const implicitState = {
31
+ $data: storyComponent.data,
32
+ }
33
+ function addImplicitState (key, value) {
34
+ if (typeof value === 'function' || (value && value.__file)) {
35
+ return
36
+ }
37
+ implicitState[key] = value
38
+ }
39
+ // From `<script setup>`'s `defineExpose`
40
+ for (const key in storyComponent.exposed) {
41
+ addImplicitState(key, storyComponent.exposed[key])
42
+ }
43
+ // We needs __VUE_PROD_DEVTOOLS__ flag set to `true` to enable `devtoolsRawSetupState`
44
+ for (const key in storyComponent.devtoolsRawSetupState) {
45
+ addImplicitState(key, storyComponent.devtoolsRawSetupState[key])
46
+ }
47
+ provide('implicitState', () => implicitState)
48
+
28
49
  return {
29
50
  story,
30
51
  }
@@ -1,7 +1,9 @@
1
1
  <script lang="ts">
2
- import { defineComponent, getCurrentInstance, PropType, useAttrs } from 'vue'
2
+ import { defineComponent, getCurrentInstance, inject, PropType, useAttrs, watch } from 'vue'
3
3
  import { Variant } from '../../types'
4
4
 
5
+ // const logLocation = location.href.includes('__sandbox') ? '[Sandbox]' : '[Host]'
6
+
5
7
  export default defineComponent({
6
8
  // eslint-disable-next-line vue/multi-word-component-names
7
9
  name: 'Variant',
@@ -31,6 +33,16 @@ export default defineComponent({
31
33
 
32
34
  const vm = getCurrentInstance()
33
35
 
36
+ const implicitState = inject<() => any>('implicitState')
37
+
38
+ watch(() => implicitState, value => {
39
+ if (typeof props.initState !== 'function') {
40
+ attrs.variant.state = value()
41
+ }
42
+ }, {
43
+ immediate: true,
44
+ })
45
+
34
46
  function updateVariant () {
35
47
  Object.assign(attrs.variant, {
36
48
  initState: async () => {
@@ -2,7 +2,7 @@
2
2
  import { App, createApp, onMounted, onUnmounted, PropType, ref, watch } from 'vue'
3
3
  import { Story, Variant } from '../../types'
4
4
  // @ts-expect-error virtual module id
5
- import setup from '$histoire-setup'
5
+ import * as setup from '$histoire-setup'
6
6
 
7
7
  const props = defineProps({
8
8
  variant: {
@@ -35,15 +35,17 @@ async function mountVariant () {
35
35
  await props.variant.initState()
36
36
 
37
37
  app = createApp({
38
+ name: 'SandboxVue3',
39
+
38
40
  render: () => {
39
41
  return props.variant.slots()?.[props.slotName]?.({
40
- state: props.variant.state,
42
+ state: props.variant.state ?? {},
41
43
  })
42
44
  },
43
45
  })
44
46
 
45
- if (typeof setup === 'function') {
46
- await setup({
47
+ if (typeof setup?.setupVue3 === 'function') {
48
+ await setup.setupVue3({
47
49
  app,
48
50
  story: props.story,
49
51
  variant: props.variant,
@@ -1,8 +1,10 @@
1
+
2
+ import { base } from '../../router'
1
3
  import { Story, Variant } from '../../types'
2
4
 
3
5
  export function getSandboxUrl (story: Story, variant: Variant) {
4
6
  const url = new URLSearchParams()
5
7
  url.append('storyId', story.id)
6
8
  url.append('variantId', variant.id)
7
- return '/__sandbox?' + url.toString()
9
+ return `${base}__sandbox?${url.toString()}`
8
10
  }
@@ -51,6 +51,7 @@ const kindLabels = {
51
51
  <div
52
52
  ref="el"
53
53
  data-test-id="search-item"
54
+ :data-selected="selected ? '' : undefined"
54
55
  >
55
56
  <BaseListItemLink
56
57
  :to="result.route"
@@ -0,0 +1,16 @@
1
+ <script lang="ts" setup>
2
+ import { Icon } from '@iconify/vue'
3
+ </script>
4
+
5
+ <template>
6
+ <div
7
+ class="htw-flex htw-items-center htw-gap-4 htw-pl-6 htw-border htw-border-transparent focus-visible:htw-border-primary-500 htw-h-[51px] htw-opacity-30"
8
+ >
9
+ <Icon
10
+ icon="carbon:search"
11
+ class="flex-none htw-w-4 htw-h-4"
12
+ />
13
+
14
+ Loading...
15
+ </div>
16
+ </template>
@@ -1,13 +1,13 @@
1
1
  <script lang="ts" setup>
2
- import { useFocus } from '@vueuse/core'
3
- import { computed, ref, watch } from 'vue'
4
- import { Icon } from '@iconify/vue'
5
- import { useStoryStore } from '../../stores/story'
6
- import BaseEmpty from '../base/BaseEmpty.vue'
7
- import type { SearchResult, Story, Variant } from '../../types'
8
- import SearchItem from './SearchItem.vue'
2
+ import { defineAsyncComponent } from 'vue'
3
+ import SearchLoading from './SearchLoading.vue'
4
+ const SearchPane = defineAsyncComponent({
5
+ loader: () => import('./SearchPane.vue'),
6
+ loadingComponent: SearchLoading,
7
+ delay: 0,
8
+ })
9
9
 
10
- const props = defineProps({
10
+ defineProps({
11
11
  shown: {
12
12
  type: Boolean,
13
13
  default: false,
@@ -21,110 +21,6 @@ const emit = defineEmits({
21
21
  function close () {
22
22
  emit('close')
23
23
  }
24
-
25
- // Autofocus
26
-
27
- const input = ref<HTMLInputElement>()
28
- const { focused } = useFocus(input, {
29
- initialValue: true,
30
- })
31
-
32
- watch(() => props.shown, value => {
33
- if (value) {
34
- requestAnimationFrame(() => {
35
- focused.value = true
36
- input.value.select()
37
- })
38
- }
39
- })
40
-
41
- // Search
42
-
43
- const search = ref('')
44
-
45
- const storyStore = useStoryStore()
46
-
47
- function storyResultFactory (story: Story, rank: number): SearchResult {
48
- return {
49
- kind: 'story',
50
- rank,
51
- id: story.id,
52
- title: story.title,
53
- route: {
54
- name: 'story',
55
- params: {
56
- storyId: story.id,
57
- },
58
- },
59
- path: story.file.path.slice(0, -1),
60
- icon: story.icon,
61
- iconColor: story.iconColor,
62
- }
63
- }
64
-
65
- function variantResultFactory (story: Story, variant: Variant, rank: number): SearchResult {
66
- return {
67
- kind: 'variant',
68
- rank,
69
- id: variant.id,
70
- title: variant.title,
71
- route: {
72
- name: 'story',
73
- params: {
74
- storyId: story.id,
75
- },
76
- query: {
77
- variantId: variant.id,
78
- },
79
- },
80
- path: [...story.file.path ?? [], story.title],
81
- icon: variant.icon,
82
- iconColor: variant.iconColor,
83
- }
84
- }
85
-
86
- const results = computed(() => {
87
- const list: SearchResult[] = []
88
- if (search.value) {
89
- const s = search.value.toLowerCase()
90
- for (const story of storyStore.stories) {
91
- const storyMatched = story.title.toLowerCase().includes(s)
92
- let storyPathMatched = false
93
- if (storyMatched || (storyPathMatched = story.file.path.some(p => p.toLowerCase().includes(s)))) {
94
- list.push(storyResultFactory(story, storyMatched ? 1 : 4))
95
- }
96
- for (const variant of story.variants) {
97
- const variantMatched = variant.title.toLowerCase().includes(s)
98
- if (storyMatched || storyPathMatched || variantMatched) {
99
- list.push(variantResultFactory(story, variant, variantMatched ? 2 : storyMatched ? 3 : 5))
100
- }
101
- }
102
- }
103
- }
104
- return list.sort((a, b) => a.rank - b.rank)
105
- })
106
-
107
- // Selection
108
-
109
- const selectedIndex = ref(0)
110
-
111
- watch(results, () => {
112
- selectedIndex.value = 0
113
- })
114
-
115
- function selectNext () {
116
- selectedIndex.value++
117
- if (selectedIndex.value > results.value.length - 1) {
118
- selectedIndex.value = 0
119
- }
120
- }
121
-
122
- function selectPrevious () {
123
- selectedIndex.value--
124
- if (selectedIndex.value < 0) {
125
- selectedIndex.value = results.value.length - 1
126
- }
127
- }
128
24
  </script>
129
25
 
130
26
  <template>
@@ -138,42 +34,10 @@ function selectPrevious () {
138
34
  @click="close()"
139
35
  />
140
36
  <div class="htw-bg-white dark:htw-bg-gray-700 md:htw-mt-16 md:htw-mx-auto htw-w-screen htw-max-w-[512px] htw-shadow-xl htw-border htw-border-gray-200 dark:htw-border-gray-850 htw-rounded-lg htw-relative htw-divide-y htw-divide-gray-200 dark:htw-divide-gray-850">
141
- <div
142
- class="htw-flex htw-items-center htw-gap-4 htw-pl-6 htw-border htw-border-transparent focus-visible:htw-border-primary-500"
143
- @click="focused = true"
144
- >
145
- <Icon
146
- icon="carbon:search"
147
- class="flex-none htw-w-4 htw-h-4"
148
- />
149
-
150
- <input
151
- ref="input"
152
- v-model="search"
153
- placeholder="Search for stories, variants..."
154
- class="htw-bg-transparent htw-w-full htw-flex-1 htw-pl-0 htw-pr-6 htw-py-4 htw-outline-none"
155
- @keydown.down.prevent="selectNext()"
156
- @keydown.up.prevent="selectPrevious()"
157
- @keydown.escape="close()"
158
- >
159
- </div>
160
-
161
- <BaseEmpty v-if="search && !results.length">
162
- No results
163
- </BaseEmpty>
164
-
165
- <div
166
- v-else-if="results.length"
167
- class="htw-max-h-[400px] htw-overflow-y-auto htw-rounded-b-lg"
168
- >
169
- <SearchItem
170
- v-for="(result, index) of results"
171
- :key="`${result.kind}-${result.id}`"
172
- :result="result"
173
- :selected="index === selectedIndex"
174
- @close="close()"
175
- />
176
- </div>
37
+ <SearchPane
38
+ :shown="shown"
39
+ @close="close()"
40
+ />
177
41
  </div>
178
42
  </div>
179
43
  </template>
@@ -0,0 +1,210 @@
1
+ <script lang="ts" setup>
2
+ import { useFocus, useDebounce } from '@vueuse/core'
3
+ import { ref, watch } from 'vue'
4
+ import { Icon } from '@iconify/vue'
5
+ import flexsearch from 'flexsearch'
6
+ import charset from 'flexsearch/dist/module/lang/latin/advanced.js'
7
+ import language from 'flexsearch/dist/module/lang/en.js'
8
+ import { useStoryStore } from '../../stores/story'
9
+ import BaseEmpty from '../base/BaseEmpty.vue'
10
+ import type { SearchResult, Story, Variant } from '../../types'
11
+ import SearchItem from './SearchItem.vue'
12
+ import { searchData, onUpdate } from './search-data.js'
13
+
14
+ const props = defineProps({
15
+ shown: {
16
+ type: Boolean,
17
+ default: false,
18
+ },
19
+ })
20
+
21
+ const emit = defineEmits({
22
+ close: () => true,
23
+ })
24
+
25
+ function close () {
26
+ emit('close')
27
+ }
28
+
29
+ // Autofocus
30
+
31
+ const input = ref<HTMLInputElement>()
32
+ const { focused } = useFocus(input, {
33
+ initialValue: true,
34
+ })
35
+
36
+ watch(() => props.shown, value => {
37
+ if (value) {
38
+ requestAnimationFrame(() => {
39
+ focused.value = true
40
+ input.value.select()
41
+ })
42
+ }
43
+ })
44
+
45
+ // Search
46
+
47
+ const searchInputText = ref('')
48
+ const rateLimitedSearch = useDebounce(searchInputText, 50)
49
+
50
+ const storyStore = useStoryStore()
51
+
52
+ let searchIndex: flexsearch.Document<any, any>
53
+
54
+ async function loadSearchIndex (data) {
55
+ searchIndex = new flexsearch.Document({
56
+ preset: 'match',
57
+ document: {
58
+ id: 'id',
59
+ index: [
60
+ 'title',
61
+ 'docs',
62
+ ],
63
+ },
64
+ worker: true,
65
+ charset,
66
+ language,
67
+ tokenize: 'full',
68
+ })
69
+
70
+ for (const key of Object.keys(data)) {
71
+ await searchIndex.import(key, data[key])
72
+ }
73
+ }
74
+
75
+ loadSearchIndex(searchData)
76
+ // Handle HMR
77
+ onUpdate(searchData => {
78
+ loadSearchIndex(searchData)
79
+ })
80
+
81
+ const results = ref<SearchResult[]>([])
82
+
83
+ watch(rateLimitedSearch, async value => {
84
+ const list: SearchResult[] = []
85
+ const raw = await searchIndex.search(value, {
86
+ enrich: true,
87
+ })
88
+ let rank = 0
89
+ for (const field of raw) {
90
+ for (const item of field.result as any[]) {
91
+ switch (item.doc.kind) {
92
+ case 'story': {
93
+ list.push(storyResultFactory(storyStore.getStoryById(item.id), rank))
94
+ rank++
95
+ break
96
+ }
97
+ case 'variant': {
98
+ const [storyId] = item.id.split(':')
99
+ const story = storyStore.getStoryById(storyId)
100
+ const variant = storyStore.getVariantById(item.id)
101
+ list.push(variantResultFactory(story, variant, rank))
102
+ rank++
103
+ break
104
+ }
105
+ }
106
+ }
107
+ }
108
+ results.value = list
109
+ })
110
+
111
+ function storyResultFactory (story: Story, rank: number): SearchResult {
112
+ return {
113
+ kind: 'story',
114
+ rank,
115
+ id: story.id,
116
+ title: story.title,
117
+ route: {
118
+ name: 'story',
119
+ params: {
120
+ storyId: story.id,
121
+ },
122
+ },
123
+ path: story.file.path.slice(0, -1),
124
+ icon: story.icon,
125
+ iconColor: story.iconColor,
126
+ }
127
+ }
128
+
129
+ function variantResultFactory (story: Story, variant: Variant, rank: number): SearchResult {
130
+ return {
131
+ kind: 'variant',
132
+ rank,
133
+ id: variant.id,
134
+ title: variant.title,
135
+ route: {
136
+ name: 'story',
137
+ params: {
138
+ storyId: story.id,
139
+ },
140
+ query: {
141
+ variantId: variant.id,
142
+ },
143
+ },
144
+ path: [...story.file.path ?? [], story.title],
145
+ icon: variant.icon,
146
+ iconColor: variant.iconColor,
147
+ }
148
+ }
149
+
150
+ // Selection
151
+
152
+ const selectedIndex = ref(0)
153
+
154
+ watch(results, () => {
155
+ selectedIndex.value = 0
156
+ })
157
+
158
+ function selectNext () {
159
+ selectedIndex.value++
160
+ if (selectedIndex.value > results.value.length - 1) {
161
+ selectedIndex.value = 0
162
+ }
163
+ }
164
+
165
+ function selectPrevious () {
166
+ selectedIndex.value--
167
+ if (selectedIndex.value < 0) {
168
+ selectedIndex.value = results.value.length - 1
169
+ }
170
+ }
171
+ </script>
172
+
173
+ <template>
174
+ <div
175
+ class="htw-flex htw-items-center htw-gap-4 htw-pl-6 htw-border htw-border-transparent focus-visible:htw-border-primary-500"
176
+ @click="focused = true"
177
+ >
178
+ <Icon
179
+ icon="carbon:search"
180
+ class="flex-none htw-w-4 htw-h-4"
181
+ />
182
+
183
+ <input
184
+ ref="input"
185
+ v-model="searchInputText"
186
+ placeholder="Search for stories, variants..."
187
+ class="htw-bg-transparent htw-w-full htw-flex-1 htw-pl-0 htw-pr-6 htw-py-4 htw-outline-none"
188
+ @keydown.down.prevent="selectNext()"
189
+ @keydown.up.prevent="selectPrevious()"
190
+ @keydown.escape="close()"
191
+ >
192
+ </div>
193
+
194
+ <BaseEmpty v-if="rateLimitedSearch && !results.length">
195
+ No results
196
+ </BaseEmpty>
197
+
198
+ <div
199
+ v-else-if="results.length"
200
+ class="htw-max-h-[400px] htw-overflow-y-auto htw-rounded-b-lg"
201
+ >
202
+ <SearchItem
203
+ v-for="(result, index) of results"
204
+ :key="`${result.kind}-${result.id}`"
205
+ :result="result"
206
+ :selected="index === selectedIndex"
207
+ @close="close()"
208
+ />
209
+ </div>
210
+ </template>
@@ -0,0 +1,9 @@
1
+ import { markRaw } from 'vue'
2
+ // @ts-expect-error virtual module
3
+ import { searchData as _searchData, onUpdate as _onUpdate } from '$histoire-search-data'
4
+
5
+ export const searchData: Record<string, any> = markRaw(_searchData)
6
+
7
+ export function onUpdate (cb: (data: typeof searchData) => unknown) {
8
+ _onUpdate(cb)
9
+ }
@@ -1,5 +1,6 @@
1
1
  <script lang="ts" setup>
2
2
  import { PropType } from 'vue'
3
+ import { Icon } from '@iconify/vue'
3
4
  import type { Story, Variant } from '../../types'
4
5
  import SandboxVue3 from '../sandbox/SandboxVue3.vue'
5
6
  import BaseEmpty from '../base/BaseEmpty.vue'
@@ -28,6 +29,10 @@ defineProps({
28
29
  />
29
30
 
30
31
  <BaseEmpty v-else>
32
+ <Icon
33
+ icon="carbon:audio-console"
34
+ class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
35
+ />
31
36
  <span>No controls available for this story</span>
32
37
  </BaseEmpty>
33
38
  </div>
@@ -1,5 +1,6 @@
1
1
  <script lang="ts" setup>
2
2
  import { PropType, ref, watchEffect } from 'vue'
3
+ import { Icon } from '@iconify/vue'
3
4
  import type { Story } from '../../types'
4
5
  import BaseEmpty from '../base/BaseEmpty.vue'
5
6
 
@@ -24,15 +25,21 @@ watchEffect(async () => {
24
25
  </script>
25
26
 
26
27
  <template>
27
- <BaseEmpty
28
- v-if="!renderedDoc"
29
- >
30
- No documentation available
31
- </BaseEmpty>
32
- <div
33
- v-else
34
- class="htw-prose dark:htw-prose-invert htw-p-4 htw-max-w-none"
35
- data-test-id="story-docs"
36
- v-html="renderedDoc"
37
- />
28
+ <div>
29
+ <BaseEmpty
30
+ v-if="!renderedDoc"
31
+ >
32
+ <Icon
33
+ icon="carbon:document-unknown"
34
+ class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
35
+ />
36
+ No documentation available
37
+ </BaseEmpty>
38
+ <div
39
+ v-else
40
+ class="htw-prose dark:htw-prose-invert htw-p-4 htw-max-w-none"
41
+ data-test-id="story-docs"
42
+ v-html="renderedDoc"
43
+ />
44
+ </div>
38
45
  </template>