-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better conversion from html attributes to react props (#130)
* Disable react/prop-types lint rule * Flesh out conversion from html attributes to react props * Properly merge decorations/props with outputspecs * Use CSSOM to parse style attributes * Re-enable react/prop-types rule
- Loading branch information
1 parent
c32cc45
commit ff0afd1
Showing
5 changed files
with
258 additions
and
68 deletions.
There are no files selected for viewing
34 changes: 17 additions & 17 deletions
34
docs/assets/index-e2175326.js → docs/assets/index-3279a106.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
import cx from "classnames"; | ||
import { HTMLProps } from "react"; | ||
|
||
export function kebabCaseToCamelCase(str: string) { | ||
return str.replaceAll(/-[a-z]/g, (g) => g[1]?.toUpperCase() ?? ""); | ||
} | ||
|
||
/** | ||
* Converts a CSS style string to an object | ||
* that can be passed to a React component's | ||
* `style` prop. | ||
*/ | ||
export function cssToStyles(css: string) { | ||
const stylesheet = new CSSStyleSheet(); | ||
stylesheet.insertRule(`* { ${css} }`); | ||
|
||
const insertedRule = stylesheet.cssRules[0] as CSSStyleRule; | ||
const declaration = insertedRule.style; | ||
const styles: Record<string, string> = {}; | ||
|
||
for (let i = 0; i < declaration.length; i++) { | ||
const property = declaration.item(i); | ||
const value = declaration.getPropertyValue(property); | ||
const camelCasePropertyName = kebabCaseToCamelCase(property); | ||
styles[camelCasePropertyName] = value; | ||
} | ||
|
||
return styles; | ||
} | ||
|
||
/** | ||
* Merges two sets of React props. Class names | ||
* will be concatenated and style objects | ||
* will be merged. | ||
*/ | ||
export function mergeReactProps( | ||
a: HTMLProps<HTMLElement>, | ||
b: HTMLProps<HTMLElement> | ||
) { | ||
return { | ||
...a, | ||
...b, | ||
className: cx(a.className, b.className), | ||
style: { | ||
...a.style, | ||
...b.style, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Given a record of HTML attributes, returns tho | ||
* equivalent React props. | ||
*/ | ||
export function htmlAttrsToReactProps( | ||
attrs: Record<string, string> | ||
): HTMLProps<HTMLElement> { | ||
const props: Record<string, unknown> = {}; | ||
for (const [attrName, attrValue] of Object.entries(attrs)) { | ||
switch (attrName.toLowerCase()) { | ||
case "class": { | ||
props.className = attrValue; | ||
break; | ||
} | ||
case "style": { | ||
props.style = cssToStyles(attrValue); | ||
break; | ||
} | ||
case "autocapitalize": { | ||
props.autoCapitalize = attrValue; | ||
break; | ||
} | ||
case "contenteditable": { | ||
props.contentEditable = attrValue != null; | ||
break; | ||
} | ||
case "draggable": { | ||
props.draggable = attrValue != null; | ||
break; | ||
} | ||
case "enterkeyhint": { | ||
props.enterKeyHint = attrValue; | ||
break; | ||
} | ||
case "for": { | ||
props.htmlFor = attrValue; | ||
break; | ||
} | ||
case "hidden": { | ||
props.hidden = attrValue; | ||
break; | ||
} | ||
case "inputmode": { | ||
props.inputMode = attrValue; | ||
break; | ||
} | ||
case "itemprop": { | ||
props.itemProp = attrValue; | ||
break; | ||
} | ||
case "spellcheck": { | ||
if (attrValue === "" || attrValue === "true") { | ||
props.spellCheck = true; | ||
break; | ||
} | ||
if (attrValue === "false") { | ||
props.spellCheck = false; | ||
} | ||
props.spellCheck = null; | ||
break; | ||
} | ||
case "tabindex": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.tabIndex = numValue; | ||
} | ||
break; | ||
} | ||
case "autocomplete": { | ||
props.autoComplete = attrValue; | ||
break; | ||
} | ||
case "autofocus": { | ||
props.autoFocus = attrValue != null; | ||
break; | ||
} | ||
case "formaction": { | ||
props.formAction = attrValue; | ||
break; | ||
} | ||
case "formenctype": { | ||
props.formEnctype = attrValue; | ||
break; | ||
} | ||
case "formmethod": { | ||
props.formMethod = attrValue; | ||
break; | ||
} | ||
case "formnovalidate": { | ||
props.formNoValidate = attrValue; | ||
break; | ||
} | ||
case "formtarget": { | ||
props.formTarget = attrValue; | ||
break; | ||
} | ||
case "maxlength": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.maxLength = attrValue; | ||
} | ||
break; | ||
} | ||
case "minlength": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.minLength = attrValue; | ||
} | ||
break; | ||
} | ||
case "max": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.max = attrValue; | ||
} | ||
break; | ||
} | ||
case "min": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.min = attrValue; | ||
} | ||
break; | ||
} | ||
case "multiple": { | ||
props.multiple = attrValue != null; | ||
break; | ||
} | ||
case "readonly": { | ||
props.readOnly = attrValue != null; | ||
break; | ||
} | ||
case "required": { | ||
props.required = attrValue != null; | ||
break; | ||
} | ||
case "size": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.size = attrValue; | ||
} | ||
break; | ||
} | ||
case "step": { | ||
if (attrValue === "any") { | ||
props.step = attrValue; | ||
break; | ||
} | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue) && numValue > 0) { | ||
props.step = attrValue; | ||
} | ||
break; | ||
} | ||
case "disabled": { | ||
props.disabled = attrValue != null; | ||
break; | ||
} | ||
case "rows": { | ||
const numValue = parseInt(attrValue, 10); | ||
if (!Number.isNaN(numValue)) { | ||
props.rows = attrValue; | ||
} | ||
break; | ||
} | ||
default: { | ||
props[attrName] = attrValue; | ||
break; | ||
} | ||
} | ||
} | ||
return props; | ||
} |