Case Converter for React Inline Styles: Turning font-size into fontSize Without the Silent Failures
Last sprint I inherited a dashboard card that had to carry its theme inline. The designer handed me a 46-line CSS block, and the card renders with React's style prop because the accent color changes per workspace. React wants camelCase. CSS wants kebab-case. Somewhere between the two, I typed fontsize instead of fontSize, shipped it, and got a card with no text sizing at all. No exception, no red squiggle. Just a wrong-looking UI and a support ticket.
Here's the conversion pipeline I should have used on day one, plus the three traps that cost me an afternoon.
The problem
The block from the design handoff looked like this:
.panel { font-size: 1.25rem; line-height: 1.6; background-color: var(--surface); -webkit-user-select: none; --panel-accent: #0a84ff; } Three different rules apply at once. Regular properties convert font-size to fontSize. Vendor prefixes like -webkit-user-select need a capital first letter after conversion. Custom properties (--panel-accent) must keep their dashes, because that dash is part of how CSS recognizes them. Hand-editing 46 properties is how typos happen, and a typo in a style object fails silently.
Method 1: a small Python script
Python 3.11.5, standard library only. Paste your property names, get a style object body back.
# kebab_to_camel.py - Python 3.11.5 import re def css_to_camel(prop: str) -> str: prop = prop.strip() if prop.startswith("--"): # custom property: keep as-is return prop return re.sub(r"-(\w)", lambda m: m.group(1).upper(), prop) props = """font-size line-height background-color -webkit-user-select --panel-accent""" for name in props.splitlines(): key = css_to_camel(name) print(f' {key}: "...",') Output:
fontSize: "...", lineHeight: "...", backgroundColor: "...", WebkitUserSelect: "...", --panel-accent: "...", The regex -(\w) capitalizes the letter after each hyphen, which handles nested hyphens like border-top-left-radius in one pass. The custom-property guard runs first so --panel-accent survives untouched.
Method 2: one pass in Node
Same idea in Node 22.13.0, but this version takes a whole declaration block instead of a bare name list, which is what you usually have after copying out of the stylesheet:
// cssblock_to_style.mjs - Node 22.13.0 const toCamel = (p) => p.startsWith("--") ? p : p.replace(/-(\w)/g, (_, c) => c.toUpperCase()); const block = `font-size: 1.25rem; line-height: 1.6; -webkit-user-select: none; --panel-accent: #0a84ff;`; const style = {}; for (const decl of block.split(";")) { const i = decl.indexOf(":"); if (i === -1) continue; // blank / stray fragment const raw = decl.slice(0, i).trim(); style[toCamel(raw)] = decl.slice(i + 1).trim(); } console.log(JSON.stringify(style, null, 2)); Output:
{ "fontSize": "1.25rem", "lineHeight": "1.6", "WebkitUserSelect": "none", "--panel-accent": "#0a84ff" } Splitting on ; and taking the first : keeps values intact, including URLs like url(https://…) where later colons belong to the value, not the property name.
Method 3: the browser-based shortcut
For a one-off check, I don't open an editor at all. When I need to confirm that -webkit-background-clip becomes WebkitBackgroundClip and not webkitBackgroundClip, I paste the name into a free online case converter and read the camelCase line off the results panel. No project, no dependencies, nothing leaves the tab. It also handles the reverse direction, which comes up when I'm generating class names from a TypeScript interface and want kebab-case again. The same page works fine for batching a whole list of property names before I paste them into the script above, and since this browser-based tool runs entirely client-side, it's safe for internal property names from code I can't ship anywhere public.
Three mistakes I actually made
1. Deleting the hyphen instead of capitalizing
My first attempt used a replace that stripped the dash but forgot the capital: prop.replace("-", ""). That turns font-size into fontsize. As raw DOM, el.style.fontsize = "1.25rem" runs without error and changes nothing, because fontsize isn't a CSS property. In a dev-mode React app you at least get a warning in the console:
Warning: Unsupported style property fontsize. Did you mean fontSize? Production builds drop that warning, which is exactly how the card reached the ticket queue. The rule: the hyphen is a signal, not garbage. Every removed hyphen must raise the next letter.
2. Vendor prefixes: React wants WebkitTransform, not webkitTransform
A naive -(\w) conversion applied to the whole name turns -webkit-transform into webkitTransform. React rejects that spelling for style objects and tells you so:
Warning: Unsupported vendor-prefixed style property webkitTransform. Did you mean WebkitTransform? The casing rule is on React's DOM components reference: every vendor prefix except ms starts with a capital letter, so it's WebkitTransform and MozTransform but msTransform. The -(\w) regex gets this right only because it capitalizes the w that follows the first hyphen. If you split on hyphens and lowercase the remainder, you'll break it.
3. Custom properties must keep their dashes
I once "helpfully" converted --panel-accent to panelAccent and set it in a style object. Nothing errored. The variable just never existed, so every var(--panel-accent, #333) downstream fell back to its fallback color, and the theme looked subtly wrong on three screens. Checking the element confirmed it:
getComputedStyle(el).getPropertyValue("--panel-accent") "" The fix is one startsWith("--") check before converting. React has supported literal custom-property keys like '--panel-accent' in style objects since version 16, and the MDN custom properties guide explains why the two leading dashes are part of the name itself. A one-line guard beats a whole afternoon of theme debugging.
Cheat sheet
| Input style | Correct output | Wrong output | What happens when it's wrong |
|---|---|---|---|
font-size | fontSize | fontsize | No effect; React dev warning only |
-webkit-transform | WebkitTransform | webkitTransform | React warning; prefix dropped in prod |
--panel-accent | '--panel-accent' (unchanged) | panelAccent | Silent fail; var() falls back |
border-top-left-radius | borderTopLeftRadius | borderTopLeftradius | Corner radius ignored |
The pattern to remember: capitalize after every hyphen, keep the first letter lowercase, leave anything starting with two hyphens completely alone. And when a name looks odd, the case converter at utilvy settles it in two seconds.
More free browser-based utilities at utilvy.com.