Skip to content
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

solveMediaQueryList API #1

Open
prushforth opened this issue Nov 5, 2024 · 0 comments
Open

solveMediaQueryList API #1

prushforth opened this issue Nov 5, 2024 · 0 comments

Comments

@prushforth
Copy link

Hi, and thanks for this nice looking library!!!

I am trying to get familiar with how to set it up to use custom / unknown media features and I wonder if you could tell me what I'm doing wrong.

I have the following code:

import { parseMediaQueryList } from 'media-query-parser';
import { solveMediaQueryList } from 'media-query-solver';

debugMatches(query) {
  const parsedQuery = parseMediaQueryList(query);


  const customFeatures = {
    'my-range': {
      type: 'range',
      valueType: 'integer',
      canBeNegative: false,
      canBeZero: true
    };

  const myCustomRange = { min: 0, max: 24}

  let matches = solveMediaQueryList(parsedQuery, {
    features: customFeatures,
    solveUnknownFeature: (featureNode) => {
      let feature, queryValue;
      try {
        feature = featureNode.feature;
        queryValue = featureNode.value.value; 
        console.log('Feature:', feature, 'Query Value:', queryValue);
      } catch (error) {
        console.error('Error accessing featureNode properties:', error);
        console.log('featureNode at error:', featureNode);
      }
      if (feature === 'my-range') {
        return queryValue >= myCustomRange.min && queryValue <= myCustomRange.max;
      }

      return false;
    }
  });
  return matches;
}

When I run the function:

$0.debugMatches('(my-range: 100)') // expecting false as the value 100 is outside the custom range
true

But it logs true. In the debugger, I can see that queryValue >= myCustomRange.min && queryValue <= myCustomRange.max is false, but the overal function returns true. What am I misunderstanding?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant