Skip to content

Commit 8d0a4d1

Browse files
committed
Update label borders and rotation (#119)
* Fixed widget rotation * Updated border styling to match phoebus and added rounded corners * Corrected offsets for 90 and -90 deg rotations * Generalise use of themes (#117) * Removed references to diamondTheme and implemented useTheme hook * Updated snapshots * Added theme prop to embeddedDisplay widget * Renamed diamondTheme to phoebusTheme * Renamed exported theme to phoebusTheme * Use phoebusTheme as default instead of the MUI default * Updated snapshots
1 parent 4c15a47 commit 8d0a4d1

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

src/ui/widgets/Label/__snapshots__/label.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports[`<Label /> > it handles transparent prop`] = `
1313
exports[`<Label /> > it handles transparent prop 1`] = `
1414
<DocumentFragment>
1515
<p
16-
class="MuiTypography-root MuiTypography-body1 css-qckjhq-MuiTypography-root"
16+
class="MuiTypography-root MuiTypography-body1 css-1y700xl-MuiTypography-root"
1717
>
1818
hello
1919
</p>
@@ -23,7 +23,7 @@ exports[`<Label /> > it handles transparent prop 1`] = `
2323
exports[`<Label /> > it matches the snapshot 1`] = `
2424
<DocumentFragment>
2525
<p
26-
class="MuiTypography-root MuiTypography-body1 css-qckjhq-MuiTypography-root"
26+
class="MuiTypography-root MuiTypography-body1 css-1y700xl-MuiTypography-root"
2727
>
2828
hello
2929
</p>

src/ui/widgets/Label/label.tsx

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
MacrosPropOpt
1515
} from "../propTypes";
1616
import { Typography as MuiTypography, styled, useTheme } from "@mui/material";
17+
import { WIDGET_DEFAULT_SIZES } from "../EmbeddedDisplay/bobParser";
1718

1819
const LabelProps = {
1920
macros: MacrosPropOpt,
@@ -28,7 +29,9 @@ const LabelProps = {
2829
backgroundColor: ColorPropOpt,
2930
border: BorderPropOpt,
3031
rotationStep: FloatPropOpt,
31-
wrapWords: BoolPropOpt
32+
wrapWords: BoolPropOpt,
33+
height: FloatPropOpt,
34+
width: FloatPropOpt
3235
};
3336

3437
const LabelWidgetProps = {
@@ -37,10 +40,11 @@ const LabelWidgetProps = {
3740
};
3841

3942
const Typography = styled(MuiTypography)({
40-
width: "100%",
41-
height: "100%",
4243
display: "flex",
4344
overflow: "hidden",
45+
borderRadius: "4px",
46+
borderWidth: "0px",
47+
lineHeight: 1,
4448
padding: 0
4549
});
4650

@@ -56,13 +60,35 @@ export const LabelComponent = (
5660
textAlignV = "top",
5761
text = "",
5862
rotationStep = 0,
59-
wrapWords = true
63+
wrapWords = true,
64+
height = WIDGET_DEFAULT_SIZES["label"][1],
65+
width = WIDGET_DEFAULT_SIZES["label"][0]
6066
} = props;
6167
const backgroundColor = transparent
6268
? "transparent"
6369
: (props.backgroundColor?.toString() ?? theme.palette.primary.main);
6470
const font = props.font?.css() ?? theme.typography;
65-
const border = props.border?.css() ?? "0px solid #000000";
71+
const borderWidth = props.border?.css().borderWidth ?? "0px";
72+
const borderColor = props.border?.css().borderColor ?? "#000000";
73+
const borderStyle = props.border?.css().borderStyle ?? "solid";
74+
75+
const inputWidth = rotationStep === 0 || rotationStep === 2 ? width : height;
76+
const inputHeight = rotationStep === 0 || rotationStep === 2 ? height : width;
77+
78+
const offset = width / 2 - height / 2;
79+
const transform = (function () {
80+
switch (rotationStep) {
81+
case 0: // 0 degrees
82+
case 2: // 180 degrees
83+
return `rotate(${rotationStep * -90}deg)`;
84+
case 1: // 90 degrees
85+
return `rotate(${rotationStep * -90}deg) translateY(${offset}px) translateX(${offset}px)`;
86+
case 3: // -90 degrees
87+
return `rotate(${rotationStep * -90}deg) translateY(${-offset}px) translateX(${-offset}px)`;
88+
default: // Unreachable
89+
return "";
90+
}
91+
})();
6692

6793
// Since display is "flex", use "flex-start" and "flex-end" to align
6894
// the content.
@@ -87,14 +113,18 @@ export const LabelComponent = (
87113
sx={{
88114
justifyContent: alignment,
89115
alignItems: alignmentV,
116+
height: inputHeight,
117+
width: inputWidth,
90118
textAlign: textAlign,
91119
wordBreak: wrapWords ? "break-word" : null,
92120
whiteSpace: wrapWords ? "break-spaces" : null,
93121
color: foregroundColor.toString(),
94122
backgroundColor: backgroundColor,
95123
fontFamily: font,
96-
transform: `rotate(${rotationStep * -90}deg)`.toString(),
97-
border: border
124+
transform: transform.toString(),
125+
outlineWidth: borderWidth,
126+
outlineColor: borderColor,
127+
outlineStyle: borderStyle
98128
}}
99129
>
100130
{text}

src/ui/widgets/Symbol/__snapshots__/symbol.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ exports[`<Symbol /> from .opi file > matches snapshot 1`] = `
8383
style="padding: 5%;"
8484
>
8585
<p
86-
class="MuiTypography-root MuiTypography-body1 css-qckjhq-MuiTypography-root"
86+
class="MuiTypography-root MuiTypography-body1 css-1y700xl-MuiTypography-root"
8787
>
8888
Fake value
8989
</p>

0 commit comments

Comments
 (0)