Svelte Component Extensions
After Svelte mounts the app and init Framework7, we will have access to Framework7's initialized instance and some other useful properties that can be imported from framework7-svelte
package.
f7ready
It is a callback function that will be executed when Framework7 fully intialized. Useful to use in components when you need to access Framework7 API and to be sure it is ready. So it is safe to put all Framework7 related logic into this callback. As an argument it receives initialized Framework7 instance. For example:
...
<script>
import { onMount } from 'svelte';
import { f7ready } from 'framework7-svelte';
onMount(() => {
f7ready(() => {
// Framework7 initialized
f7.dialog.alert('Component mounted');
})
})
</script>
f7
Main Framework7's initialized instance. It allows you to use any of Framework7 APIs.
If you are sure that on the moment when you access Framework7 instance, it was already initialized, you can import and use it directly:
<script>
import { f7 } from 'framework7-svelte';
const doSomething = () => {
f7.dialog.alert('Hello world');
}
// ...
</script>
theme
It is an object with boolean properties with information about currently used theme (iOS, MD or Aurora ): theme.ios
, theme.md
and theme.aurora
.
<script>
import { theme } from 'framework7-svelte';
if (theme.ios) {
console.log('Currently active theme is iOS-theme')
}
// ...
</script>
f7route
and f7router
Router instance and current route are passed via props to router components:
This properties only available for components loaded with router (e.g. pages, routable modals, routable tabs). If you need to access this property in "deeper" child components, then you need to pass it down using props.
<script>
import { onMount } from 'svelte';
// define props so the component will receive it
export let f7route;
export let f7router;
onMount(() => {
console.log(f7route.url)
});
const navigate = () => {
f7router.navigate('/some-page/');
}
// ...
</script>
f7route
is the current route, object with the following properties:
url
- route URLpath
- route pathquery
- object with route query. If the url is/page/?id=5&foo=bar
then it will contain the following object{id: '5', foo: 'bar'}
params
- route params. If we have matching route with/page/user/:userId/post/:postId/
path and url of the page is/page/user/55/post/12/
then it will be the following object{userId: '55', postId: '12'}
name
- route namehash
- route URL hashroute
- object with matching route from available routes
Slots
All Framework7 Svelte components use slots
to distribute children across component structure. But due to Svelte limitation it is not allowed to use slots on custom components, so sometimes you may need to wrap it with dummy HTML element or use HTML layout instead:
For example
<Page>
<List>
<ListItem title="Toggle">
<!-- wrap with extra element -->
<span slot="after">
<Toggle />
</span>
</ListItem>
</List>
</Page>
Events
All Framework7 Svelte components support events. But the way how Svelte handles events (by emitting CustomEvent) can be inconvenient when we need to get event arguments (via event.detail
) and especially when there are few arguments.
So all Framework7 Svelte components emit events with arguments as array:
<Page on:pageInit={onPageInit}>
<!-- -->
</Page>
<script>
// pageInit event has one argument with page data
function onPageInit(event) {
// it will be available in event.detail array
const pageData = event.detail[0]
console.log(pageData);
}
</script>
To workaround this, all Framework7 Svelte component events has same `on${Event}` prop callback:
<Page onPageInit={onPageInit}>
<!-- -->
</Page>
<script>
// pageInit event has one argument with page data
function onPageInit(pageData) {
console.log(pageData);
}
</script>