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(box): css style properties passed as HTML attributes #7110

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 25 additions & 2 deletions src/components/box/box.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export const Box = React.forwardRef<HTMLDivElement, BoxProps>(
borderRadius,
color,
opacity,
height,
width,
"aria-hidden": ariaHidden,
...rest
}: BoxProps,
Expand All @@ -113,6 +115,28 @@ export const Box = React.forwardRef<HTMLDivElement, BoxProps>(
);
}

let actualWidth = "";
if (typeof width === "number") {
actualWidth = width <= 1 ? `${(width * 100).toFixed(0)}%` : `${width}px`;
} else if (typeof width === "string") {
actualWidth = width;
}

let actualHeight = "";
if (typeof height === "number") {
actualHeight =
height <= 1 ? `${(height * 100).toFixed(0)}%` : `${height}px`;
} else if (typeof height === "string") {
actualHeight = height;
}

const cssProps = {
color,
opacity,
width: actualWidth,
height: actualHeight,
};

return (
<StyledBox
as={as}
Expand All @@ -131,15 +155,14 @@ export const Box = React.forwardRef<HTMLDivElement, BoxProps>(
backgroundColor={backgroundColor}
boxShadow={boxShadow}
borderRadius={borderRadius}
color={color}
opacity={opacity}
aria-hidden={ariaHidden}
{...tagComponent(dataComponent, rest)}
{...filterStyledSystemMarginProps(rest)}
{...filterStyledSystemPaddingProps(rest)}
{...filterStyledSystemFlexboxProps(rest)}
{...filterStyledSystemGridProps(rest)}
{...filterStyledSystemLayoutProps(rest)}
cssProps={cssProps}
>
{children}
</StyledBox>
Expand Down
8 changes: 0 additions & 8 deletions src/components/box/box.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default color={color} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("color", color);
await expect(boxElement).toHaveCSS("color", rgbValue);
});
});
Expand All @@ -70,7 +69,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default color={rgbValue} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("color", rgbValue);
await expect(boxElement).toHaveCSS("color", rgbValue);
});
});
Expand All @@ -82,7 +80,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default color={hexValue} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("color", hexValue);
await expect(boxElement).toHaveCSS("color", rgbValue);
});
});
Expand Down Expand Up @@ -168,7 +165,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default bg="primary" width={percentage} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("width", String(percentage));
await assertCssValueIsApproximately(boxElement, "width", parseInt(width));
});
});
Expand All @@ -180,7 +176,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default bg="primary" width={number} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("width", String(number));
await assertCssValueIsApproximately(boxElement, "width", parseInt(width));
});
});
Expand All @@ -192,7 +187,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default bg="primary" width={width} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("width", width);
await assertCssValueIsApproximately(boxElement, "width", parseInt(width));
});
});
Expand All @@ -204,7 +198,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default bg="primary" height={number} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("height", String(number));
await assertCssValueIsApproximately(
boxElement,
"height",
Expand All @@ -220,7 +213,6 @@ test.describe("should render Box component", () => {
}) => {
await mount(<Default bg="primary" height={height} />);
const boxElement = await getDataElementByValue(page, "box");
await expect(boxElement).toHaveAttribute("height", height);
await assertCssValueIsApproximately(
boxElement,
"height",
Expand Down
33 changes: 24 additions & 9 deletions src/components/box/box.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ const calculatePosition = (props: Omit<PositionProps, "zIndex">) => {
};
};

const StyledBox = styled.div<BoxProps>`
const StyledBox = styled.div<
BoxProps & {
cssProps?: {
color?: string;
opacity?: string;
height?: string;
width?: string;
};
}
>`
${space}
${layout}
${flexbox}
Expand All @@ -35,26 +44,32 @@ const StyledBox = styled.div<BoxProps>`
css`
border-radius: var(--${borderRadius});
`}

${({ cssProps, bg, backgroundColor, ...rest }) =>
styledColor({ color: cssProps?.color, bg, backgroundColor, ...rest })}

${({ color, bg, backgroundColor, ...rest }) =>
styledColor({ color, bg, backgroundColor, ...rest })}
${({ cssProps }) => css`
opacity: ${cssProps?.opacity};
`}

${({ overflowWrap }) =>
overflowWrap &&
css`
overflow-wrap: ${overflowWrap};
`}

${({ height }) =>
height &&
${({ cssProps, size }) =>
cssProps?.height &&
!size &&
css`
height: ${height};
height: ${cssProps?.height};
`}

${({ width }) =>
width &&
${({ cssProps, size }) =>
cssProps?.width &&
!size &&
css`
width: ${width};
width: ${cssProps?.width};
`}

${({ scrollVariant }) =>
Expand Down
40 changes: 40 additions & 0 deletions src/components/box/box.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ it("applies the boxShadow styling correctly when a design token is passed in", (
expect(box).toHaveStyle(`box-shadow: var(--boxShadow100)`);
});

it("applies the correct styling from the cssProps", () => {
render(
<Box
width="100px"
height="100px"
opacity="25%"
color="yellow"
data-role="box"
/>,
);

const box = screen.getByTestId("box");
expect(box).toHaveStyle({
width: "100px",
height: "100px",
opacity: "25%",
color: "yellow",
});
});

it("applies the correct styling from the cssProps, when the width and height are numeric", () => {
render(<Box width={100} height={100} data-role="box" />);

const box = screen.getByTestId("box");
expect(box).toHaveStyle({
width: "100px",
height: "100px",
});
});

it("applies the correct styling from the cssProps, when the width and height are percentage", () => {
render(<Box width={0.5} height={0.5} data-role="box" />);

const box = screen.getByTestId("box");
expect(box).toHaveStyle({
width: "50%",
height: "50%",
});
});

test("logs a deprecation warning when the `tabIndex` prop is passed with a value", () => {
const loggerSpy = jest
.spyOn(Logger, "deprecate")
Expand Down
5 changes: 0 additions & 5 deletions src/components/dismissible-box/dismissible-box.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ test.describe("DismissibleBox component", () => {
}) => {
await mount(<DefaultDismissibleBox width={`${width}px`} />);

await expect(dismissibleBoxDataComponent(page)).toHaveAttribute(
"width",
`${width}px`,
);

await assertCssValueIsApproximately(
dismissibleBoxDataComponent(page),
"width",
Expand Down
Loading