Last active
August 4, 2020 04:22
-
-
Save elwayman02/222e1d96ed2135aa8f101aae39a0f8a2 to your computer and use it in GitHub Desktop.
Ember CSS Grid Masonry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
export default class extends Component { | |
constructor() { | |
super(...arguments); | |
this.items = new Array(16); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
export default class extends Component { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@glimmer/component'; | |
import { tracked } from '@glimmer/tracking'; | |
import { htmlSafe } from '@ember/template'; | |
// The code in this component generates a random height | |
export default class extends Component { | |
@tracked height; | |
constructor() { | |
super(...arguments); | |
this.height = this.getRandomIntInclusive(50, 250); | |
} | |
get computedStyle() { | |
return htmlSafe(`height: ${this.height}px`); | |
} | |
getRandomIntInclusive(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Modifier from 'ember-modifier'; | |
export default class MasonryColumnsModifier extends Modifier { | |
get gridWidth() { | |
let gridStyle = getComputedStyle(this.element); | |
let paddingLeft = parseFloat(gridStyle.getPropertyValue('padding-left')); | |
let paddingRight = parseFloat(gridStyle.getPropertyValue('padding-right')); | |
return parseFloat(gridStyle.getPropertyValue('width')) - (paddingLeft + paddingRight); | |
} | |
get columnGap() { | |
let { columnGap } = this.args.named; | |
if (columnGap === 'auto') { | |
let possibleColumns = Math.floor(this.gridWidth / this.columnWidth); | |
let availableSpace = this.gridWidth - (this.columnWidth * possibleColumns); | |
return Math.floor(availableSpace / (possibleColumns - 1)); | |
} | |
return parseFloat(columnGap); | |
} | |
get columnCount() { | |
let { columnCount } = this.args.named; | |
if (columnCount === 'auto') { | |
return Math.floor((this.gridWidth + this.columnGap)/(this.columnWidth + this.columnGap)); | |
} | |
return parseFloat(this.args.named.columnCount); | |
} | |
get columnWidth() { | |
return parseFloat(this.args.named.columnWidth); | |
} | |
didReceiveArguments() { | |
this.configureColumns(); | |
} | |
didInstall() { | |
this.resizeObserver = new ResizeObserver((entries) => { | |
this.configureColumns(); | |
}); | |
this.resizeObserver.observe(this.element); | |
} | |
configureColumns() { | |
this.element.style.gridTemplateColumns = `repeat(${this.columnCount}, ${this.columnWidth}px)`; | |
this.element.style.gridColumnGap = `${this.columnGap}px`; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Modifier from 'ember-modifier'; | |
export default class MasonryRowsModifier extends Modifier { | |
get rowGap() { | |
return parseFloat(this.args.named.rowGap); | |
} | |
didReceiveArguments() { | |
let numRows = Math.ceil(this.element.offsetHeight / 1) + this.rowGap; | |
this.element.style.gridRowEnd = `span ${numRows}`; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.masonry-demo { | |
position: relative; | |
width: 100%; | |
} | |
.masonry { | |
display: grid; | |
grid-auto-rows: 1px; | |
border: 1px solid gray; | |
box-sizing: border-box; | |
min-width: 100%; | |
} | |
.masonry-item { | |
} | |
.content { | |
background-color: lightgray; | |
border: 1px dashed black; | |
display: flex; | |
font-size: 45pt; | |
align-items: center; | |
justify-content: center; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-modifier": "2.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment