From afbb2749517870b6e18f4144bfa8f14961d729da Mon Sep 17 00:00:00 2001 From: hta218 Date: Mon, 14 Aug 2023 17:05:49 +0700 Subject: [PATCH] fix: update typed bio on locale change --- components/homepage/TypedBios.tsx | 70 +++++++++++++++++-------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/components/homepage/TypedBios.tsx b/components/homepage/TypedBios.tsx index 4fee2d4e..56ad12e7 100644 --- a/components/homepage/TypedBios.tsx +++ b/components/homepage/TypedBios.tsx @@ -1,41 +1,49 @@ -import React from 'react' +import { useTranslation } from 'next-i18next' +import { useEffect, useRef } from 'react' import Typed from 'typed.js' import { Twemoji } from '../Twemoji' -import { useTranslation } from 'next-i18next' +import { useLocale } from '~/hooks/useLocale' + +function createTypedInstance(el: HTMLElement) { + return new Typed(el, { + stringsElement: '#bios', + typeSpeed: 40, + backSpeed: 10, + loop: true, + backDelay: 1000, + }) +} export function TypedBios() { - let el = React.useRef(null) - let typed = React.useRef(null) - let { t } = useTranslation('common') + let el = useRef(null) + let typed = useRef(null) + let [locale] = useLocale() + let tr = useTranslation('common') - React.useEffect(() => { - typed.current = new Typed(el.current, { - stringsElement: '#bios', - typeSpeed: 40, - backSpeed: 10, - loop: true, - backDelay: 1000, - }) - return () => typed.current.destroy() - }, []) + useEffect(() => { + if (tr.ready) { + typed.current?.destroy() + typed.current = createTypedInstance(el.current) + } + }, [locale, tr]) return (