-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
split-pane.tsx
202 lines (176 loc) · 4.79 KB
/
split-pane.tsx
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Build, Component, Element, Event, Host, Method, Prop, State, Watch, h } from '@stencil/core';
import { getIonMode } from '../../global/ionic-global';
// TODO(FW-2832): types
const SPLIT_PANE_MAIN = 'split-pane-main';
const SPLIT_PANE_SIDE = 'split-pane-side';
const QUERY: { [key: string]: string } = {
xs: '(min-width: 0px)',
sm: '(min-width: 576px)',
md: '(min-width: 768px)',
lg: '(min-width: 992px)',
xl: '(min-width: 1200px)',
never: '',
};
@Component({
tag: 'ion-split-pane',
styleUrls: {
ios: 'split-pane.ios.scss',
md: 'split-pane.md.scss',
},
shadow: true,
})
export class SplitPane implements ComponentInterface {
private rmL?: () => void;
@Element() el!: HTMLElement;
@State() visible = false;
/**
* The `id` of the main content. When using
* a router this is typically `ion-router-outlet`.
* When not using a router, this is typically
* your main view's `ion-content`. This is not the
* id of the `ion-content` inside of your `ion-menu`.
*/
@Prop({ reflect: true }) contentId?: string;
/**
* If `true`, the split pane will be hidden.
*/
@Prop() disabled = false;
/**
* When the split-pane should be shown.
* Can be a CSS media query expression, or a shortcut expression.
* Can also be a boolean expression.
*/
@Prop() when: string | boolean = QUERY['lg'];
/**
* Expression to be called when the split-pane visibility has changed
*/
@Event() ionSplitPaneVisible!: EventEmitter<{ visible: boolean }>;
@Watch('visible')
visibleChanged(visible: boolean) {
this.ionSplitPaneVisible.emit({ visible });
}
/**
* @internal
*/
@Method()
async isVisible(): Promise<boolean> {
return Promise.resolve(this.visible);
}
async connectedCallback() {
// TODO: connectedCallback is fired in CE build
// before WC is defined. This needs to be fixed in Stencil.
if (typeof customElements !== 'undefined' && customElements != null) {
await customElements.whenDefined('ion-split-pane');
}
this.styleMainElement();
this.updateState();
}
disconnectedCallback() {
if (this.rmL) {
this.rmL();
this.rmL = undefined;
}
}
@Watch('disabled')
@Watch('when')
protected updateState() {
if (!Build.isBrowser) {
return;
}
if (this.rmL) {
this.rmL();
this.rmL = undefined;
}
// Check if the split-pane is disabled
if (this.disabled) {
this.visible = false;
return;
}
// When query is a boolean
const query = this.when;
if (typeof query === 'boolean') {
this.visible = query;
return;
}
// When query is a string, let's find first if it is a shortcut
const mediaQuery = QUERY[query] || query;
// Media query is empty or null, we hide it
if (mediaQuery.length === 0) {
this.visible = false;
return;
}
// Listen on media query
const callback = (q: MediaQueryList) => {
this.visible = q.matches;
};
const mediaList = window.matchMedia(mediaQuery);
// TODO FW-5869
mediaList.addListener(callback as any);
this.rmL = () => mediaList.removeListener(callback as any);
this.visible = mediaList.matches;
}
/**
* Attempt to find the main content
* element inside of the split pane.
* If found, set it as the main node.
*
* We assume that the main node
* is available in the DOM on split
* pane load.
*/
private styleMainElement() {
if (!Build.isBrowser) {
return;
}
const contentId = this.contentId;
const children = this.el.children;
const nu = this.el.childElementCount;
let foundMain = false;
for (let i = 0; i < nu; i++) {
const child = children[i] as HTMLElement;
const isMain = contentId !== undefined && child.id === contentId;
if (isMain) {
if (foundMain) {
console.warn('split pane cannot have more than one main node');
return;
} else {
setPaneClass(child, isMain);
foundMain = true;
}
}
}
if (!foundMain) {
console.warn('split pane does not have a specified main node');
}
}
render() {
const mode = getIonMode(this);
return (
<Host
class={{
[mode]: true,
// Used internally for styling
[`split-pane-${mode}`]: true,
'split-pane-visible': this.visible,
}}
>
<slot></slot>
</Host>
);
}
}
const setPaneClass = (el: HTMLElement, isMain: boolean) => {
let toAdd;
let toRemove;
if (isMain) {
toAdd = SPLIT_PANE_MAIN;
toRemove = SPLIT_PANE_SIDE;
} else {
toAdd = SPLIT_PANE_SIDE;
toRemove = SPLIT_PANE_MAIN;
}
const classList = el.classList;
classList.add(toAdd);
classList.remove(toRemove);
};