Svelte library for FireFlutter
- To run with
.env.dev
, runnpm run dev -- --open --mode dev
.
The recommended way of intialization is that put the code SvelteFire.init()
inside src/routes/+layout.svelte
.
import { app } from '$lib/client/init.firebase.client';
import { SvelteFire } from '$lib/sveltefire/svelte-fire';
SvelteFire.init({ app });
Below is the src/lib/client/init.firebase.client
.
import { initializeApp } from "firebase/app";
const firebaseConfig: { [key: string]: string; } = {
apiKey: "AI....fU",
authDomain: "silbus.firebaseapp.com",
databaseURL: "https://silb.....st1.firebasedatabase.app",
projectId: "silbus",
storageBucket: "silbus.appspot.com",
messagingSenderId: "575893880018",
appId: "1:575.....fc10b",
measurementId: "G-3KQTNK89WP"
};
export const app = initializeApp(firebaseConfig);
To use sveltefire, the user must login. SvelteFire does not provide any login mechanism. It's up to you how you desgin your app. You may use email/password login or Phone sign-in, Google sign-in, etc.
First, Use the SignedIn
component to access the UID of the current user. Second, pass that UID to the Doc
or Collection
component to fetch a Firestore document owned by that user.
<SignedIn let:user>
<Doc ref={`users/${user.uid}`} let:data={user}>
<h2>{user.displayName}</h2>
</Doc>
</SignedIn>
The component Doc
, Collection
, Value
, ValueList
hydrates the SSR data.
Use the hydration data from svelte kit into the components of Value
, ValueList
, Doc
, Collection
. The HTML source code will have the data of the hydration.
<NotSignedIn>
<a href="/user/sign-in">SignIn</a>
</NotSignedIn>
The SignedIn
component renders content for the current user. It is a wrapper around the userStore
. If the user is not signed in, the children will not be rendered.
-
user
- The current Firebase user -
auth
- The current Firebase auth instance -
signOut
- A function to sign out the current user
<SignedIn let:user let:signOut let:auth>
You are signed in with {user.uid}
<button on:click={signOut}>Sign Out</button>
{auth.currentUser?.email}
</SignedIn>
Get a value of node and display
<Value path="post-all-summaries/-NvgtY78Qcowz-zwjRnN" hydrate={{ title: 'THIS IS TITLE' }} let:data>
{#each Object.keys(data) as key}
{key}: {data[key]}
{/each}
</Value>
Display a list of value node
<ValueList path="post-all-summaries" let:data>
{#each data as item}
<p>
{#each Object.keys(item) as key}
{key}: {item[key]}
{/each}
</p>
{/each}
</ValueList>
Display a document.
Display lists of documents in that collection
-
infiniteScroll
is an option to scroll infinitely.
display a UI for file upload into storage
Once you've created a project and installed dependencies with npm install
(or pnpm install
or yarn
), start a development server:
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
Everything inside src/lib
is part of your library, everything inside src/routes
can be used as a showcase or preview app.
To build your library:
npm run package
To create a production version of your showcase app:
npm run build
You can preview the production build with npm run preview
.
To deploy your app, you may need to install an adapter for your target environment.
Go into the package.json
and give your package the desired name through the "name"
option. Also consider adding a "license"
field and point it to a LICENSE
file which you can create from a template (one popular option is the MIT license).
To publish your library to npm:
npm publish
- The
InfiniteValueList
component will not bedestroyed
andmounted
if it is used in same page with different path. So, observe the chagnes of path and reset it.
<script lang="ts">
import { page } from '$app/stores';
import InfiniteValueList from '$lib/components/InfiniteValueList.svelte';
import { onMount } from 'svelte';
export let data;
let controller: InfiniteValueList;
let unsubscribe: () => void;
onMount(() => {
/// When categry changes, reset the store and fetch new data
unsubscribe = page.subscribe(() => {
controller?.onReset();
});
});
onDestroy(() => {
unsubscribe?.();
});
</script>
<InfiniteValueList
path={'posts-summary/' + data.category}
hydrate={data.posts}
let:value
bind:this={controller}
>
<p style="padding: 2em;">
<a href="/forum/{data.category}/{value.key}">{value.title}</a>
<span style="display: block;"></span>{value.key}
<span style="display: block; margin-top:1em"
>{new Date(value['createdAt']).toLocaleString()}</span
>
</p>
<p slot="noMoreData" let:length>
# No of post loaded: #{length}
<br />
No more posts
</p>
<p slot="loading">Loading data...</p>
</InfiniteValueList>