@@ -21,6 +21,7 @@ export type StickyDirection = 'top' | 'bottom' | 'left' | 'right';
21
21
*/
22
22
export const STICKY_DIRECTIONS : StickyDirection [ ] = [ 'top' , 'bottom' , 'left' , 'right' ] ;
23
23
24
+
24
25
/**
25
26
* Applies and removes sticky positioning styles to the `CdkTable` rows and columns cells.
26
27
* @docs -private
@@ -34,12 +35,16 @@ export class StickyStyler {
34
35
* @param direction The directionality context of the table (ltr/rtl); affects column positioning
35
36
* by reversing left/right positions.
36
37
* @param _isBrowser Whether the table is currently being rendered on the server or the client.
38
+ * @param _needsPositionStickyOnElement Whether we need to specify position: sticky on cells
39
+ * using inline styles. If false, it is assumed that position: sticky is included in
40
+ * the component stylesheet for _stickCellCss.
37
41
*/
38
42
constructor ( private _isNativeHtmlTable : boolean ,
39
43
private _stickCellCss : string ,
40
44
public direction : Direction ,
41
45
private _coalescedStyleScheduler : _CoalescedStyleScheduler ,
42
- private _isBrowser = true ) { }
46
+ private _isBrowser = true ,
47
+ private readonly _needsPositionStickyOnElement = true ) { }
43
48
44
49
/**
45
50
* Clears the sticky positioning styles from the row and its cells by resetting the `position`
@@ -204,13 +209,21 @@ export class StickyStyler {
204
209
for ( const dir of stickyDirections ) {
205
210
element . style [ dir ] = '' ;
206
211
}
207
- element . style . zIndex = this . _getCalculatedZIndex ( element ) ;
208
212
209
213
// If the element no longer has any more sticky directions, remove sticky positioning and
210
214
// the sticky CSS class.
211
- const hasDirection = STICKY_DIRECTIONS . some ( dir => ! ! element . style [ dir ] ) ;
212
- if ( ! hasDirection ) {
213
- element . style . position = '' ;
215
+ // Short-circuit checking element.style[dir] for stickyDirections as they
216
+ // were already removed above.
217
+ const hasDirection = STICKY_DIRECTIONS . some ( dir =>
218
+ stickyDirections . indexOf ( dir ) === - 1 && element . style [ dir ] ) ;
219
+ if ( hasDirection ) {
220
+ element . style . zIndex = this . _getCalculatedZIndex ( element ) ;
221
+ } else {
222
+ // When not hasDirection, _getCalculatedZIndex will always return ''.
223
+ element . style . zIndex = '' ;
224
+ if ( this . _needsPositionStickyOnElement ) {
225
+ element . style . position = '' ;
226
+ }
214
227
element . classList . remove ( this . _stickCellCss ) ;
215
228
}
216
229
}
@@ -223,8 +236,10 @@ export class StickyStyler {
223
236
_addStickyStyle ( element : HTMLElement , dir : StickyDirection , dirValue : number ) {
224
237
element . classList . add ( this . _stickCellCss ) ;
225
238
element . style [ dir ] = `${ dirValue } px` ;
226
- element . style . cssText += 'position: -webkit-sticky; position: sticky; ' ;
227
239
element . style . zIndex = this . _getCalculatedZIndex ( element ) ;
240
+ if ( this . _needsPositionStickyOnElement ) {
241
+ element . style . cssText += 'position: -webkit-sticky; position: sticky; ' ;
242
+ }
228
243
}
229
244
230
245
/**
0 commit comments