|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {Directive, Input, |
| 9 | + OnDestroy, AfterViewInit, ElementRef, Optional} from '@angular/core'; |
| 10 | +import {Platform} from '../core/platform'; |
| 11 | +import {Scrollable} from '../core/overlay/scroll/scrollable'; |
| 12 | +import {extendObject} from '../core/util/object-extend'; |
| 13 | +import {Subscription} from 'rxjs/Subscription'; |
| 14 | +import {fromEvent} from 'rxjs/observable/fromEvent'; |
| 15 | +import {RxChain, debounceTime} from '../core/rxjs/index'; |
| 16 | +import {isPositionStickySupported} from '@angular/cdk'; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * Directive that marks an element as a "sticky region", meant to contain exactly one sticky-header |
| 21 | + * along with the content associated with that header. The sticky-header inside of the region will |
| 22 | + * "stick" to the top of the scrolling container as long as this region is within the scrolling |
| 23 | + * viewport. |
| 24 | + * |
| 25 | + * If a user does not explicitly define a sticky-region for a sticky-header, the direct |
| 26 | + * parent node of the sticky-header will be used as the sticky-region. |
| 27 | + */ |
| 28 | +@Directive({ |
| 29 | + selector: '[cdkStickyRegion]', |
| 30 | +}) |
| 31 | +export class CdkStickyRegion { |
| 32 | + constructor(public readonly _elementRef: ElementRef) { } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +/** Class applied when the header is "stuck" */ |
| 37 | +const STICK_START_CLASS = 'cdk-sticky-header-start'; |
| 38 | + |
| 39 | +/** Class applied when the header is not "stuck" */ |
| 40 | +const STICK_END_CLASS = 'cdk-sticky-header-end'; |
| 41 | + |
| 42 | +/** |
| 43 | + * Debounce time in milliseconds for events that affect the sticky positioning (e.g. scroll, resize, |
| 44 | + * touch move). Set as 5 milliseconds which is the highest delay that doesn't drastically affect the |
| 45 | + * positioning adversely. |
| 46 | + */ |
| 47 | +const DEBOUNCE_TIME: number = 5; |
| 48 | + |
| 49 | +/** |
| 50 | + * Directive that marks an element as a sticky-header. Inside of a scrolling container (marked with |
| 51 | + * cdkScrollable), this header will "stick" to the top of the scrolling viewport while its sticky |
| 52 | + * region (see cdkStickyRegion) is in view. |
| 53 | + */ |
| 54 | +@Directive({ |
| 55 | + selector: '[cdkStickyHeader]', |
| 56 | +}) |
| 57 | +export class CdkStickyHeader implements OnDestroy, AfterViewInit { |
| 58 | + |
| 59 | + /** z-index to be applied to the sticky header (default is 10). */ |
| 60 | + @Input('cdkStickyHeaderZIndex') zIndex: number = 10; |
| 61 | + |
| 62 | + /** boolean value to mark whether the current header is stuck*/ |
| 63 | + isStuck: boolean = false; |
| 64 | + /** Whether the browser support CSS sticky positioning. */ |
| 65 | + private _isPositionStickySupported: boolean = false; |
| 66 | + |
| 67 | + /** The element with the 'cdkStickyHeader' tag. */ |
| 68 | + element: HTMLElement; |
| 69 | + /** The upper container element with the 'cdkStickyRegion' tag. */ |
| 70 | + stickyParent: HTMLElement | null; |
| 71 | + /** The upper scrollable container. */ |
| 72 | + upperScrollableContainer: HTMLElement; |
| 73 | + /** |
| 74 | + * The original css of the sticky element, used to reset the sticky element |
| 75 | + * when it is being unstuck |
| 76 | + */ |
| 77 | + private _originalStyles = {} as CSSStyleDeclaration; |
| 78 | + /** |
| 79 | + * 'getBoundingClientRect().top' of CdkStickyRegion of current sticky header. |
| 80 | + * It is used with '_stickyRegionBottomThreshold' to judge whether the current header |
| 81 | + * need to be stuck. |
| 82 | + */ |
| 83 | + private _stickyRegionTop: number; |
| 84 | + /** |
| 85 | + * Bottom of the sticky region offset by the height of the sticky header. |
| 86 | + * Once the sticky header is scrolled to this position it will stay in place |
| 87 | + * so that it will scroll naturally out of view with the rest of the sticky region. |
| 88 | + */ |
| 89 | + private _stickyRegionBottomThreshold: number; |
| 90 | + |
| 91 | + private _onScrollSubscription: Subscription; |
| 92 | + |
| 93 | + private _onTouchSubscription: Subscription; |
| 94 | + |
| 95 | + private _onResizeSubscription: Subscription; |
| 96 | + |
| 97 | + constructor(element: ElementRef, |
| 98 | + scrollable: Scrollable, |
| 99 | + @Optional() public parentRegion: CdkStickyRegion, |
| 100 | + platform: Platform) { |
| 101 | + if (platform.isBrowser) { |
| 102 | + this.element = element.nativeElement; |
| 103 | + this.upperScrollableContainer = scrollable.getElementRef().nativeElement; |
| 104 | + this._setStrategyAccordingToCompatibility(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + ngAfterViewInit(): void { |
| 109 | + if (!this._isPositionStickySupported) { |
| 110 | + |
| 111 | + this.stickyParent = this.parentRegion != null ? |
| 112 | + this.parentRegion._elementRef.nativeElement : this.element.parentElement; |
| 113 | + |
| 114 | + let headerStyles = window.getComputedStyle(this.element, ''); |
| 115 | + this._originalStyles = { |
| 116 | + position: headerStyles.position, |
| 117 | + top: headerStyles.top, |
| 118 | + right: headerStyles.right, |
| 119 | + left: headerStyles.left, |
| 120 | + bottom: headerStyles.bottom, |
| 121 | + width: headerStyles.width, |
| 122 | + zIndex: headerStyles.zIndex |
| 123 | + } as CSSStyleDeclaration; |
| 124 | + |
| 125 | + this._attachEventListeners(); |
| 126 | + this._updateStickyPositioning(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + ngOnDestroy(): void { |
| 131 | + [this._onScrollSubscription, this._onScrollSubscription, this._onResizeSubscription] |
| 132 | + .forEach(s => s && s.unsubscribe()); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Check if current browser supports sticky positioning. If yes, apply |
| 137 | + * sticky positioning. If not, use the original implementation. |
| 138 | + */ |
| 139 | + private _setStrategyAccordingToCompatibility(): void { |
| 140 | + this._isPositionStickySupported = isPositionStickySupported(); |
| 141 | + if (this._isPositionStickySupported) { |
| 142 | + this.element.style.top = '0'; |
| 143 | + this.element.style.cssText += 'position: -webkit-sticky; position: sticky; '; |
| 144 | + // TODO(sllethe): add css class with both 'sticky' and '-webkit-sticky' on position |
| 145 | + // when @Directory supports adding CSS class |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** Add listeners for events that affect sticky positioning. */ |
| 150 | + private _attachEventListeners() { |
| 151 | + this._onScrollSubscription = RxChain.from(fromEvent(this.upperScrollableContainer, 'scroll')) |
| 152 | + .call(debounceTime, DEBOUNCE_TIME).subscribe(() => this._updateStickyPositioning()); |
| 153 | + |
| 154 | + // Have to add a 'onTouchMove' listener to make sticky header work on mobile phones |
| 155 | + this._onTouchSubscription = RxChain.from(fromEvent(this.upperScrollableContainer, 'touchmove')) |
| 156 | + .call(debounceTime, DEBOUNCE_TIME).subscribe(() => this._updateStickyPositioning()); |
| 157 | + |
| 158 | + this._onResizeSubscription = RxChain.from(fromEvent(this.upperScrollableContainer, 'resize')) |
| 159 | + .call(debounceTime, DEBOUNCE_TIME).subscribe(() => this.onResize()); |
| 160 | + } |
| 161 | + |
| 162 | + onResize(): void { |
| 163 | + this._updateStickyPositioning(); |
| 164 | + // If there's already a header being stick when the page is |
| 165 | + // resized. The CSS style of the cdkStickyHeader element may be not fit |
| 166 | + // the resized window. So we need to unstuck it then re-stick it. |
| 167 | + // unstuck() can set 'isStuck' to FALSE. Then _stickElement() can work. |
| 168 | + if (this.isStuck) { |
| 169 | + this._unstickElement(); |
| 170 | + this._stickElement(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** Measures the boundaries of the sticky regions to be used in subsequent positioning. */ |
| 175 | + private _measureStickyRegionBounds(): void { |
| 176 | + if (!this.stickyParent) { |
| 177 | + return; |
| 178 | + } |
| 179 | + const boundingClientRect: any = this.stickyParent.getBoundingClientRect(); |
| 180 | + this._stickyRegionTop = boundingClientRect.top; |
| 181 | + let stickRegionHeight = boundingClientRect.height; |
| 182 | + |
| 183 | + this._stickyRegionBottomThreshold = this._stickyRegionTop + |
| 184 | + (stickRegionHeight - this.element.offsetHeight); |
| 185 | + } |
| 186 | + |
| 187 | + /** Reset element to its original CSS. */ |
| 188 | + private _resetElementStyles(): void { |
| 189 | + this.element.classList.remove(STICK_START_CLASS); |
| 190 | + extendObject(this.element.style, this._originalStyles); |
| 191 | + } |
| 192 | + |
| 193 | + /** Stuck element, make the element stick to the top of the scrollable container. */ |
| 194 | + private _stickElement(): void { |
| 195 | + this.isStuck = true; |
| 196 | + |
| 197 | + this.element.classList.remove(STICK_END_CLASS); |
| 198 | + this.element.classList.add(STICK_START_CLASS); |
| 199 | + |
| 200 | + // Have to add the translate3d function for the sticky element's css style. |
| 201 | + // Because iPhone and iPad's browser is using its owning rendering engine. And |
| 202 | + // even if you are using Chrome on an iPhone, you are just using Safari with |
| 203 | + // a Chrome skin around it. |
| 204 | + // |
| 205 | + // Safari on iPad and Safari on iPhone do not have resizable windows. |
| 206 | + // In Safari on iPhone and iPad, the window size is set to the size of |
| 207 | + // the screen (minus Safari user interface controls), and cannot be changed |
| 208 | + // by the user. To move around a webpage, the user changes the zoom level and position |
| 209 | + // of the viewport as they double tap or pinch to zoom in or out, or by touching |
| 210 | + // and dragging to pan the page. As a user changes the zoom level and position of the |
| 211 | + // viewport they are doing so within a viewable content area of fixed size |
| 212 | + // (that is, the window). This means that webpage elements that have their position |
| 213 | + // "fixed" to the viewport can end up outside the viewable content area, offscreen. |
| 214 | + // |
| 215 | + // So the 'position: fixed' does not work on iPhone and iPad. To make it work, |
| 216 | + // 'translate3d(0,0,0)' needs to be used to force Safari re-rendering the sticky element. |
| 217 | + this.element.style.transform = 'translate3d(0px,0px,0px)'; |
| 218 | + |
| 219 | + let stuckRight: number = this.upperScrollableContainer.getBoundingClientRect().right; |
| 220 | + |
| 221 | + let stickyCss = { |
| 222 | + position: 'fixed', |
| 223 | + top: this.upperScrollableContainer.offsetTop + 'px', |
| 224 | + right: stuckRight + 'px', |
| 225 | + left: this.upperScrollableContainer.offsetLeft + 'px', |
| 226 | + bottom: 'auto', |
| 227 | + width: this._originalStyles.width, |
| 228 | + zIndex: this.zIndex + '' |
| 229 | + }; |
| 230 | + extendObject(this.element.style, stickyCss); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Unsticks the header so that it goes back to scrolling normally. |
| 235 | + * |
| 236 | + * This should be called when the element reaches the bottom of its cdkStickyRegion so that it |
| 237 | + * smoothly scrolls out of view as the next sticky-header moves in. |
| 238 | + */ |
| 239 | + private _unstickElement(): void { |
| 240 | + this.isStuck = false; |
| 241 | + |
| 242 | + if (!this.stickyParent) { |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + this.element.classList.add(STICK_END_CLASS); |
| 247 | + this.stickyParent.style.position = 'relative'; |
| 248 | + let unstuckCss = { |
| 249 | + position: 'absolute', |
| 250 | + top: 'auto', |
| 251 | + right: '0', |
| 252 | + left: 'auto', |
| 253 | + bottom: '0', |
| 254 | + width: this._originalStyles.width |
| 255 | + }; |
| 256 | + extendObject(this.element.style, unstuckCss); |
| 257 | + } |
| 258 | + |
| 259 | + |
| 260 | + /** |
| 261 | + * '_applyStickyPositionStyles()' function contains the main logic of sticky-header. It decides when |
| 262 | + * a header should be stick and when should it be unstuck by comparing the offsetTop |
| 263 | + * of scrollable container with the top and bottom of the sticky region. |
| 264 | + */ |
| 265 | + _applyStickyPositionStyles(): void { |
| 266 | + let currentPosition: number = this.upperScrollableContainer.offsetTop; |
| 267 | + |
| 268 | + // unstuck when the element is scrolled out of the sticky region |
| 269 | + if (this.isStuck && |
| 270 | + (currentPosition < this._stickyRegionTop || |
| 271 | + currentPosition > this._stickyRegionBottomThreshold) || |
| 272 | + currentPosition >= this._stickyRegionBottomThreshold) { |
| 273 | + this._resetElementStyles(); |
| 274 | + if (currentPosition >= this._stickyRegionBottomThreshold) { |
| 275 | + this._unstickElement(); |
| 276 | + } |
| 277 | + this.isStuck = false; // stick when the element is within the sticky region |
| 278 | + } else if ( this.isStuck === false && |
| 279 | + currentPosition > this._stickyRegionTop && |
| 280 | + currentPosition < this._stickyRegionBottomThreshold) { |
| 281 | + this._stickElement(); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + _updateStickyPositioning(): void { |
| 286 | + this._measureStickyRegionBounds(); |
| 287 | + this._applyStickyPositionStyles(); |
| 288 | + } |
| 289 | +} |
0 commit comments