响应式
声明状态
更新状态
模板语法
样式
<h1.title>我是红色的</h1>
<button style={ "font-size": "10rem" }>我是一个按钮</button>
<button class=scopedButton>我是一个样式作用域按钮</button>
<style>
.title {
color: red;
}
</style>
<style/{ scopedButton }>
.scopedButton {
font-size: 10rem;
}
</style>
DOM循环
点击事件
DOM引用
条件渲染
static const TRAFFIC_LIGHTS = ["红灯", "黄灯", "绿灯"];
<let/lightIndex=0>
<const/light=TRAFFIC_LIGHTS[lightIndex]>
<button onClick() { lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length }>
下一个灯
</button>
<p>现在亮着的是: ${light}</p>
<p>
你应该
<if=light === "红灯">停下</if>
<else if=light === "黄灯">慢行</else>
<else>赶紧走</else>
</p>
import { track } from "ripple";
const TRAFFIC_LIGHTS = ["红灯", "黄灯", "绿灯"];
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}>"下一个灯"</button>
<p>"现在亮着的是: "{light}</p>
<p>
"你应该 "
if (light === "红灯") {
<span>"停下"</span>
} else if (light === "黄灯") {
<span>"慢行"</span>
} else if (light === "绿灯") {
<span>"赶紧走"</span>
}
</p>
</>;
}
生命周期
组件加载时
组件卸载时
<let/time=new Date()>
<script>
const id = setInterval(() => time = new Date(), 1000);
$signal.onabort = () => clearInterval(id)
</script>
<p>当前时间: ${time.toLocaleTimeString()}</p>
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>"当前时间: "{time}</h1>;
}
组件组合
组件Props传参
向父组件emit事件
<let/isHappy=true>
<p>你快乐吗?</p>
<AnswerButton
onYes() { isHappy = true }
onNo() { isHappy = false }
/>
<p style={ "font-size": 50 }>${isHappy ? "😀" : "😥"}</p>
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>"你快乐吗?"</p>
<AnswerButton onYes={onAnswerYes} onNo={onAnswerNo} />
<p style={{ fontSize: "50px" }}>{isHappy ? "😀" : "😥"}</p>
</>;
}
slot插槽
slot插槽内容
上下文
<let/user={
id: 1,
username: "abcdefg",
email: "[email protected]",
}>
<const/updateUsername(newUsername) {
user = { ...user, username: newUsername };
}>
<h1>欢迎回来, ${user.username}</h1>
<context={ ...user, updateUsername }>
<UserProfile />
</context>
import { RippleObject } from "ripple";
import { UserContext } from "./UserContext.tsrx";
import { UserProfile } from "./UserProfile.tsrx";
export function App() {
const user = new RippleObject({
id: 1,
username: "abcdefg",
email: "[email protected]",
});
UserContext.set(user);
return <>
<h1>"欢迎回来, "{user.username}</h1>
<UserProfile />
</>;
}
表单输入
输入框
复选框
<input#is-available
type="checkbox"
checked:=input.isAvailable
>
<label for="is-available">这是一个checkbox</label>
单选框
<let/picked="red">
<div>你选择了: ${picked}</div>
<input#blue-pill
type="radio"
value="blue"
checkedValue:=picked
>
<label for="blue-pill">蓝色</label>
<input#red-pill
type="radio"
value="red"
checkedValue:=picked
>
<label for="red-pill">红色</label>
import { bindGroup, track } from "ripple";
export default function PickPill() {
let &[picked, pickedTracked] = track("red");
return <>
<div>"你选择了: "{picked}</div>
<input
id="blue-pill"
type="radio"
value="blue"
ref={bindGroup(pickedTracked)}
/>
<label for="blue-pill">"蓝色"</label>
<input
id="red-pill"
type="radio"
value="red"
ref={bindGroup(pickedTracked)}
/>
<label for="red-pill">"红色"</label>
</>;
}
选择器
static const colors = [
{ id: 1, text: "红" },
{ id: 2, text: "蓝" },
{ id: 3, text: "绿" },
{ id: 4, text: "灰", isDisabled: true },
];
<let/selectedColorId=2>
<select value:=selectedColorId>
<for|{ id, isDisabled, text }| of=colors>
<option value=id disabled=isDisabled>
${text}
</option>
</for>
</select>
import { bindValue, track } from "ripple";
const colors = [
{ id: 1, text: "红" },
{ id: 2, text: "蓝" },
{ id: 3, text: "绿" },
{ id: 4, text: "灰", 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>;
}
Web应用特性
渲染应用
fetch获取数据
<try>
<await|{ results: users }|=fetch("https://randomuser.me/api/?results=3").then(res => res.json())>
<ul>
<for|{ picture, name }| of=users>
<li>
<img src=picture.thumbnail alt="user">
<p>${name.first} ${name.last}</p>
</li>
</for>
</ul>
</await>
<@placeholder>
<p>正在获取用户信息……</p>
</@placeholder>
<@catch|error|>
<p>获取用户信息出错</p>
</@catch>
</try>
import { useFetchUsers } from "./useFetchUsers.tsrx";
export function App() {
const data = useFetchUsers();
return <>
if (data.isLoading) {
<p>"正在获取用户信息……"</p>
} else if (data.error) {
<p>"获取用户信息出错"</p>
} else if (data.users) {
<ul>
for (const user of data.users; key user.login.uuid) {
<li>
<img src={user.picture.thumbnail} alt="用户" />
<p>{user.name.first}" "{user.name.last}</p>
</li>
}
</ul>
}
</>;
}