Skip to content

Commit 44d23f7

Browse files
committed
Optimised generation of head styles to remove React key warning
1 parent 471c52f commit 44d23f7

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

react-spaces/src/HeadStyles.tsx

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,50 @@ export const HeadStyles : React.FC<{ spaces: ISpace[] }> = (props) => {
88

99
if (spaces.length > 0)
1010
{
11-
return ReactDOM.createPortal(
12-
<style>
13-
{
14-
spaces.map(space => {
15-
const style = {
16-
left: (space.left !== undefined ? cssValue(space.left, space.adjustedLeft) : undefined),
17-
top: (space.top !== undefined ? cssValue(space.top, space.adjustedTop) : undefined),
18-
right: (space.right !== undefined ? cssValue(space.right, space.adjustedLeft) : undefined),
19-
bottom: (space.bottom !== undefined ? cssValue(space.bottom, space.adjustedTop) : undefined),
20-
width: isHorizontalSpace(space.anchorType) ? cssValue(space.anchorSize, space.adjustedSize) : space.width,
21-
height: isVerticalSpace(space.anchorType) ? cssValue(space.anchorSize, space.adjustedSize) : space.height,
22-
zIndex: space.zIndex
23-
};
24-
return (
25-
<>{ `#${space.id} {` } { style.left ? `left: ${style.left};` : "" } { style.top ? `top: ${style.top};` : "" } { style.right ? `right: ${style.right};` : "" } { style.bottom ? `bottom: ${style.bottom};` : "" } { style.width ? `width: ${style.width};` : "" } { style.height ? `height: ${style.height};` : "" } { style.zIndex ? `z-index: ${style.zIndex};` : "" } { `}`}</>
26-
)
27-
})
28-
}
29-
</style>
30-
,
31-
window.document.head
32-
);
11+
const styles : string[] = [];
12+
for (let i = 0; i < spaces.length; i ++) {
13+
const space = spaces[i];
14+
const css: string[] = [];
15+
const style = {
16+
left: (space.left !== undefined ? cssValue(space.left, space.adjustedLeft) : undefined),
17+
top: (space.top !== undefined ? cssValue(space.top, space.adjustedTop) : undefined),
18+
right: (space.right !== undefined ? cssValue(space.right, space.adjustedLeft) : undefined),
19+
bottom: (space.bottom !== undefined ? cssValue(space.bottom, space.adjustedTop) : undefined),
20+
width: isHorizontalSpace(space.anchorType) ? cssValue(space.anchorSize, space.adjustedSize) : space.width,
21+
height: isVerticalSpace(space.anchorType) ? cssValue(space.anchorSize, space.adjustedSize) : space.height,
22+
zIndex: space.zIndex
23+
};
24+
if (style.left) {
25+
css.push(`left: ${style.left};`);
26+
}
27+
if (style.top) {
28+
css.push(`top: ${style.top};`);
29+
}
30+
if (style.right) {
31+
css.push(`right: ${style.right};`);
32+
}
33+
if (style.bottom) {
34+
css.push(`bottom: ${style.bottom};`);
35+
}
36+
if (style.width) {
37+
css.push(`width: ${style.width};`);
38+
}
39+
if (style.height) {
40+
css.push(`height: ${style.height}`);
41+
}
42+
if (style.zIndex) {
43+
css.push(`z-index: ${style.zIndex}`);
44+
}
45+
if (css.length > 0) {
46+
styles.push(`#${space.id} { ${css.join(' ')} }`);
47+
}
48+
}
49+
50+
if (styles.length === 0) {
51+
return null;
52+
}
53+
54+
return ReactDOM.createPortal(<style>{styles.join(' ')}</style>, window.document.head);
3355
}
3456

3557
return null;

0 commit comments

Comments
 (0)