generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hlxsites/columns
first version of flexible column block
- Loading branch information
Showing
4 changed files
with
68 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
export default function decorate(block) { | ||
const cols = [...block.firstElementChild.children]; | ||
block.classList.add(`columns-${cols.length}-cols`); | ||
const id = `gen${crypto.randomUUID().split('-')[0]}`; | ||
block.id = id; | ||
|
||
// setup image columns | ||
[...block.children].forEach((row) => { | ||
[...row.children].forEach((col) => { | ||
const pic = col.querySelector('picture'); | ||
if (pic) { | ||
const picWrapper = pic.closest('div'); | ||
if (picWrapper && picWrapper.children.length === 1) { | ||
// picture is only content in column | ||
picWrapper.classList.add('columns-img-col'); | ||
} | ||
} | ||
}); | ||
}); | ||
const columns = block.querySelectorAll(':scope > div > div'); | ||
const columnCount = columns.length; | ||
// following line regex matches partition sizes separated by dashes like 1-2-3 | ||
const columnPartionRegex = /^\d{1,}(?:-\d{1,})*$/; | ||
const columnPartions = [...block.classList].find((c) => columnPartionRegex.test(c))?.split('-') || []; | ||
|
||
let variables = ''; | ||
for (let i = 0; i < columnCount; i += 1) { | ||
const partition = columnPartions.length > i ? columnPartions[i] : 1; | ||
variables += `--column${i}-flex: ${partition};`; | ||
} | ||
|
||
const style = document.createElement('style'); | ||
style.textContent = `#${id} { | ||
${variables} | ||
}`; | ||
block.parentNode.insertBefore(style, block); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters