Merge pull request #2 from Utkarsh-Singhal-26/feat/true-focus

Create <TrueFocus /> text animation
This commit is contained in:
David
2025-07-12 10:06:07 +03:00
committed by GitHub
5 changed files with 333 additions and 1 deletions

View File

@@ -19,7 +19,8 @@ export const CATEGORIES = [
'Falling Text',
'Text Cursor',
'Decrypted Text',
'Scroll Float'
'True Focus',
'Scroll Float',
]
},
{

View File

@@ -23,6 +23,7 @@ const textAnimations = {
'falling-text': () => import("../demo/TextAnimations/FallingTextDemo.vue"),
'text-cursor': () => import("../demo/TextAnimations/TextCursorDemo.vue"),
'decrypted-text': () => import("../demo/TextAnimations/DecryptedTextDemo.vue"),
'true-focus': () => import("../demo/TextAnimations/TrueFocusDemo.vue"),
'scroll-float': () => import("../demo/TextAnimations/ScrollFloatDemo.vue"),
};

View File

@@ -0,0 +1,22 @@
import code from "@/content/TextAnimations/TrueFocus/TrueFocus.vue?raw";
import type { CodeObject } from "../../../types/code";
export const trueFocus: CodeObject = {
cli: `npx jsrepo add https://vue-bits.dev/ui/TextAnimations/TrueFocus`,
installation: `npm install motion-v`,
usage: `<template>
<TrueFocus
sentence="True Focus"
:manualMode="false"
:blurAmount="5"
borderColor="red"
:animationDuration="2"
:pauseBetweenAnimations="1"
/>
</template>
<script setup lang="ts">
import TrueFocus from "./TrueFocus.vue";
</script>`,
code,
};

View File

@@ -0,0 +1,164 @@
<script setup lang="ts">
import { motion } from "motion-v";
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
interface TrueFocusProps {
sentence?: string;
manualMode?: boolean;
blurAmount?: number;
borderColor?: string;
glowColor?: string;
animationDuration?: number;
pauseBetweenAnimations?: number;
}
const props = withDefaults(defineProps<TrueFocusProps>(), {
sentence: "True Focus",
manualMode: false,
blurAmount: 5,
borderColor: "green",
glowColor: "rgba(0, 255, 0, 0.6)",
animationDuration: 0.5,
pauseBetweenAnimations: 1,
});
const words = computed(() => props.sentence.split(" "));
const currentIndex = ref(0);
const lastActiveIndex = ref<number | null>(null);
const containerRef = ref<HTMLDivElement>();
const wordRefs = ref<HTMLSpanElement[]>([]);
const focusRect = ref({ x: 0, y: 0, width: 0, height: 0 });
let interval: number | null = null;
watch(
[
() => props.manualMode,
() => props.animationDuration,
() => props.pauseBetweenAnimations,
words,
],
() => {
if (interval) {
clearInterval(interval);
interval = null;
}
if (!props.manualMode) {
interval = setInterval(
() => {
currentIndex.value = (currentIndex.value + 1) % words.value.length;
},
(props.animationDuration + props.pauseBetweenAnimations) * 1000,
);
}
},
{ immediate: true },
);
watch(
[currentIndex, words.value.length],
async () => {
if (currentIndex.value === null || currentIndex.value === -1) return;
if (!wordRefs.value[currentIndex.value] || !containerRef.value) return;
await nextTick();
const parentRect = containerRef.value.getBoundingClientRect();
const activeRect = wordRefs.value[currentIndex.value].getBoundingClientRect();
focusRect.value = {
x: activeRect.left - parentRect.left,
y: activeRect.top - parentRect.top,
width: activeRect.width,
height: activeRect.height,
};
},
{ immediate: true },
);
const handleMouseEnter = (index: number) => {
if (props.manualMode) {
lastActiveIndex.value = index;
currentIndex.value = index;
}
};
const handleMouseLeave = () => {
if (props.manualMode) {
currentIndex.value = lastActiveIndex.value || 0;
}
};
const setWordRef = (el: HTMLSpanElement | null, index: number) => {
if (el) {
wordRefs.value[index] = el;
}
};
onMounted(async () => {
await nextTick();
if (wordRefs.value[0] && containerRef.value) {
const parentRect = containerRef.value.getBoundingClientRect();
const activeRect = wordRefs.value[0].getBoundingClientRect();
focusRect.value = {
x: activeRect.left - parentRect.left,
y: activeRect.top - parentRect.top,
width: activeRect.width,
height: activeRect.height,
};
}
});
onUnmounted(() => {
if (interval) {
clearInterval(interval);
}
});
</script>
<template>
<div class="relative flex flex-wrap justify-center items-center gap-[1em]" ref="containerRef">
<span
v-for="(word, index) in words"
:key="index"
:ref="(el) => setWordRef(el as HTMLSpanElement, index)"
class="relative font-black text-5xl transition-[filter,color] duration-300 ease-in-out cursor-pointer"
:style="{
filter: index === currentIndex ? 'blur(0px)' : `blur(${blurAmount}px)`,
'--border-color': borderColor,
'--glow-color': glowColor,
transition: `filter ${animationDuration}s ease`,
}"
@mouseenter="handleMouseEnter(index)"
@mouseleave="handleMouseLeave"
>
{{ word }}
</span>
<motion.div
class="top-0 left-0 box-content absolute border-none pointer-events-none"
:animate="{
x: focusRect.x,
y: focusRect.y,
width: focusRect.width,
height: focusRect.height,
opacity: currentIndex >= 0 ? 1 : 0,
}"
:transition="{
duration: animationDuration,
}"
:style="{
'--border-color': borderColor,
'--glow-color': glowColor,
}"
>
<span class="top-[-10px] left-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-r-0 border-b-0 rounded-[3px] w-4 h-4 transition-none"></span>
<span class="top-[-10px] right-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-b-0 border-l-0 rounded-[3px] w-4 h-4 transition-none"></span>
<span class="bottom-[-10px] left-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-t-0 border-r-0 rounded-[3px] w-4 h-4 transition-none"></span>
<span class="right-[-10px] bottom-[-10px] absolute [filter:drop-shadow(0_0_4px_var(--border-color,#fff))] border-[3px] border-[var(--border-color,#fff)] border-t-0 border-l-0 rounded-[3px] w-4 h-4 transition-none"></span>
</motion.div>
</div>
</template>

View File

@@ -0,0 +1,144 @@
<template>
<div class="truefocus-demo">
<TabbedLayout>
<template #preview>
<div class="relative py-6 overflow-hidden demo-container" style="min-height: 200px">
<div :key="key" class="flex flex-col justify-center items-center m-8 pl-6 w-full">
<TrueFocus v-bind="config" />
</div>
</div>
<Customize>
<PreviewColor title="Text Color" v-model="borderColor" @update:model-value="forceRerender" />
<PreviewSwitch
title="Hover Mode"
v-model="manualMode"
@update:model-value="forceRerender"
/>
<PreviewSlider
title="Blur Amount"
v-model="blurAmount"
:min="0"
:max="15"
:step="0.5"
value-unit="px"
@update:model-value="forceRerender"
/>
<PreviewSlider
title="Animation Duration"
v-model="animationDuration"
:min="0.1"
:max="3"
:step="0.1"
value-unit="s"
:disabled="!manualMode"
@update:model-value="forceRerender"
/>
<PreviewSlider
title="Pause Between Animations"
v-model="pauseBetweenAnimations"
:min="0"
:max="5"
:step="0.5"
value-unit="s"
:disabled="manualMode"
@update:model-value="forceRerender"
/>
</Customize>
<PropTable :data="propData" />
<Dependencies :dependency-list="['framer-motion']" />
</template>
<template #code>
<CodeExample :code-object="trueFocus" />
</template>
<template #cli>
<CliInstallation :command="trueFocus.cli" />
</template>
</TabbedLayout>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from "vue";
import TabbedLayout from "../../components/common/TabbedLayout.vue";
import PropTable from "../../components/common/PropTable.vue";
import CliInstallation from "../../components/code/CliInstallation.vue";
import CodeExample from "../../components/code/CodeExample.vue";
import Dependencies from "../../components/code/Dependencies.vue";
import Customize from "../../components/common/Customize.vue";
import PreviewColor from "../../components/common/PreviewColor.vue";
import PreviewSlider from "../../components/common/PreviewSlider.vue";
import PreviewSwitch from "../../components/common/PreviewSwitch.vue";
import TrueFocus from "../../content/TextAnimations/TrueFocus/TrueFocus.vue";
import { trueFocus } from "../../constants/code/TextAnimations/trueFocusCode";
import { useForceRerender } from "@/composables/useForceRerender";
const { rerenderKey: key, forceRerender } = useForceRerender();
const manualMode = ref(false);
const blurAmount = ref(5);
const animationDuration = ref(0.5);
const pauseBetweenAnimations = ref(1);
const borderColor = ref("#27FF64");
const config = computed(() => ({
sentence: "True Focus",
manualMode: manualMode.value,
blurAmount: blurAmount.value,
borderColor: borderColor.value,
animationDuration: animationDuration.value,
pauseBetweenAnimations: pauseBetweenAnimations.value,
}));
const propData = [
{
name: "sentence",
type: "string",
default: "'True Focus'",
description: "The text to display with the focus animation.",
},
{
name: "manualMode",
type: "boolean",
default: "false",
description: "Disables automatic animation when set to true.",
},
{
name: "blurAmount",
type: "number",
default: "5",
description: "The amount of blur applied to non-active words.",
},
{
name: "borderColor",
type: "string",
default: "'green'",
description: "The color of the focus borders.",
},
{
name: "glowColor",
type: "string",
default: "'rgba(0, 255, 0, 0.6)'",
description: "The color of the glowing effect on the borders.",
},
{
name: "animationDuration",
type: "number",
default: "0.5",
description: "The duration of the animation for each word.",
},
{
name: "pauseBetweenAnimations",
type: "number",
default: "1",
description: "Time to pause between focusing on each word (in auto mode).",
},
];
</script>