Skip to content

Commit 38a0f92

Browse files
refactor logic because UE saves after each field, and for some reason background-color gets called twice and returns null on second pass
1 parent 7041f84 commit 38a0f92

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

blocks/columns/columns.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { readBlockConfig } from '../../scripts/aem.js';
22

33
export default function decorate(block) {
44
const ancestor = block.closest('.columns-container');
5-
let bkgdColorAttr = ancestor?.getAttribute('data-background-color');
6-
let textColorAttr = ancestor?.getAttribute('data-text-color');
5+
const bkgdColorAttr = ancestor?.getAttribute('data-background-color');
6+
const textColorAttr = ancestor?.getAttribute('data-text-color');
77
const columnsChild = block.querySelector('div');
88
console.log('bkgdColorAttr in columns-container class: ', bkgdColorAttr);
9+
10+
if (bkgdColorAttr) block.style.backgroundColor = bkgdColorAttr;
11+
if (textColorAttr) block.style.color = textColorAttr;
12+
913
// for UE as value needs to be saved in different element due to having to walk the DOM tree to set the values
10-
if (bkgdColorAttr == null) {
11-
console.log('bkgdColorAttr in columns-container class is NULL, why? checking columns row: ', bkgdColorAttr);
12-
bkgdColorAttr = columnsChild?.getAttribute('data-background-color');
13-
console.log('bkgdColorAttr in columns row', bkgdColorAttr);
14-
}
15-
if (textColorAttr == null) {
16-
console.log('textColorAttr in columns-container class is NULL, why? checking columns row: ', textColorAttr);
17-
textColorAttr = columnsChild?.getAttribute('data-text-color');
18-
console.log('textColorAttr in columns row', textColorAttr);
19-
}
20-
if (bkgdColorAttr != null) block.style.backgroundColor = bkgdColorAttr;
21-
if (textColorAttr != null) block.style.color = textColorAttr;
14+
const ueBkgdColorAttr = columnsChild?.getAttribute('data-background-color');
15+
console.log('bkgdColorAttr in columns row UE', ueBkgdColorAttr);
16+
17+
const ueTextColorAttr = columnsChild?.getAttribute('data-text-color');
18+
console.log('textColorAttr in columns row UE', ueTextColorAttr);
19+
20+
if (ueBkgdColorAttr && ueBkgdColorAttr != null) block.style.backgroundColor = ueBkgdColorAttr;
21+
if (ueTextColorAttr && ueTextColorAttr != null) block.style.color = ueTextColorAttr;
2222

2323
const cols = [...block.firstElementChild.children];
2424
block.classList.add(`columns-${cols.length}-cols`);

0 commit comments

Comments
 (0)