-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directives): add new directives (#6)
- Loading branch information
1 parent
804411a
commit c0aac41
Showing
4 changed files
with
93 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,98 @@ | ||
import { directiveHooks } from "@vueuse/core"; | ||
import { defineNuxtPlugin } from "#app"; | ||
import nuggetBakedPresets from "#build/nugget/presets.mjs"; | ||
import { type AnimationOptions, DirectiveOptions } from "../baked"; | ||
import { vAos } from "./use-animate-on-scroll/directive"; | ||
import { vFromTo } from "./use-gsap/directive"; | ||
import { vTextAnimate } from "./use-split-text-animation/directive"; | ||
import type { BakedPresets } from "#build/types/nugget"; | ||
import type { UseBakedAnimationOptions } from "../baked"; | ||
import { | ||
type UseBakedAnimateOnScrollOptions, | ||
useBakedAnimateOnScroll, | ||
vAos, | ||
} from "./use-animate-on-scroll"; | ||
import { useBakedFromTo, vFromTo } from "./use-gsap"; | ||
import { | ||
type UseBakedSplitTextAnimationOptions, | ||
useBakedSplitTextAnimation, | ||
vTextAnimate, | ||
} from "./use-split-text-animation"; | ||
|
||
type BakedPresetsArray = (keyof { | ||
[P in keyof BakedPresets as | ||
| P | ||
| (BakedPresets[P] extends infer V | ||
? V extends string | ||
? `${P}:${V}` | ||
: never | ||
: never)]: true; | ||
})[]; | ||
|
||
const transformBakedArrayToObject = <const T extends BakedPresetsArray>(v: T) => | ||
Object.fromEntries( | ||
v.map((val) => { | ||
const [key, value] = val.split(":"); | ||
return [key, value === undefined ? true : value]; | ||
}), | ||
) as { | ||
[P in T[number] as P extends `${infer K}:${string}` | ||
? K | ||
: P]: P extends `${string}:${infer V}` ? V : true; | ||
}; | ||
|
||
export default defineNuxtPlugin((nuxt) => { | ||
const app = nuxt.vueApp; | ||
|
||
for (const key in nuggetBakedPresets) { | ||
const typedKey = key as keyof AnimationOptions; | ||
nuggetBakedPresets; | ||
app.directive("text-animate", vTextAnimate); | ||
|
||
app.directive<HTMLElement, UseBakedSplitTextAnimationOptions>( | ||
"text-animate-baked", | ||
{ | ||
[directiveHooks.mounted]: (el, binding) => { | ||
useBakedSplitTextAnimation(el, binding.value); | ||
}, | ||
}, | ||
); | ||
|
||
for (const splitBy of ["lines", "words", "chars"] as const) { | ||
app.directive<HTMLElement, BakedPresetsArray>( | ||
`text-animate-baked-${splitBy}`, | ||
{ | ||
[directiveHooks.mounted]: (el, binding) => { | ||
useBakedSplitTextAnimation(el, { | ||
splitBy, | ||
animationOptions: transformBakedArrayToObject(binding.value), | ||
}); | ||
}, | ||
}, | ||
); | ||
} | ||
|
||
app.directive("yo", (el, binding) => {}); | ||
app.directive("aos", vAos); | ||
|
||
app.directive<HTMLElement, UseBakedAnimateOnScrollOptions>("aos-baked", { | ||
[directiveHooks.mounted]: (el, binding) => { | ||
useBakedAnimateOnScroll(el, binding.value); | ||
}, | ||
}); | ||
|
||
app.directive<HTMLElement, BakedPresetsArray>("aos-baked-animate", { | ||
[directiveHooks.mounted]: (el, binding) => { | ||
useBakedAnimateOnScroll(el, { | ||
animationOptions: transformBakedArrayToObject(binding.value), | ||
}); | ||
}, | ||
}); | ||
|
||
app.directive("from-to", vFromTo); | ||
|
||
app.directive<HTMLElement, UseBakedAnimationOptions>("from-to-baked", { | ||
[directiveHooks.mounted]: (el, binding) => { | ||
useBakedFromTo(el, binding.value); | ||
}, | ||
}); | ||
|
||
nuxt.vueApp.directive("text-animate", vTextAnimate); | ||
nuxt.vueApp.directive("aos", vAos); | ||
nuxt.vueApp.directive("from-to", vFromTo); | ||
app.directive<HTMLElement, BakedPresetsArray>("from-to-baked-animate", { | ||
[directiveHooks.mounted]: (el, binding) => { | ||
useBakedFromTo(el, { | ||
animationOptions: transformBakedArrayToObject(binding.value), | ||
}); | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters