Skip to content

Commit

Permalink
initial version of grid
Browse files Browse the repository at this point in the history
  • Loading branch information
nc-andreashaller committed Dec 19, 2023
1 parent fb399b3 commit 6065770
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 63 deletions.
3 changes: 2 additions & 1 deletion blocks/columns/columns.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
.columns > div {
align-items: center;
flex-direction: unset;
gap: 32px;
gap: var(--padding-vertical) var(--padding-horizontal);
padding: var(--padding-vertical) var(--padding-horizontal);
}

.columns > div > div:nth-child(1) {
Expand Down
18 changes: 7 additions & 11 deletions blocks/columns/columns.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
export default function decorate(block) {
const id = `gen${crypto.randomUUID().split('-')[0]}`;
block.id = id;
import {
addCssVariables,
} from '../../scripts/lib-franklin.js';

export default function decorate(block) {
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 = '';
const variables = {};
for (let i = 0; i < columnCount; i += 1) {
const partition = columnPartions.length > i ? columnPartions[i] : 1;
variables += `--column${i}-flex: ${partition};`;
variables[`column${i}-flex`] = partition;
}

const style = document.createElement('style');
style.textContent = `#${id} {
${variables}
}`;
block.parentNode.insertBefore(style, block);
addCssVariables(block, variables);
}
31 changes: 31 additions & 0 deletions blocks/grid/grid.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.section.grid {
display: grid;
grid-template-columns: var(--grid-template-columns);
grid-template-rows: var(--grid-template-rows);
}

.section.grid .element {
grid-column-start: var(--grid-column-start-position, auto);
grid-row-start: var(--grid-row-start-position, auto);
grid-column-end: var(--grid-column-end-position, auto);
grid-row-end: var(--grid-row-end-position, auto);
width: 100%;
max-width: unset;
background-color: var(--background-color, transparent);
color: var(--text-color);
margin: auto;
}

.section.grid .element .image-wrapper {
margin: 0;
padding: 0;
}

.section.grid .element img {
width: 100%;
}

.section.grid .element p {
margin: 0;
padding: var(--padding-vertical) var(--padding-horizontal);
}
34 changes: 34 additions & 0 deletions blocks/grid/grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
addCssVariables,
} from '../../scripts/lib-franklin.js';

export default function decorate(block) {
const elements = [...block.querySelectorAll(':scope > div')];

const columnTemplate = elements.find((e) => e.dataset.gridColumns)?.dataset.gridColumns;
const rowTemplate = elements.find((e) => e.dataset.gridRows)?.dataset.gridRows;
if(columnTemplate || rowTemplate) {

Check failure on line 10 in blocks/grid/grid.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "if"
const variables = {};
if(columnTemplate) {

Check failure on line 12 in blocks/grid/grid.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "if"
variables['grid-template-columns'] = columnTemplate;
}
if(columnTemplate) {

Check failure on line 15 in blocks/grid/grid.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "if"
variables['grid-template-rows'] = rowTemplate;
}
addCssVariables(block, variables);
}

elements.forEach((e) => {
e.classList.add('element');

const [[startColumnPosition, startRowPosition], [endColumnPosition, endRowPosition]] =

Check failure on line 24 in blocks/grid/grid.js

View workflow job for this annotation

GitHub Actions / build

There should be no line break before or after '='

Check failure on line 24 in blocks/grid/grid.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
e.dataset.gridPosition.split(/\s*\/\s*/).map((p) => p.split(/\s*-\s*/));

addCssVariables(e, {
'grid-column-start-position': startColumnPosition,
'grid-row-start-position': startRowPosition,
'grid-column-end-position': endColumnPosition,
'grid-row-end-position': endRowPosition,
});
});
}

Check failure on line 34 in blocks/grid/grid.js

View workflow job for this annotation

GitHub Actions / build

Newline required at end of file but not found
91 changes: 59 additions & 32 deletions scripts/lib-franklin.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,43 +329,70 @@ export function readBlockConfig(block) {
return config;
}

export function addCssVariables(element, variables) {
if (!element.id) {
const id = `gen${crypto.randomUUID().split('-')[0]}`;
element.id = id;
}

const style = document.createElement('style');
style.textContent = `#${element.id} {
${Object.keys(variables).map((k) => `--${k}: ${variables[k]};`).join(' ')}
}`;

element.parentNode.insertBefore(style, element);
}

export function decorateSection(section) {
const wrappers = [];
let defaultContent = false;
[...section.children].forEach((e) => {
if (e.tagName === 'DIV' || !defaultContent) {
const wrapper = document.createElement('div');
wrappers.push(wrapper);
defaultContent = e.tagName !== 'DIV';
if (defaultContent) wrapper.classList.add('default-content-wrapper');
}
wrappers[wrappers.length - 1].append(e);
});
wrappers.forEach((wrapper) => section.append(wrapper));
section.classList.add('section');
section.dataset.sectionStatus = 'initialized';
section.style.display = 'none';

/* process section metadata */
const sectionMeta = section.querySelector('div.section-metadata');
if (sectionMeta) {
const meta = readBlockConfig(sectionMeta);
Object.keys(meta).forEach((key) => {
if (key === 'style') {
const styles = meta.style.split(',').map((style) => toClassName(style.trim()));
styles.forEach((style) => section.classList.add(style));
} else {
section.dataset[toCamelCase(key)] = meta[key];
}
});
sectionMeta.parentNode.remove();
}

if (section.dataset.textColor || section.dataset.background) {
const variables = {};
if (section.dataset.textColor) {
variables['text-color'] = section.dataset.textColor;
}
if (section.dataset.background) {
variables['background-color'] = section.dataset.background;
}
addCssVariables(section, variables);
}
}

/**
* Decorates all sections in a container element.
* @param {Element} main The container element
*/
export function decorateSections(main) {
main.querySelectorAll(':scope > div').forEach((section) => {
const wrappers = [];
let defaultContent = false;
[...section.children].forEach((e) => {
if (e.tagName === 'DIV' || !defaultContent) {
const wrapper = document.createElement('div');
wrappers.push(wrapper);
defaultContent = e.tagName !== 'DIV';
if (defaultContent) wrapper.classList.add('default-content-wrapper');
}
wrappers[wrappers.length - 1].append(e);
});
wrappers.forEach((wrapper) => section.append(wrapper));
section.classList.add('section');
section.dataset.sectionStatus = 'initialized';
section.style.display = 'none';

/* process section metadata */
const sectionMeta = section.querySelector('div.section-metadata');
if (sectionMeta) {
const meta = readBlockConfig(sectionMeta);
Object.keys(meta).forEach((key) => {
if (key === 'style') {
const styles = meta.style.split(',').map((style) => toClassName(style.trim()));
styles.forEach((style) => section.classList.add(style));
} else {
section.dataset[toCamelCase(key)] = meta[key];
}
});
sectionMeta.parentNode.remove();
}
});
main.querySelectorAll(':scope > div').forEach((section) => decorateSection(section));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
loadFooter,
decorateButtons,
decorateIcons,
decorateSection,
decorateSections,
decorateBlocks,
decorateTemplateAndTheme,
Expand All @@ -30,6 +31,22 @@ function buildHeroBlock(main) {
}
}

function buildGrid(main) {
const gridElements = main.querySelectorAll('.section[data-grid-position]');
gridElements.forEach((e) => {
e.classList.remove('section');
delete e.dataset.sectionStatus;
e.removeAttribute('style');
});
const grid = document.createElement('div');
grid.classList.add('block', 'grid');
grid.dataset.blockName = 'grid';
grid.dataset.blockStatus = 'initialized';
main.insertBefore(grid, gridElements[0]);
decorateSection(grid);
grid.replaceChildren(...gridElements);
}

/**
* load fonts.css and set a session storage flag
*/
Expand All @@ -55,6 +72,14 @@ function buildAutoBlocks(main) {
}
}

function decorateImages(main) {
main.querySelectorAll('picture').forEach((i) => {
if(i.parentElement.tagName === 'P' && i.parentElement.children.length === 1) {

Check failure on line 77 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Expected space(s) after "if"
i.parentElement.classList.add('image-wrapper');
}
});
}

/**
* Decorates the main element.
* @param {Element} main The main element
Expand All @@ -67,6 +92,8 @@ export function decorateMain(main) {
buildAutoBlocks(main);
decorateSections(main);
decorateBlocks(main);
decorateImages(main)

Check failure on line 95 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
buildGrid(main);
}

/**
Expand Down
24 changes: 5 additions & 19 deletions styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
/* nav height */
--nav-height: 64px;

--padding-horizontal: 32px;
--padding-vertical: 16px;

}

@font-face {
Expand Down Expand Up @@ -229,23 +232,6 @@ main img {
}

main .section {
padding: 64px 16px;
}

@media (min-width: 600px) {
main .section {
padding: 64px 32px;
}
}

@media (min-width: 900px) {
.section > div {
max-width: 1200px;
margin: auto;
}
}

/* section metadata */
main .section.highlight {
background-color: var(--highlight-background-color);
background-color: var(--background-color, transparent);
color: var(--text-color);
}

0 comments on commit 6065770

Please sign in to comment.