Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DimensionBox size #903

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
position: absolute;
left: var(--left);
top: var(--top);
height: var(--box-height);
font-size: var(--font-size);
border-radius: var(--border-radius);
padding-left: var(--horizontal-padding);
padding-right: var(--horizontal-padding);
height: 18px;
font-size: 10px;
border-radius: 4px;
padding-left: 8px;
padding-right: 8px;
display: flex;
align-items: center;
white-space: nowrap;
transform: var(--box-transform);
margin: var(--margin);
}

.arrow {
Expand All @@ -23,4 +24,5 @@
height: var(--arrow-size);
transform-origin: 50% 50%;
transform: translate(-50%, -50%) rotate(45deg);
margin: var(--margin);
}
32 changes: 11 additions & 21 deletions packages/vscode-extension/src/webview/components/DimensionsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ import { Frame } from "../../common/Project";
import { DeviceProperties } from "../utilities/consts";
import "./DimensionsBox.css";

const BOX_HEIGHT_FRACTION = 0.033;
const FONT_SIZE_FRACTION = 0.6;
const BORDER_RADIUS_FRACTION = 0.15;
const HORIZONTAL_PADDING_FRACTION = 0.3;
const ARROW_SIZE_FRACTION = 0.4;

const VERTICAL_POSITION_THRESHOLD = 0.3;
const HORIZONTAL_POSITION_THRESHOLD = 0.5;

const VERTICAL_ARROW_MARGIN = 0.015;
const HORIZONTAL_ARROW_MARGIN = 0.03;
const ARROW_SIZE = 8;

type DimensionsBoxProps = {
device?: DeviceProperties;
Expand Down Expand Up @@ -41,8 +34,6 @@ function DimensionsBox({ device, frame, wrapperDivRef }: DimensionsBoxProps) {
return;
}

const { clientHeight: previewHeight } = previewDiv;

const boxPosition: DimensionsBoxPosition = (() => {
if (frame.y >= VERTICAL_POSITION_THRESHOLD) {
return "above";
Expand All @@ -60,45 +51,44 @@ function DimensionsBox({ device, frame, wrapperDivRef }: DimensionsBoxProps) {
switch (boxPosition) {
case "above":
return {
"--top": `${(frame.y - VERTICAL_ARROW_MARGIN) * 100}%`,
"--top": `${frame.y * 100}%`,
"--left": `${(frame.x + frame.width / 2) * 100}%`,
"--box-transform": "translate(-50%, -100%)",
"--margin": `-${ARROW_SIZE}px 0 0 0`,
};
case "below":
return {
"--top": `${(frame.y + frame.height + VERTICAL_ARROW_MARGIN) * 100}%`,
"--top": `${(frame.y + frame.height) * 100}%`,
"--left": `${(frame.x + frame.width / 2) * 100}%`,
"--box-transform": "translate(-50%, 0%)",
"--margin": `${ARROW_SIZE}px 0 0 0`,
};
case "right":
return {
"--top": `${(frame.y + frame.height / 2) * 100}%`,
"--left": `${(frame.x + frame.width + HORIZONTAL_ARROW_MARGIN) * 100}%`,
"--left": `${(frame.x + frame.width) * 100}%`,
"--box-transform": "translate(0%, -50%)",
"--margin": `0 0 0 ${ARROW_SIZE}px`,
};
case "left":
return {
"--top": `${(frame.y + frame.height / 2) * 100}%`,
"--left": `${(frame.x - HORIZONTAL_ARROW_MARGIN) * 100}%`,
"--left": `${frame.x * 100}%`,
"--box-transform": "translate(-100%, -50%)",
"--margin": `0 0 0 -${ARROW_SIZE}px`,
};
default:
return {
"--top": `${(frame.y + frame.height / 2) * 100}%`,
"--left": `${(frame.x + frame.width / 2) * 100}%`,
"--box-transform": "translate(-50%, -50%)",
"--margin": `0 0 0 0`,
};
}
})();

const boxHeight = previewHeight * BOX_HEIGHT_FRACTION;

const cssPropertiesForDimensionsBox = {
"--box-height": `${boxHeight}px`,
"--font-size": `${boxHeight * FONT_SIZE_FRACTION}px`,
"--border-radius": `${boxHeight * BORDER_RADIUS_FRACTION}px`,
"--horizontal-padding": `${boxHeight * HORIZONTAL_PADDING_FRACTION}px`,
"--arrow-size": `${boxHeight * ARROW_SIZE_FRACTION}px`,
"--arrow-size": `${ARROW_SIZE}px`,
...positionalProps,
};

Expand Down
Loading