}
const main = document.getElementsByTagName('main')[0];
-const initialColumns = Math.max(1, Math.floor(main.clientWidth / 320) - 1);
+const halfRemHeight = parseFloat(getComputedStyle(document.documentElement).fontSize) / 2;
/* Generic element builders */
-const createColumnElement = () => {
- const el = document.createElement('div');
- el.classList.add('column');
- return el;
-};
-
const createSkeletonElement = () => {
const el = document.createElement('article');
el.classList.add('card');
return el;
};
-/* Inject placeholder skeleton loaders */
-for (let i = 0; i < initialColumns; i++) {
- const col = createColumnElement();
- for (let j = 0; j < 12; j++) {
- const a = createSkeletonElement();
- col.insertAdjacentElement('beforeend', a);
+const setRowSpan = (card) => {
+ let contentHeight = 0;
+ for (let child of card.children) {
+ contentHeight += child.clientHeight;
}
- main.insertAdjacentElement('beforeend', col);
+ card.style.gridRowEnd = `span ${Math.ceil(contentHeight / halfRemHeight)}` // Fit the card's row span to the height of the card's content
+ console.log(contentHeight, Math.ceil(contentHeight / halfRemHeight));
}
+/* Inject placeholder skeleton loaders */
+for (let i = 0; i < 12; i++) {
+ const a = createSkeletonElement();
+ main.insertAdjacentElement('beforeend', a);
+};
+
/* Callback to inject content from API */
const inject = (resp) => {
main.innerHTML = '';
- const chunkSize = Math.ceil(resp.length / initialColumns);
- for (let i = 0; i < initialColumns; i++) {
- const col = createColumnElement();
- const chunk = resp.splice(0, chunkSize);
- for (let a of chunk) {
- col.insertAdjacentElement('beforeend', createArticleElement(a));
- }
- main.insertAdjacentElement('beforeend', col);
+ for (let a of resp) {
+ const card = createArticleElement(a);
+ main.insertAdjacentElement('beforeend', card); // Must insert the element first because contentHeight is dynamic on card width
+ setRowSpan(card);
}
};
max-width: 1440px;
}
-.column {
- display: flex;
- flex-wrap: wrap;
- align-content: flex-start;
- width: 100%;
-}
-
.card {
- margin-block-end: 1rem;
- padding: 0 1rem;
- width: 100%;
+ grid-row-end: span 16; /* Will be updated to fit the content in each card by JavaScript */
+ box-sizing: border-box;
+ display: flex;
+ flex-flow: column;
+ justify-content: center;
+ gap: 1rem;
+ padding: 1rem;
border: solid hsl(0deg 0% 55% / .1);
border-radius: 1rem;
box-shadow:
0.1px 16px 18px -2.5px hsl(0deg 0% 69% / 0.17);
}
+.card > * {
+ margin: 0; /* Margins for children elements are already handled by gap in .card */
+}
+
@keyframes shimmer {
100% {
background: hsl(0deg 0% 75%);
}
.skeleton-line {
- margin: 1rem;
height: 1rem;
animation: shimmer 1s alternate infinite;
background: hsl(0deg 0% 50%);