-
Notifications
You must be signed in to change notification settings - Fork 15
feat(client): add opt-in panel accent inheritance #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SaKaNa-Y
wants to merge
3
commits into
devframes:main
Choose a base branch
from
SaKaNa-Y:feat/panel-accent-inheritance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { watchDevframeTheme } from 'devframe/client' | ||
|
|
||
| // Match the reference dock's primary ramp. Only accent tokens change; semantic | ||
| // success/error/warning colors and each SPA's chart palettes stay independent. | ||
| const primaryStops = { | ||
| DEFAULT: 0, | ||
| 50: 95, | ||
| 100: 90, | ||
| 200: 75, | ||
| 600: 9, | ||
| 500: 18, | ||
| 400: 34, | ||
| 300: 54, | ||
| 700: -8, | ||
| 800: -25, | ||
| 900: -45, | ||
| 950: -70, | ||
| } as const | ||
|
|
||
| /** Opt a built-in devframe SPA into the hub UI provider's primary accent. */ | ||
| export function syncPanelTheme(): () => void { | ||
| const style = document.documentElement.style | ||
| const previous = Object.keys(primaryStops).map((stop) => { | ||
| const name = `--colors-primary-${stop}` | ||
| return { name, value: style.getPropertyValue(name), priority: style.getPropertyPriority(name) } | ||
| }) | ||
| function restore() { | ||
| for (const { name, value, priority } of previous) { | ||
| if (value) | ||
| style.setProperty(name, value, priority) | ||
| else | ||
| style.removeProperty(name) | ||
| } | ||
| } | ||
| const stop = watchDevframeTheme(({ primaryColor }) => { | ||
| if (!primaryColor || !CSS.supports('color', primaryColor)) { | ||
| restore() | ||
| return | ||
| } | ||
| for (const [stop, white] of Object.entries(primaryStops)) { | ||
| style.setProperty(`--colors-primary-${stop}`, white | ||
| ? `color-mix(in oklab, ${primaryColor}, ${white > 0 ? 'white' : 'black'} ${Math.abs(white)}%)` | ||
| : primaryColor) | ||
| } | ||
| }) | ||
| return () => { | ||
| stop() | ||
| restore() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { DEVFRAME_EVENTS } from '../events' | ||
| import { provideDevframeTheme, watchDevframeTheme } from './theme' | ||
|
|
||
| const channel = DEVFRAME_EVENTS.postMessage.theme | ||
|
|
||
| function fixture() { | ||
| const parent = new EventTarget() as Window | ||
| const panel = new EventTarget() as Window | ||
| const origin = 'https://panel.example' | ||
| const parentOrigin = 'https://hub.example' | ||
| const queue: (() => void)[] = [] | ||
| const send = (target: EventTarget, data: unknown, source: EventTarget, origin: string) => { | ||
| target.dispatchEvent(Object.assign(new Event('message'), { data, source, origin })) | ||
| } | ||
| Object.assign(parent, { | ||
| postMessage: (data: unknown) => queue.push(() => send(parent, data, panel, origin)), | ||
| }) | ||
| Object.assign(panel, { | ||
| parent, | ||
| postMessage: (data: unknown, targetOrigin: string) => queue.push(() => { | ||
| if (targetOrigin === '*' || targetOrigin === origin) | ||
| send(panel, data, parent, parentOrigin) | ||
| }), | ||
| }) | ||
| // Model both browsing contexts and queued postMessage delivery without a DOM. | ||
| const iframe = Object.assign(new EventTarget(), { | ||
| contentWindow: panel, | ||
| ownerDocument: { defaultView: parent } as Document, | ||
| }) as HTMLIFrameElement | ||
| vi.stubGlobal('window', panel) | ||
| return { | ||
| iframe, | ||
| panel, | ||
| parent, | ||
| send, | ||
| flush() { | ||
| while (queue.length) | ||
| queue.shift()!() | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| afterEach(() => vi.unstubAllGlobals()) | ||
|
|
||
| describe('panel theme protocol', () => { | ||
| it.each(['panel', 'provider'])('connects cross-origin when %s boots first', (first) => { | ||
| const f = fixture() | ||
| const changed = vi.fn() | ||
| const startPanel = () => watchDevframeTheme(changed) | ||
| const startProvider = () => provideDevframeTheme(f.iframe, { primaryColor: '#6b84fd' }) | ||
| if (first === 'panel') { | ||
| startPanel() | ||
| f.flush() | ||
| startProvider() | ||
| } | ||
| else { | ||
| startProvider() | ||
| f.flush() | ||
| startPanel() | ||
| } | ||
| f.flush() | ||
| expect(changed).toHaveBeenLastCalledWith({ primaryColor: '#6b84fd' }) | ||
| }) | ||
|
|
||
| it('updates and clears the accent without changing other theme state', () => { | ||
| const f = fixture() | ||
| const provider = provideDevframeTheme(f.iframe, { primaryColor: '#6b84fd' }) | ||
| const changed = vi.fn() | ||
| watchDevframeTheme(changed) | ||
| f.flush() | ||
| provider.update({ primaryColor: 'rebeccapurple' }) | ||
| f.flush() | ||
| expect(changed).toHaveBeenLastCalledWith({ primaryColor: 'rebeccapurple' }) | ||
| provider.update({}) | ||
| f.flush() | ||
| expect(changed).toHaveBeenLastCalledWith({ primaryColor: undefined }) | ||
| }) | ||
|
|
||
| it('offers no theme data until the iframe opts in', () => { | ||
| const f = fixture() | ||
| const received: unknown[] = [] | ||
| f.panel.addEventListener('message', event => received.push((event as MessageEvent).data)) | ||
| const provider = provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) | ||
| provider.update({ primaryColor: 'red' }) | ||
| f.flush() | ||
| expect(received).toEqual([{ channel, type: 'available' }]) | ||
| }) | ||
|
|
||
| it('requires a new document to opt in after navigation', () => { | ||
| const f = fixture() | ||
| const provider = provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) | ||
| const changed = vi.fn() | ||
| const stop = watchDevframeTheme(changed) | ||
| f.flush() | ||
| stop() | ||
| f.iframe.dispatchEvent(new Event('load')) | ||
| provider.update({ primaryColor: 'red' }) | ||
| f.flush() | ||
| changed.mockClear() | ||
| watchDevframeTheme(changed) | ||
| f.flush() | ||
| expect(changed).toHaveBeenLastCalledWith({ primaryColor: 'red' }) | ||
| }) | ||
|
|
||
| it('rejects theme updates from sibling windows and malformed messages', () => { | ||
| const f = fixture() | ||
| const changed = vi.fn() | ||
| watchDevframeTheme(changed) | ||
| f.send(f.panel, { channel, type: 'update', theme: { primaryColor: 'red' } }, new EventTarget(), 'https://hub.example') | ||
| for (const data of [null, 'blue', { channel, type: 'update', theme: null }, { channel, type: 'update', theme: { primaryColor: 42 } }]) | ||
| f.send(f.panel, data, f.parent, 'https://hub.example') | ||
| expect(changed).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('rejects requests from unrelated windows and opaque origins', () => { | ||
| const f = fixture() | ||
| const received: unknown[] = [] | ||
| f.panel.addEventListener('message', event => received.push((event as MessageEvent).data)) | ||
| provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) | ||
| f.flush() | ||
| received.length = 0 | ||
| f.send(f.parent, { channel, type: 'request' }, new EventTarget(), 'https://panel.example') | ||
| f.send(f.parent, { channel, type: 'request' }, f.panel, 'null') | ||
| f.flush() | ||
| expect(received).toEqual([]) | ||
| }) | ||
|
|
||
| it('cleans up both ends and reconnects a preserved iframe to a new provider', () => { | ||
| const f = fixture() | ||
| const changed = vi.fn() | ||
| const stop = watchDevframeTheme(changed) | ||
| const provider = provideDevframeTheme(f.iframe, { primaryColor: 'blue' }) | ||
| f.flush() | ||
| provider.dispose() | ||
| changed.mockClear() | ||
| provider.update({ primaryColor: 'red' }) | ||
| f.iframe.dispatchEvent(new Event('load')) | ||
| f.flush() | ||
| expect(changed).not.toHaveBeenCalled() | ||
| const next = provideDevframeTheme(f.iframe, {}) | ||
| f.flush() | ||
| expect(changed).toHaveBeenLastCalledWith({ primaryColor: undefined }) | ||
| stop() | ||
| changed.mockClear() | ||
| next.update({ primaryColor: 'red' }) | ||
| f.flush() | ||
| expect(changed).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('leaves standalone SPAs and SSR untouched', () => { | ||
| const changed = vi.fn() | ||
| vi.stubGlobal('window', undefined) | ||
| watchDevframeTheme(changed)() | ||
| const standalone = { parent: undefined as unknown } | ||
| standalone.parent = standalone | ||
| vi.stubGlobal('window', standalone) | ||
| watchDevframeTheme(changed)() | ||
| expect(changed).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, I would like to avoid introducing these APIs, as devframe itself should be agnostic, the theme is more like a hub-ui concept. And if possible, I'd also like to keep this at CSS level as much as possible instead of relying on JS apis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The branding is shipped with the connection meta and forwarded to the sub-frames; we might be able to leverage that without creating a new communication channel for that?
