@ionic-sveltekit/example
Version:
Example app demonstrating a SvelteKit project with Ionic UI components
168 lines (127 loc) ā¢ 5.44 kB
Markdown
# @ionic-sveltekit/example
This repository contains an example [Ionic](https://ionicframework.com/) project built with [SvelteKit](https://svelte.dev/docs/kit) and the [`@ionic-sveltekit/core`](https://github.com/ionic-sveltekit/core) integration package. It serves as the template for new projects created with the [`npx @ionic-sveltekit/create`](https://github.com/ionic-sveltekit/create) CLI tool, and makes use of framework components and helpers provided by [`@ionic-sveltekit/components`](https://github.com/ionic-sveltekit/components).
## Features
The example project demonstrates various aspects of modern Svelte/SvelteKit (5+) development including:
- š§© **Svelte Runes** - Using `$state`, `$derived`, `$props()`, etc.
- š **Svelte Snippets & Renders** - Modern feature for reusable template fragments with `{#snippet}` and `{@render}`
- š¦ **Svelte Stores** - State management with readable stores
- š§ **SvelteKit Routing** - File-based routing pattern
- š **Server Components** - Server-side load functions
- š **Environment Variables** - Using `$env/static/private`
- šØ **Ionic Components** - Utilizing Ionic UI components with Svelte
- š± **Cross-Platform** - Works on iOS, Android, and web
## Project Structure
```
src/
āāā lib/
ā āāā components/ # Reusable components
ā ā āāā PlanetCard.svelte # Card component for planets
ā ā āāā Timer.svelte # Timer component with tooltip
ā ā āāā planet-card-content/
ā ā āāā earth.svelte # Content for Earth
ā ā āāā mars.svelte # Content for Mars
ā ā āāā ... # Other planet contents
ā āāā stores/ # Svelte stores
ā ā āāā timer.ts # Timer store
ā āāā images/ # Image assets
āāā routes/ # SvelteKit routes
ā āāā +layout.svelte # Root layout with Ionic tabs
ā āāā +page.svelte # Homepage with interactive elements
ā āāā planets/ # Planets route
ā āāā +page.svelte # List of planets with range slider
ā āāā [planet]/ # Dynamic route for individual planets
ā āāā +page.server.ts # Server-side load function (for Mars weather)
ā āāā +page.svelte # Planet page component
ā āāā +page.ts # Client-side load function
āāā theme/ # Ionic theming
```
## Key Components
### PlanetCard.svelte
A reusable card component for displaying planet information, supporting:
- Planet name and subtitle
- Optional image
- Dynamic content via component injection
- TypeScript type safety with a `Planet` type
### Timer.svelte
A small timer component that displays how long the app has been running, using:
- Svelte stores for time tracking
- Ionic tooltip components
- CSS view transitions
### Route Structure
- **Homepage**: Interactive demo with SVG animations and clickable buttons
- **Planets List**: Adjustable list using Ionic range slider
- **Individual Planet Pages**: Dynamic routes with server and client data loading, each showcasing different Ionic UI components:
- Mercury: Popovers with information tooltips
- Earth: Reorderable lists
- Mars: Weather data with custom styling
- Saturn: List with different colored badges
- Neptune: Scrollable segments with icons
## Technical Highlights
### Svelte Runes
The project uses Svelte 5's runes API for reactive state:
```typescript
// From +page.svelte
let spinCount = $state(0);
let clickCount = $state(0);
const rotate = $derived((spinCount + clickCount) * -22.5);
```
### Snippets & Renders
The project demonstrates Svelte's modern snippet and render features for reusable template fragments:
```svelte
<!-- From planets/+page.svelte -->
{#snippet planetLink(planet: Planet)}
<a href="/planets/{planet}">{planet}</a>
{/snippet}
<!-- Later in the file -->
{@render planetLink(planet)}
```
### Dynamic Imports
The project demonstrates dynamic imports for components and assets:
```typescript
// From planets/[planet]/+page.ts
async function getDynamicImport(planet: Planet, isComponent: boolean = true) {
let module: { default: Component | string } | undefined;
try {
module = await import(isComponent
? `$components/planet-card-content/${planet}.svelte`
: `$images/planets/${planet}.jpg`);
} catch (e) {
// Error handling...
}
return module?.default;
}
```
### Server-Side Data Fetching
For the Mars planet page, the project fetches real-time weather data from NASA's API:
```typescript
// From planets/[planet]/+page.server.ts
export const load: PageServerLoad = async ({ params, fetch }) => {
if (params.planet !== 'mars') {
return {};
}
// Fetch from NASA API...
};
```
## Development
### Requirements
- Node.js 18+
- A NASA API key (for Mars weather data)
### Setup
1. Clone the repository
2. Install dependencies: `npm install`
3. Create a `.env` file with your NASA API key:
```
NASA_API_KEY=your_api_key_here
```
4. Start the development server: `npm run dev`
## Building for Production
```bash
npm run build
```
## Using This Project as a Template
This project serves as the base template when creating a new project with:
```bash
npx @ionic-sveltekit/create my-app
```
## License
[MIT](LICENSE)