Repository navigation
feat(gallery): Add interactive playground to Component Gallery - #265
Conversation
…view main before injecting content (main composer AND component gallery use monaco editor)
There was a problem hiding this comment.
Code Review
This pull request introduces an interactive playground to the Components Gallery, enabling users to edit component examples directly using either an 'Edit JSON' Monaco editor or an interactive 'Usage' property form. It updates component usage samples across Lit, Angular, and React catalogs to align with schema changes, adds a GalleryLauncher service to open drafts in the Composer, and implements comprehensive unit and E2E tests. The reviewer feedback identifies a potential runtime TypeError in matchesSimpleSchema when a sub-schema is a boolean, and suggests adding a defensive type guard to handle boolean schemas robustly.
⚡ A2UI Composer PR PreviewYour automated preview is successfully live (commit
|
…e-assist suggestion Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: Kelly Tu <[email protected]>
|
I kinda think the "Interactive Playground" collapsable group takes up extra space that would be better used to show the actual content. WDYT? |
|
To save more space, what do you think about putting the "Preview" and "Edit JSON" into tab components (like the default layout of the workspace page)? |
| }); | ||
|
|
||
| /** Memoized schema resolver and shared property names, computed once per active catalog. */ | ||
| private readonly activeCatalogResolver = computed(() => { |
There was a problem hiding this comment.
Our code is a bit hit-and-miss on this, but I find adding the type on fields where it's not immediately obvious is really helpful for reading the code.
There was a problem hiding this comment.
Done! Added the type for some of the functions
| }); | ||
|
|
||
| /** | ||
| * linkedSignal owns the local draft without propagating component state through effects. |
There was a problem hiding this comment.
"linkedSignal owns ..." this was confusing to me, since I know linkedSignal can't be a class field name.
Especially since there are multiple fields of type linkedSignal, I'd suggest specifying the name of the field "that owns..." instead of the type.
There was a problem hiding this comment.
Fixed the comments those functions
| return new Set([...first].filter(prop => rest.every(set => set.has(prop)))); | ||
| } | ||
|
|
||
| /** Property Editor form reads and writes to. Prioritize 'target' |
There was a problem hiding this comment.
Nit: Start the comment with the /** on its own line.
There was a problem hiding this comment.
Done! Fixed for other files too
There was a problem hiding this comment.
No need to undo anything you've done, but our TS style says that if the comment fits on a single line, then using /** the comment */ is fine. Using something like:
/** here
* is the comment
*/
is what's not right.
| ); | ||
| } | ||
|
|
||
| protected describeUneditable(value: unknown, control: GalleryPropertyControl): string | null { |
There was a problem hiding this comment.
Since this method just calls the describeUneditableValue without doing anything else, can we remove this and update callers to just use the describeUneditableValue method?
There was a problem hiding this comment.
Since the function is used in gallery.ng.html for checking whether nested objects/array properties are uneditable, it needs to be in gallery.ts so that gallery.ng.html can use it. So instead of having the wrapper, we can expose the function as a protected readonly field
| } | ||
|
|
||
| protected asArray(value: unknown): unknown[] { | ||
| return Array.isArray(value) ? value : []; |
There was a problem hiding this comment.
Is this right? I would have expected:
return Array.isArray(value) ? value : [value];
There was a problem hiding this comment.
Yes, the function is used to help render the items of an array property. So if a component that has an optional array property that isn't set in the sample yet, it should have an empty array (e.g. without it, Button's checks property would have an entry when it's not supposed to cause its not set in the samples). I changed the name asArray to ensureArray so its not confusing
There was a problem hiding this comment.
I'd suggest leaving a comment in the code to explain this for future readers.
| } | ||
|
|
||
| protected formatJsonValue(value: unknown): string { | ||
| return value === undefined ? '' : formatJson(value); |
There was a problem hiding this comment.
Should we just update the formatJson function to handle an undefined input?
There was a problem hiding this comment.
Agreed, I changed formatJson so it handles undefined inputs and added the test for it.
Similar to describeUneditable, formatJson as needs to be exposed as protected readonly field to be used in gallery.ng.html
| return null; | ||
| } | ||
| if ( | ||
| control.kind !== 'enum' && |
There was a problem hiding this comment.
Above, we have more specialized checks (e.g., if control.kind === 'array' *and* Array.isArray`.
But, both here and there, we're just going to return null, so it seems like this check is sufficient?
There was a problem hiding this comment.
this check is meant for the remaining editable controls types, which are the primitive types. i updated it so its more clear that its checking for primitive types (string/boolean/number)
Updates
|
| control: GalleryPropertyControl; | ||
| kind: GalleryPropertyControl['kind']; | ||
| options: GalleryPropertyControl['options']; | ||
| // The current value, or undefined when the example omits the property. |
There was a problem hiding this comment.
nit: Use JSDoc for commenting class or interface members.
| /** | ||
| * Memoized schema resolver and shared property names, computed once per active catalog. | ||
| */ | ||
| private readonly activeCatalogResolver: Signal<{ |
There was a problem hiding this comment.
If the return type is going to be used more than once (I'm not sure in the cases here and below), I'd suggest creating an interface for more clarity.
There was a problem hiding this comment.
Acknowledged, activeCatalogResolver and draftSource return types are only used once but will add interfaces for next time!
| } | ||
|
|
||
| protected asArray(value: unknown): unknown[] { | ||
| return Array.isArray(value) ? value : []; |
There was a problem hiding this comment.
I'd suggest leaving a comment in the code to explain this for future readers.
| return new Set([...first].filter(prop => rest.every(set => set.has(prop)))); | ||
| } | ||
|
|
||
| /** Property Editor form reads and writes to. Prioritize 'target' |
There was a problem hiding this comment.
No need to undo anything you've done, but our TS style says that if the comment fits on a single line, then using /** the comment */ is fine. Using something like:
/** here
* is the comment
*/
is what's not right.
| private readonly location = inject(Location); | ||
|
|
||
| /** | ||
| * Snapshots the selected example ands renderer before compressing the workspace handoff. |
There was a problem hiding this comment.
nit: "...ands..." --> "...and..."
d1715be to
fa58af3
Compare

Description
Adds interactive playground to Component Gallery where users can now inspect and edit component properties via schema-driven UI controls or live Monaco JSON editor, preview updates in real time, reset drafts to their original state, and launch their edited draft directly into the main Composer workspace.
Demo
InteractivePlaygroundDemo.mov
Changes by Functionality
1. Interactive Gallery Playground & Unit Tests & UI (
4 files)shell/src/app/gallery/gallery.ts: Manages local draft state vialinkedSignal, synchronizes edits between the schema-driven property controls and the JSON editor, preserves the last valid preview payload when invalid edits occur, and supports resetting or copying payloads.shell/src/app/gallery/gallery.spec.ts: Adds unit test coverage for live property editing, two-way JSON synchronization, invalid draft recovery, common vs. component-specific property grouping, and workspace handoff.shell/src/app/gallery/gallery.ng.html: Adds the interactive playground layout, including dynamic property controls (selects, slide toggles, inputs, nested object fields, dynamic array item lists, and JSON textareas), inline validation errors, status announcements, and action buttons (Reset example,Open in Composer).shell/src/app/gallery/gallery.scss: Styles the expandable playground panel, property editor rows, nested object/array controls, inline error states, and responsive layout adjustments.2. Schema-Driven Parsing of Types for UI Control Elements & Unit Tests (
2 file)shell/src/app/gallery/gallery-example.ts: ImplementsparseGalleryExample,galleryPropertyControl,createDefaultGalleryValue, anddescribeUneditableValueto validate{components, data}drafts against the active catalog schema, infer control kinds (enum,boolean,number,string,array,object,json), and distinguish literal values from read-only data bindings/function calls.shell/src/app/gallery/gallery-example.spec.ts: Tests schema control inference, default value generation, and draft validation edge cases.3. Composer Workspace Handoff ("Open in Composer" button) & Unit Tests (
2 file)shell/src/app/gallery/gallery-launcher.ts: Introduces theGalleryLauncherservice to compress the active gallery payload viaQueryParser.encodeSharedPayloadand navigate to the Composer workspace with#renderer,#rendererId, and#a2uihash parameters.shell/src/app/gallery/gallery-launcher.spec.ts: Tests URL generation and compressed payload handoff to the Composer workspace.4. Monaco Editor Gallery Schema Mode & Unit Tests (
2 file)shell/src/app/shared/monaco-editor/monaco-editor.ts: AddsschemaMode('layout' | 'gallery-example') and dynamicariaLabelinputs toMonacoEditor, registeringa2ui-gallery-example-schema(inmemory://model/gallery-draft-*.json) to validate{components, data}drafts against the active catalog's component schemas.shell/src/app/shared/monaco-editor/monaco-editor.spec.ts: Verifiesgallery-exampleschema registration,inmemory://model/gallery-draft-*URI prefixing, and dynamicariaLabelupdates.5. Sample Catalog Usage Updates from v0.8 to v0.9 (
3 files)samples/lit-basic-catalog/src/usages.tssamples/ng-basic-catalog/src/assets/usages.tssamples/react-basic-catalog/src/usages.tsusageHint/primary/textFieldTypetovariant,ChoicePicker.selectionstovalue,Slider.minValue/maxValuetomin/max,Iconname'check_circle'to'check', andButtonaction context to an object).6. Harness & E2E Coverage (
3 files)shell/src/app/gallery/test/gallery.harness.ts: ExtendsGalleryHarnesswith helper methods for editing draft JSON, interacting with typed/nested/array property controls, reading validation errors/notes, resetting drafts, and triggeringOpen in Composer.shell/e2e/gallery.e2e.ts: Adds Playwright E2E user journeys covering interactive playground editing, validation error states, resetting examples, and opening drafts in Composer.shell/e2e/helpers.ts: UpdateswaitForMonacoEditorto support both the Workspace Dockview tab and the Component Gallery'sEdit JSONcard. Prevents E2E timeouts when no Dockview tab is present by waiting oneditorTab.or(editorLocator).Pre-launch Checklist
If you need help, consider asking for advice on the discussion board.