@jogak/ui 0.1.0-alpha.1 → 0.1.0-alpha.10.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.
@@ -0,0 +1,101 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+ import type { ReactElement } from 'react'
3
+ import type { RegistryEntry } from '@jogak/core'
4
+
5
+ export interface IframeMountProps {
6
+ readonly entry: RegistryEntry
7
+ readonly args: Readonly<Record<string, unknown>>
8
+ /**
9
+ * 알파.9: 어댑터 dev URL (예: `http://localhost:5174`).
10
+ * 빈 문자열 시 fallback (jogak SPA Vite scope의 `/preview-frame.html`).
11
+ */
12
+ readonly userPreviewUrl: string
13
+ /**
14
+ * 알파.9: iframe entry path (예: `/__jogak_preview__/index.html`).
15
+ * 어댑터의 `previewEntryMeta.devEntryPath`.
16
+ */
17
+ readonly previewEntryPath: string
18
+ readonly className?: string
19
+ readonly 'data-testid'?: string
20
+ }
21
+
22
+ /**
23
+ * 알파.8: previewIsolation='iframe' 모드의 mount 컴포넌트.
24
+ *
25
+ * 통신:
26
+ * - 사용자 vite spawn URL이 주어지면(`userViteUrl !== ''`) iframe src를
27
+ * `${userViteUrl}/__jogak_preview__/index.html` (cross-origin)로 설정.
28
+ * - 동일 origin fallback 시 `/preview-frame.html` (jogak SPA Vite scope).
29
+ *
30
+ * 양쪽 모두 postMessage로 통신:
31
+ * - 부모 → iframe: `{ type: 'jogak:setProps', entryId, args }` | `{ type: 'jogak:unmount' }`
32
+ * - iframe → 부모: `{ type: 'jogak:ready' }` | `{ type: 'jogak:rendered', entryId }`
33
+ *
34
+ * `entry`는 객체가 아닌 **id만 전달** — iframe 안에서 `defaultRegistry.requestEntry(id)`로
35
+ * dynamic import. 사용자 vite scope의 entry 가상 모듈이 사용자 컴포넌트를 fetch하므로
36
+ * 사용자 plugins(@tailwindcss/vite, custom alias 등)이 정상 작동.
37
+ */
38
+ export function IframeMount({
39
+ entry,
40
+ args,
41
+ userPreviewUrl,
42
+ previewEntryPath,
43
+ className,
44
+ 'data-testid': dataTestId,
45
+ }: IframeMountProps): ReactElement {
46
+ const iframeRef = useRef<HTMLIFrameElement | null>(null)
47
+ const [ready, setReady] = useState(false)
48
+
49
+ const src =
50
+ userPreviewUrl !== ''
51
+ ? `${userPreviewUrl}${previewEntryPath}`
52
+ : '/preview-frame.html'
53
+
54
+ // postMessage 리스너 — iframe contentWindow 일치성 검증 후 처리.
55
+ useEffect(() => {
56
+ const handler = (event: MessageEvent): void => {
57
+ const iframe = iframeRef.current
58
+ if (iframe === null) return
59
+ if (event.source !== iframe.contentWindow) return
60
+ const data = event.data
61
+ if (data == null || typeof data !== 'object') return
62
+ if (data.type === 'jogak:ready') setReady(true)
63
+ }
64
+ window.addEventListener('message', handler)
65
+ return () => {
66
+ window.removeEventListener('message', handler)
67
+ }
68
+ }, [])
69
+
70
+ // iframe ready 또는 entry/args 변경 시 setProps.
71
+ useEffect(() => {
72
+ if (!ready) return
73
+ const iframe = iframeRef.current
74
+ if (iframe === null) return
75
+ iframe.contentWindow?.postMessage(
76
+ { type: 'jogak:setProps', entryId: entry.id, args },
77
+ '*',
78
+ )
79
+ }, [ready, entry, args])
80
+
81
+ // unmount 시 unmount 메시지 (race 회피 microtask defer).
82
+ useEffect(() => {
83
+ const iframe = iframeRef.current
84
+ return () => {
85
+ if (iframe === null) return
86
+ queueMicrotask(() => {
87
+ iframe.contentWindow?.postMessage({ type: 'jogak:unmount' }, '*')
88
+ })
89
+ }
90
+ }, [])
91
+
92
+ return (
93
+ <iframe
94
+ ref={iframeRef}
95
+ src={src}
96
+ title="Preview"
97
+ className={className}
98
+ data-testid={dataTestId}
99
+ />
100
+ )
101
+ }
@@ -0,0 +1,57 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+ import { createPortal } from 'react-dom'
3
+ import type { ReactElement, ReactNode, CSSProperties } from 'react'
4
+
5
+ export interface ShadowMountProps {
6
+ readonly children: ReactNode
7
+ readonly className?: string
8
+ readonly style?: CSSProperties
9
+ readonly 'data-testid'?: string
10
+ }
11
+
12
+ /**
13
+ * 알파.7.1: previewIsolation='shadow' 모드의 mount 컴포넌트.
14
+ *
15
+ * 책임: 양방향 격리만 제공 (Preview ↔ outer document 양방향 cascade 차단).
16
+ * - 사용자 globalCss는 main.tsx 가드로 outer document에 inject되지 않음.
17
+ * - shadow root 안에는 jogak chrome css도 사용자 css도 없음 (둘 다 외부에서 격리).
18
+ * - 사용자 컴포넌트의 utility class 컴파일은 결함 B (알파.8 사이클).
19
+ *
20
+ * 알파.7 결함 정정:
21
+ * - `syncStyleSheets`/`MutationObserver`/`adoptedStyleSheets` 흡수 로직 제거.
22
+ * 알파.7은 outer document에 사용자 css가 있는 한 chrome을 침범했고, shadow
23
+ * 안의 흡수 로직은 의미가 없었음. 알파.7.1: outer에 사용자 css 자체가 없음.
24
+ *
25
+ * Radix portal 한계 (사용자 인지 필요, README 명시):
26
+ * - default Portal target = document.body (shadow 외부). 사용자가 명시적으로
27
+ * `<Portal container={shadowRootEl}>`을 전달해야 portal 내용도 격리됨.
28
+ */
29
+ export function ShadowMount({
30
+ children,
31
+ className,
32
+ style,
33
+ 'data-testid': dataTestId,
34
+ }: ShadowMountProps): ReactElement {
35
+ const hostRef = useRef<HTMLDivElement | null>(null)
36
+ const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null)
37
+
38
+ useEffect(() => {
39
+ const host = hostRef.current
40
+ if (host === null) return
41
+ const sr = host.shadowRoot ?? host.attachShadow({ mode: 'open' })
42
+ setShadowRoot(sr)
43
+ // shadow root는 host element와 함께 GC — 명시 detach 불필요.
44
+ }, [])
45
+
46
+ return (
47
+ <div
48
+ ref={hostRef}
49
+ className={className}
50
+ data-testid={dataTestId}
51
+ // eslint-disable-next-line no-restricted-syntax -- jogak: ShadowMount caller-supplied style passthrough (host wrapper, content goes through ShadowRoot portal)
52
+ style={style}
53
+ >
54
+ {shadowRoot !== null ? createPortal(children, shadowRoot) : null}
55
+ </div>
56
+ )
57
+ }