Skip to content

Commit fb166ed

Browse files
author
Federico Zivolo
committed
v0.3.0
- Added makeElementQuery decorator
1 parent 9ceab92 commit fb166ed

File tree

6 files changed

+250
-115
lines changed

6 files changed

+250
-115
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
React component designed to let you conditionally render pieces of UI based on
44
a set of element (media) queries defined by you.
55

6-
The best thing? It weights 600 bytes gzipped (1.3KB minified) and its only
6+
The best thing? It weights ~800 bytes gzipped (1.9KB minified) and its only
77
dependency is [react-resize-aware](https://github.com/FezVrasta/react-resize-aware)
88
which weights only 1.2KB gzipped!
99

@@ -42,8 +42,30 @@ And if you need some more power, you can invert the behavior of a selector:
4242
<Matches sm={false}>I match only when `sm` isn't matching</Matches>
4343
```
4444
45+
### Need more power?
46+
47+
You can enhance your component to provide it a `matches` property you can
48+
use to programmatically check for one or more element queries.
49+
You simply have to provide a `getRef` property assigned as `ref` callback,
50+
make it render its `children` and use the `matches` property as follows:
51+
52+
```jsx
53+
import { makeElementQuery } from 'react-element-queries';
54+
const App = (getRef, children, matches) =>
55+
<div ref={getRef} style={{ color: matches('sm') ? 'yellow' : 'pink' }}>
56+
<Matches sm>🐣</Matches>
57+
{children}
58+
</div>
59+
60+
const EnhancedApp = makeElementQuery(
61+
App,
62+
{ sm: { maxWidth: 200 }, lg: { minWidth: 201 } }
63+
);
64+
```
65+
4566
## Docs
4667
68+
### Queries
4769
The `queries` property is an object with a list of properties of your choice,
4870
you can name them how you prefer, for instance, you could have `sm` or `small` or
4971
`verySmallMatcher`:
@@ -57,11 +79,24 @@ Each property contains an object with one or more expressions.
5779
So far, the supported expressions are: `maxWidth`, `minWidth`, `maxHeight` and
5880
`minHeight`.
5981
82+
### `<Matches />` component
6083
Once you have defined your element queries, you can use the `<Matches />` component
6184
to conditionally render a piece of UI.
6285
Simply add to it one or more element queries names to tell it to display its content
6386
when at least one of the matches the given expressions.
6487
88+
### `matches` property
89+
When you enhance your component with `makeElementQuery`, your component will get
90+
access to the `matches` property.
91+
This is a function you can call programmatically to perform any kind of operation
92+
with the result of the element queries you provide it.
93+
94+
```js
95+
matches('sm') // true if `sm` is matching
96+
matches('sm', 'lg') // true if `sm` and/or `lg` are matching
97+
matches({ sm: false }) // true when `sm` isn't matching
98+
matches({ sm: false }, 'lg') // true when `sm` doesn't match and `lg` does
99+
```
65100

66101
# Credits
67102

docs/example.js

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,53 @@ function _objectWithoutProperties(obj, keys) {
2424
return target;
2525
}
2626

27-
function MyComponent(_ref) {
28-
var width = _ref.width,
29-
height = _ref.height,
30-
getRef = _ref.getRef,
31-
children = _ref.children,
32-
props = _objectWithoutProperties(_ref, ['getRef', 'children']);
27+
const MyComponent = ReactElementQueries.makeElementQuery(
28+
function MyComponent(_ref) {
29+
var getRef = _ref.getRef,
30+
children = _ref.children,
31+
matches = _ref.matches,
32+
props = _objectWithoutProperties(_ref, ['getRef', 'children', 'matches']);
3333

34-
return React.createElement(
35-
'div',
36-
_extends({className: 'example', ref: getRef}, props),
37-
React.createElement(
38-
'p',
39-
null,
40-
"Hover me! I'm based on",
34+
return React.createElement(
35+
'div',
36+
_extends({className: 'example', ref: getRef}, props),
4137
React.createElement(
42-
'a',
43-
{href: 'https://github.com/FezVrasta/react-resize-aware'},
44-
'react-resize-aware'
38+
'p',
39+
null,
40+
"Hover me! I'm based on",
41+
React.createElement(
42+
'a',
43+
{href: 'https://github.com/FezVrasta/react-resize-aware'},
44+
'react-resize-aware'
45+
),
46+
'and let you render pieces of UI based on your own element queries!'
4547
),
46-
'and let you render pieces of UI based on your own element queries!'
47-
),
48-
React.createElement(
49-
ReactElementQueries.Matches,
50-
{sm: true},
51-
'small 🐣 (pio?)'
52-
),
53-
React.createElement(
54-
ReactElementQueries.Matches,
55-
{lg: true},
56-
'large 🐷 (oink!)'
57-
)
58-
);
59-
}
48+
React.createElement(
49+
ReactElementQueries.Matches,
50+
{sm: true},
51+
'small 🐣 (pio?)'
52+
),
53+
React.createElement(
54+
ReactElementQueries.Matches,
55+
{lg: true},
56+
'large 🐷 (oink!)'
57+
),
58+
React.createElement('input', {
59+
className: matches('sm') ? 'sm' : null,
60+
placeholder: "I'm yellow when 🐣 is visible",
61+
style: {width: 200, display: 'block'},
62+
}),
63+
children
64+
);
65+
},
66+
{
67+
sm: {maxHeight: 150},
68+
lg: {minHeight: 151},
69+
}
70+
);
6071

6172
function App() {
62-
return React.createElement(
63-
ReactElementQueries.ElementQuery,
64-
{
65-
style: {position: 'relative'},
66-
queries: {sm: {maxHeight: 100}, lg: {minHeight: 101}},
67-
},
68-
React.createElement(MyComponent)
69-
);
73+
return React.createElement(MyComponent);
7074
}
7175

7276
ReactDOM.render(

docs/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818
width: 100%;
1919
color: white;
2020
transition: 1s all ease-in-out;
21+
padding: 1em;
22+
position: relative;
2123
}
2224
.example:hover {
2325
height: 200px;
2426
width: 80vw;
2527
}
28+
.sm {
29+
background-color: #FFC31B;
30+
}
2631
</style>
2732
</head>
2833
<body>

docs/react-element-queries.js

Lines changed: 88 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-element-queries",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Conditionally render pieces of UI based on element queries",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)