Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the correct syntax for BoxSizing in a React CSS Object

I’m trying to give style to a prop in React but I don’t know to correct way to write it :

<PhoneInput
   inputStyle={!(window.innerWidth <= 768) ? {...InputDivStyle, ...PhoneInputStyle, textIndent: "96px"} : {...InputDivStyle, ...PhoneInputStyle, textIndent: "32px"}}
</PhoneInput>

inputStyle gives me the error :

Types of property 'boxSizing' are incompatible. Type 'string' is not assignable to type 'BoxSizing | undefined'.ts(2322)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export const PhoneInputStyle = {
  fontSize: "clamp(13px, 1.111vw, 16px)",
  lineHeight: "clamp(15px, 1.319vw, 19px)",
  position: "relative",
  width: "100%",
  height: "51px",
  cursor: "pointer",
  display: "flex",
  flexDirection: "row",
  alignItems: "center",
  padding: "8px 16px",
  border: `1px solid black`,
  boxSizing: `border-box`, //This ain't right, I tried "border-box" and it didn't work either
  borderRadius: "10px",
  outline: "none",
  gridRowStart: "1",
  gridColumnStart: "1",
}

I’m pretty sure it’s only an error of syntax but I can’t find the correct way to write boxSizing.

>Solution :

export const PhoneInputStyle = {
  // ...
  boxSizing: `border-box`,
  // ...
}

Since you don’t have an explicit type on this object, typescript will create a type automatically. It sees that boxSizing is a string, so it gives boxSizing the type string. Unfortunately, this is too broad for what you end up doing. box sizing can’t be any string, but instead can only be very specific strings.

I would recommend that you give this object an explicit type of CSSProperties. This type will, among other things, restrict boxSizing to its legal values:

import { CSSProperties } from "react";

export const PhoneInputStyle: CSSProperties = {
  // ...
  boxSizing: `border-box`,
  // ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading