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: React 19 bool prop to attr value bug #1026

Merged
merged 4 commits into from
Dec 9, 2024
Merged
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
17 changes: 14 additions & 3 deletions packages/mux-player-react/src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import React from 'react';

// React 19 supports custom elements and setting properties directly on them,
// older React versions converted all props to attributes on custom elments.
// Boolean `true` values should not be converted to empty strings in React 19+
// because that would result in a `false` value if it was set via a property.
// React 19+ handles primitive values correctly but we still need to convert
// the camelCase prop names to kebab-case attribute names for mux-player. (WL)

const IS_REACT_19_OR_NEWER = parseInt(React.version) >= 19;

// NOTE: As a forward-looking implementation, we may want to assume
// prop names -> attribute names is always a simple name.toLowerCase()
// and provide a mechanism for passing in per-component overrides for
Expand Down Expand Up @@ -27,16 +38,16 @@ export const isKeyOf = <T extends object = any>(k: KeyTypes, o: Maybe<T>): k is
const toKebabCase = (string: string) => string.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);

export const toNativeAttrName = (propName: string, propValue: any): string | undefined => {
if (typeof propValue === 'boolean' && !propValue) return undefined;
if (!IS_REACT_19_OR_NEWER && typeof propValue === 'boolean' && !propValue) return undefined;
if (isKeyOf(propName, ReactPropToAttrNameMap)) return ReactPropToAttrNameMap[propName];
if (typeof propValue == 'undefined') return undefined;
if (typeof propValue === 'undefined') return undefined;
if (/[A-Z]/.test(propName)) return toKebabCase(propName);
return propName;
};
export const toStyleAttr = <T>(x: T) => x;

export const toNativeAttrValue = (propValue: any, _propName: string) => {
if (typeof propValue === 'boolean') return '';
if (!IS_REACT_19_OR_NEWER && typeof propValue === 'boolean') return '';
return propValue;
};

Expand Down
Loading