1
1
import "@hotwired/turbo-rails" ;
2
2
import {
3
3
createAll ,
4
+ isSupported ,
4
5
Button ,
5
6
Checkboxes ,
6
7
ErrorSummary ,
@@ -15,8 +16,7 @@ import { UpgradedRadios as Radios } from "./components/radios.js";
15
16
// Configure Turbo
16
17
Turbo . session . drive = false ;
17
18
18
- // Initiate NHS.UK frontend components on page load
19
- document . addEventListener ( "DOMContentLoaded" , ( ) => {
19
+ function initializeComponents ( ) {
20
20
createAll ( Autocomplete ) ;
21
21
createAll ( Button , { preventDoubleClick : true } ) ;
22
22
createAll ( Checkboxes ) ;
@@ -25,4 +25,30 @@ document.addEventListener("DOMContentLoaded", () => {
25
25
createAll ( Radios ) ;
26
26
createAll ( NotificationBanner ) ;
27
27
createAll ( SkipLink ) ;
28
+ }
29
+
30
+ // Initiate components once page has loaded
31
+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
32
+ initializeComponents ( ) ;
33
+ } ) ;
34
+
35
+ // Reinitialize components when Turbo loads (and preloads) a page
36
+ document . addEventListener ( "turbo:load" , ( event ) => {
37
+ // Skip if initial page load (already handled by DOMContentLoaded)
38
+ if ( event . detail ?. timing ?. visitStart ) {
39
+ if ( ! isSupported ( ) ) {
40
+ document . body . classList . add ( "nhsuk-frontend-supported" ) ;
41
+ }
42
+
43
+ initializeComponents ( ) ;
44
+ }
45
+ } ) ;
46
+
47
+ // Reinitialize components when Turbo morphs a page
48
+ document . addEventListener ( "turbo:morph" , ( ) => {
49
+ if ( ! isSupported ( ) ) {
50
+ document . body . classList . add ( "nhsuk-frontend-supported" ) ;
51
+ }
52
+
53
+ initializeComponents ( ) ;
28
54
} ) ;
0 commit comments