svelte-lottie-player icon indicating copy to clipboard operation
svelte-lottie-player copied to clipboard

Add typescript declarations file

Open jneuendorf opened this issue 1 year ago • 9 comments

Closes #16.

Please double check, if the types are correct and complete 😅

jneuendorf avatar Sep 02 '23 20:09 jneuendorf

Could you add more player's methods to the type declaration file. That make controlling player via script more comfortable.

megaheart avatar Sep 15 '23 09:09 megaheart

I have just researched LottiePlayer.svelte file and add some methods here. However, I don't know whether my type declaration is correct or not.

import type { SvelteComponentTyped } from 'svelte'

export class LottiePlayer extends SvelteComponentTyped<{
    autoplay?: boolean
    background: string
    controls: boolean
    controlsLayout?: string[]
    count?: number
    defaultFrame?: number
    direction?: number
    height: number
    hover?: boolean
    loop?: boolean
    mode?: 'normal' | 'bounce'
    onToggleZoom?: (isZoomed: boolean) => void
    renderer?: 'svg' | 'canvas'
    speed?: number
    src?: string
    style?: string
    width: number
}> {
    /**
     * Returns the lottie-web version and this player's version
     */
    getVersions(): { lottieWebVersion: string, svelteLottiePlayerVersion: string };
    /**
     * Returns the lottie-web instance used in the component.
     */
    getLottie(): any;
    /**
     * Pause animation play.
     */
    pause(): void;
    /**
     * Start playing animation.
     */
    play(): void;
    /**
     * Stops animation play.
     */
    stop(): void;
    /**
     * Freeze animation play.
     * This internal state pauses animation and is used to differentiate between
     * user requested pauses and component instigated pauses.
     */
    freeze(): void;
    /**
     * Resize animation.
     */
    resize(): void;
    /**
     * Seek to a given frame.
     * @param frame Frame number or Percent string to seek to.
     */
    seek(frame: number|string): void;
    /**
     * Snapshot the current frame as SVG.
     * @param download If 'download' argument is boolean true, then a download is triggered in browser.
     */
    snapshot(download?: boolean): void;
    /**
     * Sets the looping of the animation.
     * @param value Whether to enable looping. Boolean true enables looping.
     */
    setLooping(value: boolean): void;
    /**
     * Sets the speed of the animation.
     * @param value The speed of the animation. 1 is normal speed.
     */
    setSpeed(value: number): void;
    /**
     * Animation play direction.
     * @param value Direction values.
     */
    setDirection(value: number): void;
    /**
     * Toggle playing state.
     */
    togglePlay(): void;
    /**
     * Toggle zoom state.
     */
    toggleZoom(): void;
    /**
     * Toggles animation looping.
     */
    toggleLooping(): void;
    /**
     * Sets background color.
     */
    setBackgroundColor(color: string): void;
    

}

megaheart avatar Sep 15 '23 10:09 megaheart

@megaheart Thanks for the suggestion and input. 🙏 I pretty much used your code except for getLottie and setBackground 😉

jneuendorf avatar Sep 17 '23 20:09 jneuendorf

@jneuendorf I think your package.json should also have a types field:

// package.json
  {
    ...
+   "types": "index.d.ts"
    ...
  }

See Including declarations in your npm package.

jakemckown avatar Jan 07 '24 18:01 jakemckown

@jneuendorf I think your package.json should also have a types field:

// package.json
  {
    ...
+   "types": "index.d.ts"
    ...
  }

See Including declarations in your npm package.

@jakemckown Thanks for the hint. Do you have a preference regarding the location of the file? As it seems both the src and the dist folder are part of the NPM package. Should it be placed in the dist folder rather than in the project root? I guess, this would require copying to dist and I am not sure what the best way to do this would be (extend build script or rollup config).

jneuendorf avatar Jan 30 '24 13:01 jneuendorf

@jneuendorf I think where you have it in the root is the best option for adding a declarations file to a non-TypeScript project. It technically doesn't matter as long as the types field points to it. But that does remind me that you also need to include it in the files field so that it gets published with the package.

// package.json
{
  ...
  "files": [
    "src/components",
-   "dist"
+   "dist",
+   "index.d.ts"
  ]
  ...
}

jakemckown avatar Jan 30 '24 16:01 jakemckown

⚠️ No Changeset found

Latest commit: 45a4aec2bc0c36291e45ce1388d5ec36dd4da0c4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Jan 30 '24 19:01 changeset-bot[bot]

Hey, what's the hold up on this pull request?

pjarnfelt avatar Mar 06 '24 12:03 pjarnfelt

Can this be merge or is there anything missing I can help with?

For now, I've copy pasted the contents in index.d.ts and added them to src/ambient.d.ts.

a4vg avatar Apr 23 '24 20:04 a4vg