@@ -21,6 +21,7 @@ export type StickyDirection = 'top' | 'bottom' | 'left' | 'right';
2121 */
2222export const STICKY_DIRECTIONS : StickyDirection [ ] = [ 'top' , 'bottom' , 'left' , 'right' ] ;
2323
24+
2425/**
2526 * Applies and removes sticky positioning styles to the `CdkTable` rows and columns cells.
2627 * @docs -private
@@ -34,12 +35,16 @@ export class StickyStyler {
3435 * @param direction The directionality context of the table (ltr/rtl); affects column positioning
3536 * by reversing left/right positions.
3637 * @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.
3741 */
3842 constructor ( private _isNativeHtmlTable : boolean ,
3943 private _stickCellCss : string ,
4044 public direction : Direction ,
4145 private _coalescedStyleScheduler : _CoalescedStyleScheduler ,
42- private _isBrowser = true ) { }
46+ private _isBrowser = true ,
47+ private readonly _needsPositionStickyOnElement = true ) { }
4348
4449 /**
4550 * Clears the sticky positioning styles from the row and its cells by resetting the `position`
@@ -204,13 +209,21 @@ export class StickyStyler {
204209 for ( const dir of stickyDirections ) {
205210 element . style [ dir ] = '' ;
206211 }
207- element . style . zIndex = this . _getCalculatedZIndex ( element ) ;
208212
209213 // If the element no longer has any more sticky directions, remove sticky positioning and
210214 // 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+ }
214227 element . classList . remove ( this . _stickCellCss ) ;
215228 }
216229 }
@@ -223,8 +236,10 @@ export class StickyStyler {
223236 _addStickyStyle ( element : HTMLElement , dir : StickyDirection , dirValue : number ) {
224237 element . classList . add ( this . _stickCellCss ) ;
225238 element . style [ dir ] = `${ dirValue } px` ;
226- element . style . cssText += 'position: -webkit-sticky; position: sticky; ' ;
227239 element . style . zIndex = this . _getCalculatedZIndex ( element ) ;
240+ if ( this . _needsPositionStickyOnElement ) {
241+ element . style . cssText += 'position: -webkit-sticky; position: sticky; ' ;
242+ }
228243 }
229244
230245 /**
0 commit comments