@@ -72,8 +72,9 @@ additional containers, for instance to have a navigation menu that updates when
72
72
{% endblock %}
73
73
</head>
74
74
<body
75
- {{ stimulus_controller('symfony/ux-swup/swup') }}
76
- data-containers="#swup #nav" {# list of selectors separated by spaces #}
75
+ {{ stimulus_controller('symfony/ux-swup/swup', {
76
+ containers: ['#swup', '#nav']
77
+ }) }}
77
78
>
78
79
{# ... #}
79
80
@@ -88,26 +89,37 @@ additional containers, for instance to have a navigation menu that updates when
88
89
</html>
89
90
```
90
91
91
- You can configure several other options using data-attributes on the ` body ` tag:
92
+ You can configure several other options using values on the controller.
93
+ Most of these correspond to [ Swup Options] ( https://swup.js.org/options ) ,
94
+ but there are a few extra added:
92
95
93
96
``` twig
94
97
<html lang="en">
95
98
<head>
96
99
<title>Swup</title>
97
100
</head>
98
101
<body
99
- {{ stimulus_controller('symfony/ux-swup/swup') }}
100
- data-containers="#swup #nav"
101
- data-theme="slide" {# or "fade", the default #}
102
- data-debug="data-debug" {# add this attribute to enable debug #}
103
- data-cache="data-cache" {# add this attribute to enable local cache: be careful, it only makes sense for mostly static websites #}
104
- data-animate-history-browsing="data-animate-history-browsing" {# add this attribute to animate history browsing #}
102
+ {{ stimulus_controller('symfony/ux-swup/swup', {
103
+ containers: ['#swup', '#nav'],
104
+ animateHistoryBrowsing: true
105
+ animationSelector: '[class*="transition-"]',
106
+ cache: true,
107
+ linkSelector: '...',
108
+
109
+ theme: 'slide',
110
+ debug: true,
111
+ }) }}
105
112
>
106
113
{# ... #}
107
114
</body>
108
115
</html>
109
116
```
110
117
118
+ The extra options are:
119
+
120
+ - ` theme ` : either ` slide ` or ` fade ` (the default);
121
+ - ` debug ` : add this attribute to enable debug.
122
+
111
123
### Extend the default behavior
112
124
113
125
Symfony UX Swup allows you to extend its default behavior using a custom Stimulus controller:
@@ -119,12 +131,19 @@ import { Controller } from '@hotwired/stimulus';
119
131
120
132
export default class extends Controller {
121
133
connect () {
134
+ this .element .addEventListener (' swup:pre-connect' , this ._onPreConnect );
122
135
this .element .addEventListener (' swup:connect' , this ._onConnect );
123
136
}
124
137
125
138
disconnect () {
126
139
// You should always remove listeners when the controller is disconnected to avoid side-effects
127
- this .element .removeEventListener (' swup:connect' , this ._onConnect );
140
+ this .element .removeEventListener (' swup:pre-connect' , this ._onConnect );
141
+ this .element .removeEventListener (' swup:connect' , this ._onPreConnect );
142
+ }
143
+
144
+ _onPreConnect (event ) {
145
+ // Swup has not been initialized - options can be changed
146
+ console .log (event .detail .options ); // Options that will be used to initialize Swup
128
147
}
129
148
130
149
_onConnect (event ) {
0 commit comments