mirror of
https://github.com/dong-nz/cv.git
synced 2026-11-23 00:20:40 +07:00
fix(a11y): reduced motion, screen-reader text and route-level splitting
Typewriter reads useReducedMotion: under reduced motion the timer never starts and the first role renders whole, while a screen-reader-only span exposes the full role list once instead of one keystroke at a time. BackToTop scrolls instantly under reduced motion and is labelled in the active language; the nav landmark and the mode toggle stop being English-only. The hero photo declares its measured 1200x1600 canvas plus fetch priority so the box is reserved before the 898 kB image decodes. HomePage stays eagerly imported because it owns the LCP; the other six routes lazy-load behind one Suspense boundary, which is also why the pages barrel is gone.
This commit is contained in:
1 parent
0c4aea32b5
commit
63afc1b505
8 files changed
+57
-27
No files matched your search
+15
-9
@@ -1,17 +1,19 @@
|
||||
import './App.css'
|
||||
import { lazy, Suspense } from 'react'
|
||||
import { BrowserRouter, Routes, Route } from 'react-router'
|
||||
import { ThemeProvider } from '@/hooks/useTheme'
|
||||
import { Layout } from '@/components/layout/Layout'
|
||||
import { LanguageProvider } from '@/hooks/useLanguage'
|
||||
import {
|
||||
HomePage,
|
||||
AboutPage,
|
||||
ExperiencePage,
|
||||
ProjectsListPage,
|
||||
ProjectDetailPage,
|
||||
PhilosophyPage,
|
||||
ContactPage,
|
||||
} from '@/pages'
|
||||
import { HomePage } from '@/pages/HomePage'
|
||||
|
||||
// HomePage stays eager: it is the landing route and owns the LCP, so its code must
|
||||
// already be in the entry chunk. The other six split out and load on navigation.
|
||||
const AboutPage = lazy(() => import('@/pages/AboutPage').then((m) => ({ default: m.AboutPage })))
|
||||
const ExperiencePage = lazy(() => import('@/pages/ExperiencePage').then((m) => ({ default: m.ExperiencePage })))
|
||||
const ProjectsListPage = lazy(() => import('@/pages/ProjectsListPage').then((m) => ({ default: m.ProjectsListPage })))
|
||||
const ProjectDetailPage = lazy(() => import('@/pages/ProjectDetailPage').then((m) => ({ default: m.ProjectDetailPage })))
|
||||
const PhilosophyPage = lazy(() => import('@/pages/PhilosophyPage').then((m) => ({ default: m.PhilosophyPage })))
|
||||
const ContactPage = lazy(() => import('@/pages/ContactPage').then((m) => ({ default: m.ContactPage })))
|
||||
|
||||
function App() {
|
||||
return (
|
||||
@@ -19,6 +21,9 @@ function App() {
|
||||
<ThemeProvider>
|
||||
<LanguageProvider>
|
||||
<Layout>
|
||||
{/* A null fallback keeps the shell (nav, footer) painted without reserving a
|
||||
box that would shift the page when the route chunk lands. */}
|
||||
<Suspense fallback={null}>
|
||||
<Routes>
|
||||
<Route path="/" element={<HomePage />} />
|
||||
<Route path="/about" element={<AboutPage />} />
|
||||
@@ -28,6 +33,7 @@ function App() {
|
||||
<Route path="/philosophy" element={<PhilosophyPage />} />
|
||||
<Route path="/contact" element={<ContactPage />} />
|
||||
</Routes>
|
||||
</Suspense>
|
||||
</Layout>
|
||||
</LanguageProvider>
|
||||
</ThemeProvider>
|
||||
|
||||
@@ -57,7 +57,7 @@ export function Navigation() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<nav className="nav" role="navigation" aria-label="Main navigation">
|
||||
<nav className="nav" role="navigation" aria-label={strings.nav.mainNav[lang]}>
|
||||
<div className="nav-container">
|
||||
<div className="nav-brand">
|
||||
{/* Trigger first, brand next: the hamburger is the sheet's own control and sits at
|
||||
@@ -143,8 +143,8 @@ export function Navigation() {
|
||||
<button
|
||||
className="nav-control theme-toggle"
|
||||
onClick={cycleTheme}
|
||||
aria-label={`Switch theme (currently ${resolvedTheme})`}
|
||||
title={`Theme: ${resolvedTheme}`}
|
||||
aria-label={`${strings.nav.themeSwitch[lang]} (${resolvedTheme === 'dark' ? strings.nav.themeDark[lang] : strings.nav.themeLight[lang]})`}
|
||||
title={`${strings.nav.themeMode[lang]}: ${resolvedTheme === 'dark' ? strings.nav.themeDark[lang] : strings.nav.themeLight[lang]}`}
|
||||
>
|
||||
{resolvedTheme === 'dark' ? (
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||
|
||||
@@ -135,9 +135,12 @@ export function Hero() {
|
||||
>
|
||||
<motion.img
|
||||
src="/profile-photo.webp"
|
||||
alt="Profile photo"
|
||||
alt={field(lang, profile.name, profile.nameEn)}
|
||||
className="hero-photo"
|
||||
style={{y}}
|
||||
width="1200"
|
||||
height="1600"
|
||||
fetchPriority="high"
|
||||
/>
|
||||
</motion.div>
|
||||
</motion.section>
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import { motion, AnimatePresence } from "motion/react"
|
||||
import { motion, AnimatePresence, useReducedMotion } from "motion/react"
|
||||
import { strings } from '@/i18n/strings'
|
||||
import { useLanguage } from '@/hooks/useLanguage'
|
||||
|
||||
const SCROLL_THRESHOLD = 300
|
||||
|
||||
export function BackToTop() {
|
||||
const [visible, setVisible] = useState(false)
|
||||
const reduceMotion = useReducedMotion()
|
||||
const { lang } = useLanguage()
|
||||
|
||||
useEffect(() => {
|
||||
const handleScroll = () => {
|
||||
@@ -16,7 +20,7 @@ export function BackToTop() {
|
||||
}, [])
|
||||
|
||||
const scrollToTop = () => {
|
||||
window.scrollTo({ top: 0, behavior: "smooth" })
|
||||
window.scrollTo({ top: 0, behavior: reduceMotion ? 'auto' : 'smooth' })
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -24,7 +28,7 @@ export function BackToTop() {
|
||||
{visible && (
|
||||
<motion.button
|
||||
className="back-to-top"
|
||||
aria-label="Scroll to top"
|
||||
aria-label={strings.common.backToTop[lang]}
|
||||
onClick={scrollToTop}
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import { useReducedMotion } from "motion/react"
|
||||
|
||||
interface TypewriterProps {
|
||||
texts: string[]
|
||||
@@ -13,11 +14,14 @@ export function Typewriter({
|
||||
deleteSpeed = 40,
|
||||
pauseDuration = 2000,
|
||||
}: TypewriterProps) {
|
||||
const reduceMotion = useReducedMotion()
|
||||
const [currentTextIndex, setCurrentTextIndex] = useState(0)
|
||||
const [currentText, setCurrentText] = useState("")
|
||||
|
||||
const [isDeleting, setIsDeleting] = useState(false)
|
||||
|
||||
// Reduced motion: no typing loop at all — the first role is rendered whole below.
|
||||
useEffect(() => {
|
||||
if (reduceMotion) return
|
||||
const text = texts[currentTextIndex]
|
||||
let timeout: number
|
||||
|
||||
@@ -40,12 +44,16 @@ export function Typewriter({
|
||||
}
|
||||
|
||||
return () => clearTimeout(timeout)
|
||||
}, [currentText, currentTextIndex, isDeleting, texts, typeSpeed, deleteSpeed, pauseDuration])
|
||||
}, [reduceMotion, currentText, currentTextIndex, isDeleting, texts, typeSpeed, deleteSpeed, pauseDuration])
|
||||
|
||||
return (
|
||||
<span>
|
||||
{currentText}
|
||||
<>
|
||||
{/* Assistive tech gets the whole role list once; the typed prefix is decoration. */}
|
||||
<span className="sr-only">{texts.join(', ')}</span>
|
||||
<span aria-hidden="true">
|
||||
{reduceMotion ? texts[0] : currentText}
|
||||
<span className="typewriter-cursor" />
|
||||
</span>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -21,6 +21,9 @@ export const strings = {
|
||||
langVi: {en: 'Tiếng Việt', vi: 'Tiếng Việt'},
|
||||
langSwitch: {en: 'Switch language', vi: 'Đổi ngôn ngữ'},
|
||||
switchTo: {en: 'Switch to', vi: 'Chuyển sang'},
|
||||
mainNav: {en: 'Main navigation', vi: 'Điều hướng chính'},
|
||||
themeSwitch: {en: 'Switch display mode', vi: 'Đổi chế độ hiển thị'},
|
||||
themeMode: {en: 'Display mode', vi: 'Chế độ hiển thị'},
|
||||
},
|
||||
social: {
|
||||
heading: {en: 'Connect', vi: 'Kết nối'},
|
||||
@@ -113,6 +116,7 @@ export const strings = {
|
||||
},
|
||||
common: {
|
||||
resumeDownload: {en: 'Get Resume', vi: 'Tải CV'},
|
||||
backToTop: {en: 'Scroll to top', vi: 'Cuộn lên đầu trang'},
|
||||
contactTag: {en: 'Contact', vi: 'Liên hệ'},
|
||||
experienceTag: {en: 'Experience', vi: 'Kinh nghiệm'},
|
||||
},
|
||||
|
||||
@@ -332,6 +332,18 @@ pre code {
|
||||
padding: 80px 0;
|
||||
}
|
||||
|
||||
.sr-only {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip-path: inset(50%);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.section {
|
||||
padding: 48px 0;
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
export { HomePage } from './HomePage'
|
||||
export { AboutPage } from './AboutPage'
|
||||
export { ExperiencePage } from './ExperiencePage'
|
||||
export { ProjectsListPage } from './ProjectsListPage'
|
||||
export { ProjectDetailPage } from './ProjectDetailPage'
|
||||
export { PhilosophyPage } from './PhilosophyPage.tsx'
|
||||
export { ContactPage } from './ContactPage'
|
||||
Reference in new issue
Block a user