Skip to content

Commit 7507d03

Browse files
authored
feature(grid) Size Label Tweaks (#6672)
- Added `controlForStrategy` so memoized controls can be used. - Added `StrategySizeLabel` to every strategy that included `AbsoluteResizeControl`. - Include `SizeLabel` in `NewCanvasControlsInner` if `StrategySizeLabel` hasn't been included in the strategy controls. - Extracted out `SizeLabel` into its own file. - `sizeLabel` function now supports `CSSNumber` for the `state` parameter. - `sizeLabelContents` now tries to present the unit values for the dimensions.
1 parent 6b3c5a2 commit 7507d03

13 files changed

+476
-289
lines changed

editor/src/components/canvas/canvas-strategies/canvas-strategy-types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ export interface ControlForStrategy<P> {
8585
control: React.FC<P>
8686
}
8787

88+
export function controlForStrategy<P>(control: React.FC<P>): ControlForStrategy<P> {
89+
return { type: 'ControlForStrategy', control: control }
90+
}
91+
8892
export function controlForStrategyMemoized<P>(control: React.FC<P>): ControlForStrategy<P> {
89-
return { type: 'ControlForStrategy', control: React.memo<any>(control) }
93+
return controlForStrategy(React.memo<any>(control))
9094
}
9195

9296
export type WhenToShowControl =

editor/src/components/canvas/canvas-strategies/strategies/absolute-resize-bounding-box-strategy.spec.browser2.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import { CanvasControlsContainerID } from '../../controls/new-canvas-controls'
5555
import type { CSSProperties } from 'react'
5656
import { MaxContent } from '../../../inspector/inspector-common'
5757
import {
58-
SizeLabelTestId,
5958
ResizePointTestId,
6059
AbsoluteResizeControlTestId,
6160
} from '../../controls/select-mode/absolute-resize-control'
@@ -78,6 +77,7 @@ import {
7877
} from '../../controls/bounding-box-hooks'
7978
import { act } from 'react-dom/test-utils'
8079
import { ComponentsHonouringPropsStylesProject } from './common-projects.test-utils'
80+
import { SizeLabelTestId } from '../../controls/select-mode/size-label'
8181

8282
// no mouseup here! it starts the interaction and resizes with drag delta
8383
async function startDragUsingActions(
@@ -678,7 +678,7 @@ describe('Absolute Resize Strategy', () => {
678678
EP.appendNewElementPath(TestScenePath, ['root', 'sizeless']),
679679
])
680680
const sizeLabel = await editor.renderedDOM.findByTestId(SizeLabelTestId)
681-
expect(sizeLabel.textContent).toEqual('(Children) 188 x 161')
681+
expect(sizeLabel.textContent).toEqual('(Children) 188 × 161')
682682
})
683683
it('resizes component instances that honour the size properties', async () => {
684684
const renderResult = await renderTestEditorWithCode(

editor/src/components/canvas/canvas-strategies/strategies/absolute-resize-bounding-box-strategy.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { gatherParentAndSiblingTargets } from '../../controls/guideline-helpers'
3636
import { ImmediateParentBounds } from '../../controls/parent-bounds'
3737
import { ImmediateParentOutlines } from '../../controls/parent-outlines'
3838
import { AbsoluteResizeControl } from '../../controls/select-mode/absolute-resize-control'
39+
import { StrategySizeLabel } from '../../controls/select-mode/size-label'
3940
import { ZeroSizeResizeControlWrapper } from '../../controls/zero-sized-element-controls'
4041
import {
4142
getDescriptiveStrategyLabelWithRetargetedPaths,
@@ -122,6 +123,13 @@ export function absoluteResizeBoundingBoxStrategy(
122123
show: 'visible-except-when-other-strategy-is-active',
123124
priority: 'top',
124125
}),
126+
controlWithProps({
127+
control: StrategySizeLabel,
128+
props: { targets: originalTargets, pathsWereReplaced: pathsWereReplaced },
129+
key: 'size-label',
130+
show: 'visible-except-when-other-strategy-is-active',
131+
priority: 'top',
132+
}),
125133
controlWithProps({
126134
control: ZeroSizeResizeControlWrapper,
127135
props: { targets: originalTargets },

editor/src/components/canvas/canvas-strategies/strategies/basic-resize-strategy.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { controlsForGridPlaceholders } from '../../controls/grid-controls-for-st
4040
import { ImmediateParentBounds } from '../../controls/parent-bounds'
4141
import { ImmediateParentOutlines } from '../../controls/parent-outlines'
4242
import { AbsoluteResizeControl } from '../../controls/select-mode/absolute-resize-control'
43+
import { StrategySizeLabel } from '../../controls/select-mode/size-label'
4344
import { ZeroSizeResizeControlWrapper } from '../../controls/zero-sized-element-controls'
4445
import type {
4546
CanvasStrategy,
@@ -104,6 +105,13 @@ export function basicResizeStrategy(
104105
show: 'always-visible',
105106
priority: 'top',
106107
}),
108+
controlWithProps({
109+
control: StrategySizeLabel,
110+
props: { targets: selectedElements, pathsWereReplaced: false },
111+
key: 'size-label',
112+
show: 'visible-except-when-other-strategy-is-active',
113+
priority: 'top',
114+
}),
107115
controlWithProps({
108116
control: ZeroSizeResizeControlWrapper,
109117
props: { targets: selectedElements },

editor/src/components/canvas/canvas-strategies/strategies/flex-resize-basic-strategy.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { pushIntendedBoundsAndUpdateGroups } from '../../commands/push-intended-
3838
import { queueTrueUpElement } from '../../commands/queue-true-up-command'
3939
import { treatElementAsGroupLike } from './group-helpers'
4040
import { trueUpGroupElementChanged } from '../../../editor/store/editor-state'
41+
import { StrategySizeLabel } from '../../controls/select-mode/size-label'
4142

4243
export function flexResizeBasicStrategy(
4344
canvasState: InteractionCanvasState,
@@ -109,6 +110,13 @@ export function flexResizeBasicStrategy(
109110
show: 'always-visible',
110111
priority: 'top',
111112
}),
113+
controlWithProps({
114+
control: StrategySizeLabel,
115+
props: { targets: selectedElements, pathsWereReplaced: false },
116+
key: 'size-label',
117+
show: 'visible-except-when-other-strategy-is-active',
118+
priority: 'top',
119+
}),
112120
controlWithProps({
113121
control: ZeroSizeResizeControlWrapper,
114122
props: { targets: selectedElements },

editor/src/components/canvas/canvas-strategies/strategies/flex-resize-strategy.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import { treatElementAsGroupLike } from './group-helpers'
6767
import { queueTrueUpElement } from '../../commands/queue-true-up-command'
6868
import { pushIntendedBoundsAndUpdateGroups } from '../../commands/push-intended-bounds-and-update-groups-command'
6969
import { trueUpGroupElementChanged } from '../../../editor/store/editor-state'
70+
import { StrategySizeLabel } from '../../controls/select-mode/size-label'
7071

7172
export const FLEX_RESIZE_STRATEGY_ID = 'FLEX_RESIZE'
7273

@@ -129,6 +130,13 @@ export function flexResizeStrategy(
129130
show: 'always-visible',
130131
priority: 'top',
131132
}),
133+
controlWithProps({
134+
control: StrategySizeLabel,
135+
props: { targets: selectedElements, pathsWereReplaced: false },
136+
key: 'size-label',
137+
show: 'visible-except-when-other-strategy-is-active',
138+
priority: 'top',
139+
}),
132140
controlWithProps({
133141
control: ZeroSizeResizeControlWrapper,
134142
props: { targets: selectedElements },

editor/src/components/canvas/canvas-strategies/strategies/keyboard-absolute-resize-strategy.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import type { ElementInstanceMetadataMap } from '../../../../core/shared/element
4444
import type { AllElementProps } from '../../../editor/store/editor-state'
4545
import { getDescriptiveStrategyLabelWithRetargetedPaths } from '../canvas-strategies'
4646
import { MetadataUtils } from '../../../../core/model/element-metadata-utils'
47+
import { StrategySizeLabel } from '../../controls/select-mode/size-label'
4748

4849
interface VectorAndEdge {
4950
movement: CanvasVector
@@ -168,6 +169,13 @@ export function keyboardAbsoluteResizeStrategy(
168169
show: 'visible-except-when-other-strategy-is-active',
169170
priority: 'top',
170171
}),
172+
controlWithProps({
173+
control: StrategySizeLabel,
174+
props: { targets: selectedElements, pathsWereReplaced: pathsWereReplaced },
175+
key: 'size-label',
176+
show: 'visible-except-when-other-strategy-is-active',
177+
priority: 'top',
178+
}),
171179
],
172180
fitness: getFitness(interactionSession),
173181
apply: () => {

editor/src/components/canvas/controls/new-canvas-controls.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
GridHelperControls,
8181
GridMeasurementHelpers,
8282
} from './grid-controls'
83+
import { SizeLabel, StrategySizeLabel } from './select-mode/size-label'
8384

8485
export const CanvasControlsContainerID = 'new-canvas-controls-container'
8586

@@ -291,6 +292,16 @@ const NewCanvasControlsInner = (props: NewCanvasControlsInnerProps) => {
291292
const colorTheme = useColorTheme()
292293
const { bottomStrategyControls, middleStrategyControls, topStrategyControls } =
293294
useGetApplicableStrategyControls(localSelectedViews)
295+
const sizeLabelInStrategyControls =
296+
topStrategyControls.some((control) => {
297+
return control.control === StrategySizeLabel
298+
}) ||
299+
middleStrategyControls.some((control) => {
300+
return control.control === StrategySizeLabel
301+
}) ||
302+
bottomStrategyControls.some((control) => {
303+
return control.control === StrategySizeLabel
304+
})
294305
const [inspectorHoveredControls] = useAtom(InspectorHoveredCanvasControls)
295306
const [inspectorFocusedControls] = useAtom(InspectorFocusedCanvasControls)
296307

@@ -640,6 +651,10 @@ const NewCanvasControlsInner = (props: NewCanvasControlsInnerProps) => {
640651
</>,
641652
)}
642653
{when(isSelectMode(editorMode), <DistanceGuidelineControl />)}
654+
{unless(
655+
sizeLabelInStrategyControls,
656+
<SizeLabel targets={localSelectedViews} pathsWereReplaced={false} />,
657+
)}
643658
<GuidelineControls />
644659
</>,
645660
)}

0 commit comments

Comments
 (0)