Skip to content

Commit

Permalink
Fix: Converted inline item width to CSS variable (fixes #158) (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
guywillis authored Jan 28, 2025
1 parent 1ddd416 commit 3c4c988
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
20 changes: 20 additions & 0 deletions js/HotgridView.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class HotgridView extends ComponentView {
this.onItemClicked = this.onItemClicked.bind(this);

this.setUpViewData();
this.setUpEventListeners();
this.setItemWidth();
}

remove() {
Expand All @@ -28,6 +30,24 @@ class HotgridView extends ComponentView {
this._isPopupOpen = false;
}

setUpEventListeners() {
this.listenTo(Adapt, 'device:changed', this.setItemWidth);
}

setItemWidth() {
const columns = this.model.get('_columns');
const fallbackColumns = 2;
const shouldCalculateColumns = Boolean(columns && device.isScreenSizeMin('medium'));
this.$el
.toggleClass(`has-${columns}-columns`, shouldCalculateColumns)
.toggleClass(`has-${fallbackColumns}-columns`, !shouldCalculateColumns);
const calculatedColumns = shouldCalculateColumns
? columns
: fallbackColumns;
const width = `${100 / calculatedColumns}%`;
this.el.style.setProperty('--adapt-hotgrid-item-width', width);
}

postRender() {
this.$('.hotgrid__widget').imageready(this.setReadyStatus.bind(this));

Expand Down
2 changes: 1 addition & 1 deletion less/hotgrid.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

&__item {
width: 50%;
width: var(--adapt-hotgrid-item-width);
line-height: 0;
}

Expand Down
3 changes: 0 additions & 3 deletions templates/hotgrid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ export default function Hotgrid(props) {
])}
role="listitem"
key={_index}
style={(_columns && hasColumnLayout) ?
{ width: `${100 / _columns}%` } :
null}
>

<button className={classes([
Expand Down

0 comments on commit 3c4c988

Please sign in to comment.