Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions examples/01_counter/Counter.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,50 @@ let update = (msg, model) => {

let view = (model, dispatch) => {
<div
style={ReactDOM.Style.make(
~display="flex",
~flexDirection="column",
~alignItems="center",
~gap="16px",
~padding="32px",
~fontFamily="system-ui, sans-serif",
(),
)}>
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
gap: "16px",
padding: "32px",
fontFamily: "system-ui, sans-serif",
}}
>
<h1> {React.string("Counter")} </h1>
<div
style={ReactDOM.Style.make(
~display="flex",
~alignItems="center",
~gap="16px",
(),
)}>
style={{
display: "flex",
alignItems: "center",
gap: "16px",
}}
>
<button
onClick={_ => dispatch(Decrement)}
style={ReactDOM.Style.make(~padding="8px 16px", ~fontSize="18px", ~cursor="pointer", ())}>
style={{padding: "8px 16px", fontSize: "18px", cursor: "pointer"}}
>
{React.string("-")}
</button>
<span
style={ReactDOM.Style.make(
~fontSize="48px",
~fontWeight="bold",
~minWidth="80px",
~textAlign="center",
(),
)}>
{model.count->Belt.Int.toString->React.string}
style={{
fontSize: "48px",
fontWeight: "bold",
minWidth: "80px",
textAlign: "center",
}}
>
{model.count->Int.toString->React.string}
</span>
<button
onClick={_ => dispatch(Increment)}
style={ReactDOM.Style.make(~padding="8px 16px", ~fontSize="18px", ~cursor="pointer", ())}>
style={{padding: "8px 16px", fontSize: "18px", cursor: "pointer"}}
>
{React.string("+")}
</button>
</div>
<button
onClick={_ => dispatch(Reset)}
style={ReactDOM.Style.make(~padding="8px 24px", ~fontSize="14px", ~cursor="pointer", ())}>
style={{padding: "8px 24px", fontSize: "14px", cursor: "pointer"}}
>
{React.string("Reset")}
</button>
</div>
Expand Down Expand Up @@ -124,6 +127,6 @@ let mount = () => {
let rootElement = ReactDOM.Client.createRoot(root)
rootElement->ReactDOM.Client.Root.render(<App flags=() />)
}
| None => Js.Console.error("Could not find #root element")
| None => Console.error("Could not find #root element")
}
}
108 changes: 46 additions & 62 deletions examples/02_http/HttpExample.res
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ type remoteData<'a, 'e> =
| Success('a)
| Failure('e)

type model = {
users: remoteData<array<user>, string>,
}
type model = {users: remoteData<array<user>, string>}

type msg =
| FetchUsers
Expand All @@ -51,10 +49,7 @@ let usersDecoder: Json.decoder<array<user>> = Json.array(userDecoder)
// Init
// ============================================================================

let init = _ => (
{users: NotAsked},
Cmd.none,
)
let init = _ => ({users: NotAsked}, Cmd.none)

// ============================================================================
// Update
Expand All @@ -63,15 +58,13 @@ let init = _ => (
let update = (msg, model) => {
switch msg {
| FetchUsers => (
{...model, users: Loading},
Http.getJson(
"https://jsonplaceholder.typicode.com/users",
usersDecoder,
result => GotUsers(result),
),
{users: Loading},
Http.getJson("https://jsonplaceholder.typicode.com/users", usersDecoder, result => GotUsers(
result,
)),
)
| GotUsers(Ok(users)) => ({...model, users: Success(users)}, Cmd.none)
| GotUsers(Error(err)) => ({...model, users: Failure(Http.errorToString(err))}, Cmd.none)
| GotUsers(Ok(users)) => ({users: Success(users)}, Cmd.none)
| GotUsers(Error(err)) => ({users: Failure(Http.errorToString(err))}, Cmd.none)
}
}

Expand All @@ -81,46 +74,40 @@ let update = (msg, model) => {

let viewUser = (user: user) => {
<div
key={Belt.Int.toString(user.id)}
style={ReactDOM.Style.make(
~padding="12px",
~marginBottom="8px",
~backgroundColor="#f5f5f5",
~borderRadius="4px",
(),
)}>
<div style={ReactDOM.Style.make(~fontWeight="bold", ~marginBottom="4px", ())}>
{React.string(user.name)}
</div>
<div style={ReactDOM.Style.make(~fontSize="14px", ~color="#666", ())}>
{React.string(`@${user.username}`)}
</div>
<div style={ReactDOM.Style.make(~fontSize="14px", ~color="#888", ())}>
{React.string(user.email)}
</div>
key={Int.toString(user.id)}
style={{
padding: "12px",
marginBottom: "8px",
backgroundColor: "#f5f5f5",
borderRadius: "4px",
}}
>
<div style={{fontWeight: "bold", marginBottom: "4px"}}> {React.string(user.name)} </div>
<div style={{fontSize: "14px", color: "#666"}}> {React.string(`@${user.username}`)} </div>
<div style={{fontSize: "14px", color: "#888"}}> {React.string(user.email)} </div>
</div>
}

let view = (model, dispatch) => {
<div
style={ReactDOM.Style.make(
~maxWidth="600px",
~margin="0 auto",
~padding="20px",
~fontFamily="system-ui, sans-serif",
(),
)}>
style={{
maxWidth: "600px",
margin: "0 auto",
padding: "20px",
fontFamily: "system-ui, sans-serif",
}}
>
<h1> {React.string("Users")} </h1>
<button
onClick={_ => dispatch(FetchUsers)}
disabled={model.users == Loading}
style={ReactDOM.Style.make(
~padding="10px 20px",
~fontSize="16px",
~cursor="pointer",
~marginBottom="20px",
(),
)}>
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
marginBottom: "20px",
}}
>
{React.string(
switch model.users {
| Loading => "Loading..."
Expand All @@ -129,30 +116,27 @@ let view = (model, dispatch) => {
)}
</button>
{switch model.users {
| NotAsked =>
<p style={ReactDOM.Style.make(~color="#666", ())}>
{React.string("Click the button to fetch users")}
</p>
| NotAsked => <p style={{color: "#666"}}> {React.string("Click the button to fetch users")} </p>
| Loading =>
<div style={ReactDOM.Style.make(~textAlign="center", ~padding="40px", ())}>
<div style={{textAlign: "center", padding: "40px"}}>
<div> {React.string("Loading...")} </div>
</div>
| Success(users) =>
<div>
<p style={ReactDOM.Style.make(~color="#666", ~marginBottom="16px", ())}>
{React.string(`Loaded ${Belt.Int.toString(Belt.Array.length(users))} users`)}
<p style={{color: "#666", marginBottom: "16px"}}>
{React.string(`Loaded ${Int.toString(Array.length(users))} users`)}
</p>
{users->Belt.Array.map(viewUser)->React.array}
{users->Array.map(viewUser)->React.array}
</div>
| Failure(error) =>
<div
style={ReactDOM.Style.make(
~color="#d32f2f",
~padding="16px",
~backgroundColor="#ffebee",
~borderRadius="4px",
(),
)}>
style={{
color: "#d32f2f",
padding: "16px",
backgroundColor: "#ffebee",
borderRadius: "4px",
}}
>
<strong> {React.string("Error: ")} </strong>
{React.string(error)}
</div>
Expand Down Expand Up @@ -190,6 +174,6 @@ let mount = () => {
let rootElement = ReactDOM.Client.createRoot(root)
rootElement->ReactDOM.Client.Root.render(<App flags=() />)
}
| None => Js.Console.error("Could not find #root element")
| None => Console.error("Could not find #root element")
}
}
15 changes: 5 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,33 @@
"type": "git",
"url": "https://gitlab.com/hyperpolymath/rescript-tea"
},
"main": "src/Tea.res.mjs",
"main": "lib/es6/src/Tea.res.mjs",
"files": [
"src",
"lib/es6/src",
"lib/bs/src",
"lib/bs/compiler-info.json",
"lib/rescript.lock",
"rescript.json",
"README.adoc",
"LICENSE.txt"
],
"scripts": {
"build": "rescript",
"clean": "rescript clean",
"dev": "rescript -w",
"test": "npx deno test --allow-read lib/es6/test/Tea_Cmd_test.res.mjs lib/es6/test/Tea_Json_test.res.mjs"
"test": "npx deno test --no-check --allow-read lib/es6/test/Tea_Cmd_test.res.mjs lib/es6/test/Tea_Json_test.res.mjs"
},
"peerDependencies": {
"@rescript/react": ">=0.12.0",
"react": ">=18.0.0",
"react-dom": ">=18.0.0",
"@rescript/react": ">=0.14.0",
"react": ">=19.0.0",
"react-dom": ">=19.0.0",
"rescript": ">=11.0.0"
},
"devDependencies": {
"@rescript/react": "^0.14.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"rescript": "^12.0.2"
}
}
4 changes: 2 additions & 2 deletions rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
}
],
"suffix": ".res.mjs",
"bs-dependencies": [
"dependencies": [
"@rescript/react"
],
"bs-dev-dependencies": [],
"dev-dependencies": [],
"jsx": {
"version": 4,
"mode": "automatic"
Expand Down
Loading
Loading