Skip to content

Instantly share code, notes, and snippets.

@Ravenstine
Last active August 5, 2020 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ravenstine/f99d7cb679baf906c3d6b1435e52fdf9 to your computer and use it in GitHub Desktop.
Save Ravenstine/f99d7cb679baf906c3d6b1435e52fdf9 to your computer and use it in GitHub Desktop.
Squares
<h2>Nifty Squares</h2>
<label for="toggle">Show squares?</label>
<Input id="toggle" @type="checkbox" name="isAdmin" @checked={{this.this.showSquares}} />
<div class="nifty-squares">
{{#if this.showSquares}}
{{yield}}
{{/if}}
</div>
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { customElement } from 'ember-custom-elements';
@customElement('nifty-squares')
export default class NiftySquaresComponent extends Component {
@tracked
showSquares = true;
}
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { later } from '@ember/runloop';
import { htmlSafe } from '@ember/template';
const COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'violet'];
function generateSquares() {
return Array(16).fill().map(() => {
const color = COLORS[Math.floor(Math.random() * Math.floor(COLORS.length + 1))];
return {
style: htmlSafe(`background-color: ${color};`),
visible: !!Math.floor(Math.random() * Math.floor(2))
};
});
}
export default class ApplicationController extends Controller {
@tracked
squares;
init() {
super.init(...arguments);
this.refreshSquares();
}
@action
refreshSquares() {
this.squares = generateSquares();
later(this.refreshSquares, 200);
}
}
nifty-squares {
--square-size: 50px;
}
.nifty-squares {
display: flex;
flex-shrink: 0;
flex-wrap: wrap;
margin-top: 50px;
width: calc(var(--square-size) * 4);
}
.squares-parent {
display: contents;
}
.square {
flex-shrink: 0;
height: var(--square-size);
width: var(--square-size);
}
.square-color {
height: 100%;
width: 100%;
}
<nifty-squares>
<!-- A non-dynamic parent element is required
for any dynamic content passed, otherwise
the renderer will encounter problems -->
<div class="squares-parent">
{{#each this.squares as |square|}}
<div class="square">
{{#if square.visible}}
<div class="square-color" style={{square.style}}></div>
{{/if}}
</div>
{{/each}}
</div>
</nifty-squares>
{
"version": "0.17.1",
"ENV": {
"emberCustomElements": {
"deoptimizeModuleEval": true
}
},
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": false
},
"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-custom-elements": "2.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment