Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new registerEases option #44

Merged
merged 1 commit into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GSAP module for Nuxt.
- Helps you integrate the GSAP animation library
- Provides a solution for global use
- Automatically registers plugins after activation
- Allows you to easily register global effects
- Allows you to easily register global effects & eases
- Supports Club GreenSock premium plugins
- Zero-config setup ready to go
- TypeScript friendly
Expand Down Expand Up @@ -452,6 +452,45 @@ $gsap.effects.fade('.class')
$gsap.effects.slideIn('#id')
```

## Register Eases

- Type: `object[]`
- Default: `undefined`

Provides an easy way to register global eases.

Once the ease is registered, it can be accessed directly on the `gsap` animations.

```ts
// nuxt.config.ts

{
gsap: {
registerEases: [
{
name: 'customEase',
ease: progress => {
return progress // linear
}
},
{
name: 'customEase2'
// ...
}
]
}
}
```

**Available globally**

```ts
const { $gsap } = useNuxtApp()

$gsap.to('.class', { x: 100, ease: 'customEase' })
$gsap.to('#id', { x: 200, ease: 'customEase2' })
```

## Club Plugins

Specifies GSAP premium plugins.
Expand Down
20 changes: 16 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export default defineNuxtModule<ModuleOptions>({
extraPlugins: plugins,
extraEases: eases,
clubPlugins: club,
registerEffects: effects
registerEffects: regEffects,
registerEases: regEases
} = options

const pluginImport: string[] = []
const pluginRegister: string[] = []
const pluginType: string[] = []
const pluginEffect: string[] = []
const pluginEase: string[] = []
const pluginClient: string[] = []

const addPlugin = ({
Expand Down Expand Up @@ -81,22 +83,32 @@ export default defineNuxtModule<ModuleOptions>({
if (club?.customWiggle) addPlugin({ name: 'CustomWiggle' })

// Global Effects
if (effects)
effects.forEach(effect =>
if (regEffects)
regEffects.forEach(effect =>
pluginEffect.push(`gsap.registerEffect(${stringify(effect)});`)
)

// Global Eases
if (regEases)
regEases.forEach(ease =>
pluginEase.push(
`gsap.registerEase(${stringify(ease.name)}, ${stringify(ease.ease)});`
)
)

// Client mode
if (plugins || eases || club || effects) {
if (plugins || eases || club || regEffects || regEases) {
const registerPlugin = pluginRegister.length
? `gsap.registerPlugin(${pluginRegister.join(',')});`
: ''
const registerEffect = pluginEffect.length ? pluginEffect.join('\n') : ''
const registerEase = pluginEase.length ? pluginEase.join('\n') : ''

pluginClient.push(
`if(process.client) {`,
` ${registerPlugin}`,
` ${registerEffect}`,
` ${registerEase}`,
`}`
)
}
Expand Down
25 changes: 25 additions & 0 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ export interface ModuleOptions {
* @since 2.2.0
*/
registerEffects?: object[]
/**
* Provides an easy way to register global eases.
*
* Once the ease is registered, it can be accessed directly on the `gsap` animations.
*
* @example
*
* ```ts
* {
* registerEases: [
* {
* name: 'customEase',
* ease: progress => {
* return progress // linear
* },
* }
* ]
* }
* ```
*
* @default undefined
*
* @since 2.2.0
*/
registerEases?: { name: string; ease: { (progress: number): number } }[]
}

declare module '@nuxt/schema' {
Expand Down