Skip to content

AI-native design editor. Open-source Figma alternative.

feat(app): expose auto layout min max sizing

Danila Poyarkov2a51a21598fa
diff --git a/packages/vue/src/controls/layout/helpers.ts b/packages/vue/src/controls/layout/helpers.ts
index 249aafe21..887126f02 100644
--- a/packages/vue/src/controls/layout/helpers.ts
+++ b/packages/vue/src/controls/layout/helpers.ts
@@ -2,6 +2,7 @@ import { useSceneComputed } from '#vue/internal/scene-computed/use'
import { computed, ref } from 'vue'
import type { useI18n } from '#vue/i18n'
+import type { Editor } from '@open-pencil/core/editor'
import type {
GridTrack,
LayoutAlign,
@@ -9,7 +10,6 @@ import type {
LayoutSizing,
SceneNode
} from '@open-pencil/core/scene-graph'
-import type { Editor } from '@open-pencil/core/editor'
import type { ComputedRef } from 'vue'
export type AlignCell = { primary: LayoutAlign; counter: LayoutCounterAlign }
@@ -22,6 +22,8 @@ type LayoutPanelStrings = {
sizingFill: string
}
+export type SizeLimitProp = 'minWidth' | 'maxWidth' | 'minHeight' | 'maxHeight'
+
type ValueRef<T> = { readonly value: T }
export const ALIGN_HORIZONTAL: AlignCell[] = [
@@ -172,6 +174,28 @@ export function createLayoutActions({
if (node.value) editor.updateNode(node.value.id, { [key]: value })
}
+ function updateSizeLimit(prop: SizeLimitProp, value: number) {
+ if (!node.value) return
+ editor.updateNode(node.value.id, { [prop]: value })
+ }
+
+ function commitSizeLimit(prop: SizeLimitProp, _value: number, previous: number) {
+ if (!node.value) return
+ editor.commitNodeUpdate(node.value.id, { [prop]: previous }, `Change ${prop}`)
+ }
+
+ function addSizeLimit(prop: SizeLimitProp) {
+ const n = node.value
+ if (!n) return
+ const fallback = prop === 'minWidth' || prop === 'maxWidth' ? n.width : n.height
+ editor.updateNodeWithUndo(n.id, { [prop]: Math.round(fallback) }, `Add ${prop}`)
+ }
+
+ function removeSizeLimit(prop: SizeLimitProp) {
+ if (!node.value) return
+ editor.updateNodeWithUndo(node.value.id, { [prop]: null }, `Remove ${prop}`)
+ }
+
function commitProp(key: string, _value: number | string, previous: number | string) {
if (node.value) {
editor.commitNodeUpdate(
@@ -222,6 +246,10 @@ export function createLayoutActions({
return {
updateProp,
+ updateSizeLimit,
+ commitSizeLimit,
+ addSizeLimit,
+ removeSizeLimit,
commitProp,
setWidthSizing,
setHeightSizing,
diff --git a/packages/vue/src/controls/layout/use.ts b/packages/vue/src/controls/layout/use.ts
index 4deb84580..011ebdf52 100644
--- a/packages/vue/src/controls/layout/use.ts
+++ b/packages/vue/src/controls/layout/use.ts
@@ -60,6 +60,10 @@ export function useLayout() {
hasUniformPadding,
trackSizingOptions: createTrackSizingOptions(panels.value),
updateProp: layoutActions.updateProp,
+ updateSizeLimit: layoutActions.updateSizeLimit,
+ commitSizeLimit: layoutActions.commitSizeLimit,
+ addSizeLimit: layoutActions.addSizeLimit,
+ removeSizeLimit: layoutActions.removeSizeLimit,
commitProp: layoutActions.commitProp,
setWidthSizing: layoutActions.setWidthSizing,
setHeightSizing: layoutActions.setHeightSizing,
diff --git a/packages/vue/src/i18n/messages.ts b/packages/vue/src/i18n/messages.ts
index 509a52938..d655cb7e7 100644
--- a/packages/vue/src/i18n/messages.ts
+++ b/packages/vue/src/i18n/messages.ts
@@ -206,6 +206,18 @@ export const panelMessages = i18n('panels', {
sizingFill: 'Fill',
sizingHugShort: 'Hug',
sizingFillShort: 'Fill',
+ addMinWidth: 'Add min width',
+ removeMinWidth: 'Remove min width',
+ addMaxWidth: 'Add max width',
+ removeMaxWidth: 'Remove max width',
+ addMinHeight: 'Add min height',
+ removeMinHeight: 'Remove min height',
+ addMaxHeight: 'Add max height',
+ removeMaxHeight: 'Remove max height',
+ minWidthShort: 'Min W',
+ maxWidthShort: 'Max W',
+ minHeightShort: 'Min H',
+ maxHeightShort: 'Max H',
sizingFillFr: 'Fill (fr)',
sizingFixedPx: 'Fixed (px)'
})
diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts
index 07ff5ca3b..e8133d8bd 100644
--- a/packages/vue/src/index.ts
+++ b/packages/vue/src/index.ts
@@ -47,6 +47,7 @@ export { toolCursor } from '#vue/editor/tool-cursor'
/** Property-panel composables. */
export { usePosition } from '#vue/controls/position/use'
export { useLayout } from '#vue/controls/layout/use'
+export type { SizeLimitProp } from '#vue/controls/layout/helpers'
export { useAppearance } from '#vue/controls/appearance/use'
export { useTypography } from '#vue/controls/typography/use'
export type { UseTypographyOptions } from '#vue/controls/typography/use'
diff --git a/packages/vue/src/primitives/LayoutControls/LayoutControlsRoot.vue b/packages/vue/src/primitives/LayoutControls/LayoutControlsRoot.vue
index cd4bc4b7a..0495924d6 100644
--- a/packages/vue/src/primitives/LayoutControls/LayoutControlsRoot.vue
+++ b/packages/vue/src/primitives/LayoutControls/LayoutControlsRoot.vue
@@ -4,7 +4,11 @@ import { provideLayoutControls } from '#vue/primitives/LayoutControls/context'
import { proxyRefs } from 'vue'
const ctx = useLayout()
-provideLayoutControls(proxyRefs(ctx) as ReturnType<typeof proxyRefs<typeof ctx>> & { node: NonNullable<typeof ctx.node.value> })
+provideLayoutControls(
+ proxyRefs(ctx) as ReturnType<typeof proxyRefs<typeof ctx>> & {
+ node: NonNullable<typeof ctx.node.value>
+ }
+)
</script>
<template>
@@ -24,6 +28,10 @@ provideLayoutControls(proxyRefs(ctx) as ReturnType<typeof proxyRefs<typeof ctx>>
:has-uniform-padding="ctx.hasUniformPadding.value"
:track-sizing-options="ctx.trackSizingOptions"
:update-prop="ctx.updateProp"
+ :update-size-limit="ctx.updateSizeLimit"
+ :commit-size-limit="ctx.commitSizeLimit"
+ :add-size-limit="ctx.addSizeLimit"
+ :remove-size-limit="ctx.removeSizeLimit"
:commit-prop="ctx.commitProp"
:set-width-sizing="ctx.setWidthSizing"
:set-height-sizing="ctx.setHeightSizing"
diff --git a/src/components/properties/LayoutSection/SizeControls.vue b/src/components/properties/LayoutSection/SizeControls.vue
index e30c02cd7..e5755cd39 100644
--- a/src/components/properties/LayoutSection/SizeControls.vue
+++ b/src/components/properties/LayoutSection/SizeControls.vue
@@ -15,17 +15,58 @@ import { useSelectUI } from '@/components/ui/select'
import { useI18n, useLayoutControlsContext } from '@open-pencil/vue'
import type { LayoutSizing } from '@open-pencil/core/scene-graph'
+import type { SizeLimitProp } from '@open-pencil/vue'
+
+type SizeSelectValue = LayoutSizing | `add-${SizeLimitProp}` | `remove-${SizeLimitProp}`
const ctx = useLayoutControlsContext()
const { panels } = useI18n()
const sizingSelect = useSelectUI({ item: 'rounded py-1.5 pr-2 pl-6 text-xs' })
+const widthLimitItems = [
+ {
+ prop: 'minWidth' as const,
+ addLabel: () => panels.value.addMinWidth,
+ removeLabel: () => panels.value.removeMinWidth
+ },
+ {
+ prop: 'maxWidth' as const,
+ addLabel: () => panels.value.addMaxWidth,
+ removeLabel: () => panels.value.removeMaxWidth
+ }
+]
+
+const heightLimitItems = [
+ {
+ prop: 'minHeight' as const,
+ addLabel: () => panels.value.addMinHeight,
+ removeLabel: () => panels.value.removeMinHeight
+ },
+ {
+ prop: 'maxHeight' as const,
+ addLabel: () => panels.value.addMaxHeight,
+ removeLabel: () => panels.value.removeMaxHeight
+ }
+]
+
function sizingShortLabel(sizing: LayoutSizing): string | null {
if (sizing === 'HUG') return panels.value.sizingHugShort
if (sizing === 'FILL') return panels.value.sizingFillShort
return null
}
+
+function handleSizeSelect(axis: 'width' | 'height', value: SizeSelectValue) {
+ if (value === 'FIXED' || value === 'HUG' || value === 'FILL') {
+ if (axis === 'width') ctx.setWidthSizing(value)
+ else ctx.setHeightSizing(value)
+ return
+ }
+
+ const [action, prop] = value.split('-') as ['add' | 'remove', SizeLimitProp]
+ if (action === 'add') ctx.addSizeLimit(prop)
+ else ctx.removeSizeLimit(prop)
+}
</script>
<template>
@@ -40,9 +81,10 @@ function sizingShortLabel(sizing: LayoutSizing): string | null {
<template v-if="ctx.isFlex || ctx.isInAutoLayout" #suffix>
<SelectRoot
:model-value="ctx.widthSizing"
- @update:model-value="ctx.setWidthSizing($event as LayoutSizing)"
+ @update:model-value="handleSizeSelect('width', $event as SizeSelectValue)"
>
<SelectTrigger
+ data-test-id="layout-width-sizing-menu"
class="flex shrink-0 cursor-pointer items-center self-stretch border-none bg-transparent px-1 text-[11px] text-muted outline-none"
@pointerdown.stop
>
@@ -70,6 +112,16 @@ function sizingShortLabel(sizing: LayoutSizing): string | null {
</SelectItemIndicator>
<SelectItemText>{{ opt.label }}</SelectItemText>
</SelectItem>
+ <SelectItem
+ v-for="item in widthLimitItems"
+ :key="item.prop"
+ :value="`${ctx.node[item.prop] == null ? 'add' : 'remove'}-${item.prop}`"
+ :class="sizingSelect.item"
+ >
+ <SelectItemText>
+ {{ ctx.node[item.prop] == null ? item.addLabel() : item.removeLabel() }}
+ </SelectItemText>
+ </SelectItem>
</SelectViewport>
</SelectContent>
</SelectPortal>
@@ -87,9 +139,10 @@ function sizingShortLabel(sizing: LayoutSizing): string | null {
<template v-if="ctx.isFlex || ctx.isInAutoLayout" #suffix>
<SelectRoot
:model-value="ctx.heightSizing"
- @update:model-value="ctx.setHeightSizing($event as LayoutSizing)"
+ @update:model-value="handleSizeSelect('height', $event as SizeSelectValue)"
>
<SelectTrigger
+ data-test-id="layout-height-sizing-menu"
class="flex shrink-0 cursor-pointer items-center self-stretch border-none bg-transparent px-1 text-[11px] text-muted outline-none"
@pointerdown.stop
>
@@ -117,6 +170,16 @@ function sizingShortLabel(sizing: LayoutSizing): string | null {
</SelectItemIndicator>
<SelectItemText>{{ opt.label }}</SelectItemText>
</SelectItem>
+ <SelectItem
+ v-for="item in heightLimitItems"
+ :key="item.prop"
+ :value="`${ctx.node[item.prop] == null ? 'add' : 'remove'}-${item.prop}`"
+ :class="sizingSelect.item"
+ >
+ <SelectItemText>
+ {{ ctx.node[item.prop] == null ? item.addLabel() : item.removeLabel() }}
+ </SelectItemText>
+ </SelectItem>
</SelectViewport>
</SelectContent>
</SelectPortal>
@@ -124,4 +187,51 @@ function sizingShortLabel(sizing: LayoutSizing): string | null {
</template>
</ScrubInput>
</div>
+
+ <div
+ v-if="
+ ctx.node.minWidth != null ||
+ ctx.node.maxWidth != null ||
+ ctx.node.minHeight != null ||
+ ctx.node.maxHeight != null
+ "
+ class="mt-1.5 grid grid-cols-2 gap-1.5"
+ >
+ <ScrubInput
+ v-if="ctx.node.minWidth != null"
+ data-test-id="layout-min-width-input"
+ :icon="panels.minWidthShort"
+ :model-value="Math.round(ctx.node.minWidth)"
+ :min="0"
+ @update:model-value="ctx.updateSizeLimit('minWidth', $event)"
+ @commit="(v: number, p: number) => ctx.commitSizeLimit('minWidth', v, p)"
+ />
+ <ScrubInput
+ v-if="ctx.node.maxWidth != null"
+ data-test-id="layout-max-width-input"
+ :icon="panels.maxWidthShort"
+ :model-value="Math.round(ctx.node.maxWidth)"
+ :min="0"
+ @update:model-value="ctx.updateSizeLimit('maxWidth', $event)"
+ @commit="(v: number, p: number) => ctx.commitSizeLimit('maxWidth', v, p)"
+ />
+ <ScrubInput
+ v-if="ctx.node.minHeight != null"
+ data-test-id="layout-min-height-input"
+ :icon="panels.minHeightShort"
+ :model-value="Math.round(ctx.node.minHeight)"
+ :min="0"
+ @update:model-value="ctx.updateSizeLimit('minHeight', $event)"
+ @commit="(v: number, p: number) => ctx.commitSizeLimit('minHeight', v, p)"
+ />
+ <ScrubInput
+ v-if="ctx.node.maxHeight != null"
+ data-test-id="layout-max-height-input"
+ :icon="panels.maxHeightShort"
+ :model-value="Math.round(ctx.node.maxHeight)"
+ :min="0"
+ @update:model-value="ctx.updateSizeLimit('maxHeight', $event)"
+ @commit="(v: number, p: number) => ctx.commitSizeLimit('maxHeight', v, p)"
+ />
+ </div>
</template>
diff --git a/tests/e2e/auto-layout.spec.ts b/tests/e2e/auto-layout.spec.ts
index a1b1a96b8..807e9a205 100644
--- a/tests/e2e/auto-layout.spec.ts
+++ b/tests/e2e/auto-layout.spec.ts
@@ -78,7 +78,9 @@ test('uniform padding ScrubInput sets all four padding sides', async () => {
const paddingScrub = page.locator('[data-test-id="layout-uniform-padding-input"]')
await paddingScrub.click()
await canvas.waitForRender()
- const paddingInput = page.locator('[data-test-id="layout-uniform-padding-input"] [data-test-id="scrub-input-field"]')
+ const paddingInput = page.locator(
+ '[data-test-id="layout-uniform-padding-input"] [data-test-id="scrub-input-field"]'
+ )
await paddingInput.fill('16')
await paddingInput.press('Enter')
await canvas.waitForRender()
@@ -91,6 +93,27 @@ test('uniform padding ScrubInput sets all four padding sides', async () => {
canvas.assertNoErrors()
})
+test('size dropdown adds and removes min width', async () => {
+ await selectFrame()
+
+ await page.locator('[data-test-id="layout-width-sizing-menu"]').click()
+ await page.getByText('Add min width').click()
+ await canvas.waitForRender()
+
+ let frame = await getNodeById(page, frameId)
+ expect(frame!.minWidth).toBe(Math.round(frame!.width))
+ await expect(page.locator('[data-test-id="layout-min-width-input"]')).toBeVisible()
+
+ await page.locator('[data-test-id="layout-width-sizing-menu"]').click()
+ await page.getByText('Remove min width').click()
+ await canvas.waitForRender()
+
+ frame = await getNodeById(page, frameId)
+ expect(frame!.minWidth).toBeNull()
+ await expect(page.locator('[data-test-id="layout-min-width-input"]')).toHaveCount(0)
+ canvas.assertNoErrors()
+})
+
test('alignment grid center sets CENTER alignment', async () => {
await selectFrame()
diff --git a/tests/helpers/store.ts b/tests/helpers/store.ts
index 1de6ebfb1..587808f61 100644
--- a/tests/helpers/store.ts
+++ b/tests/helpers/store.ts
@@ -16,7 +16,7 @@ export function getPageChildren(page: Page) {
height: n.height,
layoutMode: n.layoutMode,
childIds: n.childIds,
- vectorNetwork: n.vectorNetwork,
+ vectorNetwork: n.vectorNetwork
}))
})
}
@@ -46,7 +46,7 @@ export function getSelectedNode(page: Page) {
clipsContent: n.clipsContent,
fills: n.fills,
fontWeight: n.fontWeight,
- italic: n.italic,
+ italic: n.italic
}
})
}
@@ -72,6 +72,10 @@ export function getNodeById(page: Page, id: string) {
paddingRight: n.paddingRight,
paddingBottom: n.paddingBottom,
paddingLeft: n.paddingLeft,
+ minWidth: n.minWidth,
+ maxWidth: n.maxWidth,
+ minHeight: n.minHeight,
+ maxHeight: n.maxHeight,
childIds: n.childIds,
cornerRadius: n.cornerRadius,
flipX: n.flipX,
@@ -79,7 +83,7 @@ export function getNodeById(page: Page, id: string) {
fills: n.fills,
fontWeight: n.fontWeight,
italic: n.italic,
- styleRuns: n.styleRuns,
+ styleRuns: n.styleRuns
}
}, id)
}