Skip to content

Commit 5fb84e7

Browse files
committed
Match Preact behavior for boolean props on custom elements
1 parent 6e86525 commit 5fb84e7

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

packages/react-dom/src/client/DOMPropertyOperations.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export function getValueForAttribute(
115115
node: Element,
116116
name: string,
117117
expected: mixed,
118+
isCustomComponentTag: boolean,
118119
): mixed {
119120
if (__DEV__) {
120121
if (!isAttributeNameSafe(name)) {
@@ -124,6 +125,13 @@ export function getValueForAttribute(
124125
return expected === undefined ? undefined : null;
125126
}
126127
const value = node.getAttribute(name);
128+
129+
if (enableCustomElementPropertySupport) {
130+
if (isCustomComponentTag && value === '') {
131+
return true;
132+
}
133+
}
134+
127135
if (__DEV__) {
128136
checkAttributeStringCoercion(expected, name);
129137
}
@@ -196,6 +204,11 @@ export function setValueForProperty(
196204
if (shouldRemoveAttribute(name, value, propertyInfo, isCustomComponentTag)) {
197205
value = null;
198206
}
207+
if (enableCustomElementPropertySupport) {
208+
if (isCustomComponentTag && value === true) {
209+
value = '';
210+
}
211+
}
199212

200213
// If the prop isn't in the special list, treat it as a simple attribute.
201214
if (isCustomComponentTag || propertyInfo === null) {

packages/react-dom/src/client/ReactDOMComponent.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,12 @@ export function diffHydratedProperties(
10811081
} else if (isCustomComponentTag && !enableCustomElementPropertySupport) {
10821082
// $FlowFixMe - Should be inferred as not undefined.
10831083
extraAttributeNames.delete(propKey.toLowerCase());
1084-
serverValue = getValueForAttribute(domElement, propKey, nextProp);
1084+
serverValue = getValueForAttribute(
1085+
domElement,
1086+
propKey,
1087+
nextProp,
1088+
isCustomComponentTag,
1089+
);
10851090

10861091
if (nextProp !== serverValue) {
10871092
warnForPropDifference(propKey, serverValue, nextProp);
@@ -1128,7 +1133,12 @@ export function diffHydratedProperties(
11281133
// $FlowFixMe - Should be inferred as not undefined.
11291134
extraAttributeNames.delete(propKey);
11301135
}
1131-
serverValue = getValueForAttribute(domElement, propKey, nextProp);
1136+
serverValue = getValueForAttribute(
1137+
domElement,
1138+
propKey,
1139+
nextProp,
1140+
isCustomComponentTag,
1141+
);
11321142
}
11331143

11341144
const dontWarnCustomElement =

packages/react-dom/src/server/ReactDOMServerFormatConfig.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ function pushStartCustomElement(
11561156
let innerHTML = null;
11571157
for (let propKey in props) {
11581158
if (hasOwnProperty.call(props, propKey)) {
1159-
const propValue = props[propKey];
1159+
let propValue = props[propKey];
11601160
if (propValue == null) {
11611161
continue;
11621162
}
@@ -1169,6 +1169,12 @@ function pushStartCustomElement(
11691169
// so skip it.
11701170
continue;
11711171
}
1172+
if (enableCustomElementPropertySupport && propValue === false) {
1173+
continue;
1174+
}
1175+
if (enableCustomElementPropertySupport && propValue === true) {
1176+
propValue = '';
1177+
}
11721178
if (enableCustomElementPropertySupport && propKey === 'className') {
11731179
// className gets rendered as class on the client, so it should be
11741180
// rendered as class on the server.

packages/react-dom/src/shared/DOMProperty.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ export function shouldRemoveAttribute(
162162
return true;
163163
}
164164
if (isCustomComponentTag) {
165+
if (enableCustomElementPropertySupport) {
166+
if (value === false) {
167+
return true;
168+
}
169+
}
165170
return false;
166171
}
167172
if (propertyInfo !== null) {

0 commit comments

Comments
 (0)