Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jtran committed Nov 21, 2024
1 parent b1e15d9 commit 2802355
Show file tree
Hide file tree
Showing 9 changed files with 9,776 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# react-xstate-demo

Demo app with XState selector with actor outside of React.
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "react",
"version": "1.0.0",
"description": "",
"keywords": [],
"main": "src/index.tsx",
"dependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "^5.0.0",
"xstate": "5.19.0",
"@xstate/react": "5.0.0"
},
"devDependencies": {
"@types/react": "18.2.38",
"@types/react-dom": "18.2.15",
"loader-utils": "3.2.1",
"typescript": "4.4.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
43 changes: 43 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>

<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>

</html>
16 changes: 16 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useSelector } from "@xstate/react";
import "./styles.css";
import { toggleActor } from "./toggle";

export default function App() {
const state = useSelector(toggleActor, (snapshot) => snapshot.value);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<p>The current value of the actor is {state}</p>
<button onClick={() => toggleActor.send({ type: "toggle" })}>
Toggle
</button>
</div>
);
}
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import App from "./App";

const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
<StrictMode>
<App />
</StrictMode>
);
5 changes: 5 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.App {
font-family: sans-serif;
text-align: center;
}

27 changes: 27 additions & 0 deletions src/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createMachine, createActor } from "xstate";

const toggleMachine = createMachine({
initial: "inactive",
states: {
inactive: {
on: {
toggle: {
target: "active",
},
},
},
active: {
on: {
toggle: {
target: "inactive",
},
},
},
},
});

export const toggleActor = createActor(toggleMachine);
toggleActor.start();

// State transitions can come from anywhere, not just react
window.toggle = () => toggleActor.send({ type: "toggle" });
Loading

0 comments on commit 2802355

Please sign in to comment.