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">
|
<script setup lang="ts">
|
||||||
import { motion } from 'motion-v';
|
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 {
|
interface TrueFocusProps {
|
||||||
sentence?: string;
|
sentence?: string;
|
||||||
@@ -10,6 +10,9 @@ interface TrueFocusProps {
|
|||||||
glowColor?: string;
|
glowColor?: string;
|
||||||
animationDuration?: number;
|
animationDuration?: number;
|
||||||
pauseBetweenAnimations?: number;
|
pauseBetweenAnimations?: number;
|
||||||
|
index?: Array<number>;
|
||||||
|
syncGroup?: string;
|
||||||
|
defaultBlur?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<TrueFocusProps>(), {
|
const props = withDefaults(defineProps<TrueFocusProps>(), {
|
||||||
@@ -19,11 +22,13 @@ const props = withDefaults(defineProps<TrueFocusProps>(), {
|
|||||||
borderColor: 'green',
|
borderColor: 'green',
|
||||||
glowColor: 'rgba(0, 255, 0, 0.6)',
|
glowColor: 'rgba(0, 255, 0, 0.6)',
|
||||||
animationDuration: 0.5,
|
animationDuration: 0.5,
|
||||||
pauseBetweenAnimations: 1
|
pauseBetweenAnimations: 1,
|
||||||
|
defaultBlur: true
|
||||||
});
|
});
|
||||||
|
|
||||||
const words = computed(() => props.sentence.split(' '));
|
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 lastActiveIndex = ref<number | null>(null);
|
||||||
const containerRef = useTemplateRef<HTMLDivElement>('containerRef');
|
const containerRef = useTemplateRef<HTMLDivElement>('containerRef');
|
||||||
const wordRefs = ref<HTMLSpanElement[]>([]);
|
const wordRefs = ref<HTMLSpanElement[]>([]);
|
||||||
@@ -35,12 +40,19 @@ watch(
|
|||||||
[currentIndex, () => words.value.length],
|
[currentIndex, () => words.value.length],
|
||||||
async () => {
|
async () => {
|
||||||
if (currentIndex.value === null || currentIndex.value === -1) return;
|
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();
|
await nextTick();
|
||||||
|
|
||||||
const parentRect = containerRef.value.getBoundingClientRect();
|
const parentRect = containerRef.value.getBoundingClientRect();
|
||||||
const activeRect = wordRefs.value[currentIndex.value].getBoundingClientRect();
|
const activeRect = wordRefs.value[actualWordIndex].getBoundingClientRect();
|
||||||
|
|
||||||
focusRect.value = {
|
focusRect.value = {
|
||||||
x: activeRect.left - parentRect.left,
|
x: activeRect.left - parentRect.left,
|
||||||
@@ -54,46 +66,28 @@ watch(
|
|||||||
|
|
||||||
const handleMouseEnter = (index: number) => {
|
const handleMouseEnter = (index: number) => {
|
||||||
if (props.manualMode) {
|
if (props.manualMode) {
|
||||||
lastActiveIndex.value = index;
|
const mappedIndex = props.index ? props.index[index] : index;
|
||||||
currentIndex.value = index;
|
lastActiveIndex.value = mappedIndex;
|
||||||
|
currentIndex.value = mappedIndex;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMouseLeave = () => {
|
const handleMouseLeave = () => {
|
||||||
if (props.manualMode) {
|
if (props.manualMode) {
|
||||||
|
if (props.defaultBlur) {
|
||||||
currentIndex.value = lastActiveIndex.value || 0;
|
currentIndex.value = lastActiveIndex.value || 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentIndex.value = -1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const setWordRef = (el: HTMLSpanElement | null, index: number) => {
|
const setWordRef = (el: HTMLSpanElement | null, index: number) => {
|
||||||
if (el) {
|
if (el) wordRefs.value[index] = el;
|
||||||
wordRefs.value[index] = el;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
onMounted(async () => {
|
const startInterval = () => {
|
||||||
await nextTick();
|
if (interval) clearInterval(interval);
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(
|
|
||||||
[() => props.manualMode, () => props.animationDuration, () => props.pauseBetweenAnimations, () => words.value],
|
|
||||||
() => {
|
|
||||||
if (interval) {
|
|
||||||
clearInterval(interval);
|
|
||||||
interval = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!props.manualMode) {
|
if (!props.manualMode) {
|
||||||
interval = setInterval(
|
interval = setInterval(
|
||||||
() => {
|
() => {
|
||||||
@@ -102,27 +96,88 @@ onMounted(async () => {
|
|||||||
(props.animationDuration + props.pauseBetweenAnimations) * 1000
|
(props.animationDuration + props.pauseBetweenAnimations) * 1000
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
await nextTick();
|
||||||
|
|
||||||
|
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,
|
||||||
|
width: activeRect.width,
|
||||||
|
height: activeRect.height
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isOwner) {
|
||||||
|
watch(
|
||||||
|
[() => props.manualMode, () => props.animationDuration, () => props.pauseBetweenAnimations, () => words.value],
|
||||||
|
() => startInterval(),
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
);
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
if (interval) {
|
if (interval) clearInterval(interval);
|
||||||
clearInterval(interval);
|
if (props.syncGroup) unregisterSyncGroup(props.syncGroup);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
</script>
|
</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>
|
<template>
|
||||||
<div class="relative flex flex-wrap justify-center items-center gap-[1em]" ref="containerRef">
|
<div class="relative flex flex-wrap justify-center items-center gap-[1em]" ref="containerRef">
|
||||||
<span
|
<span
|
||||||
v-for="(word, index) in words"
|
v-for="(word, index) in words"
|
||||||
:key="index"
|
:key="props.index ? props.index[index] : index"
|
||||||
:ref="el => setWordRef(el as HTMLSpanElement, index)"
|
:ref="el => setWordRef(el as HTMLSpanElement, index)"
|
||||||
class="relative font-black text-7xl transition-[filter,color] duration-300 ease-in-out cursor-pointer"
|
class="relative font-black text-7xl transition-[filter,color] duration-300 ease-in-out cursor-pointer"
|
||||||
:style="{
|
: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,
|
'--border-color': borderColor,
|
||||||
'--glow-color': glowColor,
|
'--glow-color': glowColor,
|
||||||
transition: `filter ${animationDuration}s ease`
|
transition: `filter ${animationDuration}s ease`
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
<template #preview>
|
<template #preview>
|
||||||
<div class="demo-container h-[400px]">
|
<div class="demo-container h-[400px]">
|
||||||
<div :key="key" class="flex flex-col justify-center items-center m-8 pl-6 w-full">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -12,6 +14,10 @@
|
|||||||
|
|
||||||
<PreviewSwitch title="Hover Mode" v-model="manualMode" />
|
<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 title="Blur Amount" v-model="blurAmount" :min="0" :max="15" :step="0.5" value-unit="px" />
|
||||||
|
|
||||||
<PreviewSlider
|
<PreviewSlider
|
||||||
@@ -72,6 +78,8 @@ const blurAmount = ref(5);
|
|||||||
const animationDuration = ref(0.5);
|
const animationDuration = ref(0.5);
|
||||||
const pauseBetweenAnimations = ref(1);
|
const pauseBetweenAnimations = ref(1);
|
||||||
const borderColor = ref('#27FF64');
|
const borderColor = ref('#27FF64');
|
||||||
|
const syncMode = ref(false);
|
||||||
|
const defaultBlur = ref(true);
|
||||||
|
|
||||||
const config = computed(() => ({
|
const config = computed(() => ({
|
||||||
sentence: 'True Focus',
|
sentence: 'True Focus',
|
||||||
@@ -79,7 +87,9 @@ const config = computed(() => ({
|
|||||||
blurAmount: blurAmount.value,
|
blurAmount: blurAmount.value,
|
||||||
borderColor: borderColor.value,
|
borderColor: borderColor.value,
|
||||||
animationDuration: animationDuration.value,
|
animationDuration: animationDuration.value,
|
||||||
pauseBetweenAnimations: pauseBetweenAnimations.value
|
pauseBetweenAnimations: pauseBetweenAnimations.value,
|
||||||
|
syncGroup: syncMode.value ? 'sync-group-demo' : undefined,
|
||||||
|
defaultBlur: defaultBlur.value
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const propData = [
|
const propData = [
|
||||||
@@ -124,6 +134,27 @@ const propData = [
|
|||||||
type: 'number',
|
type: 'number',
|
||||||
default: '1',
|
default: '1',
|
||||||
description: 'Time to pause between focusing on each word (in auto mode).'
|
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>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user