Skip to content

Commit

Permalink
feat: 🎸 DotLottieSvelte (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf authored Feb 20, 2024
1 parent ef435fd commit 826f791
Show file tree
Hide file tree
Showing 23 changed files with 1,622 additions and 177 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ tmp/

# Renderer releases
releases/

*.svelte
15 changes: 15 additions & 0 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ module.exports = [
path: 'packages/wc/dist/index.js',
import: '*',
},
{
name: '@lottiefiles/dotlottie-svelte',
path: 'packages/svelte/dist/index.js',
import: '*',
modifyWebpackConfig: (config) => {
config.module.rules.push({
test: /\.svelte$/,
use: {
loader: 'svelte-loader',
},
});

return config;
},
},
];
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,16 @@ dotLottie is an open-source file format that aggregates one or more Lottie files

The monorepo contains the following package:

| Package | Description |
| ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **[@lottiefiles/dotlottie-web](packages/web/README.md)** | A JavaScript library for rendering Lottie and dotLottie animations in the browser or Node.js. |
| **[@lottiefiles/dotlottie-react](packages/react/README.md)** | A React component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |
| **[@lottiefiles/dotlottie-wc](packages/wc/README.md)** | A Web Component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |
| **[@lottiefiles/dotlottie-vue](packages/vue/README.md)** | A Vue component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |
| Package | Description |
| -------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **[@lottiefiles/dotlottie-web](packages/web/README.md)** | A JavaScript library for rendering Lottie and dotLottie animations in the browser or Node.js. |
| **[@lottiefiles/dotlottie-react](packages/react/README.md)** | A React component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |
| **[@lottiefiles/dotlottie-wc](packages/wc/README.md)** | A Web Component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |
| **[@lottiefiles/dotlottie-vue](packages/vue/README.md)** | A Vue component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |
| **[@lottiefiles/dotlottie-svelte](packages/svelte/README.md)** | A Svelte component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |

> Note: Each package has its own README.md with detailed documentation on usage and APIs.
### Coming Soon Packages

We are actively working on expanding our suite of packages. Here's what's coming soon:

| Package | Description |
| ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `@lottiefiles/dotlottie-svelte` | A Svelte component wrapper for `dotlottie-web` that provides a declarative API for rendering Lottie and dotLottie animations and UI controls for interacting with them. |

Stay tuned for updates and releases of these new packages!

### Getting Started

To contribute to this monorepo or use its packages in your project, follow these setup steps:
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@
"@lottiefiles/prettier-config": "3.0.0",
"@lottiefiles/remark-preset": "1.0.0",
"@lottiefiles/tsconfig": "2.0.0",
"@size-limit/preset-big-lib": "^10.0.2",
"@size-limit/preset-big-lib": "^11.0.2",
"cross-env": "7.0.3",
"eslint": "7.32.0",
"husky": "8.0.3",
"lint-staged": "13.2.1",
"npm-check-updates": "16.10.9",
"prettier": "2.8.8",
"remark-cli": "11.0.0",
"size-limit": "^10.0.2",
"size-limit": "^11.0.2",
"svelte-loader": "^3.1.9",
"syncpack": "9.8.6",
"turbo": "1.10.16",
"typescript": "5.0.4",
Expand Down
13 changes: 13 additions & 0 deletions packages/svelte/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
31 changes: 31 additions & 0 deletions packages/svelte/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @type { import("eslint").Linter.Config } */
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:svelte/recommended',
'prettier'
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
extraFileExtensions: ['.svelte']
},
env: {
browser: true,
es2017: true,
node: true
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser'
}
}
]
};
11 changes: 11 additions & 0 deletions packages/svelte/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
node_modules
/build
/dist
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions packages/svelte/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
4 changes: 4 additions & 0 deletions packages/svelte/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
8 changes: 8 additions & 0 deletions packages/svelte/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
210 changes: 210 additions & 0 deletions packages/svelte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# @lottiefiles/dotlottie-svelte

![npm](https://img.shields.io/npm/v/@lottiefiles/dotlottie-svelte)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40lottiefiles%2Fdotlottie-svelte)
![npm](https://img.shields.io/npm/dt/%40lottiefiles/dotlottie-svelte)
![NPM](https://img.shields.io/npm/l/@lottiefiles/dotlottie-svelte)

<p align="center">
<img src="https://user-images.githubusercontent.com/23125742/201124166-c2a0bc2a-018b-463b-b291-944fb767b5c2.png" />
</p>

> 🚧 **Beta Alert:** We're still refining! The APIs in this package may undergo changes.
## Contents

* [Introduction](#introduction)
* [What is dotLottie?](#what-is-dotlottie)
* [Installation](#installation)
* [Usage](#usage)
* [Live Examples](#live-examples)
* [APIs](#apis)
* [Props](#props)
* [Custom Playback Controls](#custom-playback-controls)
* [Listening to Events](#listening-to-events)
* [Development](#development)
* [Setup](#setup)
* [Dev](#dev)
* [Build](#build)

## Introduction

A Svelte component for rendering [Lottie](https://lottiefiles.github.io/lottie-docs/) and [dotLottie](https://dotlottie.io) animations. It acts as a wrapper around the [`dotLottie`](../web/README.md) web player.

### What is dotLottie?

dotLottie is an open-source file format that aggregates one or more Lottie files and their associated resources into a single file. They are ZIP archives compressed with the Deflate compression method and carry the file extension of ".lottie".

[Learn more about dotLottie](https://dotlottie.io/).

## Installation

```bash
npm install @lottiefiles/dotlottie-svelte
```

## Usage

```svelte
<script lang="ts">
import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
</script>
<DotLottieSvelte
src="path/to/animation.lottie"
loop
autoplay
/>
```

## Live Examples

## APIs

### Props

| Property name | Type | Required | Default | Description | |
| ----------------------- | ------------------------------------------------------- | :------: | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - |
| `autoplay` | boolean | | false | Auto-starts the animation on load. | |
| `loop` | boolean | | false | Determines if the animation should loop. | |
| `src` | string | | undefined | URL to the animation data (`.json` or `.lottie`). | |
| `speed` | number | | 1 | Animation playback speed. 1 is regular speed. | |
| `data` | string \| ArrayBuffer | | undefined | Animation data provided either as a Lottie JSON string or as an ArrayBuffer for .lottie animations. | |
| `mode` | string | | "forward" | Animation play mode. Accepts "forward", "reverse", "bounce", "reverse-bounce". | |
| `backgroundColor` | string | | undefined | Background color of the canvas. Accepts 6-digit or 8-digit hex color string (e.g., "#000000", "#000000FF"), | |
| `segments` | \[number, number] | | undefined | Animation segments. Accepts an array of two numbers, where the first number is the start frame and the second number is the end frame. | |
| `renderConfig` | RenderConfig | | undefined | Configuration for rendering the animation. | |
| `dotLottieRefCallback` | (dotLottie: [DotLottie](../web/README.md#apis)) => void | | undefined | Callback function that receives a reference to the [`dotLottie`](../web/README.md) web player instance once instantiated. | |
| `useFrameInterpolation` | boolean | | true | Determines if the animation should update on subframes. If set to false, the original AE frame rate will be maintained. If set to true, it will refresh at each requestAnimationFrame, including intermediate values. The default setting is true. | |
| `autoResizeCanvas` | boolean | | true | Determines if the canvas should resize automatically to its container | |

#### RenderConfig

The `renderConfig` object accepts the following properties:

| Property name | Type | Required | Default | Description |
| ------------------ | ------ | :------: | ----------------------------- | ----------------------- |
| `devicePixelRatio` | number | | window\.devicePixelRatio \| 1 | The device pixel ratio. |

## Custom Playback Controls

`DotLottieSvelte` component makes it easy to build custom playback controls for the animation. It exposes a `dotLottieRefCallback` prop that can be used to get a reference to the [`dotLottie`](../web/README.md#apis) web player instance. This instance can be used to control the playback of the animation using the methods exposed by the [`dotLottie`](../web/README.md#methods) web player instance.

Here is an example:

```svelte
<script lang="ts">
import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
import type { DotLottie } from '@lottiefiles/dotlottie-svelte';
let dotLottie: DotLottie | null = null;
function play() {
dotLottie?.play();
}
function pause() {
dotLottie?.pause();
}
function stop() {
dotLottie?.stop();
}
</script>
<DotLottieSvelte
src="path/to/your/animation.lottie"
loop={true}
autoplay={true}
dotLottieRefCallback={(ref) => dotLottie = ref}
/>
<button on:click={play}>Play</button>
<button on:click={pause}>Pause</button>
<button on:click={stop}>Stop</button>
```

You can find the list of methods that can be used to control the playback of the animation [here](../web/README.md#methods).

## Listening to Events

`DotLottieSvelte` component can receive a `dotLottieRefCallback` prop that can be used to get a reference to the [`dotLottie`](../web/README.md#apis) web player instance. This reference can be used to listen to player events emitted by the [`dotLottie`](../web/README.md#events) web instance.

Here is an example:

```svelte
<script lang="ts">
import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
import type { DotLottie } from '@lottiefiles/dotlottie-svelte';
let dotLottie: DotLottie | null = null;
function onLoaded() {
console.log("Animation loaded");
}
function onPlay() {
console.log("Animation started");
}
function onPause() {
console.log("Animation paused");
}
function onComplete() {
console.log("Animation completed");
}
function setupListeners(dotLottieInstance) {
dotLottieInstance.addEventListener('load', onLoaded);
dotLottieInstance.addEventListener('play', onPlay);
dotLottieInstance.addEventListener('pause', onPause);
dotLottieInstance.addEventListener('complete', onComplete);
}
function removeListeners(dotLottieInstance) {
dotLottieInstance.removeEventListener('load', onLoaded);
dotLottieInstance.removeEventListener('play', onPlay);
dotLottieInstance.removeEventListener('pause', onPause);
dotLottieInstance.removeEventListener('complete', onComplete);
}
onDestroy(() => {
if (dotLottie) {
removeListeners(dotLottie);
}
});
</script>
<DotLottieSvelte
src="path/to/your/animation.lottie"
loop={true}
autoplay={true}
dotLottieRefCallback={(ref) => {
dotLottie = ref;
setupListeners(dotLottie);
}}
/>
```

[dotLottie](../web/README.md#apis) instance exposes multiple events that can be listened to. You can find the list of events [here](../web/README.md#events).

## Development

### Setup

```bash
pnpm install
```

### Dev

```bash
pnpm dev
```

### Build

```bash
pnpm build
```
Loading

0 comments on commit 826f791

Please sign in to comment.