@@ -12,8 +12,9 @@ export type Flashcard = {
12
12
13
13
const Container = styled . div ``
14
14
15
- const FlashcardContainer = styled . div ( ( { theme } ) => ( {
15
+ const FlashcardContainer = styled . button ( ( { theme } ) => ( {
16
16
display : "flex" ,
17
+ width : "100%" ,
17
18
height : 300 ,
18
19
padding : 40 ,
19
20
flexDirection : "column" ,
@@ -25,6 +26,7 @@ const FlashcardContainer = styled.div(({ theme }) => ({
25
26
marginTop : "8px" ,
26
27
cursor : "pointer" ,
27
28
textAlign : "center" ,
29
+ background : "none" ,
28
30
} ) )
29
31
30
32
const Navigation = styled . div ( {
@@ -40,48 +42,54 @@ const Page = styled.div(({ theme }) => ({
40
42
...theme . typography . body2 ,
41
43
} ) )
42
44
43
- const Flashcard = React . forwardRef <
44
- HTMLDivElement ,
45
- { content : Flashcard ; "aria-label" : string }
46
- > ( ( { content, "aria-label" : ariaLabel } , ref ) => {
47
- const [ screen , setScreen ] = useState < 0 | 1 > ( 0 )
45
+ const Flashcard = React . forwardRef < HTMLButtonElement , { content : Flashcard } > (
46
+ ( { content, ...others } , ref ) => {
47
+ const [ screen , setScreen ] = useState < 0 | 1 > ( 0 )
48
48
49
- useEffect ( ( ) => setScreen ( 0 ) , [ content ] )
49
+ useEffect ( ( ) => setScreen ( 0 ) , [ content ] )
50
50
51
- const handleKeyDown = ( e : React . KeyboardEvent ) => {
52
- if ( e . key === "Enter" || e . key === " " ) {
53
- setScreen ( screen === 0 ? 1 : 0 )
51
+ const handleClick = ( ) => {
52
+ setScreen ( ( current ) => ( current === 0 ? 1 : 0 ) )
54
53
}
55
- }
56
54
57
- return (
58
- < FlashcardContainer
59
- ref = { ref }
60
- onClick = { ( ) => setScreen ( screen === 0 ? 1 : 0 ) }
61
- onKeyDown = { handleKeyDown }
62
- tabIndex = { 0 }
63
- aria-label = { ariaLabel }
64
- role = "button"
65
- >
66
- < Typography variant = "h5" >
67
- { screen === 0 ? `Q: ${ content . question } ` : `Answer: ${ content . answer } ` }
68
- </ Typography >
69
- </ FlashcardContainer >
70
- )
71
- } )
55
+ return (
56
+ < FlashcardContainer
57
+ ref = { ref }
58
+ type = "button"
59
+ onClick = { handleClick }
60
+ tabIndex = { 0 }
61
+ { ...others }
62
+ >
63
+ < Typography variant = "h5" >
64
+ { screen === 0 ? (
65
+ < >
66
+ < span aria-label = "Question:" > Q: </ span >
67
+ { content . question }
68
+ </ >
69
+ ) : (
70
+ < >
71
+ < span > Answer: </ span >
72
+ { content . answer }
73
+ </ >
74
+ ) }
75
+ </ Typography >
76
+ </ FlashcardContainer >
77
+ )
78
+ } ,
79
+ )
72
80
73
81
Flashcard . displayName = "Flashcard"
74
82
75
83
export const FlashcardsScreen = ( {
76
84
flashcards,
77
85
wasKeyboardFocus,
78
86
} : {
79
- flashcards ? : Flashcard [ ]
87
+ flashcards : Flashcard [ ]
80
88
wasKeyboardFocus : boolean
81
89
} ) => {
82
90
const [ cardIndex , setCardIndex ] = useState ( 0 )
83
91
const containerRef = useRef < HTMLDivElement > ( null )
84
- const flashcardRef = useRef < HTMLDivElement > ( null )
92
+ const flashcardRef = useRef < HTMLButtonElement > ( null )
85
93
86
94
const handleKeyDown = useCallback (
87
95
( e : KeyboardEvent ) => {
@@ -112,17 +120,14 @@ export const FlashcardsScreen = ({
112
120
return ( ) => window . removeEventListener ( "keydown" , handleKeyDown )
113
121
} , [ handleKeyDown ] )
114
122
115
- if ( ! flashcards ?. length ) {
116
- return null
117
- }
118
-
119
123
return (
120
124
< Container ref = { containerRef } >
121
- < Flashcard
122
- ref = { flashcardRef }
123
- content = { flashcards [ cardIndex ] }
125
+ < div
126
+ role = "region"
124
127
aria-label = { `Flashcard ${ cardIndex + 1 } of ${ flashcards . length } ` }
125
- />
128
+ >
129
+ < Flashcard ref = { flashcardRef } content = { flashcards [ cardIndex ] } />
130
+ </ div >
126
131
< Navigation >
127
132
< ActionButton
128
133
onClick = { ( ) => setCardIndex ( cardIndex - 1 ) }
@@ -134,7 +139,8 @@ export const FlashcardsScreen = ({
134
139
>
135
140
< RiArrowLeftLine aria-hidden />
136
141
</ ActionButton >
137
- < Page >
142
+ { /* Hide the index count here. It's used as a region label above. */ }
143
+ < Page aria-hidden >
138
144
{ cardIndex + 1 } / { flashcards . length }
139
145
</ Page >
140
146
< ActionButton
0 commit comments