histoire 0.7.2 → 0.7.5

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 (112) hide show
  1. package/client.d.ts +1 -1
  2. package/dist/bundled/StoryView.js +22 -19
  3. package/dist/bundled/dark.js +4 -0
  4. package/dist/bundled/index.js +0 -1
  5. package/dist/bundled/index2.js +6 -6
  6. package/dist/bundled/mapping.js +78 -0
  7. package/dist/bundled/sandbox.js +7 -6
  8. package/dist/bundled/state.js +13 -44
  9. package/dist/bundled/style.css +3 -3
  10. package/dist/client/app/App.vue +2 -2
  11. package/dist/client/app/components/panel/ControlsComponentPropItem.vue +13 -6
  12. package/dist/client/app/components/panel/ControlsComponentProps.vue +2 -1
  13. package/dist/client/app/components/panel/StatePresets.vue +2 -1
  14. package/dist/client/app/components/panel/StoryControls.vue +1 -1
  15. package/dist/client/app/components/panel/StorySourceCode.vue +2 -2
  16. package/dist/client/app/components/story/StoryVariantGridItem.vue +1 -1
  17. package/dist/client/app/components/story/StoryVariantSinglePreviewNative.vue +1 -1
  18. package/dist/client/app/components/story/StoryVariantSinglePreviewRemote.vue +3 -2
  19. package/dist/client/app/components/story/StoryVariantSingleView.vue +1 -1
  20. package/dist/client/app/components/toolbar/StoryVariantNewTab.vue +1 -1
  21. package/dist/client/app/components/tree/StoryGroup.vue +1 -1
  22. package/dist/client/app/index.js +0 -2
  23. package/dist/client/app/sandbox.js +4 -5
  24. package/dist/client/app/types.d.ts +1 -52
  25. package/dist/client/app/util/mapping.d.ts +1 -1
  26. package/dist/client/app/util/mapping.js +50 -36
  27. package/dist/client/app/{components/sandbox/lib.d.ts → util/sandbox.d.ts} +1 -1
  28. package/dist/client/app/{components/sandbox/lib.js → util/sandbox.js} +1 -1
  29. package/dist/client/app/util/state.d.ts +1 -5
  30. package/dist/client/app/util/state.js +12 -44
  31. package/dist/node/collect/index.d.ts +2 -2
  32. package/dist/node/collect/index.js +3 -0
  33. package/dist/node/collect/run.d.ts +2 -0
  34. package/dist/{client/server/index.js → node/collect/run.js} +1 -1
  35. package/dist/node/collect/worker.d.ts +3 -3
  36. package/dist/node/collect/worker.js +4 -1
  37. package/dist/node/config.d.ts +3 -3
  38. package/dist/node/context.d.ts +2 -2
  39. package/dist/node/plugin.d.ts +3 -3
  40. package/dist/node/stories.d.ts +4 -4
  41. package/dist/node/tree.d.ts +3 -22
  42. package/dist/node/vite.js +1 -0
  43. package/dist/style.css +76 -0
  44. package/package.json +4 -2
  45. package/src/client/app/App.vue +2 -2
  46. package/src/client/app/components/panel/ControlsComponentPropItem.vue +13 -6
  47. package/src/client/app/components/panel/ControlsComponentProps.vue +2 -1
  48. package/src/client/app/components/panel/StatePresets.vue +2 -1
  49. package/src/client/app/components/panel/StoryControls.vue +1 -1
  50. package/src/client/app/components/panel/StorySourceCode.vue +2 -2
  51. package/src/client/app/components/story/StoryVariantGridItem.vue +1 -1
  52. package/src/client/app/components/story/StoryVariantSinglePreviewNative.vue +1 -1
  53. package/src/client/app/components/story/StoryVariantSinglePreviewRemote.vue +3 -2
  54. package/src/client/app/components/story/StoryVariantSingleView.vue +1 -1
  55. package/src/client/app/components/toolbar/StoryVariantNewTab.vue +1 -1
  56. package/src/client/app/components/tree/StoryGroup.vue +1 -1
  57. package/src/client/app/index.ts +0 -2
  58. package/src/client/app/sandbox.ts +4 -5
  59. package/src/client/app/types.ts +6 -57
  60. package/src/client/app/util/mapping.ts +47 -34
  61. package/src/client/app/{components/sandbox/lib.ts → util/sandbox.ts} +2 -2
  62. package/src/client/app/util/state.ts +15 -44
  63. package/src/node/collect/index.ts +5 -2
  64. package/src/node/collect/run.ts +13 -0
  65. package/src/node/collect/worker.ts +8 -6
  66. package/src/node/config.ts +3 -3
  67. package/src/node/context.ts +2 -2
  68. package/src/node/plugin.ts +2 -2
  69. package/src/node/server.ts +2 -2
  70. package/src/node/stories.ts +4 -4
  71. package/src/node/tree.ts +7 -31
  72. package/src/node/vite.ts +1 -0
  73. package/vite.config.ts +12 -7
  74. package/dist/bundled/SandboxVue3.js +0 -706
  75. package/dist/bundled/global-components.js +0 -213
  76. package/dist/client/app/codegen/const.d.ts +0 -1
  77. package/dist/client/app/codegen/const.js +0 -3
  78. package/dist/client/app/codegen/serialize-js.d.ts +0 -1
  79. package/dist/client/app/codegen/serialize-js.js +0 -113
  80. package/dist/client/app/codegen/util.d.ts +0 -10
  81. package/dist/client/app/codegen/util.js +0 -70
  82. package/dist/client/app/codegen/vue3.d.ts +0 -4
  83. package/dist/client/app/codegen/vue3.js +0 -357
  84. package/dist/client/app/components/exposed/Story.vue +0 -106
  85. package/dist/client/app/components/exposed/Variant.vue +0 -78
  86. package/dist/client/app/components/sandbox/RouterLinkStub.d.ts +0 -21
  87. package/dist/client/app/components/sandbox/RouterLinkStub.js +0 -43
  88. package/dist/client/app/components/sandbox/SandboxVue3.vue +0 -185
  89. package/dist/client/app/global-components.d.ts +0 -2
  90. package/dist/client/app/global-components.js +0 -10
  91. package/dist/client/server/index.d.ts +0 -7
  92. package/dist/client/server/vue3/Story.vue +0 -120
  93. package/dist/client/server/vue3/Variant.vue +0 -52
  94. package/dist/client/server/vue3/run.d.ts +0 -2
  95. package/dist/client/server/vue3/run.js +0 -45
  96. package/dist/node/types.d.ts +0 -50
  97. package/dist/node/types.js +0 -1
  98. package/src/client/app/codegen/const.ts +0 -3
  99. package/src/client/app/codegen/serialize-js.ts +0 -119
  100. package/src/client/app/codegen/util.ts +0 -79
  101. package/src/client/app/codegen/vue3.ts +0 -361
  102. package/src/client/app/components/exposed/Story.vue +0 -106
  103. package/src/client/app/components/exposed/Variant.vue +0 -78
  104. package/src/client/app/components/sandbox/RouterLinkStub.ts +0 -49
  105. package/src/client/app/components/sandbox/SandboxVue3.vue +0 -185
  106. package/src/client/app/global-components.ts +0 -13
  107. package/src/client/server/index.ts +0 -19
  108. package/src/client/server/vue3/Story.vue +0 -120
  109. package/src/client/server/vue3/Variant.vue +0 -52
  110. package/src/client/server/vue3/run.ts +0 -55
  111. package/src/components.d.ts +0 -143
  112. package/src/node/types.ts +0 -53
@@ -1,361 +0,0 @@
1
- // @TODO remove @ts-ignore
2
-
3
- import { VNode, vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic, Text } from 'vue'
4
- import { pascal } from 'case'
5
- import { createAutoBuildingObject, indent } from './util'
6
- import { serializeJs } from './serialize-js'
7
- import type { Variant } from '../types'
8
- import { voidElements } from './const'
9
-
10
- export async function generateSourceCode (variant: Variant) {
11
- const vnode = variant.slots().default?.({ state: variant.state ?? {} }) ?? []
12
- const list = Array.isArray(vnode) ? vnode : [vnode]
13
- const lines: string[] = []
14
- for (const n in list) {
15
- const vnode = list[n]
16
- lines.push(...(await printVNode(vnode, variant.state?._hPropState?.[n])).lines)
17
- }
18
- return lines.join('\n')
19
- }
20
-
21
- async function printVNode (vnode: VNode, propsOverrides: Record<string, any> = null): Promise<{ lines: string[], isText?: boolean }> {
22
- if (vnode.type === Text) {
23
- return {
24
- // @ts-ignore
25
- lines: [vnode.children],
26
- isText: true,
27
- }
28
- }
29
-
30
- const lines: string[] = []
31
-
32
- if (typeof vnode.type === 'object' || typeof vnode.type === 'string') {
33
- // Wait for async component
34
- // @ts-ignore
35
- if (vnode.type?.__asyncLoader && !vnode.type.__asyncResolved) {
36
- // @ts-ignore
37
- await vnode.type.__asyncLoader()
38
- }
39
-
40
- const attrs: string[][] = []
41
- let multilineAttrs = false
42
- const skipProps: string[] = [
43
- 'key',
44
- ]
45
-
46
- // Directives
47
- function genDirective (dirName: string, dir, valueCode: string = null) {
48
- let modifiers = ''
49
- for (const key in dir.modifiers) {
50
- if (dir.modifiers[key]) {
51
- modifiers += `.${key}`
52
- }
53
- }
54
- let arg = ''
55
- if (dir.arg) {
56
- arg = `:${dir.arg}`
57
- }
58
- if (valueCode) {
59
- // Cleanup render code
60
- valueCode = valueCode.replace(/^\$(setup|props|data)\./g, '')
61
- }
62
- const valueLines = valueCode ? [valueCode] : serializeAndCleanJs(dir.value)
63
- const attr: string[] = []
64
- const dirAttr = `v-${dirName}${arg}${modifiers}="`
65
- if (valueLines.length > 1) {
66
- attr.push(`${dirAttr}${valueLines[0]}`)
67
- attr.push(...valueLines.slice(1, valueLines.length - 1))
68
- attr.push(`${valueLines[valueLines.length - 1]}"`)
69
- multilineAttrs = true
70
- } else {
71
- attr.push(`${dirAttr}${valueLines[0] ?? ''}"`)
72
- }
73
- attrs.push(attr)
74
- }
75
- if (vnode.dirs) {
76
- for (const dir of vnode.dirs) {
77
- // Vmodel
78
- if (dir.dir === vModelText || dir.dir === vModelSelect || dir.dir === vModelRadio || dir.dir === vModelCheckbox || dir.dir === vModelDynamic) {
79
- const listenerKey = `onUpdate:${dir.arg ?? 'modelValue'}`
80
- const listener = vnode.props[listenerKey]
81
- let valueCode: string = null
82
- if (listener) {
83
- skipProps.push(listenerKey)
84
- const listenerSource = listener.toString()
85
- const result = /\(\$event\) => (.*?) = \$event/.exec(listenerSource)
86
- if (result) {
87
- valueCode = result[1]
88
- }
89
- }
90
- genDirective('model', dir, valueCode)
91
- // @ts-ignore
92
- } else if (dir.instance._ || dir.instance.$) {
93
- // @ts-ignore
94
- const target = dir.instance.$ ?? dir.instance._
95
- let dirName: string
96
- for (const directives of [target.directives, target.appContext.directives]) {
97
- for (const key in directives) {
98
- if (target.directives[key] === dir.dir) {
99
- dirName = key
100
- break
101
- }
102
- }
103
- if (dirName) break
104
- }
105
- if (dirName) {
106
- genDirective(dirName, dir)
107
- }
108
- }
109
- }
110
- }
111
-
112
- // Attributes
113
- function addAttr (prop: string, value: any) {
114
- // @ts-ignore
115
- if (typeof value !== 'string' || vnode.dynamicProps?.includes(prop)) {
116
- let directive = ':'
117
- if (prop.startsWith('on')) {
118
- directive = '@'
119
- }
120
- const arg = directive === '@' ? `${prop[2].toLowerCase()}${prop.slice(3)}` : prop
121
-
122
- // v-model on component
123
- const vmodelListener = `onUpdate:${prop}`
124
- // @ts-ignore
125
- if (directive === ':' && vnode.dynamicProps?.includes(vmodelListener)) {
126
- // Listener
127
- skipProps.push(vmodelListener)
128
- const listener = vnode.props[vmodelListener]
129
- const listenerSource = listener.toString()
130
- let valueCode: string
131
- const result = /\(\$event\) => (.*?) = \$event/.exec(listenerSource)
132
- if (result) {
133
- valueCode = result[1]
134
- }
135
-
136
- // Modifiers
137
- const modifiersKey = `${prop === 'modelValue' ? 'model' : prop}Modifiers`
138
- const modifiers = vnode.props[modifiersKey] ?? {}
139
- skipProps.push(modifiersKey)
140
-
141
- // Directive
142
- genDirective('model', {
143
- arg: prop === 'modelValue' ? null : prop,
144
- modifiers,
145
- value,
146
- }, valueCode)
147
- return
148
- }
149
-
150
- let serialized: string[]
151
- if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
152
- // It was formatted from auto building object (slot props)
153
- serialized = cleanupExpression(value.substring(2, value.length - 2).trim()).split('\n')
154
- } else if (typeof value === 'function') {
155
- let code = cleanupExpression(value.toString().replace(/'/g, '\\\'').replace(/"/g, '\''))
156
- const testResult = /function ([^\s]+)\(/.exec(code)
157
- if (testResult) {
158
- // Function name only
159
- serialized = [testResult[1]]
160
- } else {
161
- if (code.startsWith('($event) => ')) {
162
- // Remove unnecessary `($event) => `
163
- code = code.substring('($event) => '.length)
164
- }
165
- serialized = code.split('\n')
166
- }
167
- } else {
168
- serialized = serializeAndCleanJs(value)
169
- }
170
- if (serialized.length > 1) {
171
- multilineAttrs = true
172
- const indented: string[] = [`${directive}${arg}="${serialized[0]}`]
173
- indented.push(...serialized.slice(1, serialized.length - 1))
174
- indented.push(`${serialized[serialized.length - 1]}"`)
175
- attrs.push(indented)
176
- } else {
177
- attrs.push([`${directive}${arg}="${serialized[0]}"`])
178
- }
179
- } else {
180
- attrs.push([`${prop}="${value}"`])
181
- }
182
- }
183
-
184
- for (const prop in vnode.props) {
185
- if (skipProps.includes(prop) || (propsOverrides && prop in propsOverrides)) {
186
- continue
187
- }
188
- const value = vnode.props[prop]
189
- addAttr(prop, value)
190
- }
191
-
192
- if (propsOverrides) {
193
- for (const prop in propsOverrides) {
194
- addAttr(prop, propsOverrides[prop])
195
- }
196
- }
197
-
198
- if (attrs.length > 1) {
199
- multilineAttrs = true
200
- }
201
-
202
- // Tags
203
- const tagName = getTagName(vnode)
204
-
205
- // Children
206
- let isChildText = false
207
- const childLines: string[] = []
208
- if (typeof vnode.children === 'string') {
209
- if (tagName === 'pre') {
210
- childLines.push(vnode.children)
211
- } else {
212
- childLines.push(...vnode.children.split('\n'))
213
- }
214
- isChildText = true
215
- } else if (Array.isArray(vnode.children)) {
216
- let isAllChildText
217
- for (const child of vnode.children) {
218
- // @ts-ignore
219
- const result = await printVNode(child)
220
- if (result.isText) {
221
- if (isAllChildText === undefined) {
222
- isAllChildText = true
223
- }
224
- const text = result.lines[0]
225
- if (!childLines.length || /^\s/.test(text)) {
226
- childLines.push(text.trim())
227
- } else {
228
- childLines[childLines.length - 1] += text
229
- }
230
- } else {
231
- if (isAllChildText === undefined) {
232
- isAllChildText = false
233
- }
234
- childLines.push(...result.lines)
235
- }
236
- }
237
- if (isAllChildText !== undefined) {
238
- isChildText = isAllChildText
239
- }
240
- }
241
-
242
- // Slots
243
- if (vnode.children && typeof vnode.children === 'object' && !Array.isArray(vnode.children)) {
244
- for (const key in vnode.children) {
245
- if (typeof vnode.children[key] === 'function') {
246
- const autoObject = createAutoBuildingObject(key => `{{ ${key} }}`, (target, p) => {
247
- // Vue 3
248
- if (p === '__v_isRef') {
249
- return () => false
250
- }
251
- })
252
- // @ts-ignore
253
- const children = vnode.children[key](autoObject.proxy)
254
- const slotLines: string[] = []
255
- for (const child of children) {
256
- slotLines.push(...(await printVNode(child)).lines)
257
- }
258
- const slotProps = Object.keys(autoObject.cache)
259
- if (slotProps.length) {
260
- childLines.push(`<template #${key}="{ ${slotProps.join(', ')} }">`)
261
- childLines.push(...indent(slotLines))
262
- childLines.push('</template>')
263
- } else if (key === 'default') {
264
- childLines.push(...slotLines)
265
- } else {
266
- childLines.push(`<template #${key}>`)
267
- childLines.push(...indent(slotLines))
268
- childLines.push(`</template>`)
269
- }
270
- }
271
- }
272
- }
273
-
274
- // Template
275
- const tag = [`<${tagName}`]
276
- if (multilineAttrs) {
277
- for (const attrLines of attrs) {
278
- tag.push(...indent(attrLines))
279
- }
280
- if (childLines.length > 0) {
281
- tag.push('>')
282
- }
283
- } else {
284
- if (attrs.length === 1) {
285
- tag[0] += ` ${attrs[0]}`
286
- }
287
- if (childLines.length > 0) {
288
- tag[0] += '>'
289
- }
290
- }
291
-
292
- const isVoid = voidElements.includes(tagName.toLowerCase())
293
-
294
- if (childLines.length > 0) {
295
- if (childLines.length === 1 && tag.length === 1 && !attrs.length && isChildText) {
296
- lines.push(`${tag[0]}${childLines[0]}</${tagName}>`)
297
- } else {
298
- lines.push(...tag)
299
- lines.push(...indent(childLines))
300
- lines.push(`</${tagName}>`)
301
- }
302
- } else if (tag.length > 1) {
303
- lines.push(...tag)
304
- lines.push(isVoid ? '>' : '/>')
305
- } else {
306
- lines.push(`${tag[0]}${isVoid ? '' : ' /'}>`)
307
- }
308
- } else if (vnode?.shapeFlag & 1 << 4) {
309
- // @ts-ignore
310
- for (const child of vnode.children) {
311
- lines.push(...(await printVNode(child)).lines)
312
- }
313
- }
314
-
315
- return {
316
- lines,
317
- }
318
- }
319
-
320
- export function getTagName (vnode: VNode) {
321
- if (typeof vnode.type === 'string') {
322
- return vnode.type
323
- // @ts-ignore
324
- } else if (vnode.type?.__asyncResolved) {
325
- // @ts-ignore
326
- const asyncComp = vnode.type?.__asyncResolved
327
- return asyncComp.name ?? getNameFromFile(asyncComp.__file)
328
- // @ts-ignore
329
- } else if (vnode.type?.name) {
330
- // @ts-ignore
331
- return vnode.type.name
332
- // @ts-ignore
333
- } else if (vnode.type?.__file) {
334
- // @ts-ignore
335
- return getNameFromFile(vnode.type.__file)
336
- }
337
- return 'Anonymous'
338
- }
339
-
340
- function getNameFromFile (file: string) {
341
- const parts = /([^/]+)\.vue$/.exec(file)
342
- if (parts) {
343
- return pascal(parts[1])
344
- }
345
- return 'Anonymous'
346
- }
347
-
348
- function serializeAndCleanJs (value: any) {
349
- const isAutoBuildingObject = !!value?.__autoBuildingObject
350
- const result = serializeJs(value)
351
- if (isAutoBuildingObject) {
352
- // @ts-ignore
353
- return [cleanupExpression(result.__autoBuildingObjectGetKey)]
354
- } else {
355
- return cleanupExpression(result).split('\n')
356
- }
357
- }
358
-
359
- function cleanupExpression (expr: string) {
360
- return expr.replace(/\$setup\./g, '')
361
- }
@@ -1,106 +0,0 @@
1
- <script lang="ts">
2
- import { computed, defineComponent, provide, useAttrs, VNode, h, PropType, getCurrentInstance } from 'vue'
3
- import type { Story } from '../../types.js'
4
- import Variant from './Variant.vue'
5
-
6
- export default defineComponent({
7
- // eslint-disable-next-line vue/multi-word-component-names
8
- name: 'Story',
9
- __histoireType: 'story',
10
-
11
- inheritAttrs: false,
12
-
13
- props: {
14
- initState: {
15
- type: Function as PropType<() => any | Promise<any>>,
16
- default: null,
17
- },
18
- },
19
-
20
- setup () {
21
- const vm = getCurrentInstance()
22
-
23
- const attrs = useAttrs() as {
24
- story: Story
25
- }
26
-
27
- const story = computed(() => attrs.story)
28
- provide('story', story)
29
-
30
- const storyComponent: any = vm.parent
31
- // Allows tracking reactivity with watchers to sync state
32
- const implicitState = {
33
- $data: storyComponent.data,
34
- }
35
- function addImplicitState (key, value) {
36
- if (typeof value === 'function' || (value && value.__file)) {
37
- return
38
- }
39
- implicitState[key] = value
40
- }
41
- // From `<script setup>`'s `defineExpose`
42
- for (const key in storyComponent.exposed) {
43
- addImplicitState(key, storyComponent.exposed[key])
44
- }
45
- // We needs __VUE_PROD_DEVTOOLS__ flag set to `true` to enable `devtoolsRawSetupState`
46
- for (const key in storyComponent.devtoolsRawSetupState) {
47
- addImplicitState(key, storyComponent.devtoolsRawSetupState[key])
48
- }
49
- // Shallow copy to prevent sharing object with different variants
50
- provide('implicitState', () => ({ ...implicitState }))
51
-
52
- function updateStory () {
53
- Object.assign(attrs.story, {
54
- slots: () => vm.proxy.$slots,
55
- })
56
- }
57
-
58
- return {
59
- story,
60
- updateStory,
61
- }
62
- },
63
-
64
- render () {
65
- this.updateStory()
66
-
67
- const [firstVariant] = this.story.variants
68
- if (firstVariant.id === '_default') {
69
- return h(Variant, {
70
- variant: firstVariant,
71
- initState: this.initState,
72
- ...this.$attrs,
73
- }, this.$slots)
74
- }
75
-
76
- let index = 0
77
- const applyAttrs = (vnodes: VNode[]) => {
78
- for (const vnode of vnodes) {
79
- // @ts-expect-error custom option
80
- if (vnode.type?.__histoireType === 'variant') {
81
- if (!vnode.props) {
82
- vnode.props = {}
83
- }
84
- vnode.props.variant = this.story.variants[index]
85
- if (!vnode.props.initState && !vnode.props['init-state']) {
86
- vnode.props.initState = this.initState
87
- }
88
- for (const attr in this.$attrs) {
89
- if (typeof vnode.props[attr] === 'undefined') {
90
- vnode.props[attr] = this.$attrs[attr]
91
- }
92
- }
93
- index++
94
- } else if (vnode.children?.length) {
95
- applyAttrs(vnode.children as VNode[])
96
- }
97
- }
98
- }
99
-
100
- // Apply variant as attribute to each child vnode (should be `<Variant>` components)
101
- const vnodes: VNode[] = this.$slots.default()
102
- applyAttrs(vnodes)
103
- return vnodes
104
- },
105
- })
106
- </script>
@@ -1,78 +0,0 @@
1
- <script lang="ts">
2
- import { defineComponent, getCurrentInstance, inject, PropType, useAttrs, watch } from 'vue'
3
- import { Variant } from '../../types'
4
- import { applyStateToVariant } from '../../util/state'
5
-
6
- // const logLocation = location.href.includes('__sandbox') ? '[Sandbox]' : '[Host]'
7
-
8
- export default defineComponent({
9
- // eslint-disable-next-line vue/multi-word-component-names
10
- name: 'Variant',
11
- __histoireType: 'variant',
12
-
13
- props: {
14
- initState: {
15
- type: Function as PropType<() => any | Promise<any>>,
16
- default: null,
17
- },
18
-
19
- source: {
20
- type: String,
21
- default: null,
22
- },
23
-
24
- responsiveDisabled: {
25
- type: Boolean,
26
- default: false,
27
- },
28
-
29
- setupApp: {
30
- type: Function,
31
- default: null,
32
- },
33
- },
34
-
35
- setup (props) {
36
- const attrs = useAttrs() as {
37
- variant: Variant
38
- }
39
-
40
- const vm = getCurrentInstance()
41
-
42
- const implicitState = inject<() => any>('implicitState')
43
-
44
- watch(() => implicitState, value => {
45
- applyStateToVariant(attrs.variant, value())
46
- }, {
47
- immediate: true,
48
- })
49
-
50
- function updateVariant () {
51
- Object.assign(attrs.variant, {
52
- initState: async () => {
53
- if (typeof props.initState === 'function') {
54
- const state = await props.initState()
55
- applyStateToVariant(attrs.variant, state)
56
- }
57
- },
58
- slots: () => vm.proxy.$slots,
59
- source: props.source,
60
- responsiveDisabled: props.responsiveDisabled,
61
- setupApp: props.setupApp,
62
- configReady: true,
63
- })
64
- }
65
- updateVariant()
66
-
67
- return {
68
- updateVariant,
69
- }
70
- },
71
-
72
- render () {
73
- // Trigger variant updates to (re-)render slots
74
- this.updateVariant()
75
- return null
76
- },
77
- })
78
- </script>
@@ -1,49 +0,0 @@
1
- // From: https://github.com/vuejs/test-utils/blob/main/src/components/RouterLinkStub.ts
2
-
3
- import { defineComponent, h, computed } from 'vue'
4
-
5
- // match return type of router.resolve: RouteLocation & { href: string }
6
- const defaultRoute = {
7
- path: '/',
8
- name: undefined as string | undefined,
9
- redirectedFrom: undefined as object | undefined,
10
- params: {},
11
- query: {},
12
- hash: '',
13
- fullPath: '/',
14
- matched: [] as object[],
15
- meta: {},
16
- href: '/',
17
- }
18
-
19
- // TODO: Borrow typings from vue-router-next
20
- export const RouterLinkStub = defineComponent({
21
- name: 'RouterLinkStub',
22
-
23
- compatConfig: { MODE: 3 },
24
-
25
- props: {
26
- to: {
27
- type: [String, Object],
28
- required: true,
29
- },
30
- custom: {
31
- type: Boolean,
32
- default: false,
33
- },
34
- },
35
-
36
- render () {
37
- const route = computed(() => defaultRoute)
38
- // mock reasonable return values to mimic vue-router's useLink
39
- const children = this.$slots?.default?.({
40
- route,
41
- href: computed(() => route.value.href),
42
- isActive: computed(() => false),
43
- isExactActive: computed(() => false),
44
- // eslint-disable-next-line @typescript-eslint/no-empty-function
45
- navigate: async () => {},
46
- })
47
- return this.custom ? children : h('a', undefined, children)
48
- },
49
- })