summaryrefslogtreecommitdiff
path: root/src/sunlight-theme.js
blob: e64000318a9c9a9b06912ce243a9b2ea71abce30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React from 'react'
import { useSunlight } from 'use-sunlight'
import { ThemeProvider } from 'emotion-theming'

const isFn = t => typeof t === 'function'

const pipeReducer = (a, b) => arg => {
  if (!isFn(a) || !isFn(b)) throw new Error('Middleware has to be function')
  return b(a(arg))
}
const pipe = (...ops) => ops.reduce(pipeReducer)

export const SunlightTheme = ({ children, middleware }) => {
  const [lightLevel] = useSunlight()
  const theme = { lightLevel }
  // if we have at least one middleware function,
  // them in a pipe (left to right),
  const transformedTheme =
    middleware && middleware.length ? pipe(...middleware)(theme) : theme
  return <ThemeProvider theme={transformedTheme}>{children}</ThemeProvider>
}