-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid): controls on inspector hover (#6302)
This PR displays subdued Grid controls on inspector hover, according to the axis <video src="https://github.com/user-attachments/assets/6db0c006-f4ba-44d1-a025-99fffe719a78"></video> **Manual Tests:** I hereby swear that: - [X] I opened a hydrogen project and it loaded - [X] I could navigate to various routes in Preview mode
- Loading branch information
Showing
2 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
editor/src/components/canvas/controls/select-mode/subdued-grid-gap-controls.tsx
This file contains 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,155 @@ | ||
import React from 'react' | ||
import { useColorTheme } from '../../../../uuiui' | ||
import { Substores, useEditorState, useRefEditorState } from '../../../editor/store/store-hook' | ||
import { useBoundingBox } from '../bounding-box-hooks' | ||
import { CanvasOffsetWrapper } from '../canvas-offset-wrapper' | ||
import type { Axis } from '../../gap-utils' | ||
import { gridGapControlBoundsFromMetadata, maybeGridGapData } from '../../gap-utils' | ||
import type { ElementPath } from 'utopia-shared/src/types' | ||
import { useGridData } from '../grid-controls' | ||
import { fallbackEmptyValue } from './controls-common' | ||
import type { CanvasRectangle } from '../../../../core/shared/math-utils' | ||
import type { CSSNumber } from '../../../../components/inspector/common/css-utils' | ||
|
||
export interface SubduedGridGapControlProps { | ||
hoveredOrFocused: 'hovered' | 'focused' | ||
axis: Axis | 'both' | ||
} | ||
|
||
export const SubduedGridGapControl = React.memo<SubduedGridGapControlProps>((props) => { | ||
const { hoveredOrFocused, axis } = props | ||
const targets = useEditorState( | ||
Substores.selectedViews, | ||
(store) => store.editor.selectedViews, | ||
'SubduedGridGapControl selectedViews', | ||
) | ||
const scale = useEditorState( | ||
Substores.canvas, | ||
(store) => store.editor.canvas.scale, | ||
'GridGapControl scale', | ||
) | ||
const metadata = useRefEditorState((store) => store.editor.jsxMetadata) | ||
const selectedElement = targets.at(0) | ||
|
||
const gridRowColumnInfo = useGridData(targets) | ||
const selectedGrid = gridRowColumnInfo.at(0) | ||
|
||
const filteredGaps = React.useMemo(() => { | ||
if (selectedElement == null || selectedGrid == null) { | ||
return [] | ||
} | ||
const gridGap = maybeGridGapData(metadata.current, selectedElement) | ||
if (gridGap == null) { | ||
return [] | ||
} | ||
|
||
const gridGapRow = gridGap.row | ||
const gridGapColumn = gridGap.column | ||
|
||
const controlBounds = gridGapControlBoundsFromMetadata( | ||
selectedElement, | ||
selectedGrid, | ||
{ | ||
row: fallbackEmptyValue(gridGapRow), | ||
column: fallbackEmptyValue(gridGapColumn), | ||
}, | ||
scale, | ||
) | ||
return controlBounds.gaps.filter((gap) => gap.axis === axis || axis === 'both') | ||
}, [axis, metadata, scale, selectedElement, selectedGrid]) | ||
|
||
if (filteredGaps.length === 0 || selectedElement == null) { | ||
return null | ||
} | ||
|
||
return ( | ||
<> | ||
{filteredGaps.map((gap) => ( | ||
<GridGapControl | ||
key={gap.gapId} | ||
targets={targets} | ||
selectedElement={selectedElement} | ||
hoveredOrFocused={hoveredOrFocused} | ||
gap={gap} | ||
/> | ||
))} | ||
</> | ||
) | ||
}) | ||
|
||
function GridGapControl({ | ||
targets, | ||
selectedElement, | ||
hoveredOrFocused, | ||
gap, | ||
}: { | ||
targets: Array<ElementPath> | ||
selectedElement: ElementPath | ||
hoveredOrFocused: 'hovered' | 'focused' | ||
gap: { | ||
bounds: CanvasRectangle | ||
gapId: string | ||
gap: CSSNumber | ||
axis: Axis | ||
} | ||
}) { | ||
const metadata = useRefEditorState((store) => store.editor.jsxMetadata) | ||
const scale = useEditorState( | ||
Substores.canvas, | ||
(store) => store.editor.canvas.scale, | ||
'GridGapControl scale', | ||
) | ||
const gridRowColumnInfo = useGridData([selectedElement]) | ||
|
||
const sideRef = useBoundingBox([selectedElement], (ref, parentBoundingBox) => { | ||
const gridGap = maybeGridGapData(metadata.current, selectedElement) | ||
const selectedGrid = gridRowColumnInfo.at(0) | ||
if (gridGap == null || selectedGrid == null) { | ||
return | ||
} | ||
|
||
const controlBounds = gridGapControlBoundsFromMetadata( | ||
selectedElement, | ||
selectedGrid, | ||
{ | ||
row: fallbackEmptyValue(gridGap.row), | ||
column: fallbackEmptyValue(gridGap.column), | ||
}, | ||
scale, | ||
) | ||
|
||
const bound = controlBounds.gaps.find((updatedGap) => updatedGap.gapId === gap.gapId) | ||
if (bound == null) { | ||
return | ||
} | ||
|
||
ref.current.style.display = 'block' | ||
ref.current.style.left = `${bound.bounds.x + parentBoundingBox.x}px` | ||
ref.current.style.top = `${bound.bounds.y + parentBoundingBox.y}px` | ||
ref.current.style.height = numberToPxValue(bound.bounds.height) | ||
ref.current.style.width = numberToPxValue(bound.bounds.width) | ||
}) | ||
|
||
const color = useColorTheme().brandNeonPink.value | ||
|
||
const solidOrDashed = hoveredOrFocused === 'focused' ? 'solid' : 'dashed' | ||
|
||
return ( | ||
<CanvasOffsetWrapper> | ||
<div | ||
ref={sideRef} | ||
style={{ | ||
position: 'absolute', | ||
border: `1px ${solidOrDashed} ${color}`, | ||
}} | ||
data-testid={getSubduedGridGaplTestID(hoveredOrFocused)} | ||
/> | ||
</CanvasOffsetWrapper> | ||
) | ||
} | ||
|
||
export function getSubduedGridGaplTestID(hoveredOrFocused: 'hovered' | 'focused'): string { | ||
return `SubduedGridGapControl-${hoveredOrFocused}` | ||
} | ||
|
||
const numberToPxValue = (n: number) => n + 'px' |
This file contains 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