See in action:
Created
January 1, 2026 01:25
-
-
Save shuantsu/570c7d00f589bd48704371933d592a45 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> | |
| <style> | |
| body.dark { background: #222; color: #fff; } | |
| .card { border: 1px solid #ccc; padding: 20px; border-radius: 8px; width: 300px; } | |
| .loud { color: red; font-weight: bold; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="card"> | |
| <h3>Dashboard (Zustand)</h3> | |
| <p>Volume: <span id="vol-val">0</span></p> | |
| <p id="vol-msg"></p> | |
| <button id="btn-down">-</button> | |
| <button id="btn-up">+</button> | |
| <hr> | |
| <button id="btn-theme">Trocar Tema</button> | |
| <button id="btn-reset">Resetar Tudo</button> | |
| </div> | |
| <script type="module"> | |
| import { create } from 'https://esm.sh/zustand' | |
| import { subscribeWithSelector } from 'https://esm.sh/zustand/middleware' | |
| const useStore = create(subscribeWithSelector((set) => ({ | |
| volume: 50, | |
| theme: 'light', | |
| setVolume: (v) => set({ volume: v }), | |
| setTheme: (t) => set({ theme: t }), | |
| reset: () => set({ volume: 50, theme: 'light' }) | |
| }))); | |
| useStore.subscribe(state => state.volume, (value, prev) => { | |
| $('#vol-val').text(value); | |
| if (value > 80) { | |
| $('#vol-msg').text('Tá muito alto!').addClass('loud'); | |
| } else { | |
| $('#vol-msg').text('Volume ok.').removeClass('loud'); | |
| } | |
| }); | |
| useStore.subscribe(state => state.theme, (value) => { | |
| $('body').toggleClass('dark', value === 'dark'); | |
| $('#btn-theme').text(`Tema: ${value.toUpperCase()}`); | |
| }); | |
| $('#btn-up').click(() => { | |
| const v = useStore.getState().volume; | |
| if (v < 100) useStore.getState().setVolume(v + 5); | |
| }); | |
| $('#btn-down').click(() => { | |
| const v = useStore.getState().volume; | |
| if (v > 0) useStore.getState().setVolume(v - 5); | |
| }); | |
| $('#btn-theme').click(() => { | |
| const novoTema = useStore.getState().theme === 'light' ? 'dark' : 'light'; | |
| useStore.getState().setTheme(novoTema); | |
| }); | |
| $('#btn-reset').click(() => useStore.getState().reset()); | |
| $('#vol-val').text(useStore.getState().volume); | |
| $('#btn-theme').text(`Tema: ${useStore.getState().theme.toUpperCase()}`); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment