Reactivity
Declare state
Update state
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
export default class CounterComponent extends Component {
@tracked name = "John";
constructor(owner, args) {
super(owner, args);
this.name = "Jane";
}
<template>
<h1>Hello {{this.name}}</h1>
</template>
}
Computed state
Ember Polaris
import Component, { tracked } from "@glimmer/component";
export default class DoubleCount extends Component {
@tracked count = 10;
get doubleCount() {
return this.count * 2;
}
<template>
<div>{{this.doubleCount}}</div>
</template>
}
Templating
Minimal template
Styling
Ember Polaris
<template>
<h1 class="title">I am red</h1>
<button style="font-size: 10rem;">I am a button</button>
<style>
.title {
color: red;
}
</style>
</template>Loop
Event click
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
export default class Counter extends Component {
@tracked count = 0;
incrementCount = () => this.count++;
<template>
<p>Counter: {{this.count}}</p>
<button {{on "click" this.incrementCount}}>+1</button>
</template>
}
Dom ref
Conditional
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
const TRAFFIC_LIGHTS = ["red", "orange", "green"];
export default class TrafficLight extends Component {
@tracked lightIndex = 0;
get light() {
return TRAFFIC_LIGHTS[this.lightIndex];
}
nextLight = () => {
this.lightIndex = (this.lightIndex + 1) % TRAFFIC_LIGHTS.length;
};
<template>
<button {{on "click" this.nextLight}}>Next light</button>
<p>Light is: {{this.light}}</p>
<p>
You must
{{#if (eq this.light "red")}}
STOP
{{else if (eq this.light "orange")}}
SLOW DOWN
{{else if (eq this.light "green")}}
GO
{{/if}}
</p>
</template>
}
<div
x-data="{
TRAFFIC_LIGHTS: ['red', 'orange', 'green'],
lightIndex: 0,
get light() { return this.TRAFFIC_LIGHTS[this.lightIndex] },
nextLight() {
this.lightIndex = (this.lightIndex + 1) % this.TRAFFIC_LIGHTS.length;
}
}"
>
<button x-on:click="nextLight">Next light</button>
<p>Light is: <span x-text="light"></span></p>
<p>
You must
<span x-show="light === 'red'">STOP</span>
<span x-show="light === 'orange'">SLOW DOWN</span>
<span x-show="light === 'green'">GO</span>
</p>
</div>
Lifecycle
On mount
On unmount
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { registerDestructor } from "@ember/destroyable";
export default class Time extends Component {
@tracked time = new Date().toLocaleTimeString();
constructor(owner, args) {
super(owner, args);
let timer = setInterval(() => {
this.time = new Date().toLocaleTimeString();
}, 1000);
registerDestructor(this, () => clearInterval(timer));
}
<template>
<p>Current time: {{this.time}}</p>
</template>
}
Component composition
Props
Ember Polaris
import UserProfile from "./user-profile.gjs";
const favoriteColors = ["green", "blue", "red"];
<template>
<UserProfile
@name="John"
@age={{20}}
@favouriteColors={{favoriteColors}}
@isAvailable={{true}}
/>
</template><!--Alpine JS suggests using a server-side templating engine or another frontend framework in conjunction with Alpine to do this-->
<div
x-data="{
name: 'John',
age: 20,
favouriteColors: ['green', 'blue', 'red'],
isAvailable: true
}"
>
<p>My name is <span x-text="John"></span></p>
<p>My age is <span x-text="age"></span></p>
<p>
My favourite colors are <span x-text="favouriteColors.join(', ')"></span>
</p>
<p>I am <span x-text="isAvailable ? 'available' : 'not available'"></span></p>
</div>
Emit to parent
Ember Polaris
<template>
<button {{on "click" @onYes}}> YES </button>
<button {{on "click" @onNo}}> NO </button>
</template>
<div
x-data="{ isHappy: true }"
x-on:yes="isHappy = true"
x-on:no="isHappy = false"
>
<p>Are you happy?</p>
<div>
<button x-on:click="$dispatch('yes')">YES</button>
<button x-on:click="$dispatch('no')">NO</button>
</div>
<p style="font-size: 50px" x-text="isHappy ? '😀' : '😥'"></p>
</div>
Slot
Ember Polaris
import FunnyButton from "./funny-button";
<template>
<FunnyButton>Click me!</FunnyButton>
</template>
<!--Alpine JS suggests using a server-side templating engine or another frontend framework in conjunction with Alpine to do this-->
<button
x-data
x-text="'Click me!'"
style="
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 10px 20px;
font-size: 30px;
border: 2px solid #fff;
margin: 8px;
transform: scale(0.9);
box-shadow: 4px 4px rgba(0, 0, 0, 0.4);
transition: transform 0.2s cubic-bezier(0.34, 1.65, 0.88, 0.925) 0s;
outline: 0;
"
>
<span>No content found</span>
</button>
Slot fallback
Ember Polaris
import FunnyButton from "./funny-button";
<template>
<FunnyButton />
<FunnyButton>I got content!</FunnyButton>
</template><!--Alpine JS suggests using a server-side templating engine or another frontend framework in conjunction with Alpine to do this-->
<button
x-data
style="
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 10px 20px;
font-size: 30px;
border: 2px solid #fff;
margin: 8px;
transform: scale(0.9);
box-shadow: 4px 4px rgba(0, 0, 0, 0.4);
transition: transform 0.2s cubic-bezier(0.34, 1.65, 0.88, 0.925) 0s;
outline: 0;
"
>
<span>No content found</span>
</button>
<button
x-data
x-text="'I got content!'"
style="
background: rgba(0, 0, 0, 0.4);
color: #fff;
padding: 10px 20px;
font-size: 30px;
border: 2px solid #fff;
margin: 8px;
transform: scale(0.9);
box-shadow: 4px 4px rgba(0, 0, 0, 0.4);
transition: transform 0.2s cubic-bezier(0.34, 1.65, 0.88, 0.925) 0s;
outline: 0;
"
>
<span>No content found</span>
</button>
Context
Ember Polaris
import UserProfile from "./user-profile";
<template>
<UserProfile />
</template>
Alpine
Missing snippet Help us to improve Component Party 
Form input
Input text
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
export default class InputHello extends Component {
@tracked text = "Hello World";
handleInput = (event) => (this.text = event.target.value);
<template>
<p>{{this.text}}</p>
<input value={{this.text}} {{on "input" this.handleInput}} />
</template>
}
Checkbox
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
export default class InputHello extends Component {
@tracked isAvailable = false;
handleChange = (event) => (this.isAvailable = event.target.checked);
<template>
<input
id="is-available"
type="checkbox"
checked={{this.isAvailable}}
{{on "change" this.handleChange}}
/>
<label for="is-available">Is available</label>
</template>
}
Radio
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
export default class PickPill extends Component {
@tracked picked = "red";
handleChange = (event) => (this.picked = event.target.value);
<template>
<div>Picked: {{this.picked}}</div>
<input
id="blue-pill"
type="radio"
value="blue"
checked={{eq this.picked "blue"}}
{{on "change" this.handleChange}}
/>
<label htmlFor="blue-pill">Blue pill</label>
<input
id="red-pill"
type="radio"
value="red"
checked={{eq this.picked "red"}}
{{on "change" this.handleChange}}
/>
<label htmlFor="red-pill">Red pill</label>
</template>
}
<div x-data="{ picked: 'red' }">
<div>Picked: <span x-text="picked"></span></div>
<input id="blue-pill" x-model="picked" type="radio" value="blue" />
<label for="blue-pill">Blue pill</label>
<input id="red-pill" x-model="picked" type="radio" value="red" />
<label for="red-pill">Red pill</label>
</div>
Select
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
export default class ColorSelect extends Component {
@tracked selectedColorId = 2;
select = (event) => (this.selectedColorId = event.target.value);
isSelected = (colorId) => this.selectedColorId === colorId;
colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
<template>
<select {{on "change" this.select}}>
{{#each this.colors as |color|}}
<option
value={{color.id}}
disabled={{color.isDisabled}}
selected={{this.isSelected color.id}}
>
{{color.text}}
</option>
{{/each}}
</select>
</template>
}
<div
x-data="{
selectedColorId: 2,
colors: [
{ id: 1, text: 'red' },
{ id: 2, text: 'blue' },
{ id: 3, text: 'green' },
{ id: 4, text: 'gray', isDisabled: true }
]
}"
>
<select x-model.number="selectedColorId">
<template x-for="color in colors" x-bind:key="color.id">
<option
x-text="color.text"
x-bind:value="color.id"
x-bind:disabled="!!color.isDisabled"
x-bind:selected="color.id === selectedColorId"
></option>
</template>
</select>
</div>
Webapp features
Render app
Fetch data
Ember Polaris
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
class State {
@tracked isLoading = false;
@tracked error = null;
@tracked data = null;
}
function fetchUsers() {
let state = new State();
async function fetchData() {
try {
let response = await fetch("https://randomuser.me/api/?results=3");
let { results: users } = await response.json();
state.data = users;
state.error = null;
} catch (err) {
state.data = null;
state.error = err;
}
state.isLoading = false;
}
fetchData();
return state;
}
export default class App extends Component {
<template>
{{#let (this.fetchUsers) as |request|}}
{{#if request.isLoading}}
<p>Fetching users...</p>
{{else if request.error}}
<p>An error occurred while fetching users</p>
{{else}}
<ul>
{{#each request.data as |user|}}
<li>
<img src={{user.picture.thumbnail}} alt="user" />
<p>{{user.name.first}} {{user.name.last}}</p>
</li>
{{/each}}
</ul>
{{/if}}
{{/let}}
</template>
}<div
x-data="
function fetchUsers() {
return {
users: null,
isLoading: false,
error: null,
async init() {
this.isLoading = true;
try {
this.users = (await (await fetch('https://randomuser.me/api/?results=3')).json()).results;
} catch (err) {
this.users = [];
this.error = err
}
this.isLoading = false;
},
};
}
"
>
<template x-if="isLoading">
<p>Loading...</p>
</template>
<template x-if="error">
<p>Error fetching users</p>
</template>
<template x-if="!error">
<ul>
<template x-for="user in users">
<li>
<img
:src="user.picture.thumbnail"
:alt="`picture of ${user.name.first} ${user.name.last}`"
/>
<p x-text="`${user.name.first} ${user.name.last}`"></p>
</li>
</template>
</ul>
</template>
</div>