Skip to content

monitorPosition passthrough #82

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 12 commits into
base: master
Choose a base branch
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
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

## Overview

`react-component-queries` is a useful abstraction of the [`react-sizeme`](https://github.com/ctrlplusb/react-sizeme) library. It allows you to define queries against the dimensions of your Component in order to produce custom props for your Component.
`react-component-queries` is a useful abstraction of the [`react-sizeme`](https://github.com/ctrlplusb/react-sizeme) library. It allows you to define queries against the dimensions of your Component in order to produce custom props for your Component.

Any time the dimensions of your rendered Component changes the queries will automatically be run again.

Expand Down Expand Up @@ -70,7 +70,7 @@ componentQueries(
if (width > 330 && width <=960) return { breakpoint: 'medium' };
return { breakpoint: 'large' };
},
// You can have multiple queries, and the props that are returned can
// You can have multiple queries, and the props that are returned can
// be of any type. Boolean's are often useful.
({ width }) => ({ isMassive: width > 1000000 })
)(MyComponent);
Expand All @@ -81,8 +81,8 @@ The above example will result in a `breakpoint` and an `isMassive` prop being pa
## Why use this instead of `react-sizeme`?

[`react-sizeme`](https://github.com/ctrlplusb/react-sizeme) is great, however, it suffers with a couple of problems in my opinion:
1. It is raw in that it provides you with the actual dimensions of your component and then requires to execute logic within your component to establish the desired behaviour of your component. This can be a bit tedious and polute your component with a lot of if-else statements.

1. It is raw in that it provides you with the actual dimensions of your component and then requires to execute logic within your component to establish the desired behaviour of your component. This can be a bit tedious and polute your component with a lot of if-else statements.
2. It is possible that your component may gets spammed with updated `size` props. This is because _any_ time your component changes in size `react-sizeme` will kick in.

`react-component-queries` was built to solve these problems. It solves problem 1 by moving the dimension based logic out of your component. It then solves problem 2 by ensuring that your component will only be called for re-render if any of the prop values change. That saves you some error prone boilerplate.
Expand All @@ -93,7 +93,7 @@ So, to recap, some of the benefits of using this abstraction are:

- Simplify your components by moving the dimension logic away from them, which in turn is easier to test in isolation.
- `shouldComponentUpdate` is implemented on your behalf.
- The _query functions_ themselves can be formed into a reusable library of queries for all your components.
- The _query functions_ themselves can be formed into a reusable library of queries for all your components.

I am not trying to take away from `react-sizeme`, but I want to highlight that it's a bit more of a low level HOC, and if you want to use it you should be aware of the problems above and consider using your own abstraction or this one.

Expand All @@ -111,7 +111,7 @@ npm install react-sizeme react-component-queries --save

## API

`react-component-queries` exports a single function to be used as an HOC around your existing components. This function supports two modes of usage: _simple_ and _configured_.
`react-component-queries` exports a single function to be used as an HOC around your existing components. This function supports two modes of usage: _simple_ and _configured_.

### _Simple_: `componentQueries(queries)`

Expand Down Expand Up @@ -139,7 +139,7 @@ componentQueries(

- `query(size, [ownProps]) : props` (_Function_): A query function which can be provided as a set of arguments, or can be contained within an array containing one or more queries.
- `size` (_Object_): Contains the current dimensions of your wrapped component. As the default configuration is being used, it will only contain th e `width` dimension.
- `width` (_Number_): The current width of your component.
- `width` (_Number_): The current width of your component.
- [`ownProps`] \(_Object_): The additional props which have been provided to your wrapped component.

### _Configured_: `componentQueries(config)`
Expand All @@ -157,8 +157,10 @@ componentQueries({
config: {
monitorWidth: true,
monitorHeight: false,
monitorPosition: false,
refreshRate: 16,
pure: true
pure: true,
sizePassthrough: 'componentDimensions'
}
})(MyComponent)
```
Expand All @@ -169,20 +171,23 @@ componentQueries({
- `queries` (_Array_): An array of query functions:
- `query(size, [ownProps]) : props` (_Function_): A query function which can be provided as a set of arguments, or can be contained within an array containing one or more queries.
- `size` (_Object_): Contains the current dimensions of your wrapped component.
- `[width]` (_Number_): Will only be provided if the `monitorWidth` configuration option is set to `true`. The current width of your component.
- `[height]` (_Number_): Will only be provided if the `monitorHeight` configuration option is set to `true`. The current height of your component.
- `[width]` (_Number_): Will only be provided if the `monitorWidth` configuration option is set to `true`. The current width of your component.
- `[height]` (_Number_): Will only be provided if the `monitorHeight` configuration option is set to `true`. The current height of your component.
- `[position]` (_Object_): Will only be provided if the `monitorPosition` configuration option is set to `true`. The current position of your component. It will include `bottom`, `top`, `left`, and `right` properties.
- [`ownProps`] \(_Object_): The additional props which have been provided to your wrapped component.
- `[config]` (_Object_): Custom configuration.
- `[monitorWidth]` (_Boolean_): If `true` then the width of your component will be tracked and provided within the `size` argument to your query functions. Defaults to `true`.
- `[monitorHeight]` (_Boolean_): If `true` then the height of your component will be tracked and provided within the `size` argument to your query functions. Defaults to `false`.
- `[monitorPosition]` (_Boolean_): If `true` then the position of your component will be tracked and provided within the `size` argument to your query functions. Defaults to `false`.
- `[refreshRate]` (_Number_): The maximum frequency, in milliseconds, at which size changes should be recalculated when changes in your Component's rendered size are being detected. This must not be set to lower than 16. Defaults to `16`.
- `[pure]` (_Boolean_): Indicates if your component should be considered "pure", i.e. it should only be rerendered if the result of your query functions change, or if new props are provided to the wrapped component. If you set it to false then the wrapped component will render _every_ time the size changes, even if it doesn't result in new query provided props. Defaults to `true`.
- `[sizePassthrough]` (_Boolean_ || _String_): If you need the size object to be passed to your component on every render, settings this to true will pass it as the `size` prop. If `sizePassthrough` is a _String_, the `size` object will be passed as the given string.
- [`conflictResolver(prev, current, key) : Any`] \(_Function_): A custom function to use in order to resolve prop conflicts when two or more query functions return a prop with the same key. This gives you an opportunity to do custom resolution for special prop types, e.g. `className` where you could instead concat the conflicted values. The default implementation will return the value from the _last_ query function provided in the query array. Please read the respective section further down in the readme for more info and examples of this.
- `prev` (_Any_): The value of the conflicted prop provided by the previously executed query function.
- `current` (_Any_): The value of the conflicted prop provided by the most recently executed query function.
- `key` (_Any_): The name of the prop which is in conflict.

## Examples
## Examples

Below are a few super simple examples highlighting the usage and capabilities of the library. They are using the ES6 syntax described above to define the queries.

Expand Down Expand Up @@ -256,7 +261,7 @@ As you can see we expose a `sizeMeConfig`, please see the [`react-sizeme`](https

## Prop Conflict Handling

As it is possible for you to provide props from multiple queries there could be cases where prop clashing occurs. By default we have an order of preference for which prop value should be resolved in the case of conflicts.
As it is possible for you to provide props from multiple queries there could be cases where prop clashing occurs. By default we have an order of preference for which prop value should be resolved in the case of conflicts.

__The rule is:__ Custom passed in props take preference followed by the last item in the query collection.

Expand All @@ -275,7 +280,7 @@ Then say we rendered our component like so, passing in a custom prop:

```
ReactDOM.render(<MyComponent foo="zip" />, container);
```
```

In this case the value of `foo` would resolve to "zip".

Expand Down Expand Up @@ -305,7 +310,7 @@ const MyComponent = componentQueries({
if (key === 'className') {
return prev.concat(' ', current);
}
// Otherwise we return the current value, overriding the prev value.
// Otherwise we return the current value, overriding the prev value.
return current;
}
})(ComponentToWrap);
Expand All @@ -326,8 +331,8 @@ Then the props that would be resolved would be:
}
```

---
---

### Credits

Rubix graphic by <a href="http://www.freepik.com/">Freepik</a> from <a href="http://www.flaticon.com/">Flaticon</a> is licensed under <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0">CC BY 3.0</a>. Made with <a href="http://logomakr.com" title="Logo Maker">Logo Maker</a>
Rubix graphic by <a href="http://www.freepik.com/">Freepik</a> from <a href="http://www.flaticon.com/">Flaticon</a> is licensed under <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0">CC BY 3.0</a>. Made with <a href="http://logomakr.com" title="Logo Maker">Logo Maker</a>
Loading