Skip to content

Commit fee2755

Browse files
committed
fix: layout components when dialog is opened
Fixes #35.
1 parent 601d4fd commit fee2755

File tree

7 files changed

+93
-12
lines changed

7 files changed

+93
-12
lines changed

packages/data-table/DataTable.svelte

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<script>
2121
import {MDCDataTable} from '@material/data-table';
2222
import {events} from '@material/data-table/constants';
23-
import {onMount, onDestroy, setContext} from 'svelte';
23+
import {onMount, onDestroy, getContext, setContext} from 'svelte';
2424
import {current_component} from 'svelte/internal';
2525
import {forwardEventsBuilder} from '@smui/common/forwardEvents.js';
2626
import {exclude} from '@smui/common/exclude.js';
@@ -50,12 +50,18 @@
5050
let checkBoxHeaderPromise = new Promise(resolve => checkBoxHeaderPromiseResolve = resolve);
5151
let checkBoxListPromiseResolve;
5252
let checkBoxListPromise = new Promise(resolve => checkBoxListPromiseResolve = resolve);
53+
let addLayoutListener = getContext('SMUI:addLayoutListener');
54+
let removeLayoutListener;
5355
5456
setContext('SMUI:generic:input:addChangeHandler', addChangeHandler);
5557
setContext('SMUI:checkbox:context', 'data-table');
5658
setContext('SMUI:checkbox:instantiate', false);
5759
setContext('SMUI:checkbox:getInstance', getCheckboxInstancePromise);
5860
61+
if (addLayoutListener) {
62+
removeLayoutListener = addLayoutListener(layout);
63+
}
64+
5965
onMount(async () => {
6066
dataTable = new MDCDataTable(element);
6167
checkBoxHeaderPromiseResolve(dataTable.headerRowCheckbox_);
@@ -72,7 +78,11 @@
7278
});
7379
7480
onDestroy(() => {
75-
dataTable && dataTable.destroy()
81+
dataTable && dataTable.destroy();
82+
83+
if (removeLayoutListener) {
84+
removeLayoutListener();
85+
}
7686
});
7787
7888
function getCheckboxInstancePromise(header) {

packages/dialog/Dialog.svelte

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<script>
2020
import {MDCDialog} from '@material/dialog';
21-
import {onMount, onDestroy, setContext} from 'svelte';
21+
import {onMount, onDestroy, getContext, setContext} from 'svelte';
2222
import {current_component} from 'svelte/internal';
2323
import {forwardEventsBuilder} from '@smui/common/forwardEvents.js';
2424
import {exclude} from '@smui/common/exclude.js';
@@ -35,21 +35,40 @@
3535
3636
let element;
3737
let dialog;
38+
let addLayoutListener = getContext('SMUI:addLayoutListener');
39+
let removeLayoutListener;
3840
let layoutListeners = [];
39-
let addLayoutListener = listener => layoutListeners.push(listener);
41+
let addLayoutListenerFn = listener => {
42+
layoutListeners.push(listener);
4043
41-
setContext('SMUI:addLayoutListener', addLayoutListener);
44+
return () => {
45+
const idx = layoutListeners.indexOf(listener);
46+
if (idx >= 0) {
47+
layoutListeners.splice(idx, 1);
48+
}
49+
};
50+
};
51+
52+
setContext('SMUI:addLayoutListener', addLayoutListenerFn);
4253
4354
$: dialog && (dialog.escapeKeyAction = escapeKeyAction);
4455
$: dialog && (dialog.scrimClickAction = scrimClickAction);
4556
$: dialog && (dialog.autoStackButtons = autoStackButtons);
4657
58+
if (addLayoutListener) {
59+
removeLayoutListener = addLayoutListener(layout);
60+
}
61+
4762
onMount(() => {
4863
dialog = new MDCDialog(element);
4964
});
5065
5166
onDestroy(() => {
52-
dialog && dialog.destroy()
67+
dialog && dialog.destroy();
68+
69+
if (removeLayoutListener) {
70+
removeLayoutListener();
71+
}
5372
});
5473
5574
function handleDialogOpened() {

packages/list/List.svelte

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
let nav = getContext('SMUI:list:nav');
6363
let instantiate = getContext('SMUI:list:instantiate');
6464
let getInstance = getContext('SMUI:list:getInstance');
65+
let addLayoutListener = getContext('SMUI:addLayoutListener');
66+
let removeLayoutListener;
6567
6668
setContext('SMUI:list:nonInteractive', nonInteractive);
6769
@@ -97,6 +99,10 @@
9799
list.selectedIndex = selectedIndex;
98100
}
99101
102+
if (addLayoutListener) {
103+
removeLayoutListener = addLayoutListener(layout);
104+
}
105+
100106
onMount(async () => {
101107
if (instantiate !== false) {
102108
list = new MDCList(element);
@@ -111,7 +117,11 @@
111117
112118
onDestroy(() => {
113119
if (instantiate !== false) {
114-
list && list.destroy()
120+
list && list.destroy();
121+
}
122+
123+
if (removeLayoutListener) {
124+
removeLayoutListener();
115125
}
116126
});
117127

packages/ripple/Ripple.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {MDCRipple} from '@material/ripple';
2+
import {getContext} from 'svelte';
23

34
export default function Ripple(node, [ripple, props = {unbounded: false, color: null}]) {
45
let instance = null;
6+
let addLayoutListener = getContext('SMUI:addLayoutListener');
7+
let removeLayoutListener;
58

69
function handleProps(ripple, props) {
710
if (ripple && !instance) {
@@ -39,6 +42,16 @@ export default function Ripple(node, [ripple, props = {unbounded: false, color:
3942
handleProps(ripple, props);
4043
}
4144

45+
if (addLayoutListener) {
46+
removeLayoutListener = addLayoutListener(layout);
47+
}
48+
49+
function layout() {
50+
if (instance) {
51+
instance.layout();
52+
}
53+
}
54+
4255
return {
4356
update([ripple, props = {unbounded: false, color: null}]) {
4457
handleProps(ripple, props);
@@ -52,6 +65,10 @@ export default function Ripple(node, [ripple, props = {unbounded: false, color:
5265
node.classList.remove('mdc-ripple-surface--primary');
5366
node.classList.remove('mdc-ripple-surface--accent');
5467
}
68+
69+
if (removeLayoutListener) {
70+
removeLayoutListener();
71+
}
5572
}
5673
}
5774
}

packages/select/Select.svelte

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@
132132
let inputElement;
133133
let menuPromiseResolve;
134134
let menuPromise = new Promise(resolve => menuPromiseResolve = resolve);
135+
let addLayoutListener = getContext('SMUI:addLayoutListener');
136+
let removeLayoutListener;
135137
136138
setContext('SMUI:menu:instantiate', false);
137139
setContext('SMUI:menu:getInstance', getMenuInstancePromise);
@@ -166,6 +168,10 @@
166168
select.required = required;
167169
}
168170
171+
if (addLayoutListener) {
172+
removeLayoutListener = addLayoutListener(layout);
173+
}
174+
169175
onMount(async () => {
170176
select = new MDCSelect(element);
171177
@@ -181,7 +187,11 @@
181187
});
182188
183189
onDestroy(() => {
184-
select && select.destroy()
190+
select && select.destroy();
191+
192+
if (removeLayoutListener) {
193+
removeLayoutListener();
194+
}
185195
});
186196
187197
function getMenuInstancePromise() {

packages/slider/Slider.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
let formField = getContext('SMUI:form-field');
6363
let inputProps = getContext('SMUI:generic:input:props') || {};
6464
let addLayoutListener = getContext('SMUI:addLayoutListener');
65+
let removeLayoutListener;
6566
6667
$: if (slider && slider.disabled !== disabled) {
6768
slider.disabled = disabled;
@@ -84,7 +85,7 @@
8485
}
8586
8687
if (addLayoutListener) {
87-
addLayoutListener(layout);
88+
removeLayoutListener = addLayoutListener(layout);
8889
}
8990
9091
onMount(() => {
@@ -96,7 +97,11 @@
9697
});
9798
9899
onDestroy(() => {
99-
slider && slider.destroy()
100+
slider && slider.destroy();
101+
102+
if (removeLayoutListener) {
103+
removeLayoutListener();
104+
}
100105
});
101106
102107
function handleChange() {

packages/textfield/Textfield.svelte

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484
<script>
8585
import {MDCTextField} from '@material/textfield';
86-
import {onMount, onDestroy} from 'svelte';
86+
import {onMount, onDestroy, getContext} from 'svelte';
8787
import {current_component} from 'svelte/internal';
8888
import {forwardEventsBuilder} from '@smui/common/forwardEvents.js';
8989
import {exclude} from '@smui/common/exclude.js';
@@ -123,6 +123,8 @@
123123
124124
let element;
125125
let textField;
126+
let addLayoutListener = getContext('SMUI:addLayoutListener');
127+
let removeLayoutListener;
126128
127129
$: valued = value !== uninitializedValue || files !== uninitializedValue;
128130
@@ -148,6 +150,10 @@
148150
textField.useNativeValidation = useNativeValidation;
149151
}
150152
153+
if (addLayoutListener) {
154+
removeLayoutListener = addLayoutListener(layout);
155+
}
156+
151157
onMount(() => {
152158
textField = new MDCTextField(element);
153159
@@ -157,7 +163,11 @@
157163
});
158164
159165
onDestroy(() => {
160-
textField && textField.destroy()
166+
textField && textField.destroy();
167+
168+
if (removeLayoutListener) {
169+
removeLayoutListener();
170+
}
161171
});
162172
163173
export function focus(...args) {

0 commit comments

Comments
 (0)