-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d108a1b
commit 4445f09
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useAppState } from './store'; | ||
|
||
export default function Counter() { | ||
const count = useAppState((s) => s.count); | ||
|
||
return ( | ||
<div className="inline-flex mx-5"> | ||
<span className="countdown font-mono text-6xl"> | ||
<span style={{ '--value': count }}></span> | ||
</span> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { setAppState, useAppState } from './store'; | ||
|
||
export default function Header() { | ||
const mode = useAppState((s) => s.mode); | ||
|
||
const toggleTheme = () => { | ||
setAppState((s) => { | ||
s.mode = s.mode === 'Light' ? 'Dark' : 'Light'; | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className="navbar"> | ||
<div className="flex-1"> | ||
<a className="btn btn-ghost text-xl">React State</a> | ||
</div> | ||
<div className="flex-none"> | ||
<div className="flex flex-row-reverse"> | ||
{mode === 'Light' ? '🔆' : '🌙'} | ||
<input | ||
type="checkbox" | ||
className="toggle" | ||
checked={mode === 'Dark'} | ||
onChange={toggleTheme} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useAppState, setAppState } from './store'; | ||
import Header from './Header'; | ||
import Counter from './Counter'; | ||
|
||
export default function App() { | ||
const mode = useAppState((s) => s.mode); | ||
const classNames = `${mode === 'Light' ? 'bg-white' : 'bg-dark'} pb-5`; | ||
return ( | ||
<div className={classNames} data-mode="light"> | ||
<Header /> | ||
<div className="flex justify-center mt-5"> | ||
<Counter /> | ||
<Counter /> | ||
<Counter /> | ||
</div> | ||
<div className="flex justify-center mt-5"> | ||
<button | ||
className="btn btn-sm btn-success" | ||
onClick={() => | ||
setAppState((s) => { | ||
s.count++; | ||
}) | ||
} | ||
> | ||
INCREMENT | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { create } from '@opentf/react-state'; | ||
|
||
const {useAppState, setAppState, api} = create({ mode: 'Light', count: 0 }); | ||
|
||
api.subscribe(console.log); | ||
|
||
export { useAppState, setAppState }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters