-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmatchContainer.js
More file actions
128 lines (112 loc) · 4.13 KB
/
matchContainer.js
File metadata and controls
128 lines (112 loc) · 4.13 KB
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
(() => {
// To guard against the case that this polyfill might be included more than once
// we must ensure that the ID generating function is a singleton
// to guarantee that no duplicated IDs are generated - however slim the chance might be.
// The IDs are unique in the current javascript runtime context.
// The IDs are constructed from the following parts:
// - a timestamp
// - the current value of a counter, incremented after each created ID
// - a static prefix
// Even if two IDs are generated within the same milisecond the increased counter ensures
// that they cannot clash.
const polyfill_key = "bae45330cd3d4e0e96b60d26b57009b5";
const polyfill = Symbol.for(polyfill_key);
window[polyfill] =
window[polyfill] ??
(() => {
let count = 0;
return { createID: () => `${polyfill_key}-${Date.now()}-${count++}` };
})();
const createID = window[polyfill].createID;
class ContainerQueryListEvent extends Event {
container;
matches;
constructor(type) {
super(type);
}
}
class ContainerQueryList extends EventTarget {
container;
matches;
constructor(element, containerQueryString) {
super();
this.container = containerQueryString;
const unique_name = "container-query-observer-" + createID();
const markerAttribute = `data-${unique_name}`;
element.setAttribute(markerAttribute, "");
const sentinelProperty = `--${unique_name}`;
const containerQuerySheet = new CSSStyleSheet();
const css = `
@property ${sentinelProperty} {
syntax: '<custom-ident>';
inherits: false;
initial-value: --false;
}
@container ${containerQueryString} {
[${markerAttribute}] {
${sentinelProperty}: --true;
}
}
[${markerAttribute}] {
@starting-style {
${sentinelProperty}: --unknown;
}
}`;
containerQuerySheet.replaceSync(css);
document.adoptedStyleSheets = [
...document.adoptedStyleSheets,
containerQuerySheet,
];
const style = getComputedStyle(element);
this.matches = style.getPropertyValue(sentinelProperty) === "--true";
this.#startObserving(sentinelProperty, containerQueryString, element);
}
#startObserving(sentinelProperty, containerQueryString, observedElement) {
const _callback = (values) => {
if (sentinelProperty in values) {
const matches = values[sentinelProperty] === "--true";
this.matches = matches;
// raise 'change' event
const event = new ContainerQueryListEvent("change");
event.matches = matches;
event.container = containerQueryString;
this.dispatchEvent(event);
}
};
const _previousValues = {};
observedElement.style.setProperty(
"transition",
`${sentinelProperty} 0.001ms step-start`
);
observedElement.style.setProperty(
"transition-behavior",
"allow-discrete"
);
const onTransitionRun = (e) => {
const targetElement = e.target;
if (observedElement === targetElement) {
const computedStyle = getComputedStyle(targetElement);
const changes = {};
const currentValue = computedStyle.getPropertyValue(sentinelProperty);
const previousValue = _previousValues[sentinelProperty];
const hasChanged = currentValue !== previousValue;
if (hasChanged) {
changes[sentinelProperty] = currentValue;
_previousValues[sentinelProperty] = currentValue;
_callback(changes);
}
}
};
observedElement.addEventListener("transitionrun", onTransitionRun);
// init _previousValues
const computedStyle = getComputedStyle(observedElement);
const currentValue = computedStyle.getPropertyValue(sentinelProperty);
_previousValues[sentinelProperty] = currentValue;
}
}
if (Element.prototype.matchContainer) return;
function matchContainer(containerQueryString) {
return new ContainerQueryList(this, containerQueryString);
}
Element.prototype.matchContainer = matchContainer;
})();