Skip to content

Commit

Permalink
Add table block
Browse files Browse the repository at this point in the history
  • Loading branch information
tdziezykDS committed Jun 7, 2024
1 parent 2244aab commit 3df62ec
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
58 changes: 58 additions & 0 deletions blocks/table/table.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.table {
width: 100%;
overflow-x: auto;
}

.table table {
width: 100%;
max-width: 100%;
border-collapse: collapse;
font-size: var(--body-font-size-xs);
}

@media (width >= 600px) {
.table table {
font-size: var(--body-font-size-s);
}
}

@media (width >= 900px) {
.table table {
font-size: var(--body-font-size-m);
}
}

.table table thead tr {
border-top: 2px solid;
border-bottom: 2px solid;
}

.table table tbody tr {
border-bottom: 1px solid;
}

.table table th {
font-weight: 700;
}

.table table th,
.table table td {
padding: 8px 16px;
text-align: left;
}

/* no header variant */
.table.no-header table tbody tr {
border-top: 1px solid;
}

/* striped variant */
.table.striped tbody tr:nth-child(odd) {
background-color: var(--overlay-background-color);
}

/* bordered variant */
.table.bordered table th,
.table.bordered table td {
border: 1px solid;
}
36 changes: 36 additions & 0 deletions blocks/table/table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Table Block
* Recreate a table
* https://www.hlx.live/developer/block-collection/table
*/

function buildCell(rowIndex) {
const cell = rowIndex ? document.createElement('td') : document.createElement('th');
if (!rowIndex) cell.setAttribute('scope', 'col');
return cell;
}

export default async function decorate(block) {
const table = document.createElement('table');
const thead = document.createElement('thead');
const tbody = document.createElement('tbody');

const header = !block.classList.contains('no-header');
if (header) {
table.append(thead);
}
table.append(tbody);

[...block.children].forEach((child, i) => {
const row = document.createElement('tr');
if (header && i === 0) thead.append(row);
else tbody.append(row);
[...child.children].forEach((col) => {
const cell = buildCell(header ? i : i + 1);
cell.innerHTML = col.innerHTML;
row.append(cell);
});
});
block.innerHTML = '';
block.append(table);
}

0 comments on commit 3df62ec

Please sign in to comment.