Reactivity
Declare state
Update state
Vue 2
<script>
export default {
data() {
return {
name: "John",
};
},
created() {
this.name = "Jane";
},
};
</script>
<template>
<h1>Hello {{ name }}</h1>
</template>
Computed state
Vue 2
<script>
export default {
data() {
return {
count: 10,
};
},
computed: {
doubleCount() {
return this.count * 2;
},
},
};
</script>
<template>
<div>{{ doubleCount }}</div>
</template>
Templating
Minimal template
Styling
Vue 2
<template>
<div>
<h1 class="title">I am red</h1>
<button style="font-size: 10rem">I am a button</button>
</div>
</template>
<style scoped>
.title {
color: red;
}
</style>
Loop
Vue 2
<script>
export default {
data() {
return {
colors: ["red", "green", "blue"],
};
},
};
</script>
<template>
<ul>
<li v-for="color in colors" :key="color">
{{ color }}
</li>
</ul>
</template>
Event click
Vue 2
<script>
export default {
data() {
return {
count: 0,
};
},
methods: {
incrementCount() {
this.count++;
},
},
};
</script>
<template>
<div>
<p>Counter: {{ count }}</p>
<button @click="incrementCount">+1</button>
</div>
</template>
Dom ref
Conditional
Vue 2
<script>
export default {
data() {
return {
TRAFFIC_LIGHTS: ["red", "orange", "green"],
lightIndex: 0,
};
},
computed: {
light() {
return this.TRAFFIC_LIGHTS[this.lightIndex];
},
},
methods: {
nextLight() {
this.lightIndex = (this.lightIndex + 1) % this.TRAFFIC_LIGHTS.length;
},
},
};
</script>
<template>
<div>
<button @click="nextLight">Next light</button>
<p>Light is: {{ light }}</p>
<p>
You must
<span v-if="light === 'red'">STOP</span>
<span v-else-if="light === 'orange'">SLOW DOWN</span>
<span v-else-if="light === 'green'">GO</span>
</p>
</div>
</template>
import { track } from "ripple";
const TRAFFIC_LIGHTS = ["red", "orange", "green"];
export default function TrafficLight() {
let &[lightIndex] = track(0);
let &[light] = track(() => TRAFFIC_LIGHTS[lightIndex]);
function nextLight() {
lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length;
}
return <>
<button onClick={nextLight}>"Next light"</button>
<p>"Light is "{light}</p>
<p>
"You must "
if (light === "red") {
<span>"STOP"</span>
} else if (light === "orange") {
<span>"SLOW DOWN"</span>
} else if (light === "green") {
<span>"GO"</span>
}
</p>
</>;
}
Lifecycle
On mount
Vue 2
<script>
export default {
data() {
return {
pageTitle: "",
};
},
mounted() {
this.pageTitle = document.title;
},
};
</script>
<template>
<p>Page title: {{ pageTitle }}</p>
</template>
On unmount
Vue 2
<script>
export default {
data() {
return {
time: new Date().toLocaleTimeString(),
timer: null,
};
},
mounted() {
this.timer = setInterval(() => {
this.time = new Date().toLocaleTimeString();
}, 1000);
},
beforeDestroy() {
clearInterval(this.timer);
},
};
</script>
<template>
<p>Current time: {{ time }}</p>
</template>
import { effect, track } from "ripple";
export default function Time() {
let &[time] = track(new Date().toLocaleTimeString());
effect(() => {
const timer = setInterval(() => {
time = new Date().toLocaleTimeString();
}, 1000);
return () => clearInterval(timer);
});
return <h1>"Current time: "{time}</h1>;
}
Component composition
Props
Vue 2
<script>
import UserProfile from "./UserProfile.vue";
export default {
components: {
UserProfile,
},
};
</script>
<template>
<UserProfile
name="John"
:age="20"
:favorite-colors="['green', 'blue', 'red']"
is-available
/>
</template>
Emit to parent
Vue 2
<script>
import AnswerButton from "./AnswerButton.vue";
export default {
components: {
AnswerButton,
},
data() {
return {
isHappy: true,
};
},
methods: {
onAnswerNo() {
this.isHappy = false;
},
onAnswerYes() {
this.isHappy = true;
},
},
};
</script>
<template>
<div>
<p>Are you happy?</p>
<AnswerButton @yes="onAnswerYes" @no="onAnswerNo" />
<p style="font-size: 50px">
{{ isHappy ? "😀" : "😥" }}
</p>
</div>
</template>
import { track } from "ripple";
import { AnswerButton } from "./AnswerButton.tsrx";
export function App() {
let &[isHappy] = track(true);
function onAnswerNo() {
isHappy = false;
}
function onAnswerYes() {
isHappy = true;
}
return <>
<p>"Are you happy?"</p>
<AnswerButton onYes={onAnswerYes} onNo={onAnswerNo} />
<p style={{ fontSize: "50px" }}>{isHappy ? "😀" : "😥"}</p>
</>;
}
Slot
Vue 2
<script>
import FunnyButton from "./FunnyButton.vue";
export default {
components: {
FunnyButton,
},
};
</script>
<template>
<FunnyButton> Click me! </FunnyButton>
</template>
Slot fallback
Vue 2
<script>
import FunnyButton from "./FunnyButton.vue";
export default {
components: {
FunnyButton,
},
};
</script>
<template>
<div>
<FunnyButton />
<FunnyButton> I got content! </FunnyButton>
</div>
</template>
Context
Vue 2
<script>
import UserProfile from "./UserProfile.vue";
export default {
components: { UserProfile },
provide() {
return {
user: Object.assign(this.user, {
updateUsername: this.updateUsername,
}),
};
},
data() {
return {
user: {
id: 1,
username: "unicorn42",
email: "[email protected]",
},
};
},
methods: {
updateUsername(newUsername) {
this.user.username = newUsername;
},
},
};
</script>
<template>
<div>
<h1>Welcome back, {{ user.username }}</h1>
<UserProfile />
</div>
</template>
import { RippleObject } from "ripple";
import { UserContext } from "./UserContext.tsrx";
import { UserProfile } from "./UserProfile.tsrx";
export function App() {
const user = new RippleObject({
id: 1,
username: "unicorn42",
email: "[email protected]",
});
UserContext.set(user);
return <>
<h1>"Welcome back, "{user.username}</h1>
<UserProfile />
</>;
}
Form input
Input text
Vue 2
<script>
export default {
data() {
return {
text: "Hello World",
};
},
};
</script>
<template>
<div>
<p>{{ text }}</p>
<input v-model="text" />
</div>
</template>
Checkbox
Vue 2
<script>
export default {
data() {
return {
isAvailable: true,
};
},
};
</script>
<template>
<div>
<input id="is-available" v-model="isAvailable" type="checkbox" />
<label for="is-available">Is available</label>
</div>
</template>
Radio
Vue 2
<script>
export default {
data() {
return {
picked: "red",
};
},
};
</script>
<template>
<div>
<div>Picked: {{ picked }}</div>
<input id="blue-pill" v-model="picked" type="radio" value="blue" />
<label for="blue-pill">Blue pill</label>
<input id="red-pill" v-model="picked" type="radio" value="red" />
<label for="red-pill">Red pill</label>
</div>
</template>
import { bindGroup, track } from "ripple";
export default function PickPill() {
let &[picked, pickedTracked] = track("red");
return <>
<div>"Picked: "{picked}</div>
<input
id="blue-pill"
type="radio"
value="blue"
ref={bindGroup(pickedTracked)}
/>
<label for="blue-pill">"Blue pill"</label>
<input
id="red-pill"
type="radio"
value="red"
ref={bindGroup(pickedTracked)}
/>
<label for="red-pill">"Red pill"</label>
</>;
}
Select
Vue 2
<script>
export default {
data() {
return {
selectedColorId: 2,
colors: [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
],
};
},
};
</script>
<template>
<select v-model="selectedColorId">
<option
v-for="color in colors"
:key="color.id"
:value="color.id"
:disabled="color.isDisabled"
>
{{ color.text }}
</option>
</select>
</template>
import { bindValue, track } from "ripple";
const colors = [
{ id: 1, text: "red" },
{ id: 2, text: "blue" },
{ id: 3, text: "green" },
{ id: 4, text: "gray", isDisabled: true },
];
export default function ColorSelect() {
let &[selectedColorId, selectedColorIdTracked] = track(2);
return <select ref={bindValue(selectedColorIdTracked)}>
for (const color of colors; key color.id) {
<option value={color.id} disabled={color.isDisabled}>
{color.text}
</option>
}
</select>;
}
Webapp features
Render app
Vue 2
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
Ripple
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
Fetch data
Vue 2
<script>
export default {
data() {
return {
isLoading: false,
error: undefined,
users: undefined,
};
},
mounted() {
this.fetchData();
},
methods: {
async fetchData() {
this.isLoading = true;
try {
const response = await fetch("https://randomuser.me/api/?results=3");
const { results: users } = await response.json();
this.users = users;
this.error = undefined;
} catch (error) {
this.error = error;
} finally {
this.users = undefined;
this.isLoading = false;
}
},
},
};
</script>
<template>
<p v-if="isLoading">Fetching users...</p>
<p v-else-if="error">An error ocurred while fetching users</p>
<ul v-else-if="users">
<li v-for="user in users" :key="user.login.uuid">
<img :src="user.picture.thumbnail" alt="user" />
<p>
{{ user.name.first }}
{{ user.name.last }}
</p>
</li>
</ul>
</template>
import { useFetchUsers } from "./useFetchUsers.tsrx";
export function App() {
const data = useFetchUsers();
return <>
if (data.isLoading) {
<p>"Fetching users..."</p>
} else if (data.error) {
<p>"An error occurred while fetching users"</p>
} else if (data.users) {
<ul>
for (const user of data.users; key user.login.uuid) {
<li>
<img src={user.picture.thumbnail} alt="user" />
<p>{user.name.first}" "{user.name.last}</p>
</li>
}
</ul>
}
</>;
}