-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackdrop-filter.ts
112 lines (88 loc) · 2.49 KB
/
backdrop-filter.ts
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
import {
LitElement,
html,
css,
CSSResult,
property,
customElement,
} from 'lit-element'
@customElement('backdrop-filter')
class BackdropFilter extends LitElement {
@property({ type: String })
public filters: string = 'none'
static styles = [
css`
:host {
display: block;
width: 100%;
height: 100%;
overflow: hidden;
background-color: white;
}
#inner {
position: relative;
width: var(--body-width);
height: var(--body-height);
top: calc(8px - var(--y));
left: calc(8px - var(--x));
will-change: transform;
filter: var(--filters);
}
`,
]
render() {
return html`
<div id="inner">
${this.cloneOutside()}
</div>
`
}
connectedCallback() {
super.connectedCallback()
const {
width: bodyWidth,
height: bodyHeight,
} = document.body.getBoundingClientRect()
this.style.setProperty('--body-width', `${bodyWidth}px`)
this.style.setProperty('--body-height', `${bodyHeight}px`)
const { left: x, top: y } = this.parentElement.getBoundingClientRect()
this.style.setProperty('--x', `${x}px`)
this.style.setProperty('--y', `${y}px`)
window.addEventListener('scroll', () => {
requestAnimationFrame(() => {
;(this.shadowRoot
.children[0] as HTMLElement).style.transform = `translateY(-${
document.body.scrollTop || document.documentElement.scrollTop
}px)`
})
})
window.addEventListener('resize', () => {
requestAnimationFrame(() => {
const { left, top } = this.parentElement.getBoundingClientRect()
const { width, height } = document.body.getBoundingClientRect()
this.style.setProperty('--body-width', `${width}px`)
this.style.setProperty('--body-height', `${height}px`)
this.style.setProperty('--x', `${left}px`)
this.style.setProperty('--y', `${top}px`)
})
})
}
attributeChangedCallback(name, old, value) {
super.attributeChangedCallback(name, old, value)
this.style.setProperty('--filters', this.filters)
}
private cloneOutside() {
let parent: HTMLElement | undefined
if (this.isConnected) {
parent = this.parentElement
parent.removeChild(this)
}
const clones = [...document.body.children, ...document.head.children].map(
child => child.cloneNode(true),
)
if (parent) {
parent.appendChild(this)
}
return clones
}
}