@wippy-fe/webcomponent-vue 0.0.25 → 0.0.27

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wippy-fe/webcomponent-vue",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "description": "Vue 3 integration layer for Wippy web components — extends @wippy-fe/webcomponent-core with reactive props, Pinia, and provider injection.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -10,13 +10,14 @@
10
10
  "package.json"
11
11
  ],
12
12
  "dependencies": {
13
- "@wippy-fe/webcomponent-core": "0.0.25"
13
+ "@wippy-fe/webcomponent-core": "0.0.27"
14
14
  },
15
15
  "peerDependencies": {
16
16
  "@iconify/vue": "^5.0.0",
17
17
  "pinia": "^2.1.0",
18
18
  "vue": "^3.5.0",
19
- "@wippy-fe/proxy": "0.0.25"
19
+ "@wippy-fe/proxy": "0.0.27",
20
+ "@wippy-fe/shared": "0.0.27"
20
21
  },
21
22
  "license": "UNLICENSED"
22
23
  }
package/src/index.ts CHANGED
@@ -1,21 +1,35 @@
1
- // Vue layer
2
- export { WippyVueElement } from './vue-element.ts'
3
- export type { WippyVueElementConfig } from './vue-element.ts'
4
-
5
1
  // Providers: base symbols, composables, and createProviders utility
6
2
  export {
7
- EVENT_PROVIDER, PROPS_PROVIDER, PROPS_ERROR_PROVIDER, CONTENT_PROVIDER,
8
- useProps, useEvents, usePropsErrors, useContent,
3
+ CONTENT_PROVIDER,
9
4
  createProviders,
5
+ EVENT_PROVIDER,
6
+ HOST_PROVIDER,
7
+ LAYOUT_BUS_PROVIDER,
8
+ PANEL_ID_PROVIDER,
9
+ PROPS_ERROR_PROVIDER,
10
+ PROPS_PROVIDER,
11
+ useContent,
12
+ useEvents,
13
+ useHost,
14
+ useLayoutBus,
15
+ usePanelId,
16
+ useProps,
17
+ usePropsErrors,
10
18
  } from './providers.ts'
11
19
  export type { TypedProviders } from './providers.ts'
12
20
 
21
+ // Vue layer
22
+ export { WippyVueElement } from './vue-element.ts'
23
+ export type { WippyVueElementConfig } from './vue-element.ts'
24
+
13
25
  // Re-export core for convenience
14
- export { WippyElement, define } from '@wippy-fe/webcomponent-core'
26
+ export { define, WippyElement } from '@wippy-fe/webcomponent-core'
15
27
  export type {
16
- WippyElementConfig,
17
- WippyPropsSchema,
18
- WippyPropDefinition,
28
+ BroadcastEnvelope,
19
29
  HostCssKey,
30
+ LayoutBusBound,
20
31
  ParseResult,
32
+ WippyElementConfig,
33
+ WippyPropDefinition,
34
+ WippyPropsSchema,
21
35
  } from '@wippy-fe/webcomponent-core'
package/src/providers.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { LayoutBusBound } from '@wippy-fe/webcomponent-core'
1
2
  import type { InjectionKey, Ref } from 'vue'
2
3
  import { inject } from 'vue'
3
4
 
@@ -17,6 +18,29 @@ export const PROPS_ERROR_PROVIDER = Symbol('wippy:props_error') as InjectionKey<
17
18
  /** Injection key for reactive content from child <template data-type="..."> elements. */
18
19
  export const CONTENT_PROVIDER = Symbol('wippy:content') as InjectionKey<Ref<string | null>>
19
20
 
21
+ /**
22
+ * Injection key for the panelId this web component lives in (when
23
+ * mounted inside a managed-layout `kind: 'component'` panel). `null`
24
+ * outside a managed-layout host.
25
+ */
26
+ export const PANEL_ID_PROVIDER = Symbol('wippy:panel-id') as InjectionKey<string | null>
27
+
28
+ /**
29
+ * Injection key for the panel-bound layout bus. Use to publish or
30
+ * subscribe to in-tab broadcasts. `null` outside a managed-layout
31
+ * host.
32
+ */
33
+ export const LAYOUT_BUS_PROVIDER = Symbol('wippy:layout-bus') as InjectionKey<LayoutBusBound | null>
34
+
35
+ /**
36
+ * Injection key for the panel-scoped `host` wrapper attached by the
37
+ * managed-layout shell. Same shape as the iframe-side `host`; layout
38
+ * broadcast / send / on are routed through the panel-bound bus so
39
+ * sourcePanelId attribution is correct. `null` outside a managed-layout
40
+ * host (callers fall back to importing `host` from `@wippy-fe/proxy`).
41
+ */
42
+ export const HOST_PROVIDER = Symbol('wippy:host') as InjectionKey<unknown>
43
+
20
44
  // ── Composables (typed) ──────────────────────────────────────
21
45
 
22
46
  /**
@@ -32,7 +56,8 @@ export const CONTENT_PROVIDER = Symbol('wippy:content') as InjectionKey<Ref<stri
32
56
  */
33
57
  export function useProps<Props>(): Ref<Props> {
34
58
  const props = inject(PROPS_PROVIDER)
35
- if (!props) throw new Error('useProps() must be called inside a WippyVueElement')
59
+ if (!props)
60
+ throw new Error('useProps() must be called inside a WippyVueElement')
36
61
  return props as unknown as Ref<Props>
37
62
  }
38
63
 
@@ -48,7 +73,8 @@ export function useProps<Props>(): Ref<Props> {
48
73
  */
49
74
  export function useEvents<Events>(): <K extends keyof Events>(event: K, detail: Events[K]) => void {
50
75
  const emit = inject(EVENT_PROVIDER)
51
- if (!emit) throw new Error('useEvents() must be called inside a WippyVueElement')
76
+ if (!emit)
77
+ throw new Error('useEvents() must be called inside a WippyVueElement')
52
78
  return emit as unknown as <K extends keyof Events>(event: K, detail: Events[K]) => void
53
79
  }
54
80
 
@@ -64,7 +90,8 @@ export function useEvents<Events>(): <K extends keyof Events>(event: K, detail:
64
90
  */
65
91
  export function usePropsErrors(): Ref<string[]> {
66
92
  const errors = inject(PROPS_ERROR_PROVIDER)
67
- if (!errors) throw new Error('usePropsErrors() must be called inside a WippyVueElement')
93
+ if (!errors)
94
+ throw new Error('usePropsErrors() must be called inside a WippyVueElement')
68
95
  return errors
69
96
  }
70
97
 
@@ -81,10 +108,72 @@ export function usePropsErrors(): Ref<string[]> {
81
108
  */
82
109
  export function useContent(): Ref<string | null> {
83
110
  const content = inject(CONTENT_PROVIDER)
84
- if (!content) throw new Error('useContent() must be called inside a WippyVueElement with contentTemplate configured')
111
+ if (!content)
112
+ throw new Error('useContent() must be called inside a WippyVueElement with contentTemplate configured')
85
113
  return content
86
114
  }
87
115
 
116
+ /**
117
+ * Inject the current panelId (from `data-wippy-panel-id` set by the
118
+ * managed-layout `ComponentResolver`). Returns `null` outside a
119
+ * managed-layout host or when not inside a `kind: 'component'` panel.
120
+ *
121
+ * **Status: Draft 1 (preview).** Part of the managed-layout API; see
122
+ * `managed-layout.md` in the gen-2-chat repo. Field names may change
123
+ * between minor releases until a production consumer drives the
124
+ * corner cases out.
125
+ *
126
+ * ```ts
127
+ * const panelId = usePanelId()
128
+ * if (panelId) console.log('I am the', panelId, 'panel')
129
+ * ```
130
+ */
131
+ export function usePanelId(): string | null {
132
+ return inject(PANEL_ID_PROVIDER, null)
133
+ }
134
+
135
+ /**
136
+ * Inject the panel-bound layout bus. `null` outside a managed-layout
137
+ * host. Use to publish on the in-tab bus or subscribe to broadcasts
138
+ * targeted at this panel.
139
+ *
140
+ * **Status: Draft 1 (preview).** Part of the managed-layout API; see
141
+ * `managed-layout.md` in the gen-2-chat repo.
142
+ *
143
+ * ```ts
144
+ * const bus = useLayoutBus()
145
+ * bus?.subscribe(({ channel, payload, sourcePanelId }) => { ... })
146
+ * bus?.broadcast('open-chat', { token: '...' })
147
+ * ```
148
+ */
149
+ export function useLayoutBus(): LayoutBusBound | null {
150
+ return inject(LAYOUT_BUS_PROVIDER, null)
151
+ }
152
+
153
+ /**
154
+ * Inject the panel-scoped `host` wrapper. `null` outside a
155
+ * managed-layout host. Subclass code that needs a host in non-layout
156
+ * contexts can fall back to `import { host } from '@wippy-fe/proxy'`.
157
+ *
158
+ * Pass the host shape as the type parameter to access fields like
159
+ * `layout.broadcast`. The default `H = unknown` keeps callers honest
160
+ * about narrowing before use.
161
+ *
162
+ * **Status: Draft 1 (preview).** Part of the managed-layout API; see
163
+ * `managed-layout.md` in the gen-2-chat repo.
164
+ *
165
+ * ```ts
166
+ * import type { ProxyApiInstance } from '@wippy-fe/proxy'
167
+ *
168
+ * const host = useHost<ProxyApiInstance['host']>()
169
+ * host?.layout.broadcast('open-chat', { token })
170
+ * host?.layout.updatePanel('right', { route: '/foo' })
171
+ * ```
172
+ */
173
+ export function useHost<H = unknown>(): H | null {
174
+ return inject(HOST_PROVIDER, null) as H | null
175
+ }
176
+
88
177
  // ── createProviders (typed injection keys) ───────────────────
89
178
 
90
179
  /**
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ *
4
+ * Covers `useHost()` Vue composable — injects HOST_PROVIDER, returns
5
+ * `null` outside a managed-layout host so callers can fall back to
6
+ * the global `host` import explicitly.
7
+ */
8
+ import { describe, expect, it } from 'vitest'
9
+ import { createApp, defineComponent, h } from 'vue'
10
+ import { HOST_PROVIDER, useHost } from './providers.ts'
11
+
12
+ function mount(rootComponent: ReturnType<typeof defineComponent>) {
13
+ const container = document.createElement('div')
14
+ document.body.appendChild(container)
15
+ const app = createApp(rootComponent)
16
+ app.mount(container)
17
+ return { app, container, unmount: () => {
18
+ app.unmount()
19
+ container.remove()
20
+ } }
21
+ }
22
+
23
+ describe('useHost', () => {
24
+ it('returns null outside a managed-layout host (no provider)', () => {
25
+ let observed: unknown = 'sentinel'
26
+ const Probe = defineComponent({
27
+ setup() {
28
+ observed = useHost()
29
+ return () => h('div')
30
+ },
31
+ })
32
+ const { unmount } = mount(Probe)
33
+ expect(observed).toBeNull()
34
+ unmount()
35
+ })
36
+
37
+ it('returns the panel-scoped wrapper when HOST_PROVIDER is provided', () => {
38
+ let observed: unknown = 'sentinel'
39
+ const fakeWrapper = { layout: { broadcast: () => {}, send: () => {}, on: () => () => {} } }
40
+ const Probe = defineComponent({
41
+ setup() {
42
+ observed = useHost()
43
+ return () => h('div')
44
+ },
45
+ })
46
+ const Parent = defineComponent({
47
+ setup() {
48
+ return () => h(Probe)
49
+ },
50
+ })
51
+ const container = document.createElement('div')
52
+ document.body.appendChild(container)
53
+ const app = createApp(Parent)
54
+ // Identity check: prove `useHost()` reads from the EXACT
55
+ // HOST_PROVIDER key. A composable that injected from a wrong key
56
+ // would still return undefined → null, falsely passing the
57
+ // "returns null" test, so this is the only place the key
58
+ // contract is actually verified.
59
+ app.provide(HOST_PROVIDER, fakeWrapper)
60
+ app.mount(container)
61
+ expect(observed).toBe(fakeWrapper)
62
+ app.unmount()
63
+ container.remove()
64
+ })
65
+
66
+ it('does NOT read from a different injection key', () => {
67
+ let observed: unknown = 'sentinel'
68
+ const fakeWrapper = { layout: {} }
69
+ const wrongKey = Symbol('wrong-key')
70
+ const Probe = defineComponent({
71
+ setup() {
72
+ observed = useHost()
73
+ return () => h('div')
74
+ },
75
+ })
76
+ const container = document.createElement('div')
77
+ document.body.appendChild(container)
78
+ const app = createApp(Probe)
79
+ app.provide(wrongKey, fakeWrapper) // NOT HOST_PROVIDER
80
+ app.mount(container)
81
+ expect(observed).toBeNull()
82
+ app.unmount()
83
+ container.remove()
84
+ })
85
+ })
@@ -1,9 +1,9 @@
1
- import type { App, Component, Ref } from 'vue'
2
1
  import type { PiniaPlugin } from 'pinia'
3
- import { WippyElement } from '@wippy-fe/webcomponent-core'
2
+ import type { App, Component, Ref } from 'vue'
3
+ import { getWippyHost, getWippyHostBus, getWippyPanelId, WippyElement } from '@wippy-fe/webcomponent-core'
4
4
  import { createPinia, getActivePinia, setActivePinia } from 'pinia'
5
5
  import { createApp, ref } from 'vue'
6
- import { EVENT_PROVIDER, PROPS_PROVIDER, PROPS_ERROR_PROVIDER, CONTENT_PROVIDER } from './providers.ts'
6
+ import { CONTENT_PROVIDER, EVENT_PROVIDER, HOST_PROVIDER, LAYOUT_BUS_PROVIDER, PANEL_ID_PROVIDER, PROPS_ERROR_PROVIDER, PROPS_PROVIDER } from './providers.ts'
7
7
 
8
8
  /**
9
9
  * Vue-specific configuration returned by `static get vueConfig()`.
@@ -16,7 +16,6 @@ export interface WippyVueElementConfig {
16
16
  /** Pinia plugins to install (e.g., createWippyPersist()). */
17
17
  piniaPlugins?: PiniaPlugin[]
18
18
  /** Extra providers to inject into the Vue app. */
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
19
  providers?: (app: App, element: WippyVueElement<any, any>) => void
21
20
  }
22
21
 
@@ -54,7 +53,10 @@ export abstract class WippyVueElement<
54
53
  private _errorsRef: Ref<string[]> = ref([])
55
54
  private _contentRef: Ref<string | null> = ref(null)
56
55
 
57
- /** @internal Phantom field to retain the Events type parameter. */
56
+ /**
57
+ * Phantom field to retain the Events type parameter.
58
+ * @internal
59
+ */
58
60
  declare readonly _events: Events
59
61
 
60
62
  /**
@@ -70,7 +72,14 @@ export abstract class WippyVueElement<
70
72
  initialProps: Props,
71
73
  initialErrors: string[],
72
74
  initialContent?: string | null,
75
+ _isRemount?: boolean,
73
76
  ): void {
77
+ // On reconnect (`_isRemount === true`) the previous Vue app was
78
+ // already torn down by `onUnmount`, which calls `_vueApp.unmount()`
79
+ // and detaches Vue's rendered DOM from the reused container. So we
80
+ // can construct a fresh Vue app here — no special-case branch needed.
81
+ // If the container ever holds residual children, Vue's mount will
82
+ // replace them.
74
83
  const vueConfig = (this.constructor as typeof WippyVueElement).vueConfig
75
84
 
76
85
  // 1. Set reactive state
@@ -110,6 +119,15 @@ export abstract class WippyVueElement<
110
119
  this._vueApp.provide(EVENT_PROVIDER, this.emitEvent.bind(this))
111
120
  this._vueApp.provide(CONTENT_PROVIDER, this._contentRef)
112
121
 
122
+ // 4b. Managed-layout integration. The `ComponentResolver` (host
123
+ // side) sets `dataset.wippyPanelId` and `__wippyHostBus` on this
124
+ // element BEFORE insertion, so they're available now in
125
+ // `connectedCallback`. Outside a managed-layout host both are
126
+ // null — composables tolerate the null path.
127
+ this._vueApp.provide(PANEL_ID_PROVIDER, getWippyPanelId(this))
128
+ this._vueApp.provide(LAYOUT_BUS_PROVIDER, getWippyHostBus(this))
129
+ this._vueApp.provide(HOST_PROVIDER, getWippyHost(this))
130
+
113
131
  // 5. Custom providers
114
132
  if (vueConfig.providers) {
115
133
  vueConfig.providers(this._vueApp, this)