@@ -14,6 +14,9 @@ export interface QuizLayoutConfig {
1414
1515const quizLayout = ( config : QuizLayoutConfig ) => {
1616 const form = window . TutorCore ?. form ;
17+ const docWithViewTransition = document as Document & {
18+ startViewTransition ?: ( callback : ( ) => void ) => { finished : Promise < void > } ;
19+ } ;
1720
1821 return {
1922 layout : config . layout ?? QuizLayoutType . QUESTION_BELOW_EACH_OTHER ,
@@ -139,8 +142,10 @@ const quizLayout = (config: QuizLayoutConfig) => {
139142 return ;
140143 }
141144 if ( this . currentIndex > 1 ) {
142- this . currentIndex -= 1 ;
143- this . revealFooterState = '' ;
145+ this . runWithViewTransition ( ( ) => {
146+ this . currentIndex -= 1 ;
147+ this . revealFooterState = '' ;
148+ } , 'back' ) ;
144149 this . scrollToQuestion ( ) ;
145150 }
146151 } ,
@@ -174,18 +179,22 @@ const quizLayout = (config: QuizLayoutConfig) => {
174179 const wait = this . getRevealWaitTime ( ) ;
175180 window . setTimeout ( ( ) => {
176181 this . isRevealing = false ;
177- this . revealFooterState = '' ;
178182 if ( this . currentIndex < this . totalQuestions ) {
179- this . currentIndex += 1 ;
183+ this . runWithViewTransition ( ( ) => {
184+ this . currentIndex += 1 ;
185+ this . revealFooterState = '' ;
186+ } ) ;
180187 this . scrollToQuestion ( ) ;
181188 }
182189 } , wait ) ;
183190 return ;
184191 }
185192
186193 if ( this . currentIndex < this . totalQuestions ) {
187- this . currentIndex += 1 ;
188- this . revealFooterState = '' ;
194+ this . runWithViewTransition ( ( ) => {
195+ this . currentIndex += 1 ;
196+ this . revealFooterState = '' ;
197+ } ) ;
189198 this . scrollToQuestion ( ) ;
190199 }
191200 } ,
@@ -197,11 +206,35 @@ const quizLayout = (config: QuizLayoutConfig) => {
197206 if ( ! index || index < 1 || index > this . totalQuestions ) {
198207 return ;
199208 }
200- this . currentIndex = index ;
201- this . revealFooterState = '' ;
209+ this . runWithViewTransition (
210+ ( ) => {
211+ this . currentIndex = index ;
212+ this . revealFooterState = '' ;
213+ } ,
214+ index < this . currentIndex ? 'back' : 'forward' ,
215+ ) ;
202216 this . scrollToQuestion ( ) ;
203217 } ,
204218
219+ runWithViewTransition ( update : ( ) => void , direction : 'forward' | 'back' = 'forward' ) {
220+ const transitionRoot = document . documentElement ;
221+ transitionRoot . style . setProperty ( '--tutor-quiz-vt-dir' , direction === 'back' ? '-1' : '1' ) ;
222+
223+ if ( ! docWithViewTransition . startViewTransition ) {
224+ update ( ) ;
225+ transitionRoot . style . removeProperty ( '--tutor-quiz-vt-dir' ) ;
226+ return ;
227+ }
228+
229+ const transition = docWithViewTransition . startViewTransition . call ( document , ( ) => {
230+ update ( ) ;
231+ } ) ;
232+
233+ transition . finished . finally ( ( ) => {
234+ transitionRoot . style . removeProperty ( '--tutor-quiz-vt-dir' ) ;
235+ } ) ;
236+ } ,
237+
205238 getRevealWaitTime ( ) : number {
206239 const feedbackWaitMs = Number ( this . revealWaitMs ?? '' ) ;
207240 if ( ! Number . isNaN ( feedbackWaitMs ) && feedbackWaitMs > 0 ) {
0 commit comments