Create dimensions provider

To measure the available space of an element when it
should take up the "rest" of the page. This matches
what `ListCollection` is doing internally but makes
the mechanism available in a composable component.
This commit is contained in:
Michael Klein 2022-10-06 16:11:39 +02:00
parent 9006b7eebb
commit 419531287b
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,4 @@
<div {{create-ref 'element'}} {{did-insert this.measureDimensions}}>
{{on-window 'resize' this.handleWindowResize}}
{{yield (hash data=this.data)}}
</div>

View File

@ -0,0 +1,53 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { ref } from 'ember-ref-bucket';
import { htmlSafe } from '@ember/template';
export default class DimensionsProvider extends Component {
@service dom;
@ref('element') element;
@tracked height;
get data() {
const { height, fillRemainingHeightStyle } = this;
return {
height,
fillRemainingHeightStyle,
};
}
get fillRemainingHeightStyle() {
return htmlSafe(`height: ${this.height}px;`);
}
get bottomBoundary() {
return this.args.bottomBoundary || this.footer;
}
get footer() {
return document.querySelector('footer[role="contentinfo"]');
}
get viewport() {
return this.dom.viewport();
}
@action measureDimensions(element) {
const { viewport, bottomBoundary } = this;
const height =
viewport.innerHeight - (element.getBoundingClientRect().top + bottomBoundary.clientHeight);
this.height = height;
}
@action handleWindowResize() {
const { element } = this;
this.measureDimensions(element);
}
}