Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#308] Fixed Layout masonry is not applied when nested Layout is used. #310

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions components/00-base/layout/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
function CivicThemeLayout(el) {
this.el = el;
this.grid = el.querySelector('.ct-layout__inner');
this.grid = el.querySelector(':scope > .ct-layout__inner');

Check warning on line 7 in components/00-base/layout/layout.js

View check run for this annotation

Codecov / codecov/patch

components/00-base/layout/layout.js#L7

Added line #L7 was not covered by tests
const gridStyle = getComputedStyle(this.grid);

if (gridStyle.gridTemplateRows === 'masonry' || this.grid.hasAttribute('data-masonry')) {
Expand All @@ -13,15 +13,16 @@

this.grid.setAttribute('data-masonry', true);

this.stl = this.grid.querySelector('.ct-layout__sidebar_top_left');
this.str = this.grid.querySelector('.ct-layout__sidebar_top_right');
this.sbl = this.grid.querySelector('.ct-layout__sidebar_bottom_left');
this.sbr = this.grid.querySelector('.ct-layout__sidebar_bottom_right');
this.stl = this.grid.querySelector(':scope > .ct-layout__sidebar_top_left');
this.str = this.grid.querySelector(':scope > .ct-layout__sidebar_top_right');
this.sbl = this.grid.querySelector(':scope > .ct-layout__sidebar_bottom_left');
this.sbr = this.grid.querySelector(':scope > .ct-layout__sidebar_bottom_right');

Check warning on line 19 in components/00-base/layout/layout.js

View check run for this annotation

Codecov / codecov/patch

components/00-base/layout/layout.js#L16-L19

Added lines #L16 - L19 were not covered by tests

// Only enable masonry if all 4 elements are present.
if (this.stl && this.str && this.sbl && this.sbr) {
// Prepare redraw variables.
this.gap = parseFloat(gridStyle.gridRowGap);
// Items include all children of the grid, not just the 4 sidebar regions.
this.items = Array.from(this.grid.children);
this.height = 0;

Expand All @@ -31,7 +32,17 @@
this.masonryRedraw();
});
});
this.resizeObserver.observe(this.grid);

// Observe all children of the grid items rather than the items themselves:
// this allows us to detect changes in the height of the children rather
// tnan of the grid items as their height will not change when children
// combined heights is less than a single grid row height.
this.items.forEach((item) => {
Array.from(item.children).forEach((child) => {
this.resizeObserver.observe(child);

Check warning on line 42 in components/00-base/layout/layout.js

View check run for this annotation

Codecov / codecov/patch

components/00-base/layout/layout.js#L40-L42

Added lines #L40 - L42 were not covered by tests
});
});

this.masonryRedraw();
}
}
Expand All @@ -51,9 +62,20 @@
* Reposition grid elements.
*/
CivicThemeLayout.prototype.masonryRedraw = function () {
// Ignore redraw if grid items total height hasn't changed since last redraw.
const newHeight = this.items.reduce((t, el) => t + el.getBoundingClientRect().height, 0);
// Calculate the new height of all children.
//
// Although masonry layout is applied only if the element has the
// CSS variable --js-masonry-enabled set and we could have check for this
// variable to preserve height reclaulation, this variable can be assigned
// within a specific media query. Therefore, we need to calculate the height
// in case --js-masonry-enabled is assigned to the element after the viewport
// has been resized.
const newHeight = this.items.reduce((totalHeight, item) => {
const childrenHeight = Array.from(item.children).reduce((childTotal, child) => childTotal + child.getBoundingClientRect().height, 0);
return totalHeight + childrenHeight;

Check warning on line 75 in components/00-base/layout/layout.js

View check run for this annotation

Codecov / codecov/patch

components/00-base/layout/layout.js#L73-L75

Added lines #L73 - L75 were not covered by tests
}, 0);

// Proceed only if the height has changed.
if (newHeight !== this.height) {
this.height = newHeight;

Expand All @@ -69,7 +91,6 @@
}
};

// Initialize CivicThemeLayout on every element.
document.querySelectorAll('.ct-layout').forEach((layout) => {
// eslint-disable-next-line no-new
new CivicThemeLayout(layout);
Expand Down
44 changes: 26 additions & 18 deletions components/00-base/layout/layout.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,24 @@ export const Layout = (parentKnobs = {}) => {
sidebar_bottom_right: knobBoolean('Show bottom right sidebar', true, parentKnobs.sidebar_bottom_right, parentKnobs.knobTab) ? placeholder('Bottom right sidebar', useLargePlaceholders ? randomInt(30, 100) : 0) : '',
hide_sidebar_right: knobBoolean('Hide right sidebar', false, parentKnobs.hide_sidebar_right, parentKnobs.knobTab),
is_contained: knobBoolean('Is contained', false, parentKnobs.is_contained, parentKnobs.knobTab),
vertical_spacing: knobRadios(
'Vertical spacing',
{
None: 'none',
Top: 'top',
Bottom: 'bottom',
Both: 'both',
},
'none',
parentKnobs.vertical_spacing,
parentKnobs.knobTab,
),
};

const showNested = knobBoolean('Show nested layout', false, parentKnobs.show_nested_layout, parentKnobs.knobTab);

knobs.content = showNested ? CivicThemeLayout({
// Show nested only if parent content is shown.
knobs.content = knobs.content && showNested ? CivicThemeLayout({
...{
sidebar_top_left: placeholder('Nested top left sidebar', useLargePlaceholders ? randomInt(30, 100) : 0),
sidebar_bottom_left: placeholder('Nested bottom left sidebar', useLargePlaceholders ? randomInt(30, 100) : 0),
content: placeholder('Nested content', useLargePlaceholders ? randomInt(500, 1000) : 0),
sidebar_top_right: placeholder('Nested top right sidebar', useLargePlaceholders ? randomInt(30, 100) : 0),
sidebar_bottom_right: placeholder('Nested bottom right sidebar', useLargePlaceholders ? randomInt(30, 100) : 0),
sidebar_top_left: knobBoolean('Show nested top left sidebar', true, parentKnobs.nested_sidebar_top_left, parentKnobs.knobTab) ? placeholder('Nested top left sidebar', useLargePlaceholders ? randomInt(30, 100) : 0) : '',
sidebar_top_left_attributes: 'data-story-nested-layout',
sidebar_bottom_left: knobBoolean('Show nested bottom left sidebar', true, parentKnobs.nested_sidebar_bottom_left, parentKnobs.knobTab) ? placeholder('Nested bottom left sidebar', useLargePlaceholders ? randomInt(30, 100) : 0) : '',
sidebar_bottom_left_attributes: 'data-story-nested-layout',
content: knobBoolean('Show nested content', true, parentKnobs.nested_content, parentKnobs.knobTab) ? placeholder('Nested content', useLargePlaceholders ? randomInt(500, 1000) : 0) : '',
content_attributes: 'data-story-nested-layout',
sidebar_top_right: knobBoolean('Show nested top right sidebar', true, parentKnobs.nested_sidebar_top_right, parentKnobs.knobTab) ? placeholder('Nested top right sidebar', useLargePlaceholders ? randomInt(30, 100) : 0) : '',
sidebar_top_right_attributes: 'data-story-nested-layout',
sidebar_bottom_right: knobBoolean('Show nested bottom right sidebar', true, parentKnobs.nested_sidebar_bottom_right, parentKnobs.knobTab) ? placeholder('Nested bottom right sidebar', useLargePlaceholders ? randomInt(30, 100) : 0) : '',
sidebar_bottom_right_attributes: 'data-story-nested-layout',
attributes: 'data-story-nested-layout',
},
...slotKnobs([
'content_top',
Expand All @@ -53,6 +48,19 @@ export const Layout = (parentKnobs = {}) => {

const showOutlines = knobBoolean('Show outlines', false, parentKnobs.show_outlines, parentKnobs.knobTab);

knobs.vertical_spacing = knobRadios(
'Vertical spacing',
{
None: 'none',
Top: 'top',
Bottom: 'bottom',
Both: 'both',
},
'none',
parentKnobs.vertical_spacing,
parentKnobs.knobTab,
);

const attributesTab = 'Attributes';
knobs.sidebar_top_left_attributes = knobs.sidebar_top_left ? knobText('Top left sidebar attributes', '', parentKnobs.sidebar_top_left_attributes, attributesTab) : '';
knobs.sidebar_bottom_left_attributes = knobs.sidebar_bottom_left ? knobText('Bottom left sidebar attributes', '', parentKnobs.sidebar_bottom_left_attributes, attributesTab) : '';
Expand Down
15 changes: 15 additions & 0 deletions components/00-base/layout/layout.stories.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
// Layout component stories.
//

@use 'sass:color';

// Nested layout.
.ct-layout {
.ct-layout {
.story-placeholder {
$_color: #6b5394;

background-color: color.adjust($_color, $alpha: -0.7);
border: dashed 1px color.adjust($_color, $alpha: -0.3);
color: color.adjust($_color, $lightness: -30%);
}
}
}

.story-layout-outlines {
$_viewport-color: blue;
$_layout-color: green;
Expand Down
2 changes: 1 addition & 1 deletion components/00-base/storybook/storybook.generators.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const themes = () => ({
dark: 'Dark',
});

export const placeholder = (content = 'Content placeholder', words = 0, cssClass = 'story-placeholder') => `<div class="${cssClass}">${content}${words > 0 ? ` ${randomSentence(words)}` : ''}</div>`;
export const placeholder = (content = 'Content placeholder', words = 0, cssClass = 'story-placeholder') => `<div class="${cssClass}" contenteditable="true">${content}${words > 0 ? ` ${randomSentence(words)}` : ''}</div>`;

export const code = (content) => `<code>${content}</code>`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ describe('Domain-Specific Generators', () => {
describe('placeholder', () => {
it('returns placeholder with default parameters', () => {
expect(placeholder())
.toBe('<div class="story-placeholder">Content placeholder</div>');
.toBe('<div class="story-placeholder" contenteditable="true">Content placeholder</div>');
});

it('returns placeholder with custom content and class', () => {
expect(placeholder('Custom content', 0, 'custom-class'))
.toBe('<div class="custom-class">Custom content</div>');
.toBe('<div class="custom-class" contenteditable="true">Custom content</div>');
});

it('returns placeholder with random sentence', () => {
const result = placeholder('Custom content', 5);
expect(result.startsWith('<div class="story-placeholder">Custom content '))
expect(result.startsWith('<div class="story-placeholder" contenteditable="true">Custom content '))
.toBe(true);
});
});
Expand Down
Loading