AI-native design editor. Open-source Figma alternative.
fix(dom-css): find parts an instance holds inside another part (#1706)
Danila Poyarkove754d6e9254c
diff --git a/changes/1706/fixed.md b/changes/1706/fixed.mdnew file mode 100644index 000000000..2f98c0cbf--- /dev/null+++ b/changes/1706/fixed.md@@ -0,0 +1 @@+- In preview, a slider's range and a progress bar's indicator follow the value when they sit inside the track, as in the shadcn/ui kit's Slider (#1706).diff --git a/packages/dom-css/src/behaviours/controls.ts b/packages/dom-css/src/behaviours/controls.tsindex 04a8f8314..c72c94acc 100644--- a/packages/dom-css/src/behaviours/controls.ts+++ b/packages/dom-css/src/behaviours/controls.ts@@ -13,7 +13,7 @@ import {} from '@open-pencil/scene-graph/behaviours'import { findComponentPropertyTargets } from '@open-pencil/scene-graph/components'import { instanceMainComponent } from '@open-pencil/scene-graph/instances'-import { instanceSlotFrames, slotPropertyId } from '@open-pencil/scene-graph/slots'+import { slotPropertyId } from '@open-pencil/scene-graph/slots'/** A boolean value of a control and the property that draws it. */export interface BooleanModel {@@ -97,6 +97,24 @@ function statesModel(}}+/**+ * The frames an instance binds to slot properties, which its parts are. A slot may sit inside+ * another, as a slider's range in its track: Figma keeps both in the component and makes the+ * inner one part of the outer one's content in an instance, so the search goes through slots+ * but not into nested instances, which are controls of their own.+ */+function partFrames(graph: SceneGraph, instance: SceneNode): SceneNode[] {+ const frames: SceneNode[] = []+ const visit = (node: SceneNode): void => {+ for (const child of graph.getChildren(node.id)) {+ if (slotPropertyId(child)) frames.push(child)+ if (child.type !== 'INSTANCE') visit(child)+ }+ }+ visit(instance)+ return frames+}+function controlModel(graph: SceneGraph, rootId: string, instance: SceneNode): ControlModel | null {const component = instanceMainComponent(graph, instance)const owner = component && behaviourOwner(graph, component)@@ -116,7 +134,7 @@ function controlModel(graph: SceneGraph, rootId: string, instance: SceneNode): Cdesigned: designedBoolean(graph, instance, definition, binding.on)}}- const frames = instanceSlotFrames(graph, instance)+ const frames = partFrames(graph, instance)const parts = Object.fromEntries(Object.entries(behaviour.parts).flatMap(([partId, propertyId]) => {const frame = frames.find((item) => slotPropertyId(item) === propertyId)diff --git a/packages/dom-css/tests/behaviours/controls.test.ts b/packages/dom-css/tests/behaviours/controls.test.tsindex 13c232b4e..a64347881 100644--- a/packages/dom-css/tests/behaviours/controls.test.ts+++ b/packages/dom-css/tests/behaviours/controls.test.ts@@ -1,5 +1,6 @@import { describe, expect, test } from 'bun:test'+import { progressComponent, sliderComponent } from '#dom-css-tests/behaviours/fixtures'import { behaviourControls, booleanOf } from '#dom-css/behaviours/controls'import { controlRoles } from '#dom-css/behaviours/roles'@@ -54,6 +55,22 @@ describe('behaviour controls', () => {expect(control && booleanOf(control, 'open')).toBeUndefined()})+ // Figma (desktop 126) keeps a slot inside another slot of a component; in an instance the inner+ // one is part of the outer one's content. Preview runs the instance, so it must still find it.+ test('finds the parts an instance holds inside another part', () => {+ for (const [build, parts] of [+ [sliderComponent, ['range', 'thumb', 'track']],+ [progressComponent, ['indicator', 'track']]+ ] as const) {+ const { graph, set } = build()+ const card = graph.createNode('FRAME', graph.getPages()[0].id, { name: 'Card' })+ const instance = graph.createInstance(set.id, card.id)+ if (!instance) throw new Error('No instance')+ const control = behaviourControls(graph, card.id).get(instance.name)+ expect(Object.keys(control?.parts ?? {}).sort()).toEqual([...parts])+ }+ })+test('gives a control root its role by layer path', () => {const { graph, card } = switchCard()const roles = controlRoles(behaviourControls(graph, card.id))diff --git a/tests/e2e/components/behaviours.spec.ts b/tests/e2e/components/behaviours.spec.tsindex 4509157f9..b5de87e33 100644--- a/tests/e2e/components/behaviours.spec.ts+++ b/tests/e2e/components/behaviours.spec.ts@@ -144,6 +144,91 @@ test('a switch inside a frame, named after its variant, toggles both ways in preawait editor.page.keyboard.press('Escape')})+/**+ * A slider component at 20 of 0 to 100 in steps of 10, its range a slot inside its track's+ * slot, as Reka draws it and Figma keeps it, and an instance of it below.+ */+async function createSlider() {+ return editor.page.evaluate(() => {+ const store = window.openPencil?.getStore?.()+ if (!store) throw new Error('OpenPencil store not initialized')+ const graph = store.graph+ const pageId = store.state.currentPageId+ const solid = (r: number, g: number, b: number) => [+ { type: 'SOLID' as const, color: { r, g, b, a: 1 }, opacity: 1, visible: true }+ ]+ const slot = (propertyId: string) => [{ propertyId, field: 'SLOT_CONTENT' as const }]+ const slider = graph.createNode('COMPONENT', pageId, {+ name: 'Volume',+ x: 120,+ y: 120,+ width: 200,+ height: 20,+ componentPropertyDefinitions: ['Track', 'Range', 'Thumb'].map((name) => ({+ id: `${name.toLowerCase()}-slot`,+ name,+ type: 'SLOT' as const,+ defaultValue: ''+ }))+ })+ const track = graph.createNode('FRAME', slider.id, {+ name: 'Track',+ componentPropertyReferences: slot('track-slot'),+ y: 8,+ width: 200,+ height: 4,+ fills: solid(0.8, 0.8, 0.85)+ })+ graph.createNode('FRAME', track.id, {+ name: 'Range',+ componentPropertyReferences: slot('range-slot'),+ width: 40,+ height: 4,+ fills: solid(0.2, 0.6, 1)+ })+ graph.createNode('FRAME', slider.id, {+ name: 'Thumb',+ componentPropertyReferences: slot('thumb-slot'),+ x: 30,+ width: 20,+ height: 20,+ cornerRadius: 10,+ fills: solid(1, 1, 1)+ })+ store.setBehaviour(slider.id, {+ kind: 'slider',+ booleans: {},+ texts: {},+ numbers: { value: { min: 0, max: 100, step: 10, default: 20 } },+ parts: { track: 'track-slot', range: 'range-slot', thumb: 'thumb-slot' }+ })+ const instance = graph.createInstance(slider.id, pageId, { x: 120, y: 260 })+ if (!instance) throw new Error('Instance not created')+ return instance.id+ })+}++// In an instance the range is part of the track's content, and preview still has Reka place it.+test('a slider instance in preview moves the range inside its track', async () => {+ const instanceId = await createSlider()+ await editor.canvas.waitForRender()+ await startPreview()++ const control = island(instanceId).getByRole('slider')+ const range = island(instanceId).locator('[data-layer$="Range"]')+ const width = async () => (await range.boundingBox())?.width ?? 0+ await expect(control).toHaveAttribute('aria-valuenow', '20')+ // A range that shows, so twice its width is a real change.+ await expect.poll(width).toBeGreaterThan(0)+ const before = await width()+ await control.press('ArrowRight')+ await control.press('ArrowRight')+ await expect(control).toHaveAttribute('aria-valuenow', '40')+ // Twice the value, twice the range: Reka places it, the design's width no longer stands.+ await expect.poll(width).toBeCloseTo(before * 2, 0)+ await editor.page.keyboard.press('Escape')+})+test('preview islands move with the frames the canvas draws, not ahead of them', async () => {const ids = await createSwitch({ nested: true })await editor.canvas.waitForRender()