Skip to content

Multiple Queries #39

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

Merged
merged 10 commits into from
Apr 20, 2017
Merged

Multiple Queries #39

merged 10 commits into from
Apr 20, 2017

Conversation

VinSpee
Copy link
Contributor

@VinSpee VinSpee commented Feb 28, 2017

Adds support for multiple queries as outlined in #38

VinSpee and others added 4 commits February 27, 2017 21:54
Add test case to check for matching `queries` prop
- changes proptypes to make query optional and to accept the `queries`
prop
- creates a list of MatchMedia objects for the `queries` prop
- update the `match` property of state to contain an object in the
format of
  ```
    {
      [mq name: string]: [matches: bool],
    }
  ```
@VinSpee VinSpee changed the title [WIP] Multiple Queries Multiple Queries Feb 28, 2017
@VinSpee
Copy link
Contributor Author

VinSpee commented Feb 28, 2017

I'd love a review - happy to make any needed changes or test cases.

Thanks!

@vesparny
Copy link

vesparny commented Apr 5, 2017

Is anything blocking this PR from being merged?

modules/Media.js Outdated
name: mq,
qs: json2mq(queries[mq]),
}))
this.mediaQueryList = queries.map(mq => ({
Copy link
Member

Choose a reason for hiding this comment

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

In the case of a single query, this.mediaQueryList is an actual MediaQueryList object. In the case of multiple queries, it's an array. I'd prefer it to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

understood and updated. Thanks!

Previously, mediaQueryList could have multiple types. Now, it's always
an array.
@souporserious
Copy link

Would love to see this get merged, I literally just made the same thing and then saw this PR 😆

@mjackson how would you feel about adding context here, and the ability to grab a breakpoint by using a HoC like this:

import React from 'react'
import PropTypes from 'prop-types'
import { CHANNEL } from './MediaQueryProvider'

export default function withMatchedQueries(WrappedComponent) {
  const Component = (props, context) => {
    return <Component matchedQueries={context[CHANNEL]} {...props} />
  }

  Component.contextTypes = {
    [CHANNEL]: PropTypes.object,
  }

  return Component
}

I can create another PR after this one gets merged if you are interested 😁

Copy link
Member

@mjackson mjackson left a comment

Choose a reason for hiding this comment

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

Getting closer. Just need to tweak some names.

modules/Media.js Outdated
]).isRequired,
]),
queries: PropTypes.shape({
[PropTypes.string]: PropTypes.oneOfType([
Copy link
Member

Choose a reason for hiding this comment

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

This isn't valid. You can't use PropTypes.string as the key of an object because it will just be converted to a string. Instead, let's use:

queries: PropTypes.objectOf(
  PropTypes.oneOfType(...)
)

To reduce on code duplication, let's also create a type for the oneOfType and call it queryType.

const queryType = PropTypes.oneOfType(...)

Then we can reuse that in both propTypes.query and propTypes.queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

understood, I've been using flow too much 🙊

modules/Media.js Outdated
this.mediaQueryList = [
{
name: 'match',
mm: window.matchMedia(query),
Copy link
Member

Choose a reason for hiding this comment

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

The object returned from matchMedia is a MediaQueryList, so let's call it something like mediaQueryList or queryList instead of mm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted for mediaQueryList. updated.

modules/Media.js Outdated
this.mediaQueryList = window.matchMedia(query)
this.mediaQueryList.addListener(this.updateMatches)
if (query) {
this.mediaQueryList = [
Copy link
Member

Choose a reason for hiding this comment

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

This is an array that we created. It's not really a MediaQueryList, just something we made to hold a bunch of media queries. Let's call it this.queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense, updated

Vince Speelman added 4 commits April 19, 2017 15:27
`mediaQueryList` was not actually a `MediaQueryList`, so name it
appropriately.
`mm` _is_ an acutal `MediaQueryList`, so name it appropriately.
PropTypes.arrayOf(PropTypes.object.isRequired)
]).isRequired,
query: queryType,
queries: PropTypes.objectOf(queryType),
Copy link
Member

Choose a reason for hiding this comment

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

👍

@mjackson mjackson merged commit b863f5a into ReactTraining:master Apr 20, 2017
@mjackson
Copy link
Member

Thanks for being so responsive, @VinSpee! :D

@VinSpee
Copy link
Contributor Author

VinSpee commented Apr 20, 2017

thanks for trusting me with such a big feature and for the detailed code reviews! Happy to see it merged.

@nezed
Copy link

nezed commented Jun 19, 2017

@mjackson Can you publish a new version with currently introduced features?
Last actual version 1.5.1 was published 4 months ago.
Thanks!

@nezed nezed mentioned this pull request Jun 21, 2017
@mjackson mjackson mentioned this pull request Jul 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants