-
Notifications
You must be signed in to change notification settings - Fork 6
/
polaris-sticky.js
161 lines (140 loc) · 3.51 KB
/
polaris-sticky.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import { htmlSafe } from '@ember/string';
import { getRectForNode } from '@shopify/javascript-utilities/geometry';
import layout from '../templates/components/polaris-sticky';
import { computedIdVariation } from '@smile-io/ember-polaris/utils/id';
/**
* Undocumented Polaris sticky component.
*/
export default Component.extend({
layout,
stickyManager: service(),
/**
* Element outlining the fixed position boundaries
*
* @property boundingElement
* @type {HTMLElement}
* @default null
* @public
*/
boundingElement: null,
/**
* Offset vertical spacing from the top of the scrollable container
*
* @property offset
* @type {Boolean}
* @default null
* @public
*/
offset: null,
/**
* Should the element remain in a fixed position when the layout is stacked (smaller screens)
*
* @property disableWhenStacked
* @type {Boolean}
* @default null
* @public
*/
disableWhenStacked: null,
/**
* @property isSticky
* @type {Boolean}
* @default false
* @private
*/
isSticky: false,
/**
* @property style
* @type {String}
* @default null
* @private
*/
style: null,
/**
* @property placeHolderNodeId
* @type {String}
* @private
*/
placeHolderNodeId: computedIdVariation('elementId', 'PlaceHolder').readOnly(),
/**
* @property stickyNodeId
* @type {String}
* @private
*/
stickyNodeId: computedIdVariation('elementId', 'Sticky').readOnly(),
/**
* @property placeHolderNode
* @type {HTMLElement}
* @private
*/
placeHolderNode: computed(function() {
return this.get('element').querySelector(
`#${this.get('placeHolderNodeId')}`
);
}),
/**
* @property stickyNode
* @type {HTMLElement}
* @private
*/
stickyNode: computed(function() {
return this.get('element').querySelector(`#${this.get('stickyNodeId')}`);
}),
handlePositioning(stick, top = 0, left = 0, width = 0) {
let isSticky = this.get('isSticky');
if ((stick && !isSticky) || (!stick && isSticky)) {
this.adjustPlaceHolderNode(stick);
this.toggleProperty('isSticky');
}
let style = stick
? htmlSafe(
`position: fixed; top: ${top}px; left: ${left}px; width: ${width}px;`
)
: null;
this.set('style', style);
},
adjustPlaceHolderNode(add) {
let { placeHolderNode, stickyNode } = this.getProperties(
'placeHolderNode',
'stickyNode'
);
if (placeHolderNode && stickyNode) {
placeHolderNode.style.paddingBottom = add
? `${getRectForNode(stickyNode).height}px`
: '0px';
}
},
didInsertElement() {
this._super(...arguments);
let stickyManager = this.get('stickyManager');
let {
stickyNode,
placeHolderNode,
offset,
boundingElement,
disableWhenStacked,
} = this.getProperties(
'stickyNode',
'placeHolderNode',
'offset',
'boundingElement',
'disableWhenStacked'
);
stickyManager.registerStickyItem({
stickyNode,
placeHolderNode,
handlePositioning: (...positioningArgs) =>
this.handlePositioning(...positioningArgs),
offset,
boundingElement,
disableWhenStacked,
});
},
willDestroyElement() {
this._super(...arguments);
let stickyManager = this.get('stickyManager');
stickyManager.unregisterStickyItem(this.get('stickyNode'));
},
});