A function to check whether a prop is valid for HTML and SVG elements
npx @tessl/cli install tessl/npm-emotion--is-prop-valid@1.3.0@emotion/is-prop-valid is a high-performance TypeScript utility function that determines whether a given prop name is valid for HTML and SVG elements. It uses memoization for optimal performance and includes comprehensive validation patterns for React props, HTML attributes, SVG properties, and event handlers.
npm install @emotion/is-prop-validimport isPropValid from "@emotion/is-prop-valid";For CommonJS:
const isPropValid = require("@emotion/is-prop-valid");import isPropValid from "@emotion/is-prop-valid";
// Valid HTML attributes
isPropValid("className"); // true
isPropValid("href"); // true
isPropValid("style"); // true
// Valid event handlers
isPropValid("onClick"); // true
isPropValid("onSubmit"); // true
isPropValid("onMouseOver"); // true
// Valid data and aria attributes
isPropValid("data-testid"); // true
isPropValid("aria-label"); // true
// Valid SVG attributes
isPropValid("viewBox"); // true
isPropValid("strokeWidth"); // true
// Invalid custom props
isPropValid("customProp"); // false
isPropValid("mySpecialAttribute"); // falseValidates whether a prop name is acceptable for HTML and SVG elements, preventing invalid attributes from being passed to DOM elements.
declare function isPropValid(prop: string): boolean;
export default isPropValid;The function validates props against two main criteria:
HTML/SVG Attributes: Tests against a comprehensive regex pattern that includes:
children, dangerouslySetInnerHTML, key, refclassName, href, style, id, title, etc.viewBox, fill, stroke, d, cx, cy, etc.data-*, aria-*, and x-* prefixed attributesEvent Handlers: Direct character code check for props starting with "on" followed by an uppercase letter
The function uses memoization for performance optimization.
Common Usage Patterns:
import isPropValid from "@emotion/is-prop-valid";
import styled from "@emotion/styled";
// Most common usage: shouldForwardProp in styled components
const StyledDiv = styled.div.withConfig({
shouldForwardProp: (prop) => isPropValid(prop),
})`
color: ${props => props.color};
`;
// Filter out custom props before passing to DOM
function filterValidProps(props: Record<string, any>) {
return Object.keys(props).reduce((validProps, key) => {
if (isPropValid(key)) {
validProps[key] = props[key];
}
return validProps;
}, {} as Record<string, any>);
}
// Usage with conditional prop forwarding
const ConditionalForwardProp = styled('div', {
shouldForwardProp: prop => prop !== 'customProp' && isPropValid(prop)
})`
background: blue;
`;The validation covers all major categories of valid HTML and SVG properties:
React Props:
children, dangerouslySetInnerHTML, key, refdefaultValue, defaultChecked, innerHTMLsuppressContentEditableWarning, suppressHydrationWarningautoFocusvalueLinkHTML Attributes:
className, id, style, title, lang, dir, roleaction, method, encType, target, formAction, formNoValidatesrc, href, alt, width, height, controls, poster, preloaddraggable, contentEditable, tabIndex, hidden, disablednonce, integrity, crossOriginloading, fetchPriority, enterKeyHint, inputModeSVG Properties:
x, y, width, height, cx, cy, r, dfill, stroke, strokeWidth, opacity, transformfontSize, fontFamily, textAnchor, dominantBaselinefilter, clipPath, mask, gradientTransformEvent Handlers: All standard DOM events starting with "on" followed by uppercase letter:
onMouseDown, onMouseUp, onMouseOver, onMouseOutonKeyDown, onKeyUp, onKeyPressonSubmit, onChange, onFocus, onBluronTouchStart, onTouchMove, onTouchEndData/Aria Attributes:
data-* (e.g., data-testid, data-value)aria-* (e.g., aria-label, aria-hidden)x-* (e.g., x-custom-attribute)Additional Supported Attributes:
for, class, autofocusabout, datatype, property, resource, typeofitemProp, itemScope, itemType, itemID, itemRef