Skip to content

Commit a76fa8a

Browse files
committed
Disabling the modifier
1 parent 319f882 commit a76fa8a

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ For example, if you put a responsive component into a tight sidebar, it will ali
2626
- [Customizing attribute prefix](#customizing-attribute-prefix)
2727
- [Using multiple modifiers on the same element](#using-multiple-modifiers-on-the-same-element)
2828
- [Using both width and height on the Element Query component](#using-both-width-and-height-on-the-element-query-component)
29+
- [Disabling](#disabling)
2930
- [Browser support](#browser-support)
3031
- [Alternatives](#alternatives)
3132
- [Comparison](#comparison)
@@ -61,13 +62,15 @@ This addon is in active development.
6162
* [x] Accepts `dimension`
6263
* [x] Updates on arugments change
6364
* [x] Add fool-proof exceptions
65+
* [x] Disabling
6466
* [ ] `<ElementQuery>` component
6567
* [ ] Exists
6668
* [ ] Applies attributes to itself
6769
* [ ] Yields block params
6870
* [ ] Accepts `sizes`
6971
* [ ] Accepts `prefix`
7072
* [ ] Accepts `dimension`
73+
* [ ] Disabling
7174
* [ ] Expose types
7275
* [ ] CI
7376
* [ ] npm package
@@ -474,6 +477,25 @@ You can also pass `true` to `@sizesHeight`, which will enable default sizes. The
474477

475478

476479

480+
### Disabling
481+
482+
Pass a truthy value into `isDisabled` (modifier) or `@isDisabled` (component) to disable element query functionality:
483+
484+
```
485+
<img
486+
{{element-query @isDisabled=true}}
487+
>
488+
```
489+
490+
```
491+
<ElementQuery
492+
@isDisabled={{true}}
493+
>
494+
</ElementQuery>
495+
```
496+
497+
498+
477499
Browser support
478500
------------------------------------------------------------------------------
479501

addon/-private/modifier.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface Args extends ModifierArgs {
1010
sizes?: Sizes;
1111
prefix?: string;
1212
dimension?: Dimension;
13+
isDisabled?: boolean;
1314
};
1415
}
1516

@@ -225,7 +226,7 @@ export default class ElementQueryModifier extends Modifier<Args> {
225226

226227
@action didResizeHandler(): void {
227228
window.requestAnimationFrame(() => {
228-
if (!this.isDestroying && !this.isDestroyed) {
229+
if (!this.args.named.isDisabled && !this.isDestroying && !this.isDestroyed) {
229230
this.applyAttributesToElement();
230231
this.callOnResize();
231232
}

tests/integration/modifier-test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import hbs from 'htmlbars-inline-precompile';
55
import sinon, { SinonSpy } from 'sinon';
66
import { TestContext } from 'ember-test-helpers';
77
import { Sizes } from 'ember-element-query/-private/modifier';
8+
import pause from '../helpers/pause';
89

910
interface TestContextCustom extends TestContext {
1011
callback?: SinonSpy;
1112
actualWidth?: number;
1213
actualHeight?: number;
1314
sizes?: Sizes;
1415
prefix?: string;
16+
isDisabled?: boolean;
1517
}
1618

1719
module('Integration | Modifier | element-query', function (hooks) {
@@ -75,6 +77,88 @@ module('Integration | Modifier | element-query', function (hooks) {
7577
assert.ok(true);
7678
});
7779

80+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
81+
test('does not call the onResize when isDisabled is initially set', async function (this: TestContextCustom, assert) {
82+
let m;
83+
let callCount = 0;
84+
this.callback = sinon.spy(() => callCount++);
85+
86+
await render(hbs`
87+
{{! template-lint-disable no-inline-styles }}
88+
<div
89+
id="test-subject"
90+
style="width: 300px; height: 100px;"
91+
{{element-query onResize=this.callback isDisabled=true}}
92+
>
93+
</div>
94+
`);
95+
96+
const element = document.getElementById('test-subject');
97+
if (!element) throw new Error('Expected element to exist');
98+
99+
m = 'Element is rendered';
100+
assert.ok(true, m);
101+
102+
await pause(500);
103+
104+
m = 'Call count';
105+
assert.equal(callCount, 0, m);
106+
107+
m = 'Attr `at-xs` presence';
108+
assert.equal(element.getAttribute('at-xs'), null, m);
109+
});
110+
111+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
112+
test('stops calling the onResize callback after updating isDisabled after rendering', async function (this: TestContextCustom, assert) {
113+
let m;
114+
let callCount = 0;
115+
this.callback = sinon.spy(() => callCount++);
116+
this.set('isDisabled', false);
117+
118+
await render(hbs`
119+
{{! template-lint-disable no-inline-styles }}
120+
<div
121+
id="test-subject"
122+
style="width: 300px; height: 100px;"
123+
{{element-query onResize=this.callback isDisabled=this.isDisabled}}
124+
>
125+
</div>
126+
`);
127+
128+
const element = document.getElementById('test-subject');
129+
if (!element) throw new Error('Expected element to exist');
130+
131+
m = 'Element is rendered';
132+
assert.ok(true, m);
133+
134+
await waitUntil(() => callCount === 1);
135+
136+
m = 'Callback called once';
137+
assert.ok(true, m);
138+
139+
sinon.assert.calledWithExactly(
140+
this.callback.firstCall,
141+
sinon.match({
142+
element,
143+
width: 300,
144+
height: 100,
145+
ratio: 3,
146+
})
147+
);
148+
149+
this.set('isDisabled', true);
150+
151+
element.style.width = '400px';
152+
153+
await pause(500);
154+
155+
m = 'Call count';
156+
assert.equal(callCount, 1, m);
157+
158+
m = 'Attr `at-s` presence';
159+
assert.equal(element.getAttribute('at-s'), null, m);
160+
});
161+
78162
//
79163

80164
// eslint-disable-next-line @typescript-eslint/no-misused-promises

0 commit comments

Comments
 (0)