Skip to content

Commit 52240a6

Browse files
committed
[Suspense] Use style.setProperty to set display
Follow up to #15861. Turns out you can't set `!important` using a normal property assignment. You have to use `style.setProperty`. Maybe Andrew *should* just learn CSS. IE9 doesn't support `style.setProperty` so we'll fall back to setting `display: none` without `important`, like we did before #15861 Our advice for apps that need to support IE9 will be to avoid using `!important`. Which seems like good advice in general, but IANACSSE. Tested on FB and using our Suspense DOM fixture.
1 parent e91dd70 commit 52240a6

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,25 @@ describe('ReactDOMSuspensePlaceholder', () => {
8383
<div ref={divs[1]}>
8484
<AsyncText ms={500} text="B" />
8585
</div>
86-
<div style={{display: 'block'}} ref={divs[2]}>
86+
<div style={{display: 'span'}} ref={divs[2]}>
8787
<Text text="C" />
8888
</div>
8989
</Suspense>
9090
);
9191
}
9292
ReactDOM.render(<App />, container);
93-
expect(divs[0].current.style.display).toEqual('none !important');
94-
expect(divs[1].current.style.display).toEqual('none !important');
95-
expect(divs[2].current.style.display).toEqual('none !important');
93+
expect(window.getComputedStyle(divs[0].current).display).toEqual('none');
94+
expect(window.getComputedStyle(divs[1].current).display).toEqual('none');
95+
expect(window.getComputedStyle(divs[2].current).display).toEqual('none');
9696

9797
await advanceTimers(500);
9898

9999
Scheduler.flushAll();
100100

101-
expect(divs[0].current.style.display).toEqual('');
102-
expect(divs[1].current.style.display).toEqual('');
101+
expect(window.getComputedStyle(divs[0].current).display).toEqual('block');
102+
expect(window.getComputedStyle(divs[1].current).display).toEqual('block');
103103
// This div's display was set with a prop.
104-
expect(divs[2].current.style.display).toEqual('block');
104+
expect(window.getComputedStyle(divs[2].current).display).toEqual('span');
105105
});
106106

107107
it('hides and unhides timed out text nodes', async () => {
@@ -156,14 +156,14 @@ describe('ReactDOMSuspensePlaceholder', () => {
156156
ReactDOM.render(<App />, container);
157157
});
158158
expect(container.innerHTML).toEqual(
159-
'<span style="display: none !important;">Sibling</span><span style=' +
160-
'"display: none !important;"></span>Loading...',
159+
'<span style="display: none;">Sibling</span><span style=' +
160+
'"display: none;"></span>Loading...',
161161
);
162162

163163
act(() => setIsVisible(true));
164164
expect(container.innerHTML).toEqual(
165-
'<span style="display: none !important;">Sibling</span><span style=' +
166-
'"display: none !important;"></span>Loading...',
165+
'<span style="display: none;">Sibling</span><span style=' +
166+
'"display: none;"></span>Loading...',
167167
);
168168

169169
await advanceTimers(500);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,12 @@ export function hideInstance(instance: Instance): void {
589589
// TODO: Does this work for all element types? What about MathML? Should we
590590
// pass host context to this method?
591591
instance = ((instance: any): HTMLElement);
592-
instance.style.display = 'none !important';
592+
const style = instance.style;
593+
if (typeof style.setProperty === 'function') {
594+
style.setProperty('display', 'none', 'important');
595+
} else {
596+
style.display = 'none';
597+
}
593598
}
594599

595600
export function hideTextInstance(textInstance: TextInstance): void {

packages/react-fresh/src/__tests__/ReactFresh-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ describe('ReactFresh', () => {
13591359
const fallbackChild = container.childNodes[1];
13601360
expect(primaryChild.textContent).toBe('Content 1');
13611361
expect(primaryChild.style.color).toBe('green');
1362-
expect(primaryChild.style.display).toBe('none !important');
1362+
expect(primaryChild.style.display).toBe('none');
13631363
expect(fallbackChild.textContent).toBe('Fallback 0');
13641364
expect(fallbackChild.style.color).toBe('green');
13651365
expect(fallbackChild.style.display).toBe('');
@@ -1373,7 +1373,7 @@ describe('ReactFresh', () => {
13731373
expect(container.childNodes[1]).toBe(fallbackChild);
13741374
expect(primaryChild.textContent).toBe('Content 1');
13751375
expect(primaryChild.style.color).toBe('green');
1376-
expect(primaryChild.style.display).toBe('none !important');
1376+
expect(primaryChild.style.display).toBe('none');
13771377
expect(fallbackChild.textContent).toBe('Fallback 1');
13781378
expect(fallbackChild.style.color).toBe('green');
13791379
expect(fallbackChild.style.display).toBe('');
@@ -1397,7 +1397,7 @@ describe('ReactFresh', () => {
13971397
expect(container.childNodes[1]).toBe(fallbackChild);
13981398
expect(primaryChild.textContent).toBe('Content 1');
13991399
expect(primaryChild.style.color).toBe('red');
1400-
expect(primaryChild.style.display).toBe('none !important');
1400+
expect(primaryChild.style.display).toBe('none');
14011401
expect(fallbackChild.textContent).toBe('Fallback 1');
14021402
expect(fallbackChild.style.color).toBe('red');
14031403
expect(fallbackChild.style.display).toBe('');

0 commit comments

Comments
 (0)