@@ -21,16 +21,15 @@ const wrapperClass = 'cdk-global-overlay-wrapper';
21
21
export class GlobalPositionStrategy implements PositionStrategy {
22
22
/** The overlay to which this strategy is attached. */
23
23
private _overlayRef : OverlayReference ;
24
- private _cssPosition : string = 'static' ;
25
- private _topOffset : string = '' ;
26
- private _bottomOffset : string = '' ;
27
- private _leftOffset : string = '' ;
28
- private _rightOffset : string = '' ;
29
- private _alignItems : string = '' ;
30
- private _justifyContent : string = '' ;
31
- private _width : string = '' ;
32
- private _height : string = '' ;
33
- private _isDisposed : boolean ;
24
+ private _cssPosition = 'static' ;
25
+ private _topOffset = '' ;
26
+ private _bottomOffset = '' ;
27
+ private _alignItems = '' ;
28
+ private _xPosition = '' ;
29
+ private _xOffset = '' ;
30
+ private _width = '' ;
31
+ private _height = '' ;
32
+ private _isDisposed = false ;
34
33
35
34
attach ( overlayRef : OverlayReference ) : void {
36
35
const config = overlayRef . getConfig ( ) ;
@@ -65,9 +64,8 @@ export class GlobalPositionStrategy implements PositionStrategy {
65
64
* @param value New left offset.
66
65
*/
67
66
left ( value : string = '' ) : this {
68
- this . _rightOffset = '' ;
69
- this . _leftOffset = value ;
70
- this . _justifyContent = 'flex-start' ;
67
+ this . _xOffset = value ;
68
+ this . _xPosition = 'left' ;
71
69
return this ;
72
70
}
73
71
@@ -87,9 +85,30 @@ export class GlobalPositionStrategy implements PositionStrategy {
87
85
* @param value New right offset.
88
86
*/
89
87
right ( value : string = '' ) : this {
90
- this . _leftOffset = '' ;
91
- this . _rightOffset = value ;
92
- this . _justifyContent = 'flex-end' ;
88
+ this . _xOffset = value ;
89
+ this . _xPosition = 'right' ;
90
+ return this ;
91
+ }
92
+
93
+ /**
94
+ * Sets the overlay to the start of the viewport, depending on the overlay direction.
95
+ * This will be to the left in LTR layouts and to the right in RTL.
96
+ * @param offset Offset from the edge of the screen.
97
+ */
98
+ start ( value : string = '' ) : this {
99
+ this . _xOffset = value ;
100
+ this . _xPosition = 'start' ;
101
+ return this ;
102
+ }
103
+
104
+ /**
105
+ * Sets the overlay to the end of the viewport, depending on the overlay direction.
106
+ * This will be to the right in LTR layouts and to the left in RTL.
107
+ * @param offset Offset from the edge of the screen.
108
+ */
109
+ end ( value : string = '' ) : this {
110
+ this . _xOffset = value ;
111
+ this . _xPosition = 'end' ;
93
112
return this ;
94
113
}
95
114
@@ -133,7 +152,7 @@ export class GlobalPositionStrategy implements PositionStrategy {
133
152
*/
134
153
centerHorizontally ( offset : string = '' ) : this {
135
154
this . left ( offset ) ;
136
- this . _justifyContent = 'center' ;
155
+ this . _xPosition = 'center' ;
137
156
return this ;
138
157
}
139
158
@@ -171,31 +190,45 @@ export class GlobalPositionStrategy implements PositionStrategy {
171
190
const shouldBeFlushVertically =
172
191
( height === '100%' || height === '100vh' ) &&
173
192
( ! maxHeight || maxHeight === '100%' || maxHeight === '100vh' ) ;
174
-
175
- styles . position = this . _cssPosition ;
176
- styles . marginLeft = shouldBeFlushHorizontally ? '0' : this . _leftOffset ;
177
- styles . marginTop = shouldBeFlushVertically ? '0' : this . _topOffset ;
178
- styles . marginBottom = this . _bottomOffset ;
179
- styles . marginRight = this . _rightOffset ;
193
+ const xPosition = this . _xPosition ;
194
+ const xOffset = this . _xOffset ;
195
+ const isRtl = this . _overlayRef . getConfig ( ) . direction === 'rtl' ;
196
+ let marginLeft = '' ;
197
+ let marginRight = '' ;
198
+ let justifyContent = '' ;
180
199
181
200
if ( shouldBeFlushHorizontally ) {
182
- parentStyles . justifyContent = 'flex-start' ;
183
- } else if ( this . _justifyContent === 'center' ) {
184
- parentStyles . justifyContent = 'center' ;
185
- } else if ( this . _overlayRef . getConfig ( ) . direction === 'rtl' ) {
186
- // In RTL the browser will invert `flex-start` and `flex-end` automatically, but we
187
- // don't want that because our positioning is explicitly `left` and `right`, hence
188
- // why we do another inversion to ensure that the overlay stays in the same position.
189
- // TODO: reconsider this if we add `start` and `end` methods.
190
- if ( this . _justifyContent === 'flex-start' ) {
191
- parentStyles . justifyContent = 'flex-end' ;
192
- } else if ( this . _justifyContent === 'flex-end' ) {
193
- parentStyles . justifyContent = 'flex-start' ;
201
+ justifyContent = 'flex-start' ;
202
+ } else if ( xPosition === 'center' ) {
203
+ justifyContent = 'center' ;
204
+
205
+ if ( isRtl ) {
206
+ marginRight = xOffset ;
207
+ } else {
208
+ marginLeft = xOffset ;
194
209
}
195
- } else {
196
- parentStyles . justifyContent = this . _justifyContent ;
210
+ } else if ( isRtl ) {
211
+ if ( xPosition === 'left' || xPosition === 'end' ) {
212
+ justifyContent = 'flex-end' ;
213
+ marginLeft = xOffset ;
214
+ } else if ( xPosition === 'right' || xPosition === 'start' ) {
215
+ justifyContent = 'flex-start' ;
216
+ marginRight = xOffset ;
217
+ }
218
+ } else if ( xPosition === 'left' || xPosition === 'start' ) {
219
+ justifyContent = 'flex-start' ;
220
+ marginLeft = xOffset ;
221
+ } else if ( xPosition === 'right' || xPosition === 'end' ) {
222
+ justifyContent = 'flex-end' ;
223
+ marginRight = xOffset ;
197
224
}
198
225
226
+ styles . position = this . _cssPosition ;
227
+ styles . marginLeft = shouldBeFlushHorizontally ? '0' : marginLeft ;
228
+ styles . marginTop = shouldBeFlushVertically ? '0' : this . _topOffset ;
229
+ styles . marginBottom = this . _bottomOffset ;
230
+ styles . marginRight = shouldBeFlushHorizontally ? '0' : marginRight ;
231
+ parentStyles . justifyContent = justifyContent ;
199
232
parentStyles . alignItems = shouldBeFlushVertically ? 'flex-start' : this . _alignItems ;
200
233
}
201
234
0 commit comments