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:
dong.nguyen committed 2026-10-18 23:45:47 +07:00
1 parent 0c4aea32b5
commit 63afc1b505
8 files changed
+71 -41

No files matched your search

+27 -21
View File
@@ -1,17 +1,19 @@
import './App.css' import './App.css'
import { lazy, Suspense } from 'react'
import { BrowserRouter, Routes, Route } from 'react-router' import { BrowserRouter, Routes, Route } from 'react-router'
import { ThemeProvider } from '@/hooks/useTheme' import { ThemeProvider } from '@/hooks/useTheme'
import { Layout } from '@/components/layout/Layout' import { Layout } from '@/components/layout/Layout'
import { LanguageProvider } from '@/hooks/useLanguage' import { LanguageProvider } from '@/hooks/useLanguage'
import { import { HomePage } from '@/pages/HomePage'
HomePage,
AboutPage, // HomePage stays eager: it is the landing route and owns the LCP, so its code must
ExperiencePage, // already be in the entry chunk. The other six split out and load on navigation.
ProjectsListPage, const AboutPage = lazy(() => import('@/pages/AboutPage').then((m) => ({ default: m.AboutPage })))
ProjectDetailPage, const ExperiencePage = lazy(() => import('@/pages/ExperiencePage').then((m) => ({ default: m.ExperiencePage })))
PhilosophyPage, const ProjectsListPage = lazy(() => import('@/pages/ProjectsListPage').then((m) => ({ default: m.ProjectsListPage })))
ContactPage, const ProjectDetailPage = lazy(() => import('@/pages/ProjectDetailPage').then((m) => ({ default: m.ProjectDetailPage })))
} from '@/pages' const PhilosophyPage = lazy(() => import('@/pages/PhilosophyPage').then((m) => ({ default: m.PhilosophyPage })))
const ContactPage = lazy(() => import('@/pages/ContactPage').then((m) => ({ default: m.ContactPage })))
function App() { function App() {
return ( return (
@@ -19,18 +21,22 @@ function App() {
<ThemeProvider> <ThemeProvider>
<LanguageProvider> <LanguageProvider>
<Layout> <Layout>
<Routes> {/* A null fallback keeps the shell (nav, footer) painted without reserving a
<Route path="/" element={<HomePage />} /> box that would shift the page when the route chunk lands. */}
<Route path="/about" element={<AboutPage />} /> <Suspense fallback={null}>
<Route path="/experience" element={<ExperiencePage />} /> <Routes>
<Route path="/projects" element={<ProjectsListPage />} /> <Route path="/" element={<HomePage />} />
<Route path="/projects/:id" element={<ProjectDetailPage />} /> <Route path="/about" element={<AboutPage />} />
<Route path="/philosophy" element={<PhilosophyPage />} /> <Route path="/experience" element={<ExperiencePage />} />
<Route path="/contact" element={<ContactPage />} /> <Route path="/projects" element={<ProjectsListPage />} />
</Routes> <Route path="/projects/:id" element={<ProjectDetailPage />} />
</Layout> <Route path="/philosophy" element={<PhilosophyPage />} />
</LanguageProvider> <Route path="/contact" element={<ContactPage />} />
</ThemeProvider> </Routes>
</Suspense>
</Layout>
</LanguageProvider>
</ThemeProvider>
</BrowserRouter> </BrowserRouter>
) )
} }
+3 -3
View File
@@ -57,7 +57,7 @@ export function Navigation() {
return ( 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-container">
<div className="nav-brand"> <div className="nav-brand">
{/* Trigger first, brand next: the hamburger is the sheet's own control and sits at {/* Trigger first, brand next: the hamburger is the sheet's own control and sits at
@@ -143,8 +143,8 @@ export function Navigation() {
<button <button
className="nav-control theme-toggle" className="nav-control theme-toggle"
onClick={cycleTheme} onClick={cycleTheme}
aria-label={`Switch theme (currently ${resolvedTheme})`} aria-label={`${strings.nav.themeSwitch[lang]} (${resolvedTheme === 'dark' ? strings.nav.themeDark[lang] : strings.nav.themeLight[lang]})`}
title={`Theme: ${resolvedTheme}`} title={`${strings.nav.themeMode[lang]}: ${resolvedTheme === 'dark' ? strings.nav.themeDark[lang] : strings.nav.themeLight[lang]}`}
> >
{resolvedTheme === 'dark' ? ( {resolvedTheme === 'dark' ? (
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"> <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
+4 -1
View File
@@ -135,9 +135,12 @@ export function Hero() {
> >
<motion.img <motion.img
src="/profile-photo.webp" src="/profile-photo.webp"
alt="Profile photo" alt={field(lang, profile.name, profile.nameEn)}
className="hero-photo" className="hero-photo"
style={{y}} style={{y}}
width="1200"
height="1600"
fetchPriority="high"
/> />
</motion.div> </motion.div>
</motion.section> </motion.section>
+7 -3
View File
@@ -1,10 +1,14 @@
import { useEffect, useState } from "react" 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 const SCROLL_THRESHOLD = 300
export function BackToTop() { export function BackToTop() {
const [visible, setVisible] = useState(false) const [visible, setVisible] = useState(false)
const reduceMotion = useReducedMotion()
const { lang } = useLanguage()
useEffect(() => { useEffect(() => {
const handleScroll = () => { const handleScroll = () => {
@@ -16,7 +20,7 @@ export function BackToTop() {
}, []) }, [])
const scrollToTop = () => { const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: "smooth" }) window.scrollTo({ top: 0, behavior: reduceMotion ? 'auto' : 'smooth' })
} }
return ( return (
@@ -24,7 +28,7 @@ export function BackToTop() {
{visible && ( {visible && (
<motion.button <motion.button
className="back-to-top" className="back-to-top"
aria-label="Scroll to top" aria-label={strings.common.backToTop[lang]}
onClick={scrollToTop} onClick={scrollToTop}
initial={{ opacity: 0, y: 10 }} initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }} animate={{ opacity: 1, y: 0 }}
+14 -6
View File
@@ -1,4 +1,5 @@
import { useEffect, useState } from "react" import { useEffect, useState } from "react"
import { useReducedMotion } from "motion/react"
interface TypewriterProps { interface TypewriterProps {
texts: string[] texts: string[]
@@ -13,11 +14,14 @@ export function Typewriter({
deleteSpeed = 40, deleteSpeed = 40,
pauseDuration = 2000, pauseDuration = 2000,
}: TypewriterProps) { }: TypewriterProps) {
const reduceMotion = useReducedMotion()
const [currentTextIndex, setCurrentTextIndex] = useState(0) const [currentTextIndex, setCurrentTextIndex] = useState(0)
const [currentText, setCurrentText] = useState("") const [currentText, setCurrentText] = useState("")
const [isDeleting, setIsDeleting] = useState(false) const [isDeleting, setIsDeleting] = useState(false)
// Reduced motion: no typing loop at all — the first role is rendered whole below.
useEffect(() => { useEffect(() => {
if (reduceMotion) return
const text = texts[currentTextIndex] const text = texts[currentTextIndex]
let timeout: number let timeout: number
@@ -40,12 +44,16 @@ export function Typewriter({
} }
return () => clearTimeout(timeout) return () => clearTimeout(timeout)
}, [currentText, currentTextIndex, isDeleting, texts, typeSpeed, deleteSpeed, pauseDuration]) }, [reduceMotion, currentText, currentTextIndex, isDeleting, texts, typeSpeed, deleteSpeed, pauseDuration])
return ( return (
<span> <>
{currentText} {/* Assistive tech gets the whole role list once; the typed prefix is decoration. */}
<span className="typewriter-cursor" /> <span className="sr-only">{texts.join(', ')}</span>
</span> <span aria-hidden="true">
{reduceMotion ? texts[0] : currentText}
<span className="typewriter-cursor" />
</span>
</>
) )
} }
+4
View File
@@ -21,6 +21,9 @@ export const strings = {
langVi: {en: 'Tiếng Việt', vi: 'Tiếng Việt'}, langVi: {en: 'Tiếng Việt', vi: 'Tiếng Việt'},
langSwitch: {en: 'Switch language', vi: 'Đổi ngôn ngữ'}, langSwitch: {en: 'Switch language', vi: 'Đổi ngôn ngữ'},
switchTo: {en: 'Switch to', vi: 'Chuyển sang'}, 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: { social: {
heading: {en: 'Connect', vi: 'Kết nối'}, heading: {en: 'Connect', vi: 'Kết nối'},
@@ -113,6 +116,7 @@ export const strings = {
}, },
common: { common: {
resumeDownload: {en: 'Get Resume', vi: 'Tải CV'}, 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ệ'}, contactTag: {en: 'Contact', vi: 'Liên hệ'},
experienceTag: {en: 'Experience', vi: 'Kinh nghiệm'}, experienceTag: {en: 'Experience', vi: 'Kinh nghiệm'},
}, },
+12
View File
@@ -332,6 +332,18 @@ pre code {
padding: 80px 0; 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) { @media (max-width: 768px) {
.section { .section {
padding: 48px 0; padding: 48px 0;
-7
View File
@@ -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'