@fluentui/priority-overflow 0.0.0-nightly-20230501-0418.1 → 0.0.0-nightly-20230503-0419.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.json +3 -3
- package/CHANGELOG.md +4 -4
- package/dist/index.d.ts +122 -0
- package/lib/debounce.js +20 -0
- package/lib/debounce.js.map +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/overflowManager.js +217 -0
- package/lib/overflowManager.js.map +1 -0
- package/lib/priorityQueue.js +90 -0
- package/lib/priorityQueue.js.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/package.json +1 -1
package/CHANGELOG.json
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
"name": "@fluentui/priority-overflow",
|
3
3
|
"entries": [
|
4
4
|
{
|
5
|
-
"date": "
|
6
|
-
"tag": "@fluentui/priority-overflow_v0.0.0-nightly-
|
7
|
-
"version": "0.0.0-nightly-
|
5
|
+
"date": "Wed, 03 May 2023 04:25:50 GMT",
|
6
|
+
"tag": "@fluentui/priority-overflow_v0.0.0-nightly-20230503-0419.1",
|
7
|
+
"version": "0.0.0-nightly-20230503-0419.1",
|
8
8
|
"comments": {
|
9
9
|
"prerelease": [
|
10
10
|
{
|
package/CHANGELOG.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# Change Log - @fluentui/priority-overflow
|
2
2
|
|
3
|
-
This log was last generated on
|
3
|
+
This log was last generated on Wed, 03 May 2023 04:25:50 GMT and should not be manually modified.
|
4
4
|
|
5
5
|
<!-- Start content -->
|
6
6
|
|
7
|
-
## [0.0.0-nightly-
|
7
|
+
## [0.0.0-nightly-20230503-0419.1](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v0.0.0-nightly-20230503-0419.1)
|
8
8
|
|
9
|
-
|
10
|
-
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.2..@fluentui/priority-overflow_v0.0.0-nightly-
|
9
|
+
Wed, 03 May 2023 04:25:50 GMT
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.2..@fluentui/priority-overflow_v0.0.0-nightly-20230503-0419.1)
|
11
11
|
|
12
12
|
### Changes
|
13
13
|
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
/**
|
2
|
+
* @internal
|
3
|
+
* @returns overflow manager instance
|
4
|
+
*/
|
5
|
+
export declare function createOverflowManager(): OverflowManager;
|
6
|
+
|
7
|
+
export declare interface ObserveOptions {
|
8
|
+
/**
|
9
|
+
* Padding (in px) at the end of the container before overflow occurs
|
10
|
+
* Useful to account for extra elements (i.e. dropdown menu)
|
11
|
+
* or to account for any kinds of margins between items which are hard to measure with JS
|
12
|
+
* @default 10
|
13
|
+
*/
|
14
|
+
padding?: number;
|
15
|
+
/**
|
16
|
+
* Direction where items are removed when overflow occurs
|
17
|
+
* @default end
|
18
|
+
*/
|
19
|
+
overflowDirection?: OverflowDirection;
|
20
|
+
/**
|
21
|
+
* Horizontal or vertical overflow
|
22
|
+
* @default horizontal
|
23
|
+
*/
|
24
|
+
overflowAxis?: OverflowAxis;
|
25
|
+
/**
|
26
|
+
* The minimum number of visible items
|
27
|
+
*/
|
28
|
+
minimumVisible?: number;
|
29
|
+
/**
|
30
|
+
* Callback when item visibility is updated
|
31
|
+
*/
|
32
|
+
onUpdateItemVisibility: OnUpdateItemVisibility;
|
33
|
+
/**
|
34
|
+
* Callback when item visibility is updated
|
35
|
+
*/
|
36
|
+
onUpdateOverflow: OnUpdateOverflow;
|
37
|
+
}
|
38
|
+
|
39
|
+
export declare type OnUpdateItemVisibility = (data: OnUpdateItemVisibilityPayload) => void;
|
40
|
+
|
41
|
+
export declare interface OnUpdateItemVisibilityPayload {
|
42
|
+
item: OverflowItemEntry;
|
43
|
+
visible: boolean;
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* signature similar to standard event listeners, but typed to handle the custom event
|
48
|
+
*/
|
49
|
+
export declare type OnUpdateOverflow = (data: OverflowEventPayload) => void;
|
50
|
+
|
51
|
+
export declare type OverflowAxis = 'horizontal' | 'vertical';
|
52
|
+
|
53
|
+
export declare type OverflowDirection = 'start' | 'end';
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Payload of the custom DOM event for overflow updates
|
57
|
+
*/
|
58
|
+
export declare interface OverflowEventPayload {
|
59
|
+
visibleItems: OverflowItemEntry[];
|
60
|
+
invisibleItems: OverflowItemEntry[];
|
61
|
+
groupVisibility: Record<string, OverflowGroupState>;
|
62
|
+
}
|
63
|
+
|
64
|
+
export declare type OverflowGroupState = 'visible' | 'hidden' | 'overflow';
|
65
|
+
|
66
|
+
export declare interface OverflowItemEntry {
|
67
|
+
/**
|
68
|
+
* HTML element that will be disappear when overflowed
|
69
|
+
*/
|
70
|
+
element: HTMLElement;
|
71
|
+
/**
|
72
|
+
* Lower priority items are invisible first when the container is overflowed
|
73
|
+
* @default 0
|
74
|
+
*/
|
75
|
+
priority: number;
|
76
|
+
/**
|
77
|
+
* Specific id, used to track visibility and provide updates to consumers
|
78
|
+
*/
|
79
|
+
id: string;
|
80
|
+
groupId?: string;
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* @internal
|
85
|
+
*/
|
86
|
+
export declare interface OverflowManager {
|
87
|
+
/**
|
88
|
+
* Starts observing the container and managing the overflow state
|
89
|
+
*/
|
90
|
+
observe: (container: HTMLElement, options: ObserveOptions) => void;
|
91
|
+
/**
|
92
|
+
* Stops observing the container
|
93
|
+
*/
|
94
|
+
disconnect: () => void;
|
95
|
+
/**
|
96
|
+
* Add overflow items
|
97
|
+
*/
|
98
|
+
addItem: (items: OverflowItemEntry) => void;
|
99
|
+
/**
|
100
|
+
* Remove overflow item
|
101
|
+
*/
|
102
|
+
removeItem: (itemId: string) => void;
|
103
|
+
/**
|
104
|
+
* Manually update the overflow, updates are batched and async
|
105
|
+
*/
|
106
|
+
update: () => void;
|
107
|
+
/**
|
108
|
+
* Manually update the overflow sync
|
109
|
+
*/
|
110
|
+
forceUpdate: () => void;
|
111
|
+
/**
|
112
|
+
* Adds an element that opens an overflow menu. This is used to calculate
|
113
|
+
* available space and check if additional items need to overflow
|
114
|
+
*/
|
115
|
+
addOverflowMenu: (element: HTMLElement) => void;
|
116
|
+
/**
|
117
|
+
* Unsets the overflow menu element
|
118
|
+
*/
|
119
|
+
removeOverflowMenu: () => void;
|
120
|
+
}
|
121
|
+
|
122
|
+
export { }
|
package/lib/debounce.js
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* Microtask debouncer
|
3
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
|
4
|
+
* @param fn - Function to debounce
|
5
|
+
* @returns debounced function
|
6
|
+
*/export function debounce(fn) {
|
7
|
+
let pending;
|
8
|
+
return () => {
|
9
|
+
if (!pending) {
|
10
|
+
pending = true;
|
11
|
+
queueMicrotask(() => {
|
12
|
+
// Need to set pending to `false` before the debounced function is run.
|
13
|
+
// React can actually interrupt the function while it's running!
|
14
|
+
pending = false;
|
15
|
+
fn();
|
16
|
+
});
|
17
|
+
}
|
18
|
+
};
|
19
|
+
}
|
20
|
+
//# sourceMappingURL=debounce.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["debounce","fn","pending","queueMicrotask"],"sources":["../src/debounce.ts"],"sourcesContent":["/**\n * Microtask debouncer\n * https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide\n * @param fn - Function to debounce\n * @returns debounced function\n */\nexport function debounce(fn: Function) {\n let pending: boolean;\n return () => {\n if (!pending) {\n pending = true;\n queueMicrotask(() => {\n // Need to set pending to `false` before the debounced function is run.\n // React can actually interrupt the function while it's running!\n pending = false;\n fn();\n });\n }\n };\n}\n"],"mappings":"AAAA;;;;;GAMA,OAAO,SAASA,SAASC,EAAY,EAAE;EACrC,IAAIC,OAAA;EACJ,OAAO,MAAM;IACX,IAAI,CAACA,OAAA,EAAS;MACZA,OAAA,GAAU,IAAI;MACdC,cAAA,CAAe,MAAM;QACnB;QACA;QACAD,OAAA,GAAU,KAAK;QACfD,EAAA;MACF;IACF;EACF;AACF"}
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["createOverflowManager"],"sources":["../src/index.ts"],"sourcesContent":["export { createOverflowManager } from './overflowManager';\nexport type {\n ObserveOptions,\n OnUpdateItemVisibility,\n OnUpdateItemVisibilityPayload,\n OnUpdateOverflow,\n OverflowAxis,\n OverflowDirection,\n OverflowEventPayload,\n OverflowGroupState,\n OverflowItemEntry,\n OverflowManager,\n} from './types';\n"],"mappings":"AAAA,SAASA,qBAAqB,QAAQ"}
|
@@ -0,0 +1,217 @@
|
|
1
|
+
import { debounce } from './debounce';
|
2
|
+
import { createPriorityQueue } from './priorityQueue';
|
3
|
+
/**
|
4
|
+
* @internal
|
5
|
+
* @returns overflow manager instance
|
6
|
+
*/
|
7
|
+
export function createOverflowManager() {
|
8
|
+
let container;
|
9
|
+
let overflowMenu;
|
10
|
+
// Set as true when resize observer is observing
|
11
|
+
let observing = false;
|
12
|
+
// If true, next update will dispatch to onUpdateOverflow even if queue top states don't change
|
13
|
+
let forceDispatch = false;
|
14
|
+
const options = {
|
15
|
+
padding: 10,
|
16
|
+
overflowAxis: 'horizontal',
|
17
|
+
overflowDirection: 'end',
|
18
|
+
minimumVisible: 0,
|
19
|
+
onUpdateItemVisibility: () => undefined,
|
20
|
+
onUpdateOverflow: () => undefined
|
21
|
+
};
|
22
|
+
const overflowItems = {};
|
23
|
+
const overflowGroups = {};
|
24
|
+
const resizeObserver = new ResizeObserver(entries => {
|
25
|
+
if (!entries[0] || !container) {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
update();
|
29
|
+
});
|
30
|
+
const invisibleItemQueue = createPriorityQueue((a, b) => {
|
31
|
+
const itemA = overflowItems[a];
|
32
|
+
const itemB = overflowItems[b];
|
33
|
+
// Higher priority at the top of the queue
|
34
|
+
const priority = itemB.priority - itemA.priority;
|
35
|
+
if (priority !== 0) {
|
36
|
+
return priority;
|
37
|
+
}
|
38
|
+
const positionStatusBit = options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING;
|
39
|
+
// equal priority, use DOM order
|
40
|
+
// eslint-disable-next-line no-bitwise
|
41
|
+
return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;
|
42
|
+
});
|
43
|
+
const visibleItemQueue = createPriorityQueue((a, b) => {
|
44
|
+
const itemA = overflowItems[a];
|
45
|
+
const itemB = overflowItems[b];
|
46
|
+
// Lower priority at the top of the queue
|
47
|
+
const priority = itemA.priority - itemB.priority;
|
48
|
+
if (priority !== 0) {
|
49
|
+
return priority;
|
50
|
+
}
|
51
|
+
const positionStatusBit = options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;
|
52
|
+
// equal priority, use DOM order
|
53
|
+
// eslint-disable-next-line no-bitwise
|
54
|
+
return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;
|
55
|
+
});
|
56
|
+
const getOffsetSize = el => {
|
57
|
+
return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight;
|
58
|
+
};
|
59
|
+
const makeItemVisible = () => {
|
60
|
+
const nextVisible = invisibleItemQueue.dequeue();
|
61
|
+
visibleItemQueue.enqueue(nextVisible);
|
62
|
+
const item = overflowItems[nextVisible];
|
63
|
+
options.onUpdateItemVisibility({
|
64
|
+
item,
|
65
|
+
visible: true
|
66
|
+
});
|
67
|
+
if (item.groupId) {
|
68
|
+
overflowGroups[item.groupId].invisibleItemIds.delete(item.id);
|
69
|
+
overflowGroups[item.groupId].visibleItemIds.add(item.id);
|
70
|
+
}
|
71
|
+
return getOffsetSize(item.element);
|
72
|
+
};
|
73
|
+
const makeItemInvisible = () => {
|
74
|
+
const nextInvisible = visibleItemQueue.dequeue();
|
75
|
+
invisibleItemQueue.enqueue(nextInvisible);
|
76
|
+
const item = overflowItems[nextInvisible];
|
77
|
+
const width = getOffsetSize(item.element);
|
78
|
+
options.onUpdateItemVisibility({
|
79
|
+
item,
|
80
|
+
visible: false
|
81
|
+
});
|
82
|
+
if (item.groupId) {
|
83
|
+
overflowGroups[item.groupId].visibleItemIds.delete(item.id);
|
84
|
+
overflowGroups[item.groupId].invisibleItemIds.add(item.id);
|
85
|
+
}
|
86
|
+
return width;
|
87
|
+
};
|
88
|
+
const dispatchOverflowUpdate = () => {
|
89
|
+
const visibleItemIds = visibleItemQueue.all();
|
90
|
+
const invisibleItemIds = invisibleItemQueue.all();
|
91
|
+
const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]);
|
92
|
+
const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]);
|
93
|
+
const groupVisibility = {};
|
94
|
+
Object.entries(overflowGroups).forEach(([groupId, groupState]) => {
|
95
|
+
if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) {
|
96
|
+
groupVisibility[groupId] = 'overflow';
|
97
|
+
} else if (groupState.visibleItemIds.size === 0) {
|
98
|
+
groupVisibility[groupId] = 'hidden';
|
99
|
+
} else {
|
100
|
+
groupVisibility[groupId] = 'visible';
|
101
|
+
}
|
102
|
+
});
|
103
|
+
options.onUpdateOverflow({
|
104
|
+
visibleItems,
|
105
|
+
invisibleItems,
|
106
|
+
groupVisibility
|
107
|
+
});
|
108
|
+
};
|
109
|
+
const processOverflowItems = () => {
|
110
|
+
if (!container) {
|
111
|
+
return false;
|
112
|
+
}
|
113
|
+
const availableSize = getOffsetSize(container) - options.padding;
|
114
|
+
const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0;
|
115
|
+
// Snapshot of the visible/invisible state to compare for updates
|
116
|
+
const visibleTop = visibleItemQueue.peek();
|
117
|
+
const invisibleTop = invisibleItemQueue.peek();
|
118
|
+
const visibleItemIds = visibleItemQueue.all();
|
119
|
+
let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => {
|
120
|
+
const child = overflowItems[visibleItemId].element;
|
121
|
+
return sum + getOffsetSize(child);
|
122
|
+
}, 0);
|
123
|
+
// Add items until available width is filled - can result in overflow
|
124
|
+
while (currentWidth < availableSize && invisibleItemQueue.size() > 0) {
|
125
|
+
currentWidth += makeItemVisible();
|
126
|
+
}
|
127
|
+
// Remove items until there's no more overflow
|
128
|
+
while (currentWidth > availableSize && visibleItemQueue.size() > 0) {
|
129
|
+
if (visibleItemQueue.size() <= options.minimumVisible) {
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
currentWidth -= makeItemInvisible();
|
133
|
+
}
|
134
|
+
// make sure the overflow menu can fit
|
135
|
+
if (visibleItemQueue.size() > options.minimumVisible && invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) {
|
136
|
+
makeItemInvisible();
|
137
|
+
}
|
138
|
+
// only update when the state of visible/invisible items has changed
|
139
|
+
if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) {
|
140
|
+
return true;
|
141
|
+
}
|
142
|
+
return false;
|
143
|
+
};
|
144
|
+
const forceUpdate = () => {
|
145
|
+
if (processOverflowItems() || forceDispatch) {
|
146
|
+
forceDispatch = false;
|
147
|
+
dispatchOverflowUpdate();
|
148
|
+
}
|
149
|
+
};
|
150
|
+
const update = debounce(forceUpdate);
|
151
|
+
const observe = (observedContainer, userOptions) => {
|
152
|
+
Object.assign(options, userOptions);
|
153
|
+
observing = true;
|
154
|
+
Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id));
|
155
|
+
container = observedContainer;
|
156
|
+
resizeObserver.observe(container);
|
157
|
+
};
|
158
|
+
const disconnect = () => {
|
159
|
+
observing = false;
|
160
|
+
resizeObserver.disconnect();
|
161
|
+
};
|
162
|
+
const addItem = item => {
|
163
|
+
if (overflowItems[item.id]) {
|
164
|
+
return;
|
165
|
+
}
|
166
|
+
overflowItems[item.id] = item;
|
167
|
+
// some options can affect priority which are only set on `observe`
|
168
|
+
if (observing) {
|
169
|
+
// Updates to elements might not change the queue tops
|
170
|
+
// i.e. new element is enqueued but the top of the queue stays the same
|
171
|
+
// force a dispatch on the next batched update
|
172
|
+
forceDispatch = true;
|
173
|
+
visibleItemQueue.enqueue(item.id);
|
174
|
+
}
|
175
|
+
if (item.groupId) {
|
176
|
+
if (!overflowGroups[item.groupId]) {
|
177
|
+
overflowGroups[item.groupId] = {
|
178
|
+
visibleItemIds: new Set(),
|
179
|
+
invisibleItemIds: new Set()
|
180
|
+
};
|
181
|
+
}
|
182
|
+
overflowGroups[item.groupId].visibleItemIds.add(item.id);
|
183
|
+
}
|
184
|
+
update();
|
185
|
+
};
|
186
|
+
const addOverflowMenu = el => {
|
187
|
+
overflowMenu = el;
|
188
|
+
};
|
189
|
+
const removeOverflowMenu = () => {
|
190
|
+
overflowMenu = undefined;
|
191
|
+
};
|
192
|
+
const removeItem = itemId => {
|
193
|
+
if (!overflowItems[itemId]) {
|
194
|
+
return;
|
195
|
+
}
|
196
|
+
const item = overflowItems[itemId];
|
197
|
+
visibleItemQueue.remove(itemId);
|
198
|
+
invisibleItemQueue.remove(itemId);
|
199
|
+
if (item.groupId) {
|
200
|
+
overflowGroups[item.groupId].visibleItemIds.delete(item.id);
|
201
|
+
overflowGroups[item.groupId].invisibleItemIds.delete(item.id);
|
202
|
+
}
|
203
|
+
delete overflowItems[itemId];
|
204
|
+
update();
|
205
|
+
};
|
206
|
+
return {
|
207
|
+
addItem,
|
208
|
+
disconnect,
|
209
|
+
forceUpdate,
|
210
|
+
observe,
|
211
|
+
removeItem,
|
212
|
+
update,
|
213
|
+
addOverflowMenu,
|
214
|
+
removeOverflowMenu
|
215
|
+
};
|
216
|
+
}
|
217
|
+
//# sourceMappingURL=overflowManager.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["debounce","createPriorityQueue","createOverflowManager","container","overflowMenu","observing","forceDispatch","options","padding","overflowAxis","overflowDirection","minimumVisible","onUpdateItemVisibility","undefined","onUpdateOverflow","overflowItems","overflowGroups","resizeObserver","ResizeObserver","entries","update","invisibleItemQueue","a","b","itemA","itemB","priority","positionStatusBit","Node","DOCUMENT_POSITION_FOLLOWING","DOCUMENT_POSITION_PRECEDING","element","compareDocumentPosition","visibleItemQueue","getOffsetSize","el","offsetWidth","offsetHeight","makeItemVisible","nextVisible","dequeue","enqueue","item","visible","groupId","invisibleItemIds","delete","id","visibleItemIds","add","makeItemInvisible","nextInvisible","width","dispatchOverflowUpdate","all","visibleItems","map","itemId","invisibleItems","groupVisibility","Object","forEach","groupState","size","processOverflowItems","availableSize","overflowMenuOffset","visibleTop","peek","invisibleTop","currentWidth","reduce","sum","visibleItemId","child","forceUpdate","observe","observedContainer","userOptions","assign","values","disconnect","addItem","Set","addOverflowMenu","removeOverflowMenu","removeItem","remove"],"sources":["../src/overflowManager.ts"],"sourcesContent":["import { debounce } from './debounce';\nimport { createPriorityQueue } from './priorityQueue';\nimport type { OverflowGroupState, OverflowItemEntry, OverflowManager, ObserveOptions } from './types';\n\n/**\n * @internal\n * @returns overflow manager instance\n */\nexport function createOverflowManager(): OverflowManager {\n let container: HTMLElement | undefined;\n let overflowMenu: HTMLElement | undefined;\n // Set as true when resize observer is observing\n let observing = false;\n // If true, next update will dispatch to onUpdateOverflow even if queue top states don't change\n let forceDispatch = false;\n const options: Required<ObserveOptions> = {\n padding: 10,\n overflowAxis: 'horizontal',\n overflowDirection: 'end',\n minimumVisible: 0,\n onUpdateItemVisibility: () => undefined,\n onUpdateOverflow: () => undefined,\n };\n\n const overflowItems: Record<string, OverflowItemEntry> = {};\n const overflowGroups: Record<string, { visibleItemIds: Set<string>; invisibleItemIds: Set<string> }> = {};\n const resizeObserver = new ResizeObserver(entries => {\n if (!entries[0] || !container) {\n return;\n }\n\n update();\n });\n\n const invisibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Higher priority at the top of the queue\n const priority = itemB.priority - itemA.priority;\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const visibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Lower priority at the top of the queue\n const priority = itemA.priority - itemB.priority;\n\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const getOffsetSize = (el: HTMLElement) => {\n return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight;\n };\n\n const makeItemVisible = () => {\n const nextVisible = invisibleItemQueue.dequeue();\n visibleItemQueue.enqueue(nextVisible);\n\n const item = overflowItems[nextVisible];\n options.onUpdateItemVisibility({ item, visible: true });\n if (item.groupId) {\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n return getOffsetSize(item.element);\n };\n\n const makeItemInvisible = () => {\n const nextInvisible = visibleItemQueue.dequeue();\n invisibleItemQueue.enqueue(nextInvisible);\n\n const item = overflowItems[nextInvisible];\n const width = getOffsetSize(item.element);\n options.onUpdateItemVisibility({ item, visible: false });\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.add(item.id);\n }\n\n return width;\n };\n\n const dispatchOverflowUpdate = () => {\n const visibleItemIds = visibleItemQueue.all();\n const invisibleItemIds = invisibleItemQueue.all();\n\n const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]);\n const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]);\n\n const groupVisibility: Record<string, OverflowGroupState> = {};\n Object.entries(overflowGroups).forEach(([groupId, groupState]) => {\n if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) {\n groupVisibility[groupId] = 'overflow';\n } else if (groupState.visibleItemIds.size === 0) {\n groupVisibility[groupId] = 'hidden';\n } else {\n groupVisibility[groupId] = 'visible';\n }\n });\n\n options.onUpdateOverflow({ visibleItems, invisibleItems, groupVisibility });\n };\n\n const processOverflowItems = (): boolean => {\n if (!container) {\n return false;\n }\n\n const availableSize = getOffsetSize(container) - options.padding;\n const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0;\n\n // Snapshot of the visible/invisible state to compare for updates\n const visibleTop = visibleItemQueue.peek();\n const invisibleTop = invisibleItemQueue.peek();\n\n const visibleItemIds = visibleItemQueue.all();\n let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => {\n const child = overflowItems[visibleItemId].element;\n return sum + getOffsetSize(child);\n }, 0);\n\n // Add items until available width is filled - can result in overflow\n while (currentWidth < availableSize && invisibleItemQueue.size() > 0) {\n currentWidth += makeItemVisible();\n }\n\n // Remove items until there's no more overflow\n while (currentWidth > availableSize && visibleItemQueue.size() > 0) {\n if (visibleItemQueue.size() <= options.minimumVisible) {\n break;\n }\n currentWidth -= makeItemInvisible();\n }\n\n // make sure the overflow menu can fit\n if (\n visibleItemQueue.size() > options.minimumVisible &&\n invisibleItemQueue.size() > 0 &&\n currentWidth + overflowMenuOffset > availableSize\n ) {\n makeItemInvisible();\n }\n\n // only update when the state of visible/invisible items has changed\n if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) {\n return true;\n }\n\n return false;\n };\n\n const forceUpdate: OverflowManager['forceUpdate'] = () => {\n if (processOverflowItems() || forceDispatch) {\n forceDispatch = false;\n dispatchOverflowUpdate();\n }\n };\n\n const update: OverflowManager['update'] = debounce(forceUpdate);\n\n const observe: OverflowManager['observe'] = (observedContainer, userOptions) => {\n Object.assign(options, userOptions);\n observing = true;\n Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id));\n\n container = observedContainer;\n resizeObserver.observe(container);\n };\n\n const disconnect: OverflowManager['disconnect'] = () => {\n observing = false;\n resizeObserver.disconnect();\n };\n\n const addItem: OverflowManager['addItem'] = item => {\n if (overflowItems[item.id]) {\n return;\n }\n\n overflowItems[item.id] = item;\n\n // some options can affect priority which are only set on `observe`\n if (observing) {\n // Updates to elements might not change the queue tops\n // i.e. new element is enqueued but the top of the queue stays the same\n // force a dispatch on the next batched update\n forceDispatch = true;\n visibleItemQueue.enqueue(item.id);\n }\n\n if (item.groupId) {\n if (!overflowGroups[item.groupId]) {\n overflowGroups[item.groupId] = {\n visibleItemIds: new Set<string>(),\n invisibleItemIds: new Set<string>(),\n };\n }\n\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n update();\n };\n\n const addOverflowMenu: OverflowManager['addOverflowMenu'] = el => {\n overflowMenu = el;\n };\n\n const removeOverflowMenu: OverflowManager['removeOverflowMenu'] = () => {\n overflowMenu = undefined;\n };\n\n const removeItem: OverflowManager['removeItem'] = itemId => {\n if (!overflowItems[itemId]) {\n return;\n }\n\n const item = overflowItems[itemId];\n visibleItemQueue.remove(itemId);\n invisibleItemQueue.remove(itemId);\n\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n }\n\n delete overflowItems[itemId];\n update();\n };\n\n return {\n addItem,\n disconnect,\n forceUpdate,\n observe,\n removeItem,\n update,\n addOverflowMenu,\n removeOverflowMenu,\n };\n}\n"],"mappings":"AAAA,SAASA,QAAQ,QAAQ;AACzB,SAASC,mBAAmB,QAAQ;AAGpC;;;;AAIA,OAAO,SAASC,sBAAA,EAAyC;EACvD,IAA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AO,CAAC,GAAG;UAC7BI,cAAA,EAAgB,IAAImC,GAAA;UACpBtC,gBAAA,EAAkB,IAAIsC,GAAA;QACxB;MACF;MAEAnE,cAAc,CAAC0B,IAAA,CAAKE,OAAO,CAAC,CAACI,cAAc,CAACC,GAAG,CAACP,IAAA,CAAKK,EAAE;IACzD;IAEA3B,MAAA;EACF;EAEA,MAAMgE,eAAA,GAAsDjD,EAAA,IAAM;IAChE/B,YAAA,GAAe+B,EAAA;EACjB;EAEA,MAAMkD,kBAAA,GAA4DA,CAAA,KAAM;IACtEjF,YAAA,GAAeS,SAAA;EACjB;EAEA,MAAMyE,UAAA,GAA4C7B,MAAA,IAAU;IAC1D,IAAI,CAAC1C,aAAa,CAAC0C,MAAA,CAAO,EAAE;MAC1B;IACF;IAEA,MAAMf,IAAA,GAAO3B,aAAa,CAAC0C,MAAA,CAAO;IAClCxB,gBAAA,CAAiBsD,MAAM,CAAC9B,MAAA;IACxBpC,kBAAA,CAAmBkE,MAAM,CAAC9B,MAAA;IAE1B,IAAIf,IAAA,CAAKE,OAAO,EAAE;MAChB5B,cAAc,CAAC0B,IAAA,CAAKE,OAAO,CAAC,CAACI,cAAc,CAACF,MAAM,CAACJ,IAAA,CAAKK,EAAE;MAC1D/B,cAAc,CAAC0B,IAAA,CAAKE,OAAO,CAAC,CAACC,gBAAgB,CAACC,MAAM,CAACJ,IAAA,CAAKK,EAAE;IAC9D;IAEA,OAAOhC,aAAa,CAAC0C,MAAA,CAAO;IAC5BrC,MAAA;EACF;EAEA,OAAO;IACL8D,OAAA;IACAD,UAAA;IACAN,WAAA;IACAC,OAAA;IACAU,UAAA;IACAlE,MAAA;IACAgE,eAAA;IACAC;EACF;AACF"}
|
@@ -0,0 +1,90 @@
|
|
1
|
+
/**
|
2
|
+
* @param compare - comparison function for items
|
3
|
+
* @returns Priority queue implemented with a min heap
|
4
|
+
*/export function createPriorityQueue(compare) {
|
5
|
+
const arr = [];
|
6
|
+
let size = 0;
|
7
|
+
const left = i => {
|
8
|
+
return 2 * i + 1;
|
9
|
+
};
|
10
|
+
const right = i => {
|
11
|
+
return 2 * i + 2;
|
12
|
+
};
|
13
|
+
const parent = i => {
|
14
|
+
return Math.floor((i - 1) / 2);
|
15
|
+
};
|
16
|
+
const swap = (a, b) => {
|
17
|
+
const tmp = arr[a];
|
18
|
+
arr[a] = arr[b];
|
19
|
+
arr[b] = tmp;
|
20
|
+
};
|
21
|
+
const heapify = i => {
|
22
|
+
let smallest = i;
|
23
|
+
const l = left(i);
|
24
|
+
const r = right(i);
|
25
|
+
if (l < size && compare(arr[l], arr[smallest]) < 0) {
|
26
|
+
smallest = l;
|
27
|
+
}
|
28
|
+
if (r < size && compare(arr[r], arr[smallest]) < 0) {
|
29
|
+
smallest = r;
|
30
|
+
}
|
31
|
+
if (smallest !== i) {
|
32
|
+
swap(smallest, i);
|
33
|
+
heapify(smallest);
|
34
|
+
}
|
35
|
+
};
|
36
|
+
const dequeue = () => {
|
37
|
+
if (size === 0) {
|
38
|
+
throw new Error('Priority queue empty');
|
39
|
+
}
|
40
|
+
const res = arr[0];
|
41
|
+
arr[0] = arr[--size];
|
42
|
+
heapify(0);
|
43
|
+
return res;
|
44
|
+
};
|
45
|
+
const peek = () => {
|
46
|
+
if (size === 0) {
|
47
|
+
return null;
|
48
|
+
}
|
49
|
+
return arr[0];
|
50
|
+
};
|
51
|
+
const enqueue = item => {
|
52
|
+
arr[size++] = item;
|
53
|
+
let i = size - 1;
|
54
|
+
let p = parent(i);
|
55
|
+
while (i > 0 && compare(arr[p], arr[i]) > 0) {
|
56
|
+
swap(p, i);
|
57
|
+
i = p;
|
58
|
+
p = parent(i);
|
59
|
+
}
|
60
|
+
};
|
61
|
+
const contains = item => {
|
62
|
+
const index = arr.indexOf(item);
|
63
|
+
return index >= 0 && index < size;
|
64
|
+
};
|
65
|
+
const remove = item => {
|
66
|
+
const i = arr.indexOf(item);
|
67
|
+
if (i === -1 || i >= size) {
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
arr[i] = arr[--size];
|
71
|
+
heapify(i);
|
72
|
+
};
|
73
|
+
const clear = () => {
|
74
|
+
size = 0;
|
75
|
+
};
|
76
|
+
const all = () => {
|
77
|
+
return arr.slice(0, size);
|
78
|
+
};
|
79
|
+
return {
|
80
|
+
all,
|
81
|
+
clear,
|
82
|
+
contains,
|
83
|
+
dequeue,
|
84
|
+
enqueue,
|
85
|
+
peek,
|
86
|
+
remove,
|
87
|
+
size: () => size
|
88
|
+
};
|
89
|
+
}
|
90
|
+
//# sourceMappingURL=priorityQueue.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["createPriorityQueue","compare","arr","size","left","i","right","parent","Math","floor","swap","a","b","tmp","heapify","smallest","l","r","dequeue","Error","res","peek","enqueue","item","p","contains","index","indexOf","remove","clear","all","slice"],"sources":["../src/priorityQueue.ts"],"sourcesContent":["export type PriorityQueueCompareFn<T> = (a: T, b: T) => number;\n\nexport interface PriorityQueue<T> {\n all: () => T[];\n clear: () => void;\n contains: (item: T) => boolean;\n dequeue: () => T;\n enqueue: (item: T) => void;\n peek: () => T | null;\n remove: (item: T) => void;\n size: () => number;\n}\n\n/**\n * @param compare - comparison function for items\n * @returns Priority queue implemented with a min heap\n */\nexport function createPriorityQueue<T>(compare: PriorityQueueCompareFn<T>): PriorityQueue<T> {\n const arr: T[] = [];\n let size = 0;\n\n const left = (i: number) => {\n return 2 * i + 1;\n };\n\n const right = (i: number) => {\n return 2 * i + 2;\n };\n\n const parent = (i: number) => {\n return Math.floor((i - 1) / 2);\n };\n\n const swap = (a: number, b: number) => {\n const tmp = arr[a];\n arr[a] = arr[b];\n arr[b] = tmp;\n };\n\n const heapify = (i: number) => {\n let smallest = i;\n const l = left(i);\n const r = right(i);\n\n if (l < size && compare(arr[l], arr[smallest]) < 0) {\n smallest = l;\n }\n\n if (r < size && compare(arr[r], arr[smallest]) < 0) {\n smallest = r;\n }\n\n if (smallest !== i) {\n swap(smallest, i);\n heapify(smallest);\n }\n };\n\n const dequeue = () => {\n if (size === 0) {\n throw new Error('Priority queue empty');\n }\n\n const res = arr[0];\n arr[0] = arr[--size];\n heapify(0);\n\n return res;\n };\n\n const peek = () => {\n if (size === 0) {\n return null;\n }\n\n return arr[0];\n };\n\n const enqueue = (item: T) => {\n arr[size++] = item;\n let i = size - 1;\n let p = parent(i);\n while (i > 0 && compare(arr[p], arr[i]) > 0) {\n swap(p, i);\n i = p;\n p = parent(i);\n }\n };\n\n const contains = (item: T) => {\n const index = arr.indexOf(item);\n return index >= 0 && index < size;\n };\n\n const remove = (item: T) => {\n const i = arr.indexOf(item);\n\n if (i === -1 || i >= size) {\n return;\n }\n\n arr[i] = arr[--size];\n heapify(i);\n };\n\n const clear = () => {\n size = 0;\n };\n\n const all = () => {\n return arr.slice(0, size);\n };\n\n return {\n all,\n clear,\n contains,\n dequeue,\n enqueue,\n peek,\n remove,\n size: () => size,\n };\n}\n"],"mappings":"AAaA;;;GAIA,OAAO,SAASA,oBAAuBC,OAAkC,EAAoB;EAC3F,MAAMC,GAAA,GAAW,EAAE;EACnB,IAAIC,IAAA,GAAO;EAEX,MAAMC,IAAA,GAAQC,CAAA,IAAc;IAC1B,OAAO,IAAIA,CAAA,GAAI;EACjB;EAEA,MAAMC,KAAA,GAASD,CAAA,IAAc;IAC3B,OAAO,IAAIA,CAAA,GAAI;EACjB;EAEA,MAAME,MAAA,GAAUF,CAAA,IAAc;IAC5B,OAAOG,IAAA,CAAKC,KAAK,CAAC,CAACJ,CAAA,GAAI,KAAK;EAC9B;EAEA,MAAMK,IAAA,GAAOA,CAACC,CAAA,EAAWC,CAAA,KAAc;IACrC,MAAMC,GAAA,GAAMX,GAAG,CAACS,CAAA,CAAE;IAClBT,GAAG,CAACS,CAAA,CAAE,GAAGT,GAAG,CAACU,CAAA,CAAE;IACfV,GAAG,CAACU,CAAA,CAAE,GAAGC,GAAA;EACX;EAEA,MAAMC,OAAA,GAAWT,CAAA,IAAc;IAC7B,IAAIU,QAAA,GAAWV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;MACRA,CAAA,GAAImB,CAAA;MACJA,CAAA,GAAIjB,MAAA,CAAOF,CAAA;IACb;EACF;EAEA,MAAMoB,QAAA,GAAYF,IAAA,IAAY;IAC5B,MAAMG,KAAA,GAAQxB,GAAA,CAAIyB,OAAO,CAACJ,IAAA;IAC1B,OAAOG,KAAA,IAAS,KAAKA,KAAA,GAAQvB,IAAA;EAC/B;EAEA,MAAMyB,MAAA,GAAUL,IAAA,IAAY;IAC1B,MAAMlB,CAAA,GAAIH,GAAA,CAAIyB,OAAO,CAACJ,IAAA;IAEtB,IAAIlB,CAAA,KAAM,CAAC,KAAKA,CAAA,IAAKF,IAAA,EAAM;MACzB;IACF;IAEAD,GAAG,CAACG,CAAA,CAAE,GAAGH,GAAG,CAAC,EAAEC,IAAA,CAAK;IACpBW,OAAA,CAAQT,CAAA;EACV;EAEA,MAAMwB,KAAA,GAAQA,CAAA,KAAM;IAClB1B,IAAA,GAAO;EACT;EAEA,MAAM2B,GAAA,GAAMA,CAAA,KAAM;IAChB,OAAO5B,GAAA,CAAI6B,KAAK,CAAC,GAAG5B,IAAA;EACtB;EAEA,OAAO;IACL2B,GAAA;IACAD,KAAA;IACAJ,QAAA;IACAP,OAAA;IACAI,OAAA;IACAD,IAAA;IACAO,MAAA;IACAzB,IAAA,EAAMA,CAAA,KAAMA;EACd;AACF"}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":[],"sources":["../src/types.ts"],"sourcesContent":["export type OverflowDirection = 'start' | 'end';\nexport type OverflowAxis = 'horizontal' | 'vertical';\nexport type OverflowGroupState = 'visible' | 'hidden' | 'overflow';\nexport interface OverflowItemEntry {\n /**\n * HTML element that will be disappear when overflowed\n */\n element: HTMLElement;\n /**\n * Lower priority items are invisible first when the container is overflowed\n * @default 0\n */\n priority: number;\n /**\n * Specific id, used to track visibility and provide updates to consumers\n */\n id: string;\n\n groupId?: string;\n}\n\n/**\n * signature similar to standard event listeners, but typed to handle the custom event\n */\nexport type OnUpdateOverflow = (data: OverflowEventPayload) => void;\n\nexport type OnUpdateItemVisibility = (data: OnUpdateItemVisibilityPayload) => void;\n\n/**\n * Payload of the custom DOM event for overflow updates\n */\nexport interface OverflowEventPayload {\n visibleItems: OverflowItemEntry[];\n invisibleItems: OverflowItemEntry[];\n groupVisibility: Record<string, OverflowGroupState>;\n}\n\nexport interface OnUpdateItemVisibilityPayload {\n item: OverflowItemEntry;\n visible: boolean;\n}\n\nexport interface ObserveOptions {\n /**\n * Padding (in px) at the end of the container before overflow occurs\n * Useful to account for extra elements (i.e. dropdown menu)\n * or to account for any kinds of margins between items which are hard to measure with JS\n * @default 10\n */\n padding?: number;\n /**\n * Direction where items are removed when overflow occurs\n * @default end\n */\n overflowDirection?: OverflowDirection;\n\n /**\n * Horizontal or vertical overflow\n * @default horizontal\n */\n overflowAxis?: OverflowAxis;\n\n /**\n * The minimum number of visible items\n */\n minimumVisible?: number;\n\n /**\n * Callback when item visibility is updated\n */\n onUpdateItemVisibility: OnUpdateItemVisibility;\n\n /**\n * Callback when item visibility is updated\n */\n onUpdateOverflow: OnUpdateOverflow;\n}\n\n/**\n * @internal\n */\nexport interface OverflowManager {\n /**\n * Starts observing the container and managing the overflow state\n */\n observe: (container: HTMLElement, options: ObserveOptions) => void;\n /**\n * Stops observing the container\n */\n disconnect: () => void;\n /**\n * Add overflow items\n */\n addItem: (items: OverflowItemEntry) => void;\n /**\n * Remove overflow item\n */\n removeItem: (itemId: string) => void;\n /**\n * Manually update the overflow, updates are batched and async\n */\n update: () => void;\n /**\n * Manually update the overflow sync\n */\n forceUpdate: () => void;\n\n /**\n * Adds an element that opens an overflow menu. This is used to calculate\n * available space and check if additional items need to overflow\n */\n addOverflowMenu: (element: HTMLElement) => void;\n\n /**\n * Unsets the overflow menu element\n */\n removeOverflowMenu: () => void;\n}\n"],"mappings":"AAAA"}
|
package/package.json
CHANGED