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

Thanks and can you explain it a little bit? #6

Closed
frederikhors opened this issue Jun 1, 2020 · 3 comments
Closed

Thanks and can you explain it a little bit? #6

frederikhors opened this issue Jun 1, 2020 · 3 comments
Labels
question Further information is requested

Comments

@frederikhors
Copy link

Thanks a lot for your work on this!

Can you explain a little bit the code in https://github.com/pearofducks/svelte-match-media/blob/master/index.js?

Thanks again!

@pearofducks
Copy link
Owner

I'm happy to answer questions you might have, I'm not sure what to explain though.

The plugin essentially is just combining window.matchMedia and Svelte stores

@pearofducks pearofducks added the question Further information is requested label Jun 1, 2020
@frederikhors
Copy link
Author

What is setupMq() for?

const setupMq = (queryString) => (set) => {
  const query = window.matchMedia(queryString)
  const callback = (e) => set(e.matches)
  query.addListener(callback)
  callback(query)
  return () => query.removeListener(callback)
}

What setup() does?

export const setup = (queries = defaultQueries) => {
  queryStores = Object.entries(queries).reduce((acc, [mediaName, queryString]) => (acc[mediaName] = readable(false, setupMq(queryString)), acc), {})
  media = derived(Object.values(queryStores), ($queryStores) => $queryStores.reduce((acc, q, i) => (acc[Object.keys(queryStores)[i]] = q, acc), {}))
}

I'm newbie...

@pearofducks
Copy link
Owner

Sorry those are pretty broad/vague questions, you might be able to find someone to walk through the code with you - as it's pretty brief. I don't have time to write out an explanation like that.

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

No branches or pull requests

2 participants