mirror of
https://github.com/DavidHDev/vue-bits.git
synced 2026-04-21 17:44:39 -06:00
Merge pull request #145 from Jian-GitHub/main
[Feat]: TrueFocus - Add defaultBlur prop and syncGroup multi-instance synchronization
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { motion } from 'motion-v';
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch, useTemplateRef } from 'vue';
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch, useTemplateRef, type Ref } from 'vue';
|
||||
|
||||
interface TrueFocusProps {
|
||||
sentence?: string;
|
||||
@@ -10,6 +10,9 @@ interface TrueFocusProps {
|
||||
glowColor?: string;
|
||||
animationDuration?: number;
|
||||
pauseBetweenAnimations?: number;
|
||||
index?: Array<number>;
|
||||
syncGroup?: string;
|
||||
defaultBlur?: boolean;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<TrueFocusProps>(), {
|
||||
@@ -19,11 +22,13 @@ const props = withDefaults(defineProps<TrueFocusProps>(), {
|
||||
borderColor: 'green',
|
||||
glowColor: 'rgba(0, 255, 0, 0.6)',
|
||||
animationDuration: 0.5,
|
||||
pauseBetweenAnimations: 1
|
||||
pauseBetweenAnimations: 1,
|
||||
defaultBlur: true
|
||||
});
|
||||
|
||||
const words = computed(() => props.sentence.split(' '));
|
||||
const currentIndex = ref(0);
|
||||
|
||||
const currentIndex = props.syncGroup ? getSyncGroupIndex(props.syncGroup) : ref(-1);
|
||||
const lastActiveIndex = ref<number | null>(null);
|
||||
const containerRef = useTemplateRef<HTMLDivElement>('containerRef');
|
||||
const wordRefs = ref<HTMLSpanElement[]>([]);
|
||||
@@ -35,12 +40,19 @@ watch(
|
||||
[currentIndex, () => words.value.length],
|
||||
async () => {
|
||||
if (currentIndex.value === null || currentIndex.value === -1) return;
|
||||
if (!wordRefs.value[currentIndex.value] || !containerRef.value) return;
|
||||
|
||||
let actualWordIndex = currentIndex.value;
|
||||
if (props.index) {
|
||||
actualWordIndex = props.index.findIndex(val => val === currentIndex.value);
|
||||
if (actualWordIndex === -1) return;
|
||||
}
|
||||
|
||||
if (!wordRefs.value[actualWordIndex] || !containerRef.value) return;
|
||||
|
||||
await nextTick();
|
||||
|
||||
const parentRect = containerRef.value.getBoundingClientRect();
|
||||
const activeRect = wordRefs.value[currentIndex.value].getBoundingClientRect();
|
||||
const activeRect = wordRefs.value[actualWordIndex].getBoundingClientRect();
|
||||
|
||||
focusRect.value = {
|
||||
x: activeRect.left - parentRect.left,
|
||||
@@ -54,30 +66,52 @@ watch(
|
||||
|
||||
const handleMouseEnter = (index: number) => {
|
||||
if (props.manualMode) {
|
||||
lastActiveIndex.value = index;
|
||||
currentIndex.value = index;
|
||||
const mappedIndex = props.index ? props.index[index] : index;
|
||||
lastActiveIndex.value = mappedIndex;
|
||||
currentIndex.value = mappedIndex;
|
||||
}
|
||||
};
|
||||
|
||||
const handleMouseLeave = () => {
|
||||
if (props.manualMode) {
|
||||
currentIndex.value = lastActiveIndex.value || 0;
|
||||
if (props.defaultBlur) {
|
||||
currentIndex.value = lastActiveIndex.value || 0;
|
||||
return;
|
||||
}
|
||||
currentIndex.value = -1;
|
||||
}
|
||||
};
|
||||
|
||||
const setWordRef = (el: HTMLSpanElement | null, index: number) => {
|
||||
if (el) {
|
||||
wordRefs.value[index] = el;
|
||||
if (el) wordRefs.value[index] = el;
|
||||
};
|
||||
|
||||
const startInterval = () => {
|
||||
if (interval) clearInterval(interval);
|
||||
if (!props.manualMode) {
|
||||
interval = setInterval(
|
||||
() => {
|
||||
currentIndex.value = (currentIndex.value + 1) % words.value.length;
|
||||
},
|
||||
(props.animationDuration + props.pauseBetweenAnimations) * 1000
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(async () => {
|
||||
await nextTick();
|
||||
|
||||
if (wordRefs.value[0] && containerRef.value) {
|
||||
const parentRect = containerRef.value.getBoundingClientRect();
|
||||
const activeRect = wordRefs.value[0].getBoundingClientRect();
|
||||
const isOwner = props.syncGroup ? registerSyncGroup(props.syncGroup) : true;
|
||||
|
||||
let initialWordIndex = 0;
|
||||
if (props.index && currentIndex.value !== null && currentIndex.value !== undefined) {
|
||||
const foundIndex = props.index.findIndex(val => val === currentIndex.value);
|
||||
if (foundIndex !== -1) initialWordIndex = foundIndex;
|
||||
}
|
||||
|
||||
if (wordRefs.value[initialWordIndex] && containerRef.value) {
|
||||
const parentRect = containerRef.value.getBoundingClientRect();
|
||||
const activeRect = wordRefs.value[initialWordIndex].getBoundingClientRect();
|
||||
focusRect.value = {
|
||||
x: activeRect.left - parentRect.left,
|
||||
y: activeRect.top - parentRect.top,
|
||||
@@ -86,43 +120,64 @@ onMounted(async () => {
|
||||
};
|
||||
}
|
||||
|
||||
watch(
|
||||
[() => props.manualMode, () => props.animationDuration, () => props.pauseBetweenAnimations, () => words.value],
|
||||
() => {
|
||||
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 }
|
||||
);
|
||||
if (isOwner) {
|
||||
watch(
|
||||
[() => props.manualMode, () => props.animationDuration, () => props.pauseBetweenAnimations, () => words.value],
|
||||
() => startInterval(),
|
||||
{ immediate: true }
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
if (interval) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
if (interval) clearInterval(interval);
|
||||
if (props.syncGroup) unregisterSyncGroup(props.syncGroup);
|
||||
});
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
interface SyncGroupState {
|
||||
index: Ref<number>;
|
||||
count: number;
|
||||
}
|
||||
|
||||
export const syncGroupMap = new Map<string, SyncGroupState>();
|
||||
|
||||
export function getSyncGroupIndex(group: string): Ref<number> {
|
||||
if (!syncGroupMap.has(group)) {
|
||||
syncGroupMap.set(group, { index: ref(-1), count: 0 });
|
||||
}
|
||||
return syncGroupMap.get(group)!.index;
|
||||
}
|
||||
|
||||
export function registerSyncGroup(group: string): boolean {
|
||||
const state = syncGroupMap.get(group)!;
|
||||
state.count += 1;
|
||||
return state.count === 1;
|
||||
}
|
||||
|
||||
export function unregisterSyncGroup(group: string): void {
|
||||
if (!syncGroupMap.has(group)) return;
|
||||
const state = syncGroupMap.get(group)!;
|
||||
state.count -= 1;
|
||||
if (state.count <= 0) {
|
||||
syncGroupMap.delete(group);
|
||||
}
|
||||
}
|
||||
</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"
|
||||
:key="props.index ? props.index[index] : index"
|
||||
:ref="el => setWordRef(el as HTMLSpanElement, index)"
|
||||
class="relative font-black text-7xl transition-[filter,color] duration-300 ease-in-out cursor-pointer"
|
||||
:style="{
|
||||
filter: index === currentIndex ? 'blur(0px)' : `blur(${blurAmount}px)`,
|
||||
filter:
|
||||
currentIndex === -1 || (props.index ? props.index[index] : index) === currentIndex
|
||||
? 'blur(0px)'
|
||||
: `blur(${blurAmount}px)`,
|
||||
'--border-color': borderColor,
|
||||
'--glow-color': glowColor,
|
||||
transition: `filter ${animationDuration}s ease`
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
<template #preview>
|
||||
<div class="demo-container h-[400px]">
|
||||
<div :key="key" class="flex flex-col justify-center items-center m-8 pl-6 w-full">
|
||||
<TrueFocus :key="key" v-bind="config" />
|
||||
<TrueFocus :key="`${key}-${syncMode}-1`" v-bind="config" />
|
||||
<br />
|
||||
<TrueFocus :key="`${key}-${syncMode}-2`" v-bind="config" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -12,6 +14,10 @@
|
||||
|
||||
<PreviewSwitch title="Hover Mode" v-model="manualMode" />
|
||||
|
||||
<PreviewSwitch title="Apply Sync Group" v-model="syncMode" />
|
||||
|
||||
<PreviewSwitch title="Default Blur" v-model="defaultBlur" />
|
||||
|
||||
<PreviewSlider title="Blur Amount" v-model="blurAmount" :min="0" :max="15" :step="0.5" value-unit="px" />
|
||||
|
||||
<PreviewSlider
|
||||
@@ -72,6 +78,8 @@ const blurAmount = ref(5);
|
||||
const animationDuration = ref(0.5);
|
||||
const pauseBetweenAnimations = ref(1);
|
||||
const borderColor = ref('#27FF64');
|
||||
const syncMode = ref(false);
|
||||
const defaultBlur = ref(true);
|
||||
|
||||
const config = computed(() => ({
|
||||
sentence: 'True Focus',
|
||||
@@ -79,7 +87,9 @@ const config = computed(() => ({
|
||||
blurAmount: blurAmount.value,
|
||||
borderColor: borderColor.value,
|
||||
animationDuration: animationDuration.value,
|
||||
pauseBetweenAnimations: pauseBetweenAnimations.value
|
||||
pauseBetweenAnimations: pauseBetweenAnimations.value,
|
||||
syncGroup: syncMode.value ? 'sync-group-demo' : undefined,
|
||||
defaultBlur: defaultBlur.value
|
||||
}));
|
||||
|
||||
const propData = [
|
||||
@@ -124,6 +134,27 @@ const propData = [
|
||||
type: 'number',
|
||||
default: '1',
|
||||
description: 'Time to pause between focusing on each word (in auto mode).'
|
||||
},
|
||||
{
|
||||
name: 'index',
|
||||
type: 'Array<number>',
|
||||
default: 'undefined',
|
||||
description:
|
||||
'Maps each word to a shared index value, used to coordinate focus position across multiple instances in the same syncGroup.'
|
||||
},
|
||||
{
|
||||
name: 'syncGroup',
|
||||
type: 'string',
|
||||
default: 'undefined',
|
||||
description:
|
||||
'A group identifier. All instances sharing the same syncGroup will stay in sync, hovering or animating one will reflect on all others.'
|
||||
},
|
||||
{
|
||||
name: 'defaultBlur',
|
||||
type: 'boolean',
|
||||
default: 'true',
|
||||
description:
|
||||
'In manualMode, determines behavior on mouse leave: true restores focus to the last hovered word, false clears all blur.'
|
||||
}
|
||||
];
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user