diff options
| author | Maksymilian <maks@jopek.eu> | 2022-09-23 09:44:56 +0200 |
|---|---|---|
| committer | Maksymilian <maks@jopek.eu> | 2022-09-23 09:44:56 +0200 |
| commit | 8886308c56223f04df535a065234414a6deb11ba (patch) | |
| tree | bf432ab5e6a4ae754c4017bd9e0e663c25ccc055 /src/lib | |
| download | svelte-pokeapi-master.tar.gz svelte-pokeapi-master.tar.zst svelte-pokeapi-master.zip | |
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Counter.svelte | 12 | ||||
| -rw-r--r-- | src/lib/Tailwind.css | 3 | ||||
| -rw-r--r-- | src/lib/hmr-stores.js | 29 |
3 files changed, 44 insertions, 0 deletions
diff --git a/src/lib/Counter.svelte b/src/lib/Counter.svelte new file mode 100644 index 0000000..e417505 --- /dev/null +++ b/src/lib/Counter.svelte @@ -0,0 +1,12 @@ +<script> + import { getStore } from './hmr-stores'; + export let id; + const count = getStore(id, 0); + export const increment = () => { + $count += 1 + }; +</script> + +<button class="px-8 py-4 w-52 text-red-500 bg-red-200 rounded-full" {id} on:click={increment}> + Clicks: {$count} +</button>
\ No newline at end of file diff --git a/src/lib/Tailwind.css b/src/lib/Tailwind.css new file mode 100644 index 0000000..bd6213e --- /dev/null +++ b/src/lib/Tailwind.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities;
\ No newline at end of file diff --git a/src/lib/hmr-stores.js b/src/lib/hmr-stores.js new file mode 100644 index 0000000..9cd6d0e --- /dev/null +++ b/src/lib/hmr-stores.js @@ -0,0 +1,29 @@ +// Customized HMR-safe stores +// Based off https://github.com/svitejs/svite/blob/ddec6b9/packages/playground/hmr/src/stores/hmr-stores.js +import { writable } from 'svelte/store' + +/** + * @type { Record<string, import('svelte/store').Writable<any>> } + */ +let stores = {} + +/** + * @template T + * @param { string } id + * @param { T } initialValue + * @returns { import('svelte/store').Writable<T> } + */ +export function getStore(id, initialValue) { + return stores[id] || (stores[id] = writable(initialValue)) +} + +// preserve the store across HMR updates +if (import.meta.hot) { + if (import.meta.hot.data.stores) { + stores = import.meta.hot.data.stores + } + import.meta.hot.accept() + import.meta.hot.dispose(() => { + import.meta.hot.data.stores = stores + }) +} |
