Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fixed params to be set when mounted after resize events #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions 34 src/ContainerQueryCore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ResizeObserverLite from 'resize-observer-lite';
import ResizeObserverLite, {ResizeObserverSize} from 'resize-observer-lite';
import matchQueries from 'container-query-toolkit/lib/matchQueries';
import {Params, Query} from './interfaces';
import isShallowEqual from './isShallowEqual';
Expand All @@ -7,21 +7,39 @@ export default class ContainerQueryCore {
private rol: ResizeObserverLite;
private result: Params = {};

constructor(query: Query, callback: (params: Params) => void) {
constructor(private query: Query, private callback: (params: Params) => void) {
this.rol = new ResizeObserverLite((size) => {
const result = matchQueries(query)(size);
if (!isShallowEqual(this.result, result)) {
callback(result);
this.result = result;
}
this._processSize(size);
});
}

observe(element: Element) {
this.rol.observe(element);
// Check its an Element node. It could be null, or a Text node if the props.children function returns a string
if (element instanceof Element) {
const styles = window.getComputedStyle(element);
this._processSize({
width: this._getNumber(styles.width),
height: this._getNumber(styles.height),
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply.

Hmm, this is interesting. I don't fully understand. I have to investigate a bit. I thought once observe is called, the callback registered with ResizeObserverLite will be triggered immediately. But look like it's not.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It is interesting. I didn't look into ResizeObserverLite to see if there is a problem in there. I just assumed that if all the resize events were finished by the time the component was mounted then ResizeObserverLite would have no trigger to fire it.


this.rol.observe(element);
}
}

disconnect() {
this.rol.disconnect();
}

private _processSize(size: ResizeObserverSize) {
const result = matchQueries(this.query)(size);
if (!isShallowEqual(this.result, result)) {
this.callback(result);
this.result = result;
}
}

private _getNumber(str: string | null) {
const m = str ? /^([0-9\.]+)px$/.exec(str) : null;
return m ? parseFloat(m[1]) : 0;
}
}
38 changes: 38 additions & 0 deletions 38 test/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,44 @@ describe('ContainerQuery', function () {
}, 100);
});

it('sets params when mounted after resize events', function (done) {
class TestApp extends Component {
constructor(props) {
super(props);
this.state = {
renderChild: false
};
}

componentDidMount() {
setTimeout(() => {
this.setState({
renderChild: true
});
}, 200);
}

render() {
return (
<div style={{width: '200px'}}>
{this.state.renderChild && (
<ContainerQuery query={query}>
{(params) => <p>{JSON.stringify(params)}</p>}
</ContainerQuery>
)}
</div>
);
}
}

const node = findDOMNode(render(<div><TestApp/></div>, $div));

setTimeout(() => {
expect(node.children[0].innerHTML).toBe('<p>{"mobile":true,"desktop":false}</p>');
done();
}, 200);
});

});

describe('applyContainerQuery', function () {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.