-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitcherLayout.js
90 lines (78 loc) · 2.42 KB
/
SwitcherLayout.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
customElements.define(
'switcher-layout',
class extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
}
render() {
// prettier-ignore
this.shadowRoot.innerHTML = `
<style>
:host {
display: flex;
flex-wrap: wrap;
gap: ${this.space};
}
::slotted(*) {
flex-grow: 1;
flex-basis: calc(${this.threshold} - 100%) * 999);
}
</style>
<slot></slot>
`
// console.log('switcher:', this.shadowRoot.innerHTML)
}
connectedCallback() {
this.render()
}
attributeChangedCallback() {
this.render()
}
get switchToVertical() {
const limit = parseInt(this.limit)
const children = this.childElementCount
const result = limit + 1 < children
// console.log('switch, limit, children', result, limit, children)
return result
}
static get observedAttributes() {
return ['threshold', 'space', 'limit']
}
get threshold() {
return this.getAttribute('threshold') || 'var(--measure)'
}
set threshold(val) {
return this.setAttribute('threshold', val)
}
get space() {
return this.getAttribute('space') || 'var(--s1)'
}
set space(val) {
return this.setAttribute('space', val)
}
get limit() {
return this.getAttribute('limit') || '4'
}
set limit(val) {
return this.setAttribute('limit', val)
}
}
)
// flex-basis: calc(( 30rem - 100%) * 999);
/*
::slotted(:nth-last-child(n+ 5)) ,
:nth-last-child(n+ 5) ~ * {
flex-basis: 100%;
}
${ this.switchToVertical ? `
::slotted(*) {
flex-basis: 100%;
}` : ''
}
${ this.switchToVertical ? `
::slotted(:nth-child(n + 2) {
flex-basis: 100%;
}` : ''
}
*/